Skip to content

Commit

Permalink
test: unit test for getNextSchedule
Browse files Browse the repository at this point in the history
Adding unit test for getNextSchedule function in the
reclaimspacecronjob_controller.

Signed-off-by: karthik-us <[email protected]>
  • Loading branch information
karthik-us committed Feb 16, 2024
1 parent b5051aa commit 43c740e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
1 change: 0 additions & 1 deletion controllers/csiaddons/reclaimspacecronjob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
85 changes: 85 additions & 0 deletions controllers/csiaddons/reclaimspacecronjob_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ package controllers

import (
"fmt"
"reflect"
"testing"
"time"

csiaddonsv1alpha1 "github.com/csi-addons/kubernetes-csi-addons/apis/csiaddons/v1alpha1"

"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
)

func TestGetScheduledTimeForRSJob(t *testing.T) {
Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit 43c740e

Please sign in to comment.