From 43c740ec2b560f62253375882dbae4c1607587db Mon Sep 17 00:00:00 2001 From: karthik-us Date: Fri, 16 Feb 2024 10:46:55 +0530 Subject: [PATCH] test: unit test for getNextSchedule Adding unit test for getNextSchedule function in the reclaimspacecronjob_controller. Signed-off-by: karthik-us --- .../reclaimspacecronjob_controller.go | 1 - .../reclaimspacecronjob_controller_test.go | 85 +++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/controllers/csiaddons/reclaimspacecronjob_controller.go b/controllers/csiaddons/reclaimspacecronjob_controller.go index 80250a0af..79a3f3fdc 100644 --- a/controllers/csiaddons/reclaimspacecronjob_controller.go +++ b/controllers/csiaddons/reclaimspacecronjob_controller.go @@ -352,7 +352,6 @@ func getScheduledTimeForRSJob(rsJob *csiaddonsv1alpha1.ReclaimSpaceJob) (*time.T return &timeParsed, nil } -// TODO: add unit test for getNextSchedule // getNextSchedule returns lastMissed and next time after parsing the schedule. // This function returns error if there are more than 100 missed start times. func getNextSchedule( diff --git a/controllers/csiaddons/reclaimspacecronjob_controller_test.go b/controllers/csiaddons/reclaimspacecronjob_controller_test.go index 62a9a9110..1882305a7 100644 --- a/controllers/csiaddons/reclaimspacecronjob_controller_test.go +++ b/controllers/csiaddons/reclaimspacecronjob_controller_test.go @@ -18,6 +18,7 @@ package controllers import ( "fmt" + "reflect" "testing" "time" @@ -25,6 +26,7 @@ import ( "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/ptr" ) func TestGetScheduledTimeForRSJob(t *testing.T) { @@ -108,3 +110,86 @@ func TestGetScheduledTimeForRSJob(t *testing.T) { }) } } + +func TestGetNextSchedule(t *testing.T) { + type args struct { + rsCronJob *csiaddonsv1alpha1.ReclaimSpaceCronJob + now time.Time + } + tests := []struct { + name string + args args + lastMissed time.Time + nextSchedule time.Time + wantErr bool + }{ + { + name: "Valid schedule, no deadline, last schedule time exists", + args: args{ + rsCronJob: &csiaddonsv1alpha1.ReclaimSpaceCronJob{ + Spec: csiaddonsv1alpha1.ReclaimSpaceCronJobSpec{ + Schedule: "0 0 * * *", + }, + Status: csiaddonsv1alpha1.ReclaimSpaceCronJobStatus{ + LastScheduleTime: &metav1.Time{Time: time.Now().Add(-time.Hour)}, + }, + }, + now: time.Now(), + }, + lastMissed: time.Time{}, + nextSchedule: time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Local).Add(time.Hour * 24), + wantErr: false, + }, + { + name: "Valid schedule, deadline not exceeded, last schedule time exists", + args: args{ + rsCronJob: &csiaddonsv1alpha1.ReclaimSpaceCronJob{ + Spec: csiaddonsv1alpha1.ReclaimSpaceCronJobSpec{ + Schedule: "0 0 * * *", + StartingDeadlineSeconds: ptr.To(int64(3600)), + }, + Status: csiaddonsv1alpha1.ReclaimSpaceCronJobStatus{ + LastScheduleTime: &metav1.Time{Time: time.Now().Add(-time.Hour)}, + }, + }, + now: time.Now(), + }, + lastMissed: time.Time{}, + nextSchedule: time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Local).Add(time.Hour * 24), + wantErr: false, + }, + { + name: "Valid schedule, deadline exceeded, last schedule time exists", + args: args{ + rsCronJob: &csiaddonsv1alpha1.ReclaimSpaceCronJob{ + Spec: csiaddonsv1alpha1.ReclaimSpaceCronJobSpec{ + Schedule: "*/1 * * * *", + StartingDeadlineSeconds: ptr.To(int64(6060)), + }, + Status: csiaddonsv1alpha1.ReclaimSpaceCronJobStatus{ + LastScheduleTime: &metav1.Time{Time: time.Now().Add(-time.Hour * 2)}, + }, + }, + now: time.Now(), + }, + lastMissed: time.Time{}, + nextSchedule: time.Time{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotLastMissed, gotNextSchedule, err := getNextSchedule(tt.args.rsCronJob, tt.args.now) + if (err != nil) != tt.wantErr { + t.Errorf("getNextSchedule() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotLastMissed, tt.lastMissed) { + t.Errorf("getNextSchedule() got last missed = %v, want %v", gotLastMissed, tt.lastMissed) + } + if !reflect.DeepEqual(gotNextSchedule, tt.nextSchedule) { + t.Errorf("getNextSchedule() got next schedule = %v, want %v", gotNextSchedule, tt.nextSchedule) + } + }) + } +}