Skip to content

Commit d3d06c3

Browse files
authored
Merge pull request kubernetes#123826 from tenzen-y/use-fake-client-job-unit
Job: Use the fake clock in TestTrackJobStatusAndRemoveFinalizers
2 parents 28c4d00 + f2508df commit d3d06c3

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

pkg/controller/job/job_controller_test.go

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,9 +1274,12 @@ func TestGetNewFinshedPods(t *testing.T) {
12741274

12751275
func TestTrackJobStatusAndRemoveFinalizers(t *testing.T) {
12761276
logger, ctx := ktesting.NewTestContext(t)
1277-
completedCond := newCondition(batch.JobComplete, v1.ConditionTrue, "", "", realClock.Now())
1278-
succeededCond := newCondition(batch.JobSuccessCriteriaMet, v1.ConditionTrue, "", "", realClock.Now())
1279-
failedCond := newCondition(batch.JobFailed, v1.ConditionTrue, "", "", realClock.Now())
1277+
fakeClock := clocktesting.NewFakeClock(time.Now())
1278+
now := fakeClock.Now()
1279+
minuteAgo := now.Add(-time.Minute)
1280+
completedCond := newCondition(batch.JobComplete, v1.ConditionTrue, "", "", now)
1281+
succeededCond := newCondition(batch.JobSuccessCriteriaMet, v1.ConditionTrue, "", "", minuteAgo)
1282+
failedCond := newCondition(batch.JobFailed, v1.ConditionTrue, "", "", now)
12801283
indexedCompletion := batch.IndexedCompletion
12811284
mockErr := errors.New("mock error")
12821285
cases := map[string]struct {
@@ -1443,7 +1446,7 @@ func TestTrackJobStatusAndRemoveFinalizers(t *testing.T) {
14431446
Succeeded: 1,
14441447
Failed: 1,
14451448
Conditions: []batch.JobCondition{*succeededCond, *completedCond},
1446-
CompletionTime: &succeededCond.LastTransitionTime,
1449+
CompletionTime: ptr.To(metav1.NewTime(now)),
14471450
},
14481451
},
14491452
wantSucceededPodsMetric: 1,
@@ -1933,7 +1936,7 @@ func TestTrackJobStatusAndRemoveFinalizers(t *testing.T) {
19331936
defer featuregatetesting.SetFeatureGateDuringTest(t, feature.DefaultFeatureGate, features.JobSuccessPolicy, tc.enableJobSuccessPolicy)()
19341937

19351938
clientSet := clientset.NewForConfigOrDie(&restclient.Config{Host: "", ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
1936-
manager, _ := newControllerFromClient(ctx, t, clientSet, controller.NoResyncPeriodFunc)
1939+
manager, _ := newControllerFromClientWithClock(ctx, t, clientSet, controller.NoResyncPeriodFunc, fakeClock)
19371940
fakePodControl := controller.FakePodControl{Err: tc.podControlErr}
19381941
metrics.JobPodsFinished.Reset()
19391942
manager.podControl = &fakePodControl
@@ -1966,24 +1969,10 @@ func TestTrackJobStatusAndRemoveFinalizers(t *testing.T) {
19661969
if !errors.Is(err, tc.wantErr) {
19671970
t.Errorf("Got error %v, want %v", err, tc.wantErr)
19681971
}
1969-
cmpOpts := []cmp.Option{cmpopts.IgnoreFields(batch.JobCondition{}, "LastProbeTime", "LastTransitionTime")}
1970-
if tc.finishedCond != nil && tc.finishedCond.Type == batch.JobSuccessCriteriaMet {
1971-
cmpOpts = append(cmpOpts, cmpopts.IgnoreFields(batch.JobStatus{}, "CompletionTime"))
1972-
}
1973-
if diff := cmp.Diff(tc.wantStatusUpdates, statusUpdates, cmpOpts...); diff != "" {
1972+
if diff := cmp.Diff(tc.wantStatusUpdates, statusUpdates,
1973+
cmpopts.IgnoreFields(batch.JobCondition{}, "LastProbeTime", "LastTransitionTime")); diff != "" {
19741974
t.Errorf("Unexpected status updates (-want,+got):\n%s", diff)
19751975
}
1976-
// If we set successCondition with the SuccessCriteriaMet, the job-controller adds the Complete condition to the Job while reconciling,
1977-
// then the added Complete condition LastTransitionTime is used as a CompletionTime.
1978-
// So, we verify if the CompletionTime is after the SuccessCriteriaMet LastTransitionTime.
1979-
if tc.finishedCond != nil && tc.finishedCond.Type == batch.JobSuccessCriteriaMet && len(tc.wantStatusUpdates) != 0 {
1980-
for i := range tc.wantStatusUpdates {
1981-
if tc.wantStatusUpdates[i].CompletionTime != nil && !tc.wantStatusUpdates[i].CompletionTime.Before(statusUpdates[i].CompletionTime) {
1982-
t.Errorf("Unexpected completionTime; completionTime %v must be after %v",
1983-
tc.wantStatusUpdates[i].CompletionTime, statusUpdates[i].CompletionTime)
1984-
}
1985-
}
1986-
}
19871976
rmFinalizers := len(fakePodControl.Patches)
19881977
if rmFinalizers != tc.wantRmFinalizers {
19891978
t.Errorf("Removed %d finalizers, want %d", rmFinalizers, tc.wantRmFinalizers)

0 commit comments

Comments
 (0)