@@ -33,7 +33,6 @@ import (
3333 storagelisters "github.com/kubernetes-csi/external-snapshotter/pkg/client/listers/volumesnapshot/v1beta1"
3434 "github.com/kubernetes-csi/external-snapshotter/pkg/utils"
3535 v1 "k8s.io/api/core/v1"
36- storagev1 "k8s.io/api/storage/v1"
3736 "k8s.io/apimachinery/pkg/api/resource"
3837 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3938 "k8s.io/apimachinery/pkg/runtime"
@@ -50,29 +49,26 @@ import (
5049 "k8s.io/klog"
5150)
5251
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
5554// 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
5857// modified" events and periodic sync), until the controller settles down and
5958// does not modify anything.
6059// 3) Simulate almost real API server/etcd and call add/update/delete
61- // content/snapshot .
60+ // content.
6261// 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
6463// differences.
6564
6665// 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
6867// controller before the test starts. The test then contains a reference to
6968// 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.
7369// - testSyncContent - calls syncContent on the first content in initialContents.
7470// - 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
7672// of the test and list of generated events.
7773type controllerTest struct {
7874 // Name of the test, for logging
@@ -106,19 +102,17 @@ const mockDriverName = "csi-mock-plugin"
106102
107103var errVersionConflict = errors .New ("VersionError" )
108104var nocontents []* crdv1.VolumeSnapshotContent
109- var nosnapshots []* crdv1.VolumeSnapshot
110105var noevents = []string {}
111106var noerrors = []reactorError {}
112107
113108// snapshotReactor is a core.Reactor that simulates etcd and API server. It
114109// stores:
115110// - Latest version of snapshots contents saved by the controller.
116- // - Queue of all saves (to simulate "content/snapshot updated" events). This queue
117- // contains all intermediate state of an object - e.g. a snapshot.VolumeName
118- // is updated first and snapshot.Phase second. This queue will then contain both
111+ // - Queue of all saves (to simulate "content updated" events). This queue
112+ // contains all intermediate state of an object. This queue will then contain both
119113// updates as separate entries.
120114// - Number of changes since the last call to snapshotReactor.syncAll().
121- // - Optionally, content and snapshot fake watchers which should be the same ones
115+ // - Optionally, content watcher which should be the same ones
122116// used by the controller. Any time an event function like deleteContentEvent
123117// is called to simulate an event, the reactor's stores are updated and the
124118// controller is sent the event via the fake watcher.
@@ -129,24 +123,19 @@ var noerrors = []reactorError{}
129123// the list.
130124type snapshotReactor struct {
131125 secrets map [string ]* v1.Secret
132- storageClasses map [string ]* storagev1.StorageClass
133- volumes map [string ]* v1.PersistentVolume
134- claims map [string ]* v1.PersistentVolumeClaim
135126 contents map [string ]* crdv1.VolumeSnapshotContent
136- snapshots map [string ]* crdv1.VolumeSnapshot
137127 changedObjects []interface {}
138128 changedSinceLastSync int
139129 ctrl * csiSnapshotSideCarController
140130 fakeContentWatch * watch.FakeWatcher
141- fakeSnapshotWatch * watch.FakeWatcher
142131 lock sync.Mutex
143132 errors []reactorError
144133}
145134
146135// reactorError is an error that is returned by test reactor (=simulated
147136// etcd+/API server) when an action performed by the reactor matches given verb
148137// ("get", "update", "create", "delete" or "*"") on given resource
149- // ("volumesnapshotcontents", "volumesnapshots" or "*").
138+ // ("volumesnapshotcontents" or "*").
150139type reactorError struct {
151140 verb string
152141 resource string
@@ -203,9 +192,9 @@ func (r *snapshotReactor) React(action core.Action) (handled bool, ret runtime.O
203192 content := obj .(* crdv1.VolumeSnapshotContent )
204193
205194 // Check and bump object version
206- storedVolume , found := r .contents [content .Name ]
195+ storedContent , found := r .contents [content .Name ]
207196 if found {
208- storedVer , _ := strconv .Atoi (storedVolume .ResourceVersion )
197+ storedVer , _ := strconv .Atoi (storedContent .ResourceVersion )
209198 requestedVer , _ := strconv .Atoi (content .ResourceVersion )
210199 if storedVer != requestedVer {
211200 return true , obj , errVersionConflict
@@ -314,41 +303,6 @@ func (r *snapshotReactor) checkContents(expectedContents []*crdv1.VolumeSnapshot
314303 return nil
315304}
316305
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-
352306// checkEvents compares all expectedEvents with events generated during the test
353307// and reports differences.
354308func checkEvents (t * testing.T , expectedEvents []string , ctrl * csiSnapshotSideCarController ) error {
@@ -414,9 +368,6 @@ func (r *snapshotReactor) popChange() interface{} {
414368 case * crdv1.VolumeSnapshotContent :
415369 vol , _ := obj .(* crdv1.VolumeSnapshotContent )
416370 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 )
420371 }
421372 }
422373
@@ -426,23 +377,17 @@ func (r *snapshotReactor) popChange() interface{} {
426377 return obj
427378}
428379
429- // syncAll simulates the controller periodic sync of contents and snapshot . It
380+ // syncAll simulates the controller periodic sync of contents. It
430381// 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
382+ // should be used when the test manually calls syncContent. Test that
432383// use real controller loop (ctrl.Run()) will get periodic sync automatically.
433384func (r * snapshotReactor ) syncAll () {
434385 r .lock .Lock ()
435386 defer r .lock .Unlock ()
436387
437- for _ , c := range r .snapshots {
438- r .changedObjects = append (r .changedObjects , c )
439- }
440388 for _ , v := range r .contents {
441389 r .changedObjects = append (r .changedObjects , v )
442390 }
443- for _ , pvc := range r .claims {
444- r .changedObjects = append (r .changedObjects , pvc )
445- }
446391 r .changedSinceLastSync = 0
447392}
448393
@@ -511,22 +456,6 @@ func (r *snapshotReactor) deleteContentEvent(content *crdv1.VolumeSnapshotConten
511456 }
512457}
513458
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-
530459// addContentEvent simulates that a content has been added in etcd and the
531460// controller receives 'content added' event.
532461func (r * snapshotReactor ) addContentEvent (content * crdv1.VolumeSnapshotContent ) {
@@ -555,20 +484,6 @@ func (r *snapshotReactor) modifyContentEvent(content *crdv1.VolumeSnapshotConten
555484 }
556485}
557486
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-
572487func newSnapshotReactor (kubeClient * kubefake.Clientset , client * fake.Clientset , ctrl * csiSnapshotSideCarController , fakeVolumeWatch , fakeClaimWatch * watch.FakeWatcher , errors []reactorError ) * snapshotReactor {
573488 reactor := & snapshotReactor {
574489 secrets : make (map [string ]* v1.Secret ),
@@ -580,11 +495,8 @@ func newSnapshotReactor(kubeClient *kubefake.Clientset, client *fake.Clientset,
580495
581496 client .AddReactor ("create" , "volumesnapshotcontents" , reactor .React )
582497 client .AddReactor ("update" , "volumesnapshotcontents" , reactor .React )
583- client .AddReactor ("update" , "volumesnapshots" , reactor .React )
584498 client .AddReactor ("get" , "volumesnapshotcontents" , reactor .React )
585- client .AddReactor ("get" , "volumesnapshots" , reactor .React )
586499 client .AddReactor ("delete" , "volumesnapshotcontents" , reactor .React )
587- client .AddReactor ("delete" , "volumesnapshots" , reactor .React )
588500
589501 return reactor
590502}
@@ -765,7 +677,7 @@ func wrapTestWithInjectedOperation(toWrap testCall, injectBeforeOperation func(c
765677 klog .V (4 ).Infof ("reactor:injecting call" )
766678 injectBeforeOperation (ctrl , reactor )
767679
768- // Run the tested function (typically syncSnapshot/ syncContent) in a
680+ // Run the tested function (typically syncContent) in a
769681 // separate goroutine.
770682 var testError error
771683 var testFinished int32
@@ -798,7 +710,7 @@ func evaluateTestResults(ctrl *csiSnapshotSideCarController, reactor *snapshotRe
798710 }
799711}
800712
801- // Test single call to syncSnapshot and syncContent methods.
713+ // Test single call to syncContent methods.
802714// For all tests:
803715// 1. Fill in the controller with initial data
804716// 2. Call the tested function (syncContent) via
0 commit comments