From d04d363786c873aaf87964368fb5480238c08642 Mon Sep 17 00:00:00 2001 From: karthik-us Date: Tue, 5 Mar 2024 23:08:13 +0530 Subject: [PATCH] ci: fix TestGetNextSchedule failure The test case was failing if the test was run at some particular time of the day. For ex: If the test was triggered @0:15:0, the last scheduled time will be 23:15:0 of the previous day since we are setting it to an hour earlier than the current time. Now when we calculate the getNextSchedule, the next schedule will point to the current day, when the cron job is scheduled to run at midnight every day. So in this case, the lastMissed will be pointing to a valid time. This leads to some test cases to get unexpected value for the lastMissed time which expects it to be time.Time{}. Though we get the expected wantErr in such cases too, since we are comparing the other return values seperately. To avoid such failures, this patch checks for multiple expected last scheduled times. Also the last scheduled time is aligned with the cron job schedule to keep it relative. Signed-off-by: karthik-us --- .../reclaimspacecronjob_controller_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/controllers/csiaddons/reclaimspacecronjob_controller_test.go b/controllers/csiaddons/reclaimspacecronjob_controller_test.go index f84aea4cf..b9676c490 100644 --- a/controllers/csiaddons/reclaimspacecronjob_controller_test.go +++ b/controllers/csiaddons/reclaimspacecronjob_controller_test.go @@ -18,7 +18,6 @@ package controllers import ( "fmt" - "reflect" "testing" "time" @@ -113,6 +112,7 @@ func TestGetScheduledTimeForRSJob(t *testing.T) { func TestGetNextSchedule(t *testing.T) { now := time.Now() + expectedLastMissed := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local) type args struct { rsCronJob *csiaddonsv1alpha1.ReclaimSpaceCronJob now time.Time @@ -132,12 +132,12 @@ func TestGetNextSchedule(t *testing.T) { Schedule: "0 0 * * *", }, Status: csiaddonsv1alpha1.ReclaimSpaceCronJobStatus{ - LastScheduleTime: &metav1.Time{Time: now.Add(-time.Hour)}, + LastScheduleTime: &metav1.Time{Time: now.Add(-24 * time.Hour).Truncate(24 * time.Hour).Local()}, }, }, now: now, }, - lastMissed: time.Time{}, + lastMissed: expectedLastMissed, nextSchedule: time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Add(time.Hour * 24), wantErr: false, }, @@ -150,12 +150,12 @@ func TestGetNextSchedule(t *testing.T) { StartingDeadlineSeconds: ptr.To(int64(3600)), }, Status: csiaddonsv1alpha1.ReclaimSpaceCronJobStatus{ - LastScheduleTime: &metav1.Time{Time: now.Add(-time.Hour)}, + LastScheduleTime: &metav1.Time{Time: now.Add(-24 * time.Hour).Truncate(24 * time.Hour).Local()}, }, }, now: now, }, - lastMissed: time.Time{}, + lastMissed: expectedLastMissed, nextSchedule: time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Add(time.Hour * 24), wantErr: false, }, @@ -203,10 +203,10 @@ func TestGetNextSchedule(t *testing.T) { t.Errorf("getNextSchedule() error = %v, wantErr %v", err, tt.wantErr) return } - if !reflect.DeepEqual(gotLastMissed, tt.lastMissed) { + if !gotLastMissed.Equal(tt.lastMissed) && !gotLastMissed.Equal(time.Time{}) { t.Errorf("getNextSchedule() got last missed = %v, want %v", gotLastMissed, tt.lastMissed) } - if !reflect.DeepEqual(gotNextSchedule, tt.nextSchedule) { + if !gotNextSchedule.Equal(tt.nextSchedule) { t.Errorf("getNextSchedule() got next schedule = %v, want %v", gotNextSchedule, tt.nextSchedule) } })