Skip to content

Commit b3e769b

Browse files
authored
Merge pull request kubernetes#126228 from googs1025/fix_informer
chore(Job): make trivial improvements to job controller unit test
2 parents 6f3f115 + 6626b9c commit b3e769b

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

pkg/controller/job/job_controller_test.go

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2756,7 +2756,7 @@ 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-
clientset := fake.NewSimpleClientset()
2759+
clientset := fake.NewClientset()
27602760
fakeClock := clocktesting.NewFakeClock(time.Now().Truncate(time.Second))
27612761
manager, sharedInformerFactory := newControllerFromClientWithClock(ctx, t, clientset, controller.NoResyncPeriodFunc, fakeClock)
27622762
manager.podStoreSynced = alwaysReady
@@ -6044,7 +6044,7 @@ func TestGetPodsForJob(t *testing.T) {
60446044
if tc.jobDeleted {
60456045
job.DeletionTimestamp = &metav1.Time{}
60466046
}
6047-
clientSet := fake.NewSimpleClientset(job, otherJob)
6047+
clientSet := fake.NewClientset(job, otherJob)
60486048
jm, informer := newControllerFromClient(ctx, t, clientSet, controller.NoResyncPeriodFunc)
60496049
jm.podStoreSynced = alwaysReady
60506050
jm.jobStoreSynced = alwaysReady
@@ -6416,7 +6416,7 @@ func TestSyncJobExpectations(t *testing.T) {
64166416

64176417
func TestWatchJobs(t *testing.T) {
64186418
_, ctx := ktesting.NewTestContext(t)
6419-
clientset := fake.NewSimpleClientset()
6419+
clientset := fake.NewClientset()
64206420
fakeWatch := watch.NewFake()
64216421
clientset.PrependWatchReactor("jobs", core.DefaultWatchReactor(fakeWatch, nil))
64226422
manager, sharedInformerFactory := newControllerFromClient(ctx, t, clientset, controller.NoResyncPeriodFunc)
@@ -6446,9 +6446,8 @@ func TestWatchJobs(t *testing.T) {
64466446
}
64476447
// Start only the job watcher and the workqueue, send a watch event,
64486448
// and make sure it hits the sync method.
6449-
stopCh := make(chan struct{})
6450-
defer close(stopCh)
6451-
sharedInformerFactory.Start(stopCh)
6449+
sharedInformerFactory.Start(ctx.Done())
6450+
sharedInformerFactory.WaitForCacheSync(ctx.Done())
64526451
go manager.Run(ctx, 1)
64536452

64546453
// We're sending new job to see if it reaches syncHandler.
@@ -6462,7 +6461,7 @@ func TestWatchJobs(t *testing.T) {
64626461
func TestWatchPods(t *testing.T) {
64636462
_, ctx := ktesting.NewTestContext(t)
64646463
testJob := newJob(2, 2, 6, batch.NonIndexedCompletion)
6465-
clientset := fake.NewSimpleClientset(testJob)
6464+
clientset := fake.NewClientset(testJob)
64666465
fakeWatch := watch.NewFake()
64676466
clientset.PrependWatchReactor("pods", core.DefaultWatchReactor(fakeWatch, nil))
64686467
manager, sharedInformerFactory := newControllerFromClient(ctx, t, clientset, controller.NoResyncPeriodFunc)
@@ -6493,9 +6492,7 @@ func TestWatchPods(t *testing.T) {
64936492
}
64946493
// Start only the pod watcher and the workqueue, send a watch event,
64956494
// and make sure it hits the sync method for the right job.
6496-
stopCh := make(chan struct{})
6497-
defer close(stopCh)
6498-
go sharedInformerFactory.Core().V1().Pods().Informer().Run(stopCh)
6495+
go sharedInformerFactory.Core().V1().Pods().Informer().Run(ctx.Done())
64996496
go manager.Run(ctx, 1)
65006497

65016498
pods := newPodList(1, v1.PodRunning, testJob)
@@ -6509,7 +6506,7 @@ func TestWatchPods(t *testing.T) {
65096506

65106507
func TestWatchOrphanPods(t *testing.T) {
65116508
_, ctx := ktesting.NewTestContext(t)
6512-
clientset := fake.NewSimpleClientset()
6509+
clientset := fake.NewClientset()
65136510
sharedInformers := informers.NewSharedInformerFactory(clientset, controller.NoResyncPeriodFunc())
65146511
manager, err := NewController(ctx, sharedInformers.Core().V1().Pods(), sharedInformers.Batch().V1().Jobs(), clientset)
65156512
if err != nil {
@@ -6518,11 +6515,9 @@ func TestWatchOrphanPods(t *testing.T) {
65186515
manager.podStoreSynced = alwaysReady
65196516
manager.jobStoreSynced = alwaysReady
65206517

6521-
stopCh := make(chan struct{})
6522-
defer close(stopCh)
65236518
podInformer := sharedInformers.Core().V1().Pods().Informer()
6524-
go podInformer.Run(stopCh)
6525-
cache.WaitForCacheSync(stopCh, podInformer.HasSynced)
6519+
go podInformer.Run(ctx.Done())
6520+
cache.WaitForCacheSync(ctx.Done(), podInformer.HasSynced)
65266521
go manager.Run(ctx, 1)
65276522

65286523
// Create job but don't add it to the store.
@@ -6582,7 +6577,7 @@ func TestWatchOrphanPods(t *testing.T) {
65826577

65836578
func TestSyncOrphanPod(t *testing.T) {
65846579
_, ctx := ktesting.NewTestContext(t)
6585-
clientset := fake.NewSimpleClientset()
6580+
clientset := fake.NewClientset()
65866581
sharedInformers := informers.NewSharedInformerFactory(clientset, controller.NoResyncPeriodFunc())
65876582
manager, err := NewController(ctx, sharedInformers.Core().V1().Pods(), sharedInformers.Batch().V1().Jobs(), clientset)
65886583
if err != nil {
@@ -6591,11 +6586,9 @@ func TestSyncOrphanPod(t *testing.T) {
65916586
manager.podStoreSynced = alwaysReady
65926587
manager.jobStoreSynced = alwaysReady
65936588

6594-
stopCh := make(chan struct{})
6595-
defer close(stopCh)
65966589
podInformer := sharedInformers.Core().V1().Pods().Informer()
6597-
go podInformer.Run(stopCh)
6598-
cache.WaitForCacheSync(stopCh, podInformer.HasSynced)
6590+
go podInformer.Run(ctx.Done())
6591+
cache.WaitForCacheSync(ctx.Done(), podInformer.HasSynced)
65996592
go manager.Run(ctx, 1)
66006593

66016594
cases := map[string]struct {
@@ -7462,7 +7455,7 @@ func TestEnsureJobConditions(t *testing.T) {
74627455

74637456
func TestFinalizersRemovedExpectations(t *testing.T) {
74647457
_, ctx := ktesting.NewTestContext(t)
7465-
clientset := fake.NewSimpleClientset()
7458+
clientset := fake.NewClientset()
74667459
sharedInformers := informers.NewSharedInformerFactory(clientset, controller.NoResyncPeriodFunc())
74677460
manager, err := NewController(ctx, sharedInformers.Core().V1().Pods(), sharedInformers.Batch().V1().Jobs(), clientset)
74687461
if err != nil {
@@ -7506,10 +7499,8 @@ func TestFinalizersRemovedExpectations(t *testing.T) {
75067499
t.Errorf("Different expectations for removed finalizers after syncJob (-want,+got):\n%s", diff)
75077500
}
75087501

7509-
stopCh := make(chan struct{})
7510-
defer close(stopCh)
7511-
go sharedInformers.Core().V1().Pods().Informer().Run(stopCh)
7512-
cache.WaitForCacheSync(stopCh, podInformer.HasSynced)
7502+
go sharedInformers.Core().V1().Pods().Informer().Run(ctx.Done())
7503+
cache.WaitForCacheSync(ctx.Done(), podInformer.HasSynced)
75137504

75147505
// Make sure the first syncJob sets the expectations, even after the caches synced.
75157506
gotExpectedUIDs = manager.finalizerExpectations.getExpectedUIDs(jobKey)
@@ -7568,7 +7559,7 @@ func TestFinalizerCleanup(t *testing.T) {
75687559
ctx, cancel := context.WithCancel(ctx)
75697560
defer cancel()
75707561

7571-
clientset := fake.NewSimpleClientset()
7562+
clientset := fake.NewClientset()
75727563
sharedInformers := informers.NewSharedInformerFactory(clientset, controller.NoResyncPeriodFunc())
75737564
manager, err := NewController(ctx, sharedInformers.Core().V1().Pods(), sharedInformers.Batch().V1().Jobs(), clientset)
75747565
if err != nil {

0 commit comments

Comments
 (0)