From 0a57046ea8699e1d47463fc2834669b3e5626c5c Mon Sep 17 00:00:00 2001 From: Nolan Brubaker Date: Wed, 11 Dec 2019 15:11:33 -0500 Subject: [PATCH] Add tests for shouldDelete function Signed-off-by: Nolan Brubaker --- .../snapshot_controller_test.go | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/pkg/sidecar-controller/snapshot_controller_test.go b/pkg/sidecar-controller/snapshot_controller_test.go index c4d598e9..c3303e3b 100644 --- a/pkg/sidecar-controller/snapshot_controller_test.go +++ b/pkg/sidecar-controller/snapshot_controller_test.go @@ -18,6 +18,7 @@ import ( crdv1 "github.com/kubernetes-csi/external-snapshotter/pkg/apis/volumesnapshot/v1beta1" "github.com/kubernetes-csi/external-snapshotter/pkg/utils" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/tools/cache" ) @@ -89,3 +90,53 @@ func TestControllerCacheParsingError(t *testing.T) { t.Errorf("Expected parsing error, got nil instead") } } + +// TestShouldDelete tests logic for deleting VolumeSnapshotContent objects. +func TestShouldDelete(t *testing.T) { + // Use an empty controller, since there's no struct + // state we need to use in this test. + ctrl := &csiSnapshotSideCarController{} + + tests := []struct { + name string + expectedReturn bool + content *crdv1.VolumeSnapshotContent + }{ + { + name: "DeletionTimeStamp is nil", + expectedReturn: false, + content: newContent("test-content", "snap-uuid", "snapName", "desiredHandle", "default", "desiredHandle", "volHandle", crdv1.VolumeSnapshotContentDelete, nil, &defaultSize, false, nil), + }, + { + name: "Content is not bound", + expectedReturn: true, + content: newContent("test-content-not-bound", "", "", "snapshotHandle", "", "", "", crdv1.VolumeSnapshotContentDelete, nil, &defaultSize, false, &timeNowMetav1), + }, + { + name: "AnnVolumeSnapshotBeingDeleted annotation is set. ", + expectedReturn: true, + // DeletionTime means that annotation is set, and being bound means the other cases are skipped. + content: newContent("test-content", "snap-uuid", "snapName", "desiredHandle", "default", "desiredHandle", "volHandle", crdv1.VolumeSnapshotContentDelete, nil, &defaultSize, false, &timeNowMetav1), + }, + { + name: "If no other cases match, then should not delete", + expectedReturn: false, + // Use an object that does not conform to newContent's logic in order to skip the conditionals inside shouldDelete + content: &crdv1.VolumeSnapshotContent{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-content", + DeletionTimestamp: &timeNowMetav1, + }, + }, + }, + } + + for _, test := range tests { + result := ctrl.shouldDelete(test.content) + + if result != test.expectedReturn { + t.Errorf("Got %t but expected %t for test: %s", result, test.expectedReturn, test.name) + } + + } +}