Merge pull request #101464 from Nick-Triller/deflake_assert_events

e2e: deflake test by not relying on events
This commit is contained in:
Kubernetes Prow Robot 2021-06-24 17:44:45 -07:00 committed by GitHub
commit 7eae33cb0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 14 deletions

View File

@ -51,7 +51,21 @@ var _ = SIGDescribe("RuntimeClass", func() {
handler := f.Namespace.Name + "-handler"
rcName := createRuntimeClass(f, "unconfigured-handler", handler)
pod := f.PodClient().Create(e2enode.NewRuntimeClassPod(rcName))
expectSandboxFailureEvent(f, pod, handler)
eventSelector := fields.Set{
"involvedObject.kind": "Pod",
"involvedObject.name": pod.Name,
"involvedObject.namespace": f.Namespace.Name,
"reason": events.FailedCreatePodSandBox,
}.AsSelector().String()
// Events are unreliable, don't depend on the event. It's used only to speed up the test.
err := e2eevents.WaitTimeoutForEvent(f.ClientSet, f.Namespace.Name, eventSelector, handler, framework.PodEventTimeout)
if err != nil {
framework.Logf("Warning: did not get event about FailedCreatePodSandBox. Err: %v", err)
}
// Check the pod is still not running
p, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(context.TODO(), pod.Name, metav1.GetOptions{})
framework.ExpectNoError(err, "could not re-read the pod after event (or timeout)")
framework.ExpectEqual(p.Status.Phase, v1.PodPending, "Pod phase isn't pending")
})
// This test requires that the PreconfiguredRuntimeHandler has already been set up on nodes.
@ -273,16 +287,3 @@ func expectPodSuccess(f *framework.Framework, pod *v1.Pod) {
framework.ExpectNoError(e2epod.WaitForPodSuccessInNamespace(
f.ClientSet, pod.Name, f.Namespace.Name))
}
// expectSandboxFailureEvent polls for an event with reason "FailedCreatePodSandBox" containing the
// expected message string.
func expectSandboxFailureEvent(f *framework.Framework, pod *v1.Pod, msg string) {
eventSelector := fields.Set{
"involvedObject.kind": "Pod",
"involvedObject.name": pod.Name,
"involvedObject.namespace": f.Namespace.Name,
"reason": events.FailedCreatePodSandBox,
}.AsSelector().String()
framework.ExpectNoError(e2eevents.WaitTimeoutForEvent(
f.ClientSet, f.Namespace.Name, eventSelector, msg, framework.PodEventTimeout))
}

View File

@ -31,6 +31,7 @@ import (
type Action func() error
// WaitTimeoutForEvent waits the given timeout duration for an event to occur.
// Please note delivery of events is not guaranteed. Asserting on events can lead to flaky tests.
func WaitTimeoutForEvent(c clientset.Interface, namespace, eventSelector, msg string, timeout time.Duration) error {
interval := 2 * time.Second
return wait.PollImmediate(interval, timeout, eventOccurred(c, namespace, eventSelector, msg))