Skip to content

Commit 4a282f7

Browse files
committed
Cleanup framework_test comments and old code
Signed-off-by: Grant Griffiths <[email protected]>
1 parent 2ac906d commit 4a282f7

File tree

1 file changed

+16
-103
lines changed

1 file changed

+16
-103
lines changed

pkg/sidecar-controller/framework_test.go

+16-103
Original file line numberDiff line numberDiff line change
@@ -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.
7773
type controllerTest struct {
7874
// Name of the test, for logging
@@ -106,7 +102,6 @@ const mockDriverName = "csi-mock-plugin"
106102

107103
var errVersionConflict = errors.New("VersionError")
108104
var nocontents []*crdv1.VolumeSnapshotContent
109-
var nosnapshots []*crdv1.VolumeSnapshot
110105
var noevents = []string{}
111106
var noerrors = []reactorError{}
112107

@@ -118,7 +113,7 @@ var noerrors = []reactorError{}
118113
// is updated first and snapshot.Phase second. This queue will then contain both
119114
// updates as separate entries.
120115
// - 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
122117
// used by the controller. Any time an event function like deleteContentEvent
123118
// is called to simulate an event, the reactor's stores are updated and the
124119
// controller is sent the event via the fake watcher.
@@ -129,24 +124,19 @@ var noerrors = []reactorError{}
129124
// the list.
130125
type snapshotReactor struct {
131126
secrets map[string]*v1.Secret
132-
storageClasses map[string]*storagev1.StorageClass
133-
volumes map[string]*v1.PersistentVolume
134-
claims map[string]*v1.PersistentVolumeClaim
135127
contents map[string]*crdv1.VolumeSnapshotContent
136-
snapshots map[string]*crdv1.VolumeSnapshot
137128
changedObjects []interface{}
138129
changedSinceLastSync int
139130
ctrl *csiSnapshotSideCarController
140131
fakeContentWatch *watch.FakeWatcher
141-
fakeSnapshotWatch *watch.FakeWatcher
142132
lock sync.Mutex
143133
errors []reactorError
144134
}
145135

146136
// reactorError is an error that is returned by test reactor (=simulated
147137
// etcd+/API server) when an action performed by the reactor matches given verb
148138
// ("get", "update", "create", "delete" or "*"") on given resource
149-
// ("volumesnapshotcontents", "volumesnapshots" or "*").
139+
// ("volumesnapshotcontents" or "*").
150140
type reactorError struct {
151141
verb string
152142
resource string
@@ -203,9 +193,9 @@ func (r *snapshotReactor) React(action core.Action) (handled bool, ret runtime.O
203193
content := obj.(*crdv1.VolumeSnapshotContent)
204194

205195
// Check and bump object version
206-
storedVolume, found := r.contents[content.Name]
196+
storedContent, found := r.contents[content.Name]
207197
if found {
208-
storedVer, _ := strconv.Atoi(storedVolume.ResourceVersion)
198+
storedVer, _ := strconv.Atoi(storedContent.ResourceVersion)
209199
requestedVer, _ := strconv.Atoi(content.ResourceVersion)
210200
if storedVer != requestedVer {
211201
return true, obj, errVersionConflict
@@ -314,41 +304,6 @@ func (r *snapshotReactor) checkContents(expectedContents []*crdv1.VolumeSnapshot
314304
return nil
315305
}
316306

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-
352307
// checkEvents compares all expectedEvents with events generated during the test
353308
// and reports differences.
354309
func checkEvents(t *testing.T, expectedEvents []string, ctrl *csiSnapshotSideCarController) error {
@@ -414,9 +369,6 @@ func (r *snapshotReactor) popChange() interface{} {
414369
case *crdv1.VolumeSnapshotContent:
415370
vol, _ := obj.(*crdv1.VolumeSnapshotContent)
416371
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)
420372
}
421373
}
422374

@@ -426,23 +378,17 @@ func (r *snapshotReactor) popChange() interface{} {
426378
return obj
427379
}
428380

429-
// syncAll simulates the controller periodic sync of contents and snapshot. It
381+
// syncAll simulates the controller periodic sync of contents. It
430382
// 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
432384
// use real controller loop (ctrl.Run()) will get periodic sync automatically.
433385
func (r *snapshotReactor) syncAll() {
434386
r.lock.Lock()
435387
defer r.lock.Unlock()
436388

437-
for _, c := range r.snapshots {
438-
r.changedObjects = append(r.changedObjects, c)
439-
}
440389
for _, v := range r.contents {
441390
r.changedObjects = append(r.changedObjects, v)
442391
}
443-
for _, pvc := range r.claims {
444-
r.changedObjects = append(r.changedObjects, pvc)
445-
}
446392
r.changedSinceLastSync = 0
447393
}
448394

@@ -511,22 +457,6 @@ func (r *snapshotReactor) deleteContentEvent(content *crdv1.VolumeSnapshotConten
511457
}
512458
}
513459

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-
530460
// addContentEvent simulates that a content has been added in etcd and the
531461
// controller receives 'content added' event.
532462
func (r *snapshotReactor) addContentEvent(content *crdv1.VolumeSnapshotContent) {
@@ -555,20 +485,6 @@ func (r *snapshotReactor) modifyContentEvent(content *crdv1.VolumeSnapshotConten
555485
}
556486
}
557487

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-
572488
func newSnapshotReactor(kubeClient *kubefake.Clientset, client *fake.Clientset, ctrl *csiSnapshotSideCarController, fakeVolumeWatch, fakeClaimWatch *watch.FakeWatcher, errors []reactorError) *snapshotReactor {
573489
reactor := &snapshotReactor{
574490
secrets: make(map[string]*v1.Secret),
@@ -580,11 +496,8 @@ func newSnapshotReactor(kubeClient *kubefake.Clientset, client *fake.Clientset,
580496

581497
client.AddReactor("create", "volumesnapshotcontents", reactor.React)
582498
client.AddReactor("update", "volumesnapshotcontents", reactor.React)
583-
client.AddReactor("update", "volumesnapshots", reactor.React)
584499
client.AddReactor("get", "volumesnapshotcontents", reactor.React)
585-
client.AddReactor("get", "volumesnapshots", reactor.React)
586500
client.AddReactor("delete", "volumesnapshotcontents", reactor.React)
587-
client.AddReactor("delete", "volumesnapshots", reactor.React)
588501

589502
return reactor
590503
}
@@ -765,7 +678,7 @@ func wrapTestWithInjectedOperation(toWrap testCall, injectBeforeOperation func(c
765678
klog.V(4).Infof("reactor:injecting call")
766679
injectBeforeOperation(ctrl, reactor)
767680

768-
// Run the tested function (typically syncSnapshot/syncContent) in a
681+
// Run the tested function (typically syncContent) in a
769682
// separate goroutine.
770683
var testError error
771684
var testFinished int32
@@ -798,7 +711,7 @@ func evaluateTestResults(ctrl *csiSnapshotSideCarController, reactor *snapshotRe
798711
}
799712
}
800713

801-
// Test single call to syncSnapshot and syncContent methods.
714+
// Test single call to syncContent methods.
802715
// For all tests:
803716
// 1. Fill in the controller with initial data
804717
// 2. Call the tested function (syncContent) via

0 commit comments

Comments
 (0)