Skip to content

Commit 234c055

Browse files
committed
Use logger context in job controller tests
Signed-off-by: Kevin <[email protected]>
1 parent e3a81ab commit 234c055

File tree

2 files changed

+68
-64
lines changed

2 files changed

+68
-64
lines changed

pkg/controller/job/job_controller_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2756,6 +2756,8 @@ func getCondition(job *batch.Job, condition batch.JobConditionType, status v1.Co
27562756
// reaching the active deadline, at which point it is marked as Failed.
27572757
func TestPastDeadlineJobFinished(t *testing.T) {
27582758
_, ctx := ktesting.NewTestContext(t)
2759+
ctx, cancel := context.WithCancel(ctx)
2760+
t.Cleanup(cancel)
27592761
clientset := fake.NewClientset()
27602762
fakeClock := clocktesting.NewFakeClock(time.Now().Truncate(time.Second))
27612763
manager, sharedInformerFactory := newControllerFromClientWithClock(ctx, t, clientset, controller.NoResyncPeriodFunc, fakeClock)
@@ -2765,8 +2767,6 @@ func TestPastDeadlineJobFinished(t *testing.T) {
27652767
controller.NewControllerExpectations(), true, func() {
27662768
},
27672769
}
2768-
ctx, cancel := context.WithCancel(context.Background())
2769-
defer cancel()
27702770
sharedInformerFactory.Start(ctx.Done())
27712771
sharedInformerFactory.WaitForCacheSync(ctx.Done())
27722772

@@ -6549,6 +6549,8 @@ func TestWatchPods(t *testing.T) {
65496549

65506550
func TestWatchOrphanPods(t *testing.T) {
65516551
_, ctx := ktesting.NewTestContext(t)
6552+
ctx, cancel := context.WithCancel(ctx)
6553+
t.Cleanup(cancel)
65526554
clientset := fake.NewClientset()
65536555
sharedInformers := informers.NewSharedInformerFactory(clientset, controller.NoResyncPeriodFunc())
65546556
manager, err := NewController(ctx, sharedInformers.Core().V1().Pods(), sharedInformers.Batch().V1().Jobs(), clientset)
@@ -6594,19 +6596,19 @@ func TestWatchOrphanPods(t *testing.T) {
65946596
sharedInformers.Batch().V1().Jobs().Informer().GetIndexer().Delete(tc.job)
65956597
})
65966598
}
6597-
6599+
_, ctx := ktesting.NewTestContext(t)
65986600
podBuilder := buildPod().name(name).deletionTimestamp().trackingFinalizer()
65996601
if tc.job != nil {
66006602
podBuilder = podBuilder.job(tc.job)
66016603
}
66026604
orphanPod := podBuilder.Pod
6603-
orphanPod, err := clientset.CoreV1().Pods("default").Create(context.Background(), orphanPod, metav1.CreateOptions{})
6605+
orphanPod, err := clientset.CoreV1().Pods("default").Create(ctx, orphanPod, metav1.CreateOptions{})
66046606
if err != nil {
66056607
t.Fatalf("Creating orphan pod: %v", err)
66066608
}
66076609

66086610
if err := wait.PollUntilContextTimeout(ctx, 100*time.Millisecond, wait.ForeverTestTimeout, false, func(ctx context.Context) (bool, error) {
6609-
p, err := clientset.CoreV1().Pods(orphanPod.Namespace).Get(context.Background(), orphanPod.Name, metav1.GetOptions{})
6611+
p, err := clientset.CoreV1().Pods(orphanPod.Namespace).Get(ctx, orphanPod.Name, metav1.GetOptions{})
66106612
if err != nil {
66116613
return false, err
66126614
}
@@ -7610,7 +7612,7 @@ func TestFinalizersRemovedExpectations(t *testing.T) {
76107612
func TestFinalizerCleanup(t *testing.T) {
76117613
_, ctx := ktesting.NewTestContext(t)
76127614
ctx, cancel := context.WithCancel(ctx)
7613-
defer cancel()
7615+
t.Cleanup(cancel)
76147616

76157617
clientset := fake.NewClientset()
76167618
sharedInformers := informers.NewSharedInformerFactory(clientset, controller.NoResyncPeriodFunc())

0 commit comments

Comments
 (0)