Skip to content

Commit a04d964

Browse files
committed
Add more snapshot and content sync tests
Signed-off-by: Grant Griffiths <[email protected]>
1 parent 7811e59 commit a04d964

File tree

3 files changed

+99
-21
lines changed

3 files changed

+99
-21
lines changed

pkg/common-controller/framework_test.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ func withSnapshotFinalizers(snapshots []*crdv1.VolumeSnapshot, finalizers ...str
175175

176176
func withContentFinalizer(content *crdv1.VolumeSnapshotContent) *crdv1.VolumeSnapshotContent {
177177
content.ObjectMeta.Finalizers = append(content.ObjectMeta.Finalizers, utils.VolumeSnapshotContentFinalizer)
178-
metav1.SetMetaDataAnnotation(&content.ObjectMeta, utils.AnnVolumeSnapshotBeingDeleted, "yes")
179178
return content
180179
}
181180

@@ -826,6 +825,18 @@ func newContent(contentName, boundToSnapshotUID, boundToSnapshotName, snapshotHa
826825
return &content
827826
}
828827

828+
func withContentAnnotations(contents []*crdv1.VolumeSnapshotContent, annotations map[string]string) []*crdv1.VolumeSnapshotContent {
829+
for i := range contents {
830+
if contents[i].ObjectMeta.Annotations == nil {
831+
contents[i].ObjectMeta.Annotations = make(map[string]string)
832+
}
833+
for k, v := range annotations {
834+
contents[i].ObjectMeta.Annotations[k] = v
835+
}
836+
}
837+
return contents
838+
}
839+
829840
func newContentArray(contentName, boundToSnapshotUID, boundToSnapshotName, snapshotHandle, snapshotClassName, desiredSnapshotHandle, volumeHandle string,
830841
deletionPolicy crdv1.DeletionPolicy, size, creationTime *int64,
831842
withFinalizer bool) []*crdv1.VolumeSnapshotContent {
@@ -1073,6 +1084,14 @@ func testSyncContent(ctrl *csiSnapshotCommonController, reactor *snapshotReactor
10731084
return ctrl.syncContent(test.initialContents[0])
10741085
}
10751086

1087+
func testSyncContentError(ctrl *csiSnapshotCommonController, reactor *snapshotReactor, test controllerTest) error {
1088+
err := ctrl.syncContent(test.initialContents[0])
1089+
if err != nil {
1090+
return nil
1091+
}
1092+
return fmt.Errorf("syncContent succeeded when failure was expected")
1093+
}
1094+
10761095
func testAddPVCFinalizer(ctrl *csiSnapshotCommonController, reactor *snapshotReactor, test controllerTest) error {
10771096
return ctrl.ensurePVCFinalizer(test.initialSnapshots[0])
10781097
}

pkg/common-controller/snapshot_delete_test.go

+24-20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package common_controller
1818

1919
import (
2020
//"errors"
21+
22+
"errors"
2123
"testing"
2224

2325
crdv1 "github.com/kubernetes-csi/external-snapshotter/v2/pkg/apis/volumesnapshot/v1beta1"
@@ -40,13 +42,13 @@ var class3Parameters = map[string]string{
4042
}
4143

4244
var class4Parameters = map[string]string{
43-
//utils.SnapshotterSecretNameKey: "emptysecret",
44-
//utils.SnapshotterSecretNamespaceKey: "default",
45+
//utils.SnapshotterSecretNameKey: "emptysecret",
46+
//utils.SnapshotterSecretNamespaceKey: "default",
4547
}
4648

4749
var class5Parameters = map[string]string{
48-
//utils.SnapshotterSecretNameKey: "secret",
49-
//utils.SnapshotterSecretNamespaceKey: "default",
50+
//utils.SnapshotterSecretNameKey: "secret",
51+
//utils.SnapshotterSecretNamespaceKey: "default",
5052
}
5153

5254
var timeNowMetav1 = metav1.Now()
@@ -174,22 +176,7 @@ func TestDeleteSync(t *testing.T) {
174176
initialSecrets: []*v1.Secret{secret()},
175177
//expectedDeleteCalls: []deleteCall{{"sid1-3", map[string]string{"foo": "bar"}, nil}},
176178
test: testSyncContent,
177-
}, /*{
178-
name: "1-6 - api server delete content returns error",
179-
initialContents: newContentArray("content1-6", "sid1-6", "snap1-6", "sid1-6", validSecretClass, "", "", deletionPolicy, nil, nil, true),
180-
expectedContents: newContentArray("content1-6", "sid1-6", "snap1-6", "sid1-6", validSecretClass, "", "", deletionPolicy, nil, nil, true),
181-
initialSnapshots: nosnapshots,
182-
expectedSnapshots: nosnapshots,
183-
initialSecrets: []*v1.Secret{secret()},
184-
//expectedDeleteCalls: []deleteCall{{"sid1-6", map[string]string{"foo": "bar"}, nil}},
185-
expectedEvents: []string{"Warning SnapshotContentObjectDeleteError"},
186-
errors: []reactorError{
187-
// Inject error to the first client.VolumesnapshotV1beta1().VolumeSnapshotContents().Delete call.
188-
// All other calls will succeed.
189-
{"delete", "volumesnapshotcontents", errors.New("mock delete error")},
190-
},
191-
test: testSyncContent,
192-
},*/
179+
},
193180
{
194181
// delete success - snapshot that the content was pointing to was deleted, and another
195182
// with the same name created.
@@ -312,6 +299,23 @@ func TestDeleteSync(t *testing.T) {
312299
errors: noerrors,
313300
test: testSyncSnapshot,
314301
},
302+
{
303+
name: "3-2 - content will not be deleted if deletion API call fails",
304+
initialContents: newContentArray("content3-2", "", "snap3-2", "sid3-2", validSecretClass, "", "", deletePolicy, nil, nil, true),
305+
expectedContents: newContentArray("content3-2", "", "snap3-2", "sid3-2", validSecretClass, "", "", deletePolicy, nil, nil, true),
306+
initialSnapshots: newSnapshotArray("snap3-2", "snapuid3-2", "claim3-2", "", validSecretClass, "content3-2", &False, nil, nil, nil, false, true, &timeNowMetav1),
307+
expectedSnapshots: newSnapshotArray("snap3-2", "snapuid3-2", "claim3-2", "", validSecretClass, "content3-2", &False, nil, nil, nil, false, true, &timeNowMetav1),
308+
initialClaims: newClaimArray("claim3-2", "pvc-uid3-2", "1Gi", "volume3-2", v1.ClaimBound, &classEmpty),
309+
expectedEvents: []string{"Warning SnapshotContentObjectDeleteError"},
310+
initialSecrets: []*v1.Secret{secret()},
311+
errors: []reactorError{
312+
// Inject error to the first client.VolumesnapshotV1beta1().VolumeSnapshotContents().Delete call.
313+
// All other calls will succeed.
314+
{"delete", "volumesnapshotcontents", errors.New("mock delete error")},
315+
},
316+
expectSuccess: false,
317+
test: testSyncSnapshotError,
318+
},
315319
}
316320
runSyncTests(t, tests, snapshotClasses)
317321
}

pkg/common-controller/snapshot_update_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ package common_controller
1818

1919
import (
2020
//"errors"
21+
"errors"
2122
"testing"
2223
"time"
2324

25+
"github.com/kubernetes-csi/external-snapshotter/v2/pkg/utils"
2426
v1 "k8s.io/api/core/v1"
2527
storagev1beta1 "k8s.io/api/storage/v1beta1"
2628
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -282,6 +284,59 @@ func TestSync(t *testing.T) {
282284
errors: noerrors,
283285
test: testSyncSnapshot,
284286
},
287+
{
288+
name: "5-1 - content missing finalizer is updated to have finalizer",
289+
initialContents: newContentArray("content5-1", "snapuid5-1", "snap5-1", "sid5-1", validSecretClass, "", "", deletionPolicy, nil, nil, false),
290+
expectedContents: newContentArray("content5-1", "snapuid5-1", "snap5-1", "sid5-1", validSecretClass, "", "", deletionPolicy, nil, nil, true),
291+
initialClaims: newClaimArray("claim5-1", "pvc-uid5-1", "1Gi", "volume5-1", v1.ClaimBound, &classEmpty),
292+
initialVolumes: newVolumeArray("volume5-1", "pv-uid5-1", "pv-handle5-1", "1Gi", "pvc-uid5-1", "claim5-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
293+
initialSecrets: []*v1.Secret{secret()},
294+
errors: noerrors,
295+
test: testSyncContent,
296+
},
297+
{
298+
name: "5-2 - content missing finalizer update attempt fails because of failed API call",
299+
initialContents: newContentArray("content5-2", "snapuid5-2", "snap5-2", "sid5-2", validSecretClass, "", "", deletionPolicy, nil, nil, false),
300+
expectedContents: newContentArray("content5-2", "snapuid5-2", "snap5-2", "sid5-2", validSecretClass, "", "", deletionPolicy, nil, nil, false),
301+
initialClaims: newClaimArray("claim5-2", "pvc-uid5-2", "1Gi", "volume5-2", v1.ClaimBound, &classEmpty),
302+
initialVolumes: newVolumeArray("volume5-2", "pv-uid5-2", "pv-handle5-2", "1Gi", "pvc-uid5-2", "claim5-2", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
303+
initialSecrets: []*v1.Secret{secret()},
304+
errors: []reactorError{
305+
// Inject error to the forth client.VolumesnapshotV1beta1().VolumeSnapshots().Update call.
306+
{"update", "volumesnapshotcontents", errors.New("mock update error")},
307+
},
308+
expectSuccess: false,
309+
test: testSyncContentError,
310+
},
311+
{
312+
name: "5-3 - snapshot deletion candidate marked for deletion",
313+
initialSnapshots: newSnapshotArray("snap5-3", "snapuid5-3", "claim5-3", "", validSecretClass, "content5-3", &False, nil, nil, nil, false, true, &timeNowMetav1),
314+
expectedSnapshots: newSnapshotArray("snap5-3", "snapuid5-3", "claim5-3", "", validSecretClass, "content5-3", &False, nil, nil, nil, false, true, &timeNowMetav1),
315+
initialContents: newContentArray("content5-3", "snapuid5-3", "snap5-3", "sid5-3", validSecretClass, "", "", deletionPolicy, nil, nil, true),
316+
expectedContents: withContentAnnotations(newContentArray("content5-3", "snapuid5-3", "snap5-3", "sid5-3", validSecretClass, "", "", deletionPolicy, nil, nil, true), map[string]string{utils.AnnVolumeSnapshotBeingDeleted: "yes"}),
317+
initialClaims: newClaimArray("claim5-3", "pvc-uid5-3", "1Gi", "volume5-3", v1.ClaimBound, &classEmpty),
318+
initialVolumes: newVolumeArray("volume5-3", "pv-uid5-3", "pv-handle5-3", "1Gi", "pvc-uid5-3", "claim5-3", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
319+
initialSecrets: []*v1.Secret{secret()},
320+
expectSuccess: true,
321+
test: testSyncContent,
322+
},
323+
{
324+
name: "5-4 - snapshot deletion candidate fail to mark for deletion due to failed API call",
325+
initialSnapshots: newSnapshotArray("snap5-4", "snapuid5-4", "claim5-4", "", validSecretClass, "content5-4", &False, nil, nil, nil, false, true, &timeNowMetav1),
326+
expectedSnapshots: newSnapshotArray("snap5-4", "snapuid5-4", "claim5-4", "", validSecretClass, "content5-4", &False, nil, nil, nil, false, true, &timeNowMetav1),
327+
initialContents: newContentArray("content5-4", "snapuid5-4", "snap5-4", "sid5-4", validSecretClass, "", "", deletionPolicy, nil, nil, true),
328+
// result of the test framework - annotation is still set in memory, but update call fails.
329+
expectedContents: withContentAnnotations(newContentArray("content5-4", "snapuid5-4", "snap5-4", "sid5-4", validSecretClass, "", "", deletionPolicy, nil, nil, true), map[string]string{utils.AnnVolumeSnapshotBeingDeleted: "yes"}),
330+
initialClaims: newClaimArray("claim5-4", "pvc-uid5-4", "1Gi", "volume5-4", v1.ClaimBound, &classEmpty),
331+
initialVolumes: newVolumeArray("volume5-4", "pv-uid5-4", "pv-handle5-4", "1Gi", "pvc-uid5-4", "claim5-4", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
332+
initialSecrets: []*v1.Secret{secret()},
333+
errors: []reactorError{
334+
// Inject error to the forth client.VolumesnapshotV1beta1().VolumeSnapshots().Update call.
335+
{"update", "volumesnapshotcontents", errors.New("mock update error")},
336+
},
337+
expectSuccess: false,
338+
test: testSyncContentError,
339+
},
285340
}
286341

287342
runSyncTests(t, tests, snapshotClasses)

0 commit comments

Comments
 (0)