Skip to content

Commit 5856d3e

Browse files
committedMar 20, 2025
DRA taint eviction: fix waiting in unit test
Events get recorded in the apiserver asynchronously, so even if the test knows that the event has been evicted because the pod is deleted, it still has to also check for the event to be recorded. This caused a flake in the "Consistently" check of events.
1 parent ac6e47c commit 5856d3e

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed
 

‎pkg/controller/devicetainteviction/device_taint_eviction_test.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -1343,18 +1343,18 @@ func TestEviction(t *testing.T) {
13431343
}()
13441344

13451345
// Eventually the controller should have synced it's informers.
1346-
require.Eventually(tCtx, func() bool {
1346+
ktesting.Eventually(tCtx, func(tCtx ktesting.TContext) bool {
13471347
return controller.hasSynced.Load() > 0
1348-
}, 30*time.Second, time.Millisecond, "controller synced")
1348+
}).WithTimeout(30 * time.Second).Should(gomega.BeTrueBecause("controller synced"))
13491349
if tt.afterSync != nil {
13501350
tt.afterSync(tCtx)
13511351
}
13521352

13531353
// Eventually the pod gets deleted (= evicted).
1354-
assert.Eventually(tCtx, func() bool {
1354+
ktesting.Eventually(tCtx, func(tCtx ktesting.TContext) bool {
13551355
_, err := fakeClientset.CoreV1().Pods(pod.Namespace).Get(tCtx, pod.Name, metav1.GetOptions{})
13561356
return apierrors.IsNotFound(err)
1357-
}, 30*time.Second, time.Millisecond, "pod evicted")
1357+
}).WithTimeout(30 * time.Second).Should(gomega.BeTrueBecause("pod evicted"))
13581358

13591359
pod := pod.DeepCopy()
13601360
pod.Status.Conditions = []v1.PodCondition{{
@@ -1369,7 +1369,10 @@ func TestEviction(t *testing.T) {
13691369

13701370
// Shortly after deletion we should also see updated metrics.
13711371
// This is the last thing the controller does for a pod.
1372+
// However, actually creating the event on the server is asynchronous,
1373+
// so we also have to wait for that.
13721374
ktesting.Eventually(tCtx, func(tCtx ktesting.TContext) error {
1375+
gomega.NewWithT(tCtx).Expect(listEvents(tCtx)).Should(matchDeletionEvent())
13731376
return testPodDeletionsMetrics(controller, 1)
13741377
}).WithTimeout(30*time.Second).Should(gomega.Succeed(), "pod eviction done")
13751378

@@ -1547,11 +1550,11 @@ func TestParallelPodDeletion(t *testing.T) {
15471550
}()
15481551

15491552
// Eventually the pod gets deleted, in this test by us.
1550-
assert.Eventually(tCtx, func() bool {
1553+
ktesting.Eventually(tCtx, func(tCtx ktesting.TContext) bool {
15511554
mutex.Lock()
15521555
defer mutex.Unlock()
15531556
return podGets >= 1
1554-
}, 30*time.Second, time.Millisecond, "pod eviction started")
1557+
}).WithTimeout(30 * time.Second).Should(gomega.BeTrueBecause("pod eviction started"))
15551558

15561559
// We don't want any events.
15571560
ktesting.Consistently(tCtx, func(tCtx ktesting.TContext) error {
@@ -1625,8 +1628,9 @@ func TestRetry(t *testing.T) {
16251628
assert.NoError(tCtx, controller.Run(tCtx), "eviction controller failed")
16261629
}()
16271630

1628-
// Eventually the pod gets deleted.
1631+
// Eventually the pod gets deleted and the event is recorded.
16291632
ktesting.Eventually(tCtx, func(tCtx ktesting.TContext) error {
1633+
gomega.NewWithT(tCtx).Expect(listEvents(tCtx)).Should(matchDeletionEvent())
16301634
return testPodDeletionsMetrics(controller, 1)
16311635
}).WithTimeout(30*time.Second).Should(gomega.Succeed(), "pod eviction done")
16321636

@@ -1698,11 +1702,11 @@ func TestEvictionFailure(t *testing.T) {
16981702
}()
16991703

17001704
// Eventually deletion is attempted a few times.
1701-
assert.Eventually(tCtx, func() bool {
1705+
ktesting.Eventually(tCtx, func(tCtx ktesting.TContext) int {
17021706
mutex.Lock()
17031707
defer mutex.Unlock()
1704-
return podDeletions >= retries
1705-
}, 30*time.Second, time.Millisecond, "pod eviction failed")
1708+
return podDeletions
1709+
}).WithTimeout(30*time.Second).Should(gomega.BeNumerically(">=", retries), "pod eviction failed")
17061710

17071711
// Now we can check the API calls.
17081712
ktesting.Consistently(tCtx, func(tCtx ktesting.TContext) error {

0 commit comments

Comments
 (0)