Skip to content

Commit

Permalink
Merge pull request kubernetes#126228 from googs1025/fix_informer
Browse files Browse the repository at this point in the history
chore(Job):  make trivial improvements to job controller unit test
  • Loading branch information
k8s-ci-robot authored Jul 19, 2024
2 parents 6f3f115 + 6626b9c commit b3e769b
Showing 1 changed file with 17 additions and 26 deletions.
43 changes: 17 additions & 26 deletions pkg/controller/job/job_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2756,7 +2756,7 @@ func getCondition(job *batch.Job, condition batch.JobConditionType, status v1.Co
// reaching the active deadline, at which point it is marked as Failed.
func TestPastDeadlineJobFinished(t *testing.T) {
_, ctx := ktesting.NewTestContext(t)
clientset := fake.NewSimpleClientset()
clientset := fake.NewClientset()
fakeClock := clocktesting.NewFakeClock(time.Now().Truncate(time.Second))
manager, sharedInformerFactory := newControllerFromClientWithClock(ctx, t, clientset, controller.NoResyncPeriodFunc, fakeClock)
manager.podStoreSynced = alwaysReady
Expand Down Expand Up @@ -6044,7 +6044,7 @@ func TestGetPodsForJob(t *testing.T) {
if tc.jobDeleted {
job.DeletionTimestamp = &metav1.Time{}
}
clientSet := fake.NewSimpleClientset(job, otherJob)
clientSet := fake.NewClientset(job, otherJob)
jm, informer := newControllerFromClient(ctx, t, clientSet, controller.NoResyncPeriodFunc)
jm.podStoreSynced = alwaysReady
jm.jobStoreSynced = alwaysReady
Expand Down Expand Up @@ -6416,7 +6416,7 @@ func TestSyncJobExpectations(t *testing.T) {

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

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

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

func TestWatchOrphanPods(t *testing.T) {
_, ctx := ktesting.NewTestContext(t)
clientset := fake.NewSimpleClientset()
clientset := fake.NewClientset()
sharedInformers := informers.NewSharedInformerFactory(clientset, controller.NoResyncPeriodFunc())
manager, err := NewController(ctx, sharedInformers.Core().V1().Pods(), sharedInformers.Batch().V1().Jobs(), clientset)
if err != nil {
Expand All @@ -6518,11 +6515,9 @@ func TestWatchOrphanPods(t *testing.T) {
manager.podStoreSynced = alwaysReady
manager.jobStoreSynced = alwaysReady

stopCh := make(chan struct{})
defer close(stopCh)
podInformer := sharedInformers.Core().V1().Pods().Informer()
go podInformer.Run(stopCh)
cache.WaitForCacheSync(stopCh, podInformer.HasSynced)
go podInformer.Run(ctx.Done())
cache.WaitForCacheSync(ctx.Done(), podInformer.HasSynced)
go manager.Run(ctx, 1)

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

func TestSyncOrphanPod(t *testing.T) {
_, ctx := ktesting.NewTestContext(t)
clientset := fake.NewSimpleClientset()
clientset := fake.NewClientset()
sharedInformers := informers.NewSharedInformerFactory(clientset, controller.NoResyncPeriodFunc())
manager, err := NewController(ctx, sharedInformers.Core().V1().Pods(), sharedInformers.Batch().V1().Jobs(), clientset)
if err != nil {
Expand All @@ -6591,11 +6586,9 @@ func TestSyncOrphanPod(t *testing.T) {
manager.podStoreSynced = alwaysReady
manager.jobStoreSynced = alwaysReady

stopCh := make(chan struct{})
defer close(stopCh)
podInformer := sharedInformers.Core().V1().Pods().Informer()
go podInformer.Run(stopCh)
cache.WaitForCacheSync(stopCh, podInformer.HasSynced)
go podInformer.Run(ctx.Done())
cache.WaitForCacheSync(ctx.Done(), podInformer.HasSynced)
go manager.Run(ctx, 1)

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

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

stopCh := make(chan struct{})
defer close(stopCh)
go sharedInformers.Core().V1().Pods().Informer().Run(stopCh)
cache.WaitForCacheSync(stopCh, podInformer.HasSynced)
go sharedInformers.Core().V1().Pods().Informer().Run(ctx.Done())
cache.WaitForCacheSync(ctx.Done(), podInformer.HasSynced)

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

clientset := fake.NewSimpleClientset()
clientset := fake.NewClientset()
sharedInformers := informers.NewSharedInformerFactory(clientset, controller.NoResyncPeriodFunc())
manager, err := NewController(ctx, sharedInformers.Core().V1().Pods(), sharedInformers.Batch().V1().Jobs(), clientset)
if err != nil {
Expand Down

0 comments on commit b3e769b

Please sign in to comment.