Merge pull request #69300 from mrunalp/subpath_test_wait_for_event

test: Wait for pod event to show up
pull/58/head
k8s-ci-robot 2018-10-02 01:12:00 -07:00 committed by GitHub
commit a27adf1612
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 5 deletions

View File

@ -119,6 +119,9 @@ const (
// How long to wait for a pod to be deleted // How long to wait for a pod to be deleted
PodDeleteTimeout = 5 * time.Minute 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 // 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 // on a long lived cluster. A long wait here is preferably to spurious test
// failures caused by leaked resources from a previous test run. // 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. // Waits default amount of time (DefaultPodDeletionTimeout) for the specified pod to stop running.
// Returns an error if timeout occurs first. // Returns an error if timeout occurs first.
func WaitForPodNoLongerRunningInNamespace(c clientset.Interface, podName, namespace string) error { func WaitForPodNoLongerRunningInNamespace(c clientset.Interface, podName, namespace string) error {

View File

@ -596,11 +596,8 @@ func testPodFailSubpathError(f *framework.Framework, pod *v1.Pod, errorMsg strin
"involvedObject.namespace": f.Namespace.Name, "involvedObject.namespace": f.Namespace.Name,
"reason": "Failed", "reason": "Failed",
}.AsSelector().String() }.AsSelector().String()
options := metav1.ListOptions{FieldSelector: selector} err = framework.WaitTimeoutForPodEvent(f.ClientSet, pod.Name, f.Namespace.Name, selector, errorMsg, framework.PodEventTimeout)
events, err := f.ClientSet.CoreV1().Events(f.Namespace.Name).List(options) Expect(err).To(HaveOccurred(), "while waiting for failed event to occur")
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))
} }
// Tests that the existing subpath mount is detected when a container restarts // Tests that the existing subpath mount is detected when a container restarts