@@ -7624,9 +7624,9 @@ func TestFinalizerCleanup(t *testing.T) {
7624
7624
// Start the Pod and Job informers.
7625
7625
sharedInformers .Start (ctx .Done ())
7626
7626
sharedInformers .WaitForCacheSync (ctx .Done ())
7627
- // Initialize the controller with 0 workers to make sure the
7628
- // pod finalizers are not removed by the "syncJob" function.
7629
- go manager .Run (ctx , 0 )
7627
+ // Initialize the controller with 1 worker to make sure the
7628
+ // pod finalizers are removed by the "syncJob" function.
7629
+ go manager .Run (ctx , 1 )
7630
7630
// Make sure the pod finalizers are removed by the "orphanWorker" function.
7631
7631
go wait .UntilWithContext (ctx , manager .orphanWorker , time .Second )
7632
7632
@@ -7645,22 +7645,24 @@ func TestFinalizerCleanup(t *testing.T) {
7645
7645
t .Fatalf ("Waiting for Job object to appear in jobLister: %v" , err )
7646
7646
}
7647
7647
7648
- // Create a Pod with the job tracking finalizer
7649
- pod := newPod ("test-pod" , job )
7650
- pod .Finalizers = append (pod .Finalizers , batch .JobTrackingFinalizer )
7651
- pod , err = clientset .CoreV1 ().Pods (pod .GetNamespace ()).Create (ctx , pod , metav1.CreateOptions {})
7648
+ selector , err := metav1 .LabelSelectorAsSelector (job .Spec .Selector )
7652
7649
if err != nil {
7653
- t .Fatalf ("Creating pod : %v" , err )
7650
+ t .Fatalf ("Error parsing job selector : %v" , err )
7654
7651
}
7655
7652
7656
- // Await for the Pod to appear in the podStore to ensure that the pod exists when cleaning up the Job.
7657
- // In a production environment, there wouldn't be these guarantees, but the Pod would be cleaned up
7658
- // by the orphan pod worker, when the Pod finishes.
7653
+ var pods []* v1.Pod
7654
+ // Wait for the Pod to be created by the Job controller
7659
7655
if err := wait .PollUntilContextTimeout (ctx , 10 * time .Millisecond , wait .ForeverTestTimeout , false , func (ctx context.Context ) (bool , error ) {
7660
- pod , _ := manager .podStore .Pods (pod .GetNamespace ()).Get (pod .Name )
7661
- return pod != nil , nil
7656
+ pods , err = manager .podStore .Pods (job .Namespace ).List (selector )
7657
+ if err != nil {
7658
+ return false , err
7659
+ }
7660
+ if len (pods ) > 0 {
7661
+ return true , nil
7662
+ }
7663
+ return false , nil
7662
7664
}); err != nil {
7663
- t .Fatalf ("Waiting for Pod to appear in podLister : %v" , err )
7665
+ t .Fatalf ("Waiting for Pod to be created by Job : %v" , err )
7664
7666
}
7665
7667
7666
7668
// Mark Job as complete.
@@ -7673,10 +7675,9 @@ func TestFinalizerCleanup(t *testing.T) {
7673
7675
t .Fatalf ("Updating job status: %v" , err )
7674
7676
}
7675
7677
7676
- // Verify the pod finalizer is removed for a finished Job,
7677
- // even if the jobs pods are not tracked by the main reconciliation loop.
7678
+ // Verify the pod finalizer is removed for a finished Job
7678
7679
if err := wait .PollUntilContextTimeout (ctx , 100 * time .Millisecond , wait .ForeverTestTimeout , true , func (ctx context.Context ) (bool , error ) {
7679
- p , err := clientset .CoreV1 ().Pods (pod .Namespace ).Get (ctx , pod .Name , metav1.GetOptions {})
7680
+ p , err := clientset .CoreV1 ().Pods (pods [ 0 ] .Namespace ).Get (ctx , pods [ 0 ] .Name , metav1.GetOptions {})
7680
7681
if err != nil {
7681
7682
return false , err
7682
7683
}
0 commit comments