Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Wait for pod event to show up #69300

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions test/e2e/framework/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 2 additions & 5 deletions test/e2e/storage/testsuites/subpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down