From c771de1e6bc5f05690937ab67210a1c1a7facaff Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Mon, 1 Oct 2018 11:32:00 -0700 Subject: [PATCH] test: Wait for pod event to show up We are seeing flakes where pod event isn't yet visible when we check for it leading to test failure. Signed-off-by: Mrunal Patel --- test/e2e/framework/util.go | 26 ++++++++++++++++++++++++++ test/e2e/storage/testsuites/subpath.go | 7 ++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/test/e2e/framework/util.go b/test/e2e/framework/util.go index 66eb8728019..fde9a151bac 100644 --- a/test/e2e/framework/util.go +++ b/test/e2e/framework/util.go @@ -119,6 +119,9 @@ const ( // How long to wait for a pod to be deleted PodDeleteTimeout = 5 * time.Minute + // PodEventTimeout is how much we wait for a pod event to occur. + PodEventTimeout = 2 * time.Minute + // If there are any orphaned namespaces to clean up, this test is running // on a long lived cluster. A long wait here is preferably to spurious test // failures caused by leaked resources from a previous test run. @@ -1460,6 +1463,29 @@ func podRunning(c clientset.Interface, podName, namespace string) wait.Condition } } +// WaitTimeoutForPodEvent waits for an event to occur for a pod +func WaitTimeoutForPodEvent(c clientset.Interface, podName, namespace, eventSelector, msg string, timeout time.Duration) error { + return wait.PollImmediate(Poll, timeout, eventOccured(c, podName, namespace, eventSelector, msg)) +} + +func eventOccured(c clientset.Interface, podName, namespace, eventSelector, msg string) wait.ConditionFunc { + options := metav1.ListOptions{FieldSelector: eventSelector} + return func() (bool, error) { + events, err := c.CoreV1().Events(namespace).List(options) + if err != nil { + return false, fmt.Errorf("got error while getting pod events: %s", err) + } + if len(events.Items) == 0 { + return false, fmt.Errorf("no events found") + } + if strings.Contains(events.Items[0].Message, msg) { + return false, fmt.Errorf("%q error not found", msg) + } else { + return true, nil + } + } +} + // Waits default amount of time (DefaultPodDeletionTimeout) for the specified pod to stop running. // Returns an error if timeout occurs first. func WaitForPodNoLongerRunningInNamespace(c clientset.Interface, podName, namespace string) error { diff --git a/test/e2e/storage/testsuites/subpath.go b/test/e2e/storage/testsuites/subpath.go index e7bc6dbde73..c5ff0ead938 100644 --- a/test/e2e/storage/testsuites/subpath.go +++ b/test/e2e/storage/testsuites/subpath.go @@ -596,11 +596,8 @@ func testPodFailSubpathError(f *framework.Framework, pod *v1.Pod, errorMsg strin "involvedObject.namespace": f.Namespace.Name, "reason": "Failed", }.AsSelector().String() - options := metav1.ListOptions{FieldSelector: selector} - events, err := f.ClientSet.CoreV1().Events(f.Namespace.Name).List(options) - Expect(err).NotTo(HaveOccurred(), "while getting pod events") - Expect(len(events.Items)).NotTo(Equal(0), "no events found") - Expect(events.Items[0].Message).To(ContainSubstring(errorMsg), fmt.Sprintf("%q error not found", errorMsg)) + err = framework.WaitTimeoutForPodEvent(f.ClientSet, pod.Name, f.Namespace.Name, selector, errorMsg, framework.PodEventTimeout) + Expect(err).To(HaveOccurred(), "while waiting for failed event to occur") } // Tests that the existing subpath mount is detected when a container restarts