Skip to content

Commit 0a57046

Browse files
committed
Add tests for shouldDelete function
Signed-off-by: Nolan Brubaker <[email protected]>
1 parent 53469c2 commit 0a57046

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

pkg/sidecar-controller/snapshot_controller_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
crdv1 "github.com/kubernetes-csi/external-snapshotter/pkg/apis/volumesnapshot/v1beta1"
2020
"github.com/kubernetes-csi/external-snapshotter/pkg/utils"
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2122
"k8s.io/client-go/tools/cache"
2223
)
2324

@@ -89,3 +90,53 @@ func TestControllerCacheParsingError(t *testing.T) {
8990
t.Errorf("Expected parsing error, got nil instead")
9091
}
9192
}
93+
94+
// TestShouldDelete tests logic for deleting VolumeSnapshotContent objects.
95+
func TestShouldDelete(t *testing.T) {
96+
// Use an empty controller, since there's no struct
97+
// state we need to use in this test.
98+
ctrl := &csiSnapshotSideCarController{}
99+
100+
tests := []struct {
101+
name string
102+
expectedReturn bool
103+
content *crdv1.VolumeSnapshotContent
104+
}{
105+
{
106+
name: "DeletionTimeStamp is nil",
107+
expectedReturn: false,
108+
content: newContent("test-content", "snap-uuid", "snapName", "desiredHandle", "default", "desiredHandle", "volHandle", crdv1.VolumeSnapshotContentDelete, nil, &defaultSize, false, nil),
109+
},
110+
{
111+
name: "Content is not bound",
112+
expectedReturn: true,
113+
content: newContent("test-content-not-bound", "", "", "snapshotHandle", "", "", "", crdv1.VolumeSnapshotContentDelete, nil, &defaultSize, false, &timeNowMetav1),
114+
},
115+
{
116+
name: "AnnVolumeSnapshotBeingDeleted annotation is set. ",
117+
expectedReturn: true,
118+
// DeletionTime means that annotation is set, and being bound means the other cases are skipped.
119+
content: newContent("test-content", "snap-uuid", "snapName", "desiredHandle", "default", "desiredHandle", "volHandle", crdv1.VolumeSnapshotContentDelete, nil, &defaultSize, false, &timeNowMetav1),
120+
},
121+
{
122+
name: "If no other cases match, then should not delete",
123+
expectedReturn: false,
124+
// Use an object that does not conform to newContent's logic in order to skip the conditionals inside shouldDelete
125+
content: &crdv1.VolumeSnapshotContent{
126+
ObjectMeta: metav1.ObjectMeta{
127+
Name: "test-content",
128+
DeletionTimestamp: &timeNowMetav1,
129+
},
130+
},
131+
},
132+
}
133+
134+
for _, test := range tests {
135+
result := ctrl.shouldDelete(test.content)
136+
137+
if result != test.expectedReturn {
138+
t.Errorf("Got %t but expected %t for test: %s", result, test.expectedReturn, test.name)
139+
}
140+
141+
}
142+
}

0 commit comments

Comments
 (0)