@@ -33,7 +33,6 @@ import (
33
33
storagelisters "github.com/kubernetes-csi/external-snapshotter/pkg/client/listers/volumesnapshot/v1beta1"
34
34
"github.com/kubernetes-csi/external-snapshotter/pkg/utils"
35
35
v1 "k8s.io/api/core/v1"
36
- storagev1 "k8s.io/api/storage/v1"
37
36
"k8s.io/apimachinery/pkg/api/resource"
38
37
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
39
38
"k8s.io/apimachinery/pkg/runtime"
@@ -50,29 +49,26 @@ import (
50
49
"k8s.io/klog"
51
50
)
52
51
53
- // This is a unit test framework for snapshot controller.
54
- // It fills the controller with test snapshots/ contents and can simulate these
52
+ // This is a unit test framework for snapshot sidecar controller.
53
+ // It fills the controller with test contents and can simulate these
55
54
// scenarios:
56
- // 1) Call syncSnapshot/ syncContent once.
57
- // 2) Call syncSnapshot/ syncContent several times (both simulating "snapshot/ content
55
+ // 1) Call syncContent once.
56
+ // 2) Call syncContent several times (both simulating "content
58
57
// modified" events and periodic sync), until the controller settles down and
59
58
// does not modify anything.
60
59
// 3) Simulate almost real API server/etcd and call add/update/delete
61
- // content/snapshot .
60
+ // content.
62
61
// In all these scenarios, when the test finishes, the framework can compare
63
- // resulting snapshots/ contents with list of expected snapshots/ contents and report
62
+ // resulting contents with list of expected contents and report
64
63
// differences.
65
64
66
65
// controllerTest contains a single controller test input.
67
- // Each test has initial set of contents and snapshots that are filled into the
66
+ // Each test has initial set of contents that are filled into the
68
67
// controller before the test starts. The test then contains a reference to
69
68
// function to call as the actual test. Available functions are:
70
- // - testSyncSnapshot - calls syncSnapshot on the first snapshot in initialSnapshots.
71
- // - testSyncSnapshotError - calls syncSnapshot on the first snapshot in initialSnapshots
72
- // and expects an error to be returned.
73
69
// - testSyncContent - calls syncContent on the first content in initialContents.
74
70
// - any custom function for specialized tests.
75
- // The test then contains list of contents/snapshots that are expected at the end
71
+ // The test then contains list of contents that are expected at the end
76
72
// of the test and list of generated events.
77
73
type controllerTest struct {
78
74
// Name of the test, for logging
@@ -106,7 +102,6 @@ const mockDriverName = "csi-mock-plugin"
106
102
107
103
var errVersionConflict = errors .New ("VersionError" )
108
104
var nocontents []* crdv1.VolumeSnapshotContent
109
- var nosnapshots []* crdv1.VolumeSnapshot
110
105
var noevents = []string {}
111
106
var noerrors = []reactorError {}
112
107
@@ -129,11 +124,7 @@ var noerrors = []reactorError{}
129
124
// the list.
130
125
type snapshotReactor struct {
131
126
secrets map [string ]* v1.Secret
132
- storageClasses map [string ]* storagev1.StorageClass
133
- volumes map [string ]* v1.PersistentVolume
134
- claims map [string ]* v1.PersistentVolumeClaim
135
127
contents map [string ]* crdv1.VolumeSnapshotContent
136
- snapshots map [string ]* crdv1.VolumeSnapshot
137
128
changedObjects []interface {}
138
129
changedSinceLastSync int
139
130
ctrl * csiSnapshotSideCarController
@@ -146,7 +137,7 @@ type snapshotReactor struct {
146
137
// reactorError is an error that is returned by test reactor (=simulated
147
138
// etcd+/API server) when an action performed by the reactor matches given verb
148
139
// ("get", "update", "create", "delete" or "*"") on given resource
149
- // ("volumesnapshotcontents", "volumesnapshots" or "*").
140
+ // ("volumesnapshotcontents" or "*").
150
141
type reactorError struct {
151
142
verb string
152
143
resource string
@@ -203,9 +194,9 @@ func (r *snapshotReactor) React(action core.Action) (handled bool, ret runtime.O
203
194
content := obj .(* crdv1.VolumeSnapshotContent )
204
195
205
196
// Check and bump object version
206
- storedVolume , found := r .contents [content .Name ]
197
+ storedContent , found := r .contents [content .Name ]
207
198
if found {
208
- storedVer , _ := strconv .Atoi (storedVolume .ResourceVersion )
199
+ storedVer , _ := strconv .Atoi (storedContent .ResourceVersion )
209
200
requestedVer , _ := strconv .Atoi (content .ResourceVersion )
210
201
if storedVer != requestedVer {
211
202
return true , obj , errVersionConflict
@@ -314,41 +305,6 @@ func (r *snapshotReactor) checkContents(expectedContents []*crdv1.VolumeSnapshot
314
305
return nil
315
306
}
316
307
317
- // checkSnapshots compares all expectedSnapshots with set of snapshots at the end of the
318
- // test and reports differences.
319
- func (r * snapshotReactor ) checkSnapshots (expectedSnapshots []* crdv1.VolumeSnapshot ) error {
320
- r .lock .Lock ()
321
- defer r .lock .Unlock ()
322
-
323
- expectedMap := make (map [string ]* crdv1.VolumeSnapshot )
324
- gotMap := make (map [string ]* crdv1.VolumeSnapshot )
325
- for _ , c := range expectedSnapshots {
326
- // Don't modify the existing object
327
- c = c .DeepCopy ()
328
- c .ResourceVersion = ""
329
- if c .Status .Error != nil {
330
- c .Status .Error .Time = & metav1.Time {}
331
- }
332
- expectedMap [c .Name ] = c
333
- }
334
- for _ , c := range r .snapshots {
335
- // We must clone the snapshot because of golang race check - it was
336
- // written by the controller without any locks on it.
337
- c = c .DeepCopy ()
338
- c .ResourceVersion = ""
339
- if c .Status .Error != nil {
340
- c .Status .Error .Time = & metav1.Time {}
341
- }
342
- gotMap [c .Name ] = c
343
- }
344
- if ! reflect .DeepEqual (expectedMap , gotMap ) {
345
- // Print ugly but useful diff of expected and received objects for
346
- // easier debugging.
347
- return fmt .Errorf ("snapshot check failed [A-expected, B-got result]: %s" , diff .ObjectDiff (expectedMap , gotMap ))
348
- }
349
- return nil
350
- }
351
-
352
308
// checkEvents compares all expectedEvents with events generated during the test
353
309
// and reports differences.
354
310
func checkEvents (t * testing.T , expectedEvents []string , ctrl * csiSnapshotSideCarController ) error {
@@ -414,9 +370,6 @@ func (r *snapshotReactor) popChange() interface{} {
414
370
case * crdv1.VolumeSnapshotContent :
415
371
vol , _ := obj .(* crdv1.VolumeSnapshotContent )
416
372
klog .V (4 ).Infof ("reactor queue: %s" , vol .Name )
417
- case * crdv1.VolumeSnapshot :
418
- snapshot , _ := obj .(* crdv1.VolumeSnapshot )
419
- klog .V (4 ).Infof ("reactor queue: %s" , snapshot .Name )
420
373
}
421
374
}
422
375
@@ -428,21 +381,15 @@ func (r *snapshotReactor) popChange() interface{} {
428
381
429
382
// syncAll simulates the controller periodic sync of contents and snapshot. It
430
383
// simply adds all these objects to the internal queue of updates. This method
431
- // should be used when the test manually calls syncSnapshot/ syncContent. Test that
384
+ // should be used when the test manually calls syncContent. Test that
432
385
// use real controller loop (ctrl.Run()) will get periodic sync automatically.
433
386
func (r * snapshotReactor ) syncAll () {
434
387
r .lock .Lock ()
435
388
defer r .lock .Unlock ()
436
389
437
- for _ , c := range r .snapshots {
438
- r .changedObjects = append (r .changedObjects , c )
439
- }
440
390
for _ , v := range r .contents {
441
391
r .changedObjects = append (r .changedObjects , v )
442
392
}
443
- for _ , pvc := range r .claims {
444
- r .changedObjects = append (r .changedObjects , pvc )
445
- }
446
393
r .changedSinceLastSync = 0
447
394
}
448
395
@@ -511,22 +458,6 @@ func (r *snapshotReactor) deleteContentEvent(content *crdv1.VolumeSnapshotConten
511
458
}
512
459
}
513
460
514
- // deleteSnapshotEvent simulates that a snapshot has been deleted in etcd and the
515
- // controller receives 'snapshot deleted' event.
516
- func (r * snapshotReactor ) deleteSnapshotEvent (snapshot * crdv1.VolumeSnapshot ) {
517
- r .lock .Lock ()
518
- defer r .lock .Unlock ()
519
-
520
- // Remove the snapshot from list of resulting snapshots.
521
- delete (r .snapshots , snapshot .Name )
522
-
523
- // Generate deletion event. Cloned content is needed to prevent races (and we
524
- // would get a clone from etcd too).
525
- if r .fakeSnapshotWatch != nil {
526
- r .fakeSnapshotWatch .Delete (snapshot .DeepCopy ())
527
- }
528
- }
529
-
530
461
// addContentEvent simulates that a content has been added in etcd and the
531
462
// controller receives 'content added' event.
532
463
func (r * snapshotReactor ) addContentEvent (content * crdv1.VolumeSnapshotContent ) {
@@ -555,20 +486,6 @@ func (r *snapshotReactor) modifyContentEvent(content *crdv1.VolumeSnapshotConten
555
486
}
556
487
}
557
488
558
- // addSnapshotEvent simulates that a snapshot has been deleted in etcd and the
559
- // controller receives 'snapshot added' event.
560
- func (r * snapshotReactor ) addSnapshotEvent (snapshot * crdv1.VolumeSnapshot ) {
561
- r .lock .Lock ()
562
- defer r .lock .Unlock ()
563
-
564
- r .snapshots [snapshot .Name ] = snapshot
565
- // Generate event. No cloning is needed, this snapshot is not stored in the
566
- // controller cache yet.
567
- if r .fakeSnapshotWatch != nil {
568
- r .fakeSnapshotWatch .Add (snapshot )
569
- }
570
- }
571
-
572
489
func newSnapshotReactor (kubeClient * kubefake.Clientset , client * fake.Clientset , ctrl * csiSnapshotSideCarController , fakeVolumeWatch , fakeClaimWatch * watch.FakeWatcher , errors []reactorError ) * snapshotReactor {
573
490
reactor := & snapshotReactor {
574
491
secrets : make (map [string ]* v1.Secret ),
@@ -580,11 +497,8 @@ func newSnapshotReactor(kubeClient *kubefake.Clientset, client *fake.Clientset,
580
497
581
498
client .AddReactor ("create" , "volumesnapshotcontents" , reactor .React )
582
499
client .AddReactor ("update" , "volumesnapshotcontents" , reactor .React )
583
- client .AddReactor ("update" , "volumesnapshots" , reactor .React )
584
500
client .AddReactor ("get" , "volumesnapshotcontents" , reactor .React )
585
- client .AddReactor ("get" , "volumesnapshots" , reactor .React )
586
501
client .AddReactor ("delete" , "volumesnapshotcontents" , reactor .React )
587
- client .AddReactor ("delete" , "volumesnapshots" , reactor .React )
588
502
589
503
return reactor
590
504
}
@@ -765,7 +679,7 @@ func wrapTestWithInjectedOperation(toWrap testCall, injectBeforeOperation func(c
765
679
klog .V (4 ).Infof ("reactor:injecting call" )
766
680
injectBeforeOperation (ctrl , reactor )
767
681
768
- // Run the tested function (typically syncSnapshot/ syncContent) in a
682
+ // Run the tested function (typically syncContent) in a
769
683
// separate goroutine.
770
684
var testError error
771
685
var testFinished int32
@@ -798,7 +712,7 @@ func evaluateTestResults(ctrl *csiSnapshotSideCarController, reactor *snapshotRe
798
712
}
799
713
}
800
714
801
- // Test single call to syncSnapshot and syncContent methods.
715
+ // Test single call to syncContent methods.
802
716
// For all tests:
803
717
// 1. Fill in the controller with initial data
804
718
// 2. Call the tested function (syncContent) via
0 commit comments