Skip to content

Commit

Permalink
reclaimspace: add unit test case for schedule handling
Browse files Browse the repository at this point in the history
Signed-off-by: Praveen M <[email protected]>
  • Loading branch information
iPraveenParihar committed Jun 27, 2024
1 parent a2439ac commit dcac38b
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions controllers/csiaddons/persistentvolumeclaim_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ import (
"testing"

csiaddonsv1alpha1 "github.com/csi-addons/kubernetes-csi-addons/apis/csiaddons/v1alpha1"
"github.com/csi-addons/kubernetes-csi-addons/internal/connection"
"github.com/go-logr/logr"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/log"
)

Expand Down Expand Up @@ -248,3 +252,96 @@ func TestGetScheduleFromAnnotation(t *testing.T) {
})
}
}

func TestDetermineScheduleAndRequeue(t *testing.T) {
type args struct {
pvcAnnotations map[string]string
nsAnnotations map[string]string
scAnnotations map[string]string
}
tests := []struct {
name string
args args
want string
}{
{
name: "pvc annotation set",
args: args{
pvcAnnotations: map[string]string{rsCronJobScheduleTimeAnnotation: "@daily"},
},
want: "@daily",
},
{
name: "sc annotation set",
args: args{
scAnnotations: map[string]string{rsCronJobScheduleTimeAnnotation: "@monthly"},
},
want: "@monthly",
},
{
name: "pvc & sc annotation set",
args: args{
pvcAnnotations: map[string]string{rsCronJobScheduleTimeAnnotation: "@daily"},
scAnnotations: map[string]string{rsCronJobScheduleTimeAnnotation: "@weekly"},
},
want: "@daily",
},
}

ctx := context.TODO()
logger := logr.Discard()
client := fake.NewClientBuilder().Build()
driverName := "test-driver"

ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-namespace",
},
}
sc := &storagev1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: "test-sc",
},
}
pvc := &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pvc",
Namespace: ns.Name,
},
Spec: corev1.PersistentVolumeClaimSpec{
StorageClassName: &sc.Name,
},
}

r := &PersistentVolumeClaimReconciler{
Client: client,
ConnPool: connection.NewConnectionPool(),
}

// Create the namespace, storage class, and PVC
err := r.Client.Create(ctx, ns)
assert.NoError(t, err)
err = r.Client.Create(ctx, sc)
assert.NoError(t, err)
err = r.Client.Create(ctx, pvc)
assert.NoError(t, err)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pvc.Annotations = tt.args.pvcAnnotations
ns.Annotations = tt.args.nsAnnotations
sc.Annotations = tt.args.scAnnotations

err = r.Client.Update(ctx, ns)
assert.NoError(t, err)
err = r.Client.Update(ctx, sc)
assert.NoError(t, err)
err = r.Client.Update(ctx, pvc)
assert.NoError(t, err)

schedule, error := r.determineScheduleAndRequeue(ctx, &logger, pvc, driverName)
assert.NoError(t, error)
assert.Equal(t, tt.want, schedule)
})
}
}

0 comments on commit dcac38b

Please sign in to comment.