Skip to content

Commit 2edd121

Browse files
committed
Add snapshot controller delete test
Signed-off-by: Grant Griffiths <[email protected]>
1 parent 4b7aec3 commit 2edd121

6 files changed

+120
-71
lines changed

pkg/common-controller/framework_test.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -863,14 +863,15 @@ func newContentWithUnmatchDriverArray(contentName, boundToSnapshotUID, boundToSn
863863
func newSnapshot(
864864
snapshotName, snapshotUID, pvcName, targetContentName, snapshotClassName, boundContentName string,
865865
readyToUse *bool, creationTime *metav1.Time, restoreSize *resource.Quantity,
866-
err *crdv1.VolumeSnapshotError, nilStatus bool, withFinalizer bool) *crdv1.VolumeSnapshot {
866+
err *crdv1.VolumeSnapshotError, nilStatus bool, withFinalizer bool, deletionTimestamp *metav1.Time) *crdv1.VolumeSnapshot {
867867
snapshot := crdv1.VolumeSnapshot{
868868
ObjectMeta: metav1.ObjectMeta{
869-
Name: snapshotName,
870-
Namespace: testNamespace,
871-
UID: types.UID(snapshotUID),
872-
ResourceVersion: "1",
873-
SelfLink: "/apis/snapshot.storage.k8s.io/v1beta1/namespaces/" + testNamespace + "/volumesnapshots/" + snapshotName,
869+
Name: snapshotName,
870+
Namespace: testNamespace,
871+
UID: types.UID(snapshotUID),
872+
ResourceVersion: "1",
873+
SelfLink: "/apis/snapshot.storage.k8s.io/v1beta1/namespaces/" + testNamespace + "/volumesnapshots/" + snapshotName,
874+
DeletionTimestamp: deletionTimestamp,
874875
},
875876
Spec: crdv1.VolumeSnapshotSpec{
876877
VolumeSnapshotClassName: nil,
@@ -912,9 +913,9 @@ func newSnapshot(
912913
func newSnapshotArray(
913914
snapshotName, snapshotUID, pvcName, targetContentName, snapshotClassName, boundContentName string,
914915
readyToUse *bool, creationTime *metav1.Time, restoreSize *resource.Quantity,
915-
err *crdv1.VolumeSnapshotError, nilStatus bool, withFinalizer bool) []*crdv1.VolumeSnapshot {
916+
err *crdv1.VolumeSnapshotError, nilStatus bool, withFinalizer bool, deletionTimestamp *metav1.Time) []*crdv1.VolumeSnapshot {
916917
return []*crdv1.VolumeSnapshot{
917-
newSnapshot(snapshotName, snapshotUID, pvcName, targetContentName, snapshotClassName, boundContentName, readyToUse, creationTime, restoreSize, err, nilStatus, withFinalizer),
918+
newSnapshot(snapshotName, snapshotUID, pvcName, targetContentName, snapshotClassName, boundContentName, readyToUse, creationTime, restoreSize, err, nilStatus, withFinalizer, deletionTimestamp),
918919
}
919920
}
920921

pkg/common-controller/snapshot_create_test.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ func TestCreateSnapshotSync(t *testing.T) {
7171
name: "6-1 - successful create snapshot with snapshot class gold",
7272
initialContents: nocontents,
7373
expectedContents: newContentArrayNoStatus("snapcontent-snapuid6-1", "snapuid6-1", "snap6-1", "sid6-1", classGold, "", "pv-handle6-1", deletionPolicy, nil, nil, false, false),
74-
initialSnapshots: newSnapshotArray("snap6-1", "snapuid6-1", "claim6-1", "", classGold, "", &False, nil, nil, nil, false, true),
75-
expectedSnapshots: newSnapshotArray("snap6-1", "snapuid6-1", "claim6-1", "", classGold, "snapcontent-snapuid6-1", &False, nil, nil, nil, false, true),
74+
initialSnapshots: newSnapshotArray("snap6-1", "snapuid6-1", "claim6-1", "", classGold, "", &False, nil, nil, nil, false, true, nil),
75+
expectedSnapshots: newSnapshotArray("snap6-1", "snapuid6-1", "claim6-1", "", classGold, "snapcontent-snapuid6-1", &False, nil, nil, nil, false, true, nil),
7676
initialClaims: newClaimArray("claim6-1", "pvc-uid6-1", "1Gi", "volume6-1", v1.ClaimBound, &classEmpty),
7777
initialVolumes: newVolumeArray("volume6-1", "pv-uid6-1", "pv-handle6-1", "1Gi", "pvc-uid6-1", "claim6-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
7878
errors: noerrors,
@@ -82,8 +82,8 @@ func TestCreateSnapshotSync(t *testing.T) {
8282
name: "6-2 - successful create snapshot with snapshot class silver",
8383
initialContents: nocontents,
8484
expectedContents: newContentArrayNoStatus("snapcontent-snapuid6-2", "snapuid6-2", "snap6-2", "sid6-2", classSilver, "", "pv-handle6-2", deletionPolicy, nil, nil, false, false),
85-
initialSnapshots: newSnapshotArray("snap6-2", "snapuid6-2", "claim6-2", "", classSilver, "", &False, nil, nil, nil, false, true),
86-
expectedSnapshots: newSnapshotArray("snap6-2", "snapuid6-2", "claim6-2", "", classSilver, "snapcontent-snapuid6-2", &False, nil, nil, nil, false, true),
85+
initialSnapshots: newSnapshotArray("snap6-2", "snapuid6-2", "claim6-2", "", classSilver, "", &False, nil, nil, nil, false, true, nil),
86+
expectedSnapshots: newSnapshotArray("snap6-2", "snapuid6-2", "claim6-2", "", classSilver, "snapcontent-snapuid6-2", &False, nil, nil, nil, false, true, nil),
8787
initialClaims: newClaimArray("claim6-2", "pvc-uid6-2", "1Gi", "volume6-2", v1.ClaimBound, &classEmpty),
8888
initialVolumes: newVolumeArray("volume6-2", "pv-uid6-2", "pv-handle6-2", "1Gi", "pvc-uid6-2", "claim6-2", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
8989
errors: noerrors,
@@ -93,8 +93,8 @@ func TestCreateSnapshotSync(t *testing.T) {
9393
name: "7-1 - fail to create snapshot with non-existing snapshot class",
9494
initialContents: nocontents,
9595
expectedContents: nocontents,
96-
initialSnapshots: newSnapshotArray("snap7-1", "snapuid7-1", "claim7-1", "", classNonExisting, "", &False, nil, nil, nil, false, true),
97-
expectedSnapshots: newSnapshotArray("snap7-1", "snapuid7-1", "claim7-1", "", classNonExisting, "", &False, nil, nil, newVolumeError("Failed to create snapshot content with error failed to get input parameters to create snapshot snap7-1: \"failed to retrieve snapshot class non-existing from the informer: \\\"volumesnapshotclass.snapshot.storage.k8s.io \\\\\\\"non-existing\\\\\\\" not found\\\"\""), false, true),
96+
initialSnapshots: newSnapshotArray("snap7-1", "snapuid7-1", "claim7-1", "", classNonExisting, "", &False, nil, nil, nil, false, true, nil),
97+
expectedSnapshots: newSnapshotArray("snap7-1", "snapuid7-1", "claim7-1", "", classNonExisting, "", &False, nil, nil, newVolumeError("Failed to create snapshot content with error failed to get input parameters to create snapshot snap7-1: \"failed to retrieve snapshot class non-existing from the informer: \\\"volumesnapshotclass.snapshot.storage.k8s.io \\\\\\\"non-existing\\\\\\\" not found\\\"\""), false, true, nil),
9898
initialClaims: newClaimArray("claim7-1", "pvc-uid7-1", "1Gi", "volume7-1", v1.ClaimBound, &classEmpty),
9999
initialVolumes: newVolumeArray("volume7-1", "pv-uid7-1", "pv-handle7-1", "1Gi", "pvc-uid7-1", "claim7-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
100100
expectedEvents: []string{"Warning SnapshotContentCreationFailed"},
@@ -107,8 +107,8 @@ func TestCreateSnapshotSync(t *testing.T) {
107107
name: "7-2 - fail to update snapshot reports warning event",
108108
initialContents: newContentArrayWithReadyToUse("snapcontent-snapuid7-2", "snapuid7-2", "snap7-2", "sid7-2", classGold, "sid7-2", "pv-handle7-2", deletionPolicy, nil, nil, &True, false),
109109
expectedContents: newContentArrayWithReadyToUse("snapcontent-snapuid7-2", "snapuid7-2", "snap7-2", "sid7-2", classGold, "sid7-2", "pv-handle7-2", deletionPolicy, nil, nil, &True, false),
110-
initialSnapshots: newSnapshotArray("snap7-2", "snapuid7-2", "claim7-2", "", classGold, "snapcontent-snapuid7-2", &False, nil, nil, nil, false, true),
111-
expectedSnapshots: newSnapshotArray("snap7-2", "snapuid7-2", "claim7-2", "", classGold, "snapcontent-snapuid7-2", &False, nil, nil, newVolumeError("Snapshot status update failed, snapshot controller failed to update default/snap7-2 on API server: mock update error"), false, true),
110+
initialSnapshots: newSnapshotArray("snap7-2", "snapuid7-2", "claim7-2", "", classGold, "snapcontent-snapuid7-2", &False, nil, nil, nil, false, true, nil),
111+
expectedSnapshots: newSnapshotArray("snap7-2", "snapuid7-2", "claim7-2", "", classGold, "snapcontent-snapuid7-2", &False, nil, nil, newVolumeError("Snapshot status update failed, snapshot controller failed to update default/snap7-2 on API server: mock update error"), false, true, nil),
112112
initialClaims: newClaimArray("claim7-2", "pvc-uid7-2", "1Gi", "volume7-2", v1.ClaimBound, &classGold),
113113
initialVolumes: newVolumeArray("volume7-2", "pv-uid7-2", "pv-handle7-2", "1Gi", "pvc-uid7-2", "claim7-2", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold),
114114
expectedEvents: []string{"Warning SnapshotStatusUpdateFailed"},
@@ -138,8 +138,8 @@ func TestCreateSnapshotSync(t *testing.T) {
138138
name: "7-4 - fail create snapshot with no-existing claim",
139139
initialContents: nocontents,
140140
expectedContents: nocontents,
141-
initialSnapshots: newSnapshotArray("snap7-4", "snapuid7-4", "claim7-4", "", classGold, "", &False, nil, nil, nil, false, true),
142-
expectedSnapshots: newSnapshotArray("snap7-4", "snapuid7-4", "claim7-4", "", classGold, "", &False, nil, nil, newVolumeError("Failed to create snapshot content with error snapshot controller failed to update snap7-4 on API server: cannot get claim from snapshot"), false, true),
141+
initialSnapshots: newSnapshotArray("snap7-4", "snapuid7-4", "claim7-4", "", classGold, "", &False, nil, nil, nil, false, true, nil),
142+
expectedSnapshots: newSnapshotArray("snap7-4", "snapuid7-4", "claim7-4", "", classGold, "", &False, nil, nil, newVolumeError("Failed to create snapshot content with error snapshot controller failed to update snap7-4 on API server: cannot get claim from snapshot"), false, true, nil),
143143
initialVolumes: newVolumeArray("volume7-4", "pv-uid7-4", "pv-handle7-4", "1Gi", "pvc-uid7-4", "claim7-4", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
144144
expectedEvents: []string{"Warning SnapshotContentCreationFailed"},
145145
errors: noerrors,
@@ -150,8 +150,8 @@ func TestCreateSnapshotSync(t *testing.T) {
150150
name: "7-5 - fail create snapshot with no-existing volume",
151151
initialContents: nocontents,
152152
expectedContents: nocontents,
153-
initialSnapshots: newSnapshotArray("snap7-5", "snapuid7-5", "claim7-5", "", classGold, "", &False, nil, nil, nil, false, true),
154-
expectedSnapshots: newSnapshotArray("snap7-5", "snapuid7-5", "claim7-5", "", classGold, "", &False, nil, nil, newVolumeError("Failed to create snapshot content with error failed to get input parameters to create snapshot snap7-5: \"failed to retrieve PV volume7-5 from the API server: \\\"cannot find volume volume7-5\\\"\""), false, true),
153+
initialSnapshots: newSnapshotArray("snap7-5", "snapuid7-5", "claim7-5", "", classGold, "", &False, nil, nil, nil, false, true, nil),
154+
expectedSnapshots: newSnapshotArray("snap7-5", "snapuid7-5", "claim7-5", "", classGold, "", &False, nil, nil, newVolumeError("Failed to create snapshot content with error failed to get input parameters to create snapshot snap7-5: \"failed to retrieve PV volume7-5 from the API server: \\\"cannot find volume volume7-5\\\"\""), false, true, nil),
155155
initialClaims: newClaimArray("claim7-5", "pvc-uid7-5", "1Gi", "volume7-5", v1.ClaimBound, &classEmpty),
156156
expectedEvents: []string{"Warning SnapshotContentCreationFailed"},
157157
errors: noerrors,
@@ -163,8 +163,8 @@ func TestCreateSnapshotSync(t *testing.T) {
163163
name: "7-6 - fail create snapshot with claim that is not yet bound",
164164
initialContents: nocontents,
165165
expectedContents: nocontents,
166-
initialSnapshots: newSnapshotArray("snap7-6", "snapuid7-6", "claim7-6", "", classGold, "", &False, nil, nil, nil, false, true),
167-
expectedSnapshots: newSnapshotArray("snap7-6", "snapuid7-6", "claim7-6", "", classGold, "", &False, nil, nil, newVolumeError("Failed to create snapshot content with error failed to get input parameters to create snapshot snap7-6: \"the PVC claim7-6 is not yet bound to a PV, will not attempt to take a snapshot\""), false, true),
166+
initialSnapshots: newSnapshotArray("snap7-6", "snapuid7-6", "claim7-6", "", classGold, "", &False, nil, nil, nil, false, true, nil),
167+
expectedSnapshots: newSnapshotArray("snap7-6", "snapuid7-6", "claim7-6", "", classGold, "", &False, nil, nil, newVolumeError("Failed to create snapshot content with error failed to get input parameters to create snapshot snap7-6: \"the PVC claim7-6 is not yet bound to a PV, will not attempt to take a snapshot\""), false, true, nil),
168168
initialClaims: newClaimArray("claim7-6", "pvc-uid7-6", "1Gi", "", v1.ClaimPending, &classEmpty),
169169
expectedEvents: []string{"Warning SnapshotContentCreationFailed"},
170170
errors: noerrors,

pkg/common-controller/snapshot_delete_test.go

+54-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/kubernetes-csi/external-snapshotter/v2/pkg/utils"
2525
v1 "k8s.io/api/core/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/types"
2728
)
2829

2930
var class1Parameters = map[string]string{
@@ -49,6 +50,11 @@ var class5Parameters = map[string]string{
4950
//utils.SnapshotterSecretNamespaceKey: "default",
5051
}
5152

53+
var timeNowMetav1 = metav1.Now()
54+
55+
var content31 = "content3-1"
56+
var claim31 = "claim3-1"
57+
5258
var snapshotClasses = []*crdv1.VolumeSnapshotClass{
5359
{
5460
TypeMeta: metav1.TypeMeta{
@@ -214,8 +220,8 @@ func TestDeleteSync(t *testing.T) {
214220
name: "1-7 - prebound content is deleted while the snapshot exists",
215221
initialContents: newContentArray("content1-7", "sid1-7", "snap1-7", "sid1-7", emptySecretClass, "", "", deletionPolicy, nil, nil, true),
216222
expectedContents: newContentArray("content1-7", "sid1-7", "snap1-7", "sid1-7", emptySecretClass, "", "", deletionPolicy, nil, nil, true),
217-
initialSnapshots: newSnapshotArray("snap1-7", "snapuid1-7-x", "claim1-7", "", validSecretClass, "", &False, nil, nil, nil, false, true),
218-
expectedSnapshots: newSnapshotArray("snap1-7", "snapuid1-7-x", "claim1-7", "", validSecretClass, "", &False, nil, nil, nil, false, true),
223+
initialSnapshots: newSnapshotArray("snap1-7", "snapuid1-7-x", "claim1-7", "", validSecretClass, "", &False, nil, nil, nil, false, true, nil),
224+
expectedSnapshots: newSnapshotArray("snap1-7", "snapuid1-7-x", "claim1-7", "", validSecretClass, "", &False, nil, nil, nil, false, true, nil),
219225
initialSecrets: []*v1.Secret{secret()},
220226
//expectedDeleteCalls: []deleteCall{{"sid1-7", map[string]string{"foo": "bar"}, nil}},
221227
expectedEvents: noevents,
@@ -255,8 +261,8 @@ func TestDeleteSync(t *testing.T) {
255261
name: "1-10 - will not delete content with retain policy set which is bound to a snapshot incorrectly",
256262
initialContents: newContentArray("content1-10", "snapuid1-10-x", "snap1-10", "sid1-10", validSecretClass, "", "", retainPolicy, nil, nil, true),
257263
expectedContents: newContentArray("content1-10", "snapuid1-10-x", "snap1-10", "sid1-10", validSecretClass, "", "", retainPolicy, nil, nil, true),
258-
initialSnapshots: newSnapshotArray("snap1-10", "snapuid1-10", "claim1-10", "", validSecretClass, "content1-10", &False, nil, nil, nil, false, true),
259-
expectedSnapshots: newSnapshotArray("snap1-10", "snapuid1-10", "claim1-10", "", validSecretClass, "content1-10", &False, nil, nil, nil, false, true),
264+
initialSnapshots: newSnapshotArray("snap1-10", "snapuid1-10", "claim1-10", "", validSecretClass, "content1-10", &False, nil, nil, nil, false, true, nil),
265+
expectedSnapshots: newSnapshotArray("snap1-10", "snapuid1-10", "claim1-10", "", validSecretClass, "content1-10", &False, nil, nil, nil, false, true, nil),
260266
expectedEvents: noevents,
261267
initialSecrets: []*v1.Secret{secret()},
262268
errors: noerrors,
@@ -266,8 +272,8 @@ func TestDeleteSync(t *testing.T) {
266272
name: "1-11 - content will not be deleted if it is bound to a snapshot correctly, snapsht uid is not specified",
267273
initialContents: newContentArray("content1-11", "", "snap1-11", "sid1-11", validSecretClass, "", "", deletePolicy, nil, nil, true),
268274
expectedContents: newContentArray("content1-11", "", "snap1-11", "sid1-11", validSecretClass, "", "", deletePolicy, nil, nil, true),
269-
initialSnapshots: newSnapshotArray("snap1-11", "snapuid1-11", "claim1-11", "", validSecretClass, "content1-11", &False, nil, nil, nil, false, true),
270-
expectedSnapshots: newSnapshotArray("snap1-11", "snapuid1-11", "claim1-11", "", validSecretClass, "content1-11", &False, nil, nil, nil, false, true),
275+
initialSnapshots: newSnapshotArray("snap1-11", "snapuid1-11", "claim1-11", "", validSecretClass, "content1-11", &False, nil, nil, nil, false, true, nil),
276+
expectedSnapshots: newSnapshotArray("snap1-11", "snapuid1-11", "claim1-11", "", validSecretClass, "content1-11", &False, nil, nil, nil, false, true, nil),
271277
expectedEvents: noevents,
272278
initialSecrets: []*v1.Secret{secret()},
273279
errors: noerrors,
@@ -316,6 +322,48 @@ func TestDeleteSync(t *testing.T) {
316322
//expectedDeleteCalls: []deleteCall{{"sid1-16", nil, nil}},
317323
test: testSyncContent,
318324
},
325+
{
326+
name: "3-1 - content will be deleted if snapshot deletion timestamp is set",
327+
initialContents: newContentArray("content3-1", "", "snap3-1", "sid3-1", validSecretClass, "", "", deletePolicy, nil, nil, true),
328+
expectedContents: nocontents,
329+
initialSnapshots: newSnapshotArray("snap3-1", "snapuid3-1", "claim3-1", "", validSecretClass, "content3-1", &False, nil, nil, nil, false, true, &timeNowMetav1),
330+
expectedSnapshots: []*crdv1.VolumeSnapshot{
331+
&crdv1.VolumeSnapshot{
332+
ObjectMeta: metav1.ObjectMeta{
333+
Name: "snap3-1",
334+
Namespace: testNamespace,
335+
UID: types.UID("snapuid3-1"),
336+
ResourceVersion: "1",
337+
Finalizers: []string{
338+
"snapshot.storage.kubernetes.io/volumesnapshotcontent-bound-protection",
339+
"snapshot.storage.kubernetes.io/volumesnapshot-bound-protection",
340+
},
341+
SelfLink: "/apis/snapshot.storage.k8s.io/v1beta1/namespaces/" + testNamespace + "/volumesnapshots/" + "snap3-1",
342+
DeletionTimestamp: &timeNowMetav1,
343+
},
344+
Spec: crdv1.VolumeSnapshotSpec{
345+
VolumeSnapshotClassName: &validSecretClass,
346+
Source: crdv1.VolumeSnapshotSource{
347+
PersistentVolumeClaimName: &claim31,
348+
},
349+
},
350+
351+
Status: &crdv1.VolumeSnapshotStatus{
352+
CreationTime: nil,
353+
ReadyToUse: &False,
354+
Error: nil,
355+
RestoreSize: nil,
356+
BoundVolumeSnapshotContentName: &content31,
357+
},
358+
},
359+
},
360+
//expectedSnapshots: newSnapshotArray("snap3-1", "snapuid3-1", "claim3-1", "", validSecretClass, "content3-1", &False, nil, nil, nil, false, true, &timeNowMetav1),
361+
initialClaims: newClaimArray("claim3-1", "pvc-uid3-1", "1Gi", "volume3-1", v1.ClaimBound, &classEmpty),
362+
expectedEvents: noevents,
363+
initialSecrets: []*v1.Secret{secret()},
364+
errors: noerrors,
365+
test: testSyncSnapshot,
366+
},
319367
}
320368
runSyncTests(t, tests, snapshotClasses)
321369
}

0 commit comments

Comments
 (0)