fix: Refactor TestFinalizerCleanup unit test by asserting job and pod informer state (#121856)

* fix: change informer resync period in TestFinalizerCleanup unit test

* refactor TestFinalizerCleanup to avoid flakiness by asserting job and pod informer state

* minor cleanups in TestFinalizerCleanup test in job_controller_test.go

another minor cleanup in TestFinalizerCleanup test in job_controller_test.go

* reduce poll time to 10ms in TestFinalizerCleanup in job_controller_test.go when waiting for podStore cache to get updated

* remove a whitespace from TestFinalizerCleanup to keep diff smaller
This commit is contained in:
Dejan Zele Pejchev 2023-12-13 23:55:28 +01:00 committed by GitHub
parent b0ccc04e47
commit 31710a799d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5261,13 +5261,12 @@ func TestFinalizerCleanup(t *testing.T) {
manager.podStoreSynced = alwaysReady
manager.jobStoreSynced = alwaysReady
// Initialize the controller with 0 workers to make sure the
// pod finalizers are not removed by the "syncJob" function.
go manager.Run(ctx, 0)
// Start the Pod and Job informers.
sharedInformers.Start(ctx.Done())
sharedInformers.WaitForCacheSync(ctx.Done())
// Initialize the controller with 0 workers to make sure the
// pod finalizers are not removed by the "syncJob" function.
go manager.Run(ctx, 0)
// Create a simple Job
job := newJob(1, 1, 1, batch.NonIndexedCompletion)
@ -5276,6 +5275,14 @@ func TestFinalizerCleanup(t *testing.T) {
t.Fatalf("Creating job: %v", err)
}
// Await for the Job to appear in the jobLister to ensure so that Job Pod gets tracked correctly.
if err := wait.PollUntilContextTimeout(ctx, 10*time.Millisecond, wait.ForeverTestTimeout, false, func(ctx context.Context) (bool, error) {
job, _ := manager.jobLister.Jobs(job.GetNamespace()).Get(job.Name)
return job != nil, nil
}); err != nil {
t.Fatalf("Waiting for Job object to appear in jobLister: %v", err)
}
// Create a Pod with the job tracking finalizer
pod := newPod("test-pod", job)
pod.Finalizers = append(pod.Finalizers, batch.JobTrackingFinalizer)
@ -5284,6 +5291,16 @@ func TestFinalizerCleanup(t *testing.T) {
t.Fatalf("Creating pod: %v", err)
}
// Await for the Pod to appear in the podStore to ensure that the pod exists when cleaning up the Job.
// In a production environment, there wouldn't be these guarantees, but the Pod would be cleaned up
// by the orphan pod worker, when the Pod finishes.
if err := wait.PollUntilContextTimeout(ctx, 10*time.Millisecond, wait.ForeverTestTimeout, false, func(ctx context.Context) (bool, error) {
pod, _ := manager.podStore.Pods(pod.GetNamespace()).Get(pod.Name)
return pod != nil, nil
}); err != nil {
t.Fatalf("Waiting for Pod to appear in podLister: %v", err)
}
// Mark Job as complete.
job.Status.Conditions = append(job.Status.Conditions, batch.JobCondition{
Type: batch.JobComplete,