Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for shouldDelete function #207

Merged
merged 1 commit into from
Dec 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions pkg/sidecar-controller/snapshot_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}

}
}