From 670199679a5434b7e5864f7095edf9238b6ab34f Mon Sep 17 00:00:00 2001 From: Clayton Coleman Date: Wed, 22 Jul 2020 18:05:33 -0400 Subject: [PATCH] test: Report an error message when a pod fails in a cmd test The logic for testing for pod failure lost the reason why, so no debug output was possible. Instead, update the one shot pod to capture fallback logs and then have the executor report that as the error message. E.g. Jul 22 20:13:43.152: INFO: pod "test-cmd" errored trying to run the command: because err was nil in the context it was invoked --- test/extended/util/framework.go | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/test/extended/util/framework.go b/test/extended/util/framework.go index 8fc0932c22fa..28324f53510b 100644 --- a/test/extended/util/framework.go +++ b/test/extended/util/framework.go @@ -5,6 +5,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "io/ioutil" "net" @@ -1658,9 +1659,9 @@ func RunOneShotCommandPod( return false, nil } - if podHasErrored(cmdPod) { + if err := podHasErrored(cmdPod); err != nil { e2e.Logf("pod %q errored trying to run the command: %v", pod.Name, err) - return false, nil + return false, err } return podHasCompleted(cmdPod), nil }) @@ -1693,10 +1694,13 @@ func podHasCompleted(pod *corev1.Pod) bool { pod.Status.ContainerStatuses[0].State.Terminated.Reason == "Completed" } -func podHasErrored(pod *corev1.Pod) bool { - return len(pod.Status.ContainerStatuses) > 0 && +func podHasErrored(pod *corev1.Pod) error { + if len(pod.Status.ContainerStatuses) > 0 && pod.Status.ContainerStatuses[0].State.Terminated != nil && - pod.Status.ContainerStatuses[0].State.Terminated.Reason == "Error" + pod.Status.ContainerStatuses[0].State.Terminated.Reason == "Error" { + return errors.New(pod.Status.ContainerStatuses[0].State.Terminated.Message) + } + return nil } func getPodLogs(oc *CLI, pod *corev1.Pod) (string, error) { @@ -1722,13 +1726,14 @@ func newCommandPod(name, image, command string, args []string, volumeMounts []co RestartPolicy: corev1.RestartPolicyOnFailure, Containers: []corev1.Container{ { - Name: name, - Image: image, - Command: []string{command}, - Args: args, - VolumeMounts: volumeMounts, - ImagePullPolicy: "Always", - Env: env, + Name: name, + Image: image, + Command: []string{command}, + Args: args, + VolumeMounts: volumeMounts, + ImagePullPolicy: "Always", + Env: env, + TerminationMessagePolicy: corev1.TerminationMessageFallbackToLogsOnError, }, }, },