Skip to content

Commit 7811e59

Browse files
committed
Refactor delete test to use withSnapshotFinalizers function
Signed-off-by: Grant Griffiths <[email protected]>
1 parent 62e1dbb commit 7811e59

File tree

2 files changed

+15
-41
lines changed

2 files changed

+15
-41
lines changed

pkg/common-controller/framework_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,13 @@ type reactorError struct {
164164
error error
165165
}
166166

167-
func withSnapshotFinalizer(snapshot *crdv1.VolumeSnapshot) *crdv1.VolumeSnapshot {
168-
snapshot.ObjectMeta.Finalizers = append(snapshot.ObjectMeta.Finalizers, utils.VolumeSnapshotContentFinalizer)
169-
snapshot.ObjectMeta.Finalizers = append(snapshot.ObjectMeta.Finalizers, utils.VolumeSnapshotAsSourceFinalizer)
170-
snapshot.ObjectMeta.Finalizers = append(snapshot.ObjectMeta.Finalizers, utils.VolumeSnapshotBoundFinalizer)
171-
return snapshot
167+
func withSnapshotFinalizers(snapshots []*crdv1.VolumeSnapshot, finalizers ...string) []*crdv1.VolumeSnapshot {
168+
for i := range snapshots {
169+
for _, f := range finalizers {
170+
snapshots[i].ObjectMeta.Finalizers = append(snapshots[i].ObjectMeta.Finalizers, f)
171+
}
172+
}
173+
return snapshots
172174
}
173175

174176
func withContentFinalizer(content *crdv1.VolumeSnapshotContent) *crdv1.VolumeSnapshotContent {
@@ -863,7 +865,7 @@ func newContentWithUnmatchDriverArray(contentName, boundToSnapshotUID, boundToSn
863865
func newSnapshot(
864866
snapshotName, snapshotUID, pvcName, targetContentName, snapshotClassName, boundContentName string,
865867
readyToUse *bool, creationTime *metav1.Time, restoreSize *resource.Quantity,
866-
err *crdv1.VolumeSnapshotError, nilStatus bool, withFinalizer bool, deletionTimestamp *metav1.Time) *crdv1.VolumeSnapshot {
868+
err *crdv1.VolumeSnapshotError, nilStatus bool, withAllFinalizers bool, deletionTimestamp *metav1.Time) *crdv1.VolumeSnapshot {
867869
snapshot := crdv1.VolumeSnapshot{
868870
ObjectMeta: metav1.ObjectMeta{
869871
Name: snapshotName,
@@ -904,18 +906,18 @@ func newSnapshot(
904906
VolumeSnapshotContentName: &targetContentName,
905907
}
906908
}
907-
if withFinalizer {
908-
return withSnapshotFinalizer(&snapshot)
909+
if withAllFinalizers {
910+
return withSnapshotFinalizers([]*crdv1.VolumeSnapshot{&snapshot}, utils.VolumeSnapshotContentFinalizer, utils.VolumeSnapshotAsSourceFinalizer, utils.VolumeSnapshotBoundFinalizer)[0]
909911
}
910912
return &snapshot
911913
}
912914

913915
func newSnapshotArray(
914916
snapshotName, snapshotUID, pvcName, targetContentName, snapshotClassName, boundContentName string,
915917
readyToUse *bool, creationTime *metav1.Time, restoreSize *resource.Quantity,
916-
err *crdv1.VolumeSnapshotError, nilStatus bool, withFinalizer bool, deletionTimestamp *metav1.Time) []*crdv1.VolumeSnapshot {
918+
err *crdv1.VolumeSnapshotError, nilStatus bool, withAllFinalizers bool, deletionTimestamp *metav1.Time) []*crdv1.VolumeSnapshot {
917919
return []*crdv1.VolumeSnapshot{
918-
newSnapshot(snapshotName, snapshotUID, pvcName, targetContentName, snapshotClassName, boundContentName, readyToUse, creationTime, restoreSize, err, nilStatus, withFinalizer, deletionTimestamp),
920+
newSnapshot(snapshotName, snapshotUID, pvcName, targetContentName, snapshotClassName, boundContentName, readyToUse, creationTime, restoreSize, err, nilStatus, withAllFinalizers, deletionTimestamp),
919921
}
920922
}
921923

pkg/common-controller/snapshot_delete_test.go

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ 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"
2827
)
2928

3029
var class1Parameters = map[string]string{
@@ -304,36 +303,9 @@ func TestDeleteSync(t *testing.T) {
304303
initialContents: newContentArray("content3-1", "", "snap3-1", "sid3-1", validSecretClass, "", "", deletePolicy, nil, nil, true),
305304
expectedContents: nocontents,
306305
initialSnapshots: newSnapshotArray("snap3-1", "snapuid3-1", "claim3-1", "", validSecretClass, "content3-1", &False, nil, nil, nil, false, true, &timeNowMetav1),
307-
expectedSnapshots: []*crdv1.VolumeSnapshot{
308-
&crdv1.VolumeSnapshot{
309-
ObjectMeta: metav1.ObjectMeta{
310-
Name: "snap3-1",
311-
Namespace: testNamespace,
312-
UID: types.UID("snapuid3-1"),
313-
ResourceVersion: "1",
314-
Finalizers: []string{
315-
"snapshot.storage.kubernetes.io/volumesnapshotcontent-bound-protection",
316-
"snapshot.storage.kubernetes.io/volumesnapshot-bound-protection",
317-
},
318-
SelfLink: "/apis/snapshot.storage.k8s.io/v1beta1/namespaces/" + testNamespace + "/volumesnapshots/" + "snap3-1",
319-
DeletionTimestamp: &timeNowMetav1,
320-
},
321-
Spec: crdv1.VolumeSnapshotSpec{
322-
VolumeSnapshotClassName: &validSecretClass,
323-
Source: crdv1.VolumeSnapshotSource{
324-
PersistentVolumeClaimName: &claim31,
325-
},
326-
},
327-
328-
Status: &crdv1.VolumeSnapshotStatus{
329-
CreationTime: nil,
330-
ReadyToUse: &False,
331-
Error: nil,
332-
RestoreSize: nil,
333-
BoundVolumeSnapshotContentName: &content31,
334-
},
335-
},
336-
},
306+
expectedSnapshots: withSnapshotFinalizers(newSnapshotArray("snap3-1", "snapuid3-1", "claim3-1", "", validSecretClass, "content3-1", &False, nil, nil, nil, false, false, &timeNowMetav1),
307+
utils.VolumeSnapshotContentFinalizer, utils.VolumeSnapshotBoundFinalizer,
308+
),
337309
initialClaims: newClaimArray("claim3-1", "pvc-uid3-1", "1Gi", "volume3-1", v1.ClaimBound, &classEmpty),
338310
expectedEvents: noevents,
339311
initialSecrets: []*v1.Secret{secret()},

0 commit comments

Comments
 (0)