Integration test for backoff limit and finalizers

Change-Id: Ic231ce9a5504d3aae4191901d7eb5fe69bf017ac
This commit is contained in:
Aldo Culquicondor 2022-04-14 11:14:14 -04:00
parent a8c582a765
commit f2c8030845

View File

@ -575,6 +575,55 @@ func TestOrphanPodsFinalizersClearedWithGC(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to delete job: %v", err) t.Fatalf("Failed to delete job: %v", err)
} }
validateNoOrphanPodsWithFinalizers(ctx, t, clientSet, jobObj)
})
}
}
func TestFinalizersClearedWhenBackoffLimitExceeded(t *testing.T) {
defer featuregatetesting.SetFeatureGateDuringTest(t, feature.DefaultFeatureGate, features.JobTrackingWithFinalizers, true)()
closeFn, restConfig, clientSet, ns := setup(t, "simple")
defer closeFn()
ctx, cancel := startJobController(restConfig)
defer func() {
cancel()
}()
// Job tracking with finalizers requires less calls in Indexed mode,
// so it's more likely to process all finalizers before all the pods
// are visible.
mode := batchv1.IndexedCompletion
jobObj, err := createJobWithDefaults(ctx, clientSet, ns.Name, &batchv1.Job{
Spec: batchv1.JobSpec{
CompletionMode: &mode,
Completions: pointer.Int32(500),
Parallelism: pointer.Int32(500),
BackoffLimit: pointer.Int32(0),
},
})
if err != nil {
t.Fatalf("Could not create job: %v", err)
}
// Fail a pod ASAP.
err = wait.PollImmediate(time.Millisecond, wait.ForeverTestTimeout, func() (done bool, err error) {
if err := setJobPodsPhase(ctx, clientSet, jobObj, v1.PodFailed, 1); err != nil {
return false, nil
}
return true, nil
})
if err != nil {
t.Fatalf("Could not fail pod: %v", err)
}
validateJobFailed(ctx, t, clientSet, jobObj)
validateNoOrphanPodsWithFinalizers(ctx, t, clientSet, jobObj)
}
func validateNoOrphanPodsWithFinalizers(ctx context.Context, t *testing.T, clientSet clientset.Interface, jobObj *batchv1.Job) {
t.Helper()
orphanPods := 0 orphanPods := 0
if err := wait.Poll(waitInterval, wait.ForeverTestTimeout, func() (done bool, err error) { if err := wait.Poll(waitInterval, wait.ForeverTestTimeout, func() (done bool, err error) {
pods, err := clientSet.CoreV1().Pods(jobObj.Namespace).List(ctx, metav1.ListOptions{ pods, err := clientSet.CoreV1().Pods(jobObj.Namespace).List(ctx, metav1.ListOptions{
@ -594,8 +643,6 @@ func TestOrphanPodsFinalizersClearedWithGC(t *testing.T) {
t.Errorf("Failed waiting for pods to be freed from finalizer: %v", err) t.Errorf("Failed waiting for pods to be freed from finalizer: %v", err)
t.Logf("Last saw %d orphan pods", orphanPods) t.Logf("Last saw %d orphan pods", orphanPods)
} }
})
}
} }
func TestOrphanPodsFinalizersClearedWithFeatureDisabled(t *testing.T) { func TestOrphanPodsFinalizersClearedWithFeatureDisabled(t *testing.T) {
@ -974,16 +1021,26 @@ func getJobConditionStatus(ctx context.Context, job *batchv1.Job, cType batchv1.
return "" return ""
} }
func validateJobFailed(ctx context.Context, t *testing.T, clientSet clientset.Interface, jobObj *batchv1.Job) {
t.Helper()
validateJobCondition(ctx, t, clientSet, jobObj, batchv1.JobFailed)
}
func validateJobSucceeded(ctx context.Context, t *testing.T, clientSet clientset.Interface, jobObj *batchv1.Job) { func validateJobSucceeded(ctx context.Context, t *testing.T, clientSet clientset.Interface, jobObj *batchv1.Job) {
t.Helper()
validateJobCondition(ctx, t, clientSet, jobObj, batchv1.JobComplete)
}
func validateJobCondition(ctx context.Context, t *testing.T, clientSet clientset.Interface, jobObj *batchv1.Job, cond batchv1.JobConditionType) {
t.Helper() t.Helper()
if err := wait.Poll(waitInterval, wait.ForeverTestTimeout, func() (bool, error) { if err := wait.Poll(waitInterval, wait.ForeverTestTimeout, func() (bool, error) {
j, err := clientSet.BatchV1().Jobs(jobObj.Namespace).Get(ctx, jobObj.Name, metav1.GetOptions{}) j, err := clientSet.BatchV1().Jobs(jobObj.Namespace).Get(ctx, jobObj.Name, metav1.GetOptions{})
if err != nil { if err != nil {
t.Fatalf("Failed to obtain updated Job: %v", err) t.Fatalf("Failed to obtain updated Job: %v", err)
} }
return getJobConditionStatus(ctx, j, batchv1.JobComplete) == v1.ConditionTrue, nil return getJobConditionStatus(ctx, j, cond) == v1.ConditionTrue, nil
}); err != nil { }); err != nil {
t.Errorf("Waiting for Job to succeed: %v", err) t.Errorf("Waiting for Job to have condition %s: %v", cond, err)
} }
} }