Skip to content

Commit

Permalink
e2e: 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 1bb8af6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
2 changes: 1 addition & 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 Expand Up @@ -397,5 +396,6 @@ func getNextSchedule(
" delete and recreate reclaimspacecronjob.")
}
}

return lastMissed, sched.Next(now), nil
}
86 changes: 85 additions & 1 deletion controllers/csiaddons/reclaimspacecronjob_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ 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/pointer"
)

func TestGetScheduledTimeForRSJob(t *testing.T) {
Expand Down Expand Up @@ -108,3 +109,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
want time.Time
want1 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(),
},
want: time.Time{},
want1: 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: pointer.Int64(3600),
},
Status: csiaddonsv1alpha1.ReclaimSpaceCronJobStatus{
LastScheduleTime: &metav1.Time{Time: time.Now().Add(-time.Hour)},
},
},
now: time.Now(),
},
want: time.Time{},
want1: 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: pointer.Int64(6060),
},
Status: csiaddonsv1alpha1.ReclaimSpaceCronJobStatus{
LastScheduleTime: &metav1.Time{Time: time.Now().Add(-time.Hour * 2)},
},
},
now: time.Now(),
},
want: time.Time{},
want1: time.Time{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, 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(got, tt.want) {
t.Errorf("getNextSchedule() got = %v, want %v", got, tt.want)
}
if !reflect.DeepEqual(got1, tt.want1) {
t.Errorf("getNextSchedule() got1 = %v, want %v", got1, tt.want1)
}
})
}
}

0 comments on commit 1bb8af6

Please sign in to comment.