Skip to content

Commit

Permalink
You should be able to get logs from a specific container in a Pod
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobcrawford committed Jan 31, 2023
1 parent aefcdbf commit d796b1d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
21 changes: 15 additions & 6 deletions modules/k8s/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,27 @@ func IsPodAvailable(pod *corev1.Pod) bool {
return pod.Status.Phase == corev1.PodRunning
}

// GetPodLogsE returns the logs of a Pod at the time when the function was called. If the Pod is not running an Error is returned.
func GetPodLogsE(t testing.TestingT, options *KubectlOptions, pod *corev1.Pod) (string, error) {
output, err := RunKubectlAndGetOutputE(t, options, "logs", pod.Name)
// GetPodLogsE returns the logs of a Pod at the time when the function was called. Pass container name if there are more containers in the Pod or set to "" if there is only one.
// If the Pod is not running an Error is returned.
// If the provided containerName is not the name of a container in the Pod an Error is returned.
func GetPodLogsE(t testing.TestingT, options *KubectlOptions, pod *corev1.Pod, containerName string) (string, error) {
var output string
var err error
if containerName == "" {
output, err = RunKubectlAndGetOutputE(t, options, "logs", pod.Name)
} else {
output, err = RunKubectlAndGetOutputE(t, options, "logs", pod.Name, fmt.Sprintf("-c%s", containerName))
}

if err != nil {
return "", err
}
return output, nil
}

// GetPodLogsE returns the logs of a Pod at the time when the function was called.
func GetPodLogs(t testing.TestingT, options *KubectlOptions, pod *corev1.Pod) string {
logs, err := GetPodLogsE(t, options, pod)
// GetPodLogsE returns the logs of a Pod at the time when the function was called. Pass container name if there are more containers in the Pod or set to "" if there is only one.
func GetPodLogs(t testing.TestingT, options *KubectlOptions, pod *corev1.Pod, containerName string) string {
logs, err := GetPodLogsE(t, options, pod, containerName)
require.NoError(t, err)
return logs
}
21 changes: 15 additions & 6 deletions test/kubernetes_basic_example_logs_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build kubeall || kubernetes
// +build kubeall kubernetes

// NOTE: we have build tags to differentiate kubernetes tests from non-kubernetes tests. This is done because minikube
// is heavy and can interfere with docker related tests in terratest. Specifically, many of the tests start to fail with
// `connection refused` errors from `minikube`. To avoid overloading the system, we run the kubernetes tests and helm
Expand All @@ -19,11 +16,11 @@ import (

"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/gruntwork-io/terratest/modules/random"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// An example of how to do more expanded verification of the Kubernetes resource config in examples/kubernetes-basic-example using Terratest.
func TestKubernetesBasicExampleServiceCheck(t *testing.T) {
func setupLogsTest(t *testing.T) (*k8s.KubectlOptions, v1.Pod) {
t.Parallel()

// Path to the Kubernetes resource config we will test
Expand Down Expand Up @@ -76,7 +73,19 @@ func TestKubernetesBasicExampleServiceCheck(t *testing.T) {
// Wait fot the pod to be started and ready
k8s.WaitUntilPodAvailable(t, options, pod.Name, 5, 10*time.Second)

logs := k8s.GetPodLogs(t, options, &pod)
return options, pod
}

func TestKubernetesBasicExampleLogsCheckWithContainerName(t *testing.T) {
options, pod := setupLogsTest(t)
logs := k8s.GetPodLogs(t, options, &pod, "podinfo")

require.Contains(t, logs, "Starting podinfo")
}

func TestKubernetesBasicExampleLogsCheckWithNoContainerName(t *testing.T) {
options, pod := setupLogsTest(t)
logs := k8s.GetPodLogs(t, options, &pod, "")

require.Contains(t, logs, "Starting podinfo")
}

0 comments on commit d796b1d

Please sign in to comment.