Skip to content

Commit

Permalink
Merge pull request #101464 from Nick-Triller/deflake_assert_events
Browse files Browse the repository at this point in the history
e2e: deflake test by not relying on events
  • Loading branch information
k8s-ci-robot committed Jun 25, 2021
2 parents 042472d + 5ea0162 commit 7eae33c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
29 changes: 15 additions & 14 deletions test/e2e/common/node/runtimeclass.go
Expand Up @@ -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.
Expand Down Expand Up @@ -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))
}
1 change: 1 addition & 0 deletions test/e2e/framework/events/events.go
Expand Up @@ -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))
Expand Down

0 comments on commit 7eae33c

Please sign in to comment.