@@ -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
@@ -118,7 +113,7 @@ var noerrors = []reactorError{}
118
113
// is updated first and snapshot.Phase second. This queue will then contain both
119
114
// updates as separate entries.
120
115
// - Number of changes since the last call to snapshotReactor.syncAll().
121
- // - Optionally, content and snapshot fake watchers which should be the same ones
116
+ // - Optionally, content watcher which should be the same ones
122
117
// used by the controller. Any time an event function like deleteContentEvent
123
118
// is called to simulate an event, the reactor's stores are updated and the
124
119
// controller is sent the event via the fake watcher.
@@ -129,24 +124,19 @@ 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
140
131
fakeContentWatch * watch.FakeWatcher
141
- fakeSnapshotWatch * watch.FakeWatcher
142
132
lock sync.Mutex
143
133
errors []reactorError
144
134
}
145
135
146
136
// reactorError is an error that is returned by test reactor (=simulated
147
137
// etcd+/API server) when an action performed by the reactor matches given verb
148
138
// ("get", "update", "create", "delete" or "*"") on given resource
149
- // ("volumesnapshotcontents", "volumesnapshots" or "*").
139
+ // ("volumesnapshotcontents" or "*").
150
140
type reactorError struct {
151
141
verb string
152
142
resource string
@@ -203,9 +193,9 @@ func (r *snapshotReactor) React(action core.Action) (handled bool, ret runtime.O
203
193
content := obj .(* crdv1.VolumeSnapshotContent )
204
194
205
195
// Check and bump object version
206
- storedVolume , found := r .contents [content .Name ]
196
+ storedContent , found := r .contents [content .Name ]
207
197
if found {
208
- storedVer , _ := strconv .Atoi (storedVolume .ResourceVersion )
198
+ storedVer , _ := strconv .Atoi (storedContent .ResourceVersion )
209
199
requestedVer , _ := strconv .Atoi (content .ResourceVersion )
210
200
if storedVer != requestedVer {
211
201
return true , obj , errVersionConflict
@@ -314,41 +304,6 @@ func (r *snapshotReactor) checkContents(expectedContents []*crdv1.VolumeSnapshot
314
304
return nil
315
305
}
316
306
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
307
// checkEvents compares all expectedEvents with events generated during the test
353
308
// and reports differences.
354
309
func checkEvents (t * testing.T , expectedEvents []string , ctrl * csiSnapshotSideCarController ) error {
@@ -414,9 +369,6 @@ func (r *snapshotReactor) popChange() interface{} {
414
369
case * crdv1.VolumeSnapshotContent :
415
370
vol , _ := obj .(* crdv1.VolumeSnapshotContent )
416
371
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
372
}
421
373
}
422
374
@@ -426,23 +378,17 @@ func (r *snapshotReactor) popChange() interface{} {
426
378
return obj
427
379
}
428
380
429
- // syncAll simulates the controller periodic sync of contents and snapshot . It
381
+ // syncAll simulates the controller periodic sync of contents. It
430
382
// 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
383
+ // should be used when the test manually calls syncContent. Test that
432
384
// use real controller loop (ctrl.Run()) will get periodic sync automatically.
433
385
func (r * snapshotReactor ) syncAll () {
434
386
r .lock .Lock ()
435
387
defer r .lock .Unlock ()
436
388
437
- for _ , c := range r .snapshots {
438
- r .changedObjects = append (r .changedObjects , c )
439
- }
440
389
for _ , v := range r .contents {
441
390
r .changedObjects = append (r .changedObjects , v )
442
391
}
443
- for _ , pvc := range r .claims {
444
- r .changedObjects = append (r .changedObjects , pvc )
445
- }
446
392
r .changedSinceLastSync = 0
447
393
}
448
394
@@ -511,22 +457,6 @@ func (r *snapshotReactor) deleteContentEvent(content *crdv1.VolumeSnapshotConten
511
457
}
512
458
}
513
459
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
460
// addContentEvent simulates that a content has been added in etcd and the
531
461
// controller receives 'content added' event.
532
462
func (r * snapshotReactor ) addContentEvent (content * crdv1.VolumeSnapshotContent ) {
@@ -555,20 +485,6 @@ func (r *snapshotReactor) modifyContentEvent(content *crdv1.VolumeSnapshotConten
555
485
}
556
486
}
557
487
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
488
func newSnapshotReactor (kubeClient * kubefake.Clientset , client * fake.Clientset , ctrl * csiSnapshotSideCarController , fakeVolumeWatch , fakeClaimWatch * watch.FakeWatcher , errors []reactorError ) * snapshotReactor {
573
489
reactor := & snapshotReactor {
574
490
secrets : make (map [string ]* v1.Secret ),
@@ -580,11 +496,8 @@ func newSnapshotReactor(kubeClient *kubefake.Clientset, client *fake.Clientset,
580
496
581
497
client .AddReactor ("create" , "volumesnapshotcontents" , reactor .React )
582
498
client .AddReactor ("update" , "volumesnapshotcontents" , reactor .React )
583
- client .AddReactor ("update" , "volumesnapshots" , reactor .React )
584
499
client .AddReactor ("get" , "volumesnapshotcontents" , reactor .React )
585
- client .AddReactor ("get" , "volumesnapshots" , reactor .React )
586
500
client .AddReactor ("delete" , "volumesnapshotcontents" , reactor .React )
587
- client .AddReactor ("delete" , "volumesnapshots" , reactor .React )
588
501
589
502
return reactor
590
503
}
@@ -765,7 +678,7 @@ func wrapTestWithInjectedOperation(toWrap testCall, injectBeforeOperation func(c
765
678
klog .V (4 ).Infof ("reactor:injecting call" )
766
679
injectBeforeOperation (ctrl , reactor )
767
680
768
- // Run the tested function (typically syncSnapshot/ syncContent) in a
681
+ // Run the tested function (typically syncContent) in a
769
682
// separate goroutine.
770
683
var testError error
771
684
var testFinished int32
@@ -798,7 +711,7 @@ func evaluateTestResults(ctrl *csiSnapshotSideCarController, reactor *snapshotRe
798
711
}
799
712
}
800
713
801
- // Test single call to syncSnapshot and syncContent methods.
714
+ // Test single call to syncContent methods.
802
715
// For all tests:
803
716
// 1. Fill in the controller with initial data
804
717
// 2. Call the tested function (syncContent) via
0 commit comments