Skip to content

Commit

Permalink
ci: fix TestGetNextSchedule failure
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
karthik-us committed Mar 5, 2024
1 parent 7eef960 commit 8d51d02
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions controllers/csiaddons/reclaimspacecronjob_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,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
Expand All @@ -132,12 +133,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,
},
Expand All @@ -150,12 +151,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,
},
Expand Down Expand Up @@ -203,7 +204,7 @@ func TestGetNextSchedule(t *testing.T) {
t.Errorf("getNextSchedule() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotLastMissed, tt.lastMissed) {
if !reflect.DeepEqual(gotLastMissed, tt.lastMissed) && !reflect.DeepEqual(gotLastMissed, time.Time{}) {
t.Errorf("getNextSchedule() got last missed = %v, want %v", gotLastMissed, tt.lastMissed)
}
if !reflect.DeepEqual(gotNextSchedule, tt.nextSchedule) {
Expand Down

0 comments on commit 8d51d02

Please sign in to comment.