Skip to content

Commit 16392a3

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

File tree

1 file changed

+14
-100
lines changed

1 file changed

+14
-100
lines changed

pkg/sidecar-controller/framework_test.go

+14-100
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

@@ -129,11 +124,7 @@ 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
@@ -146,7 +137,7 @@ type snapshotReactor struct {
146137
// reactorError is an error that is returned by test reactor (=simulated
147138
// etcd+/API server) when an action performed by the reactor matches given verb
148139
// ("get", "update", "create", "delete" or "*"") on given resource
149-
// ("volumesnapshotcontents", "volumesnapshots" or "*").
140+
// ("volumesnapshotcontents" or "*").
150141
type reactorError struct {
151142
verb string
152143
resource string
@@ -203,9 +194,9 @@ func (r *snapshotReactor) React(action core.Action) (handled bool, ret runtime.O
203194
content := obj.(*crdv1.VolumeSnapshotContent)
204195

205196
// Check and bump object version
206-
storedVolume, found := r.contents[content.Name]
197+
storedContent, found := r.contents[content.Name]
207198
if found {
208-
storedVer, _ := strconv.Atoi(storedVolume.ResourceVersion)
199+
storedVer, _ := strconv.Atoi(storedContent.ResourceVersion)
209200
requestedVer, _ := strconv.Atoi(content.ResourceVersion)
210201
if storedVer != requestedVer {
211202
return true, obj, errVersionConflict
@@ -314,41 +305,6 @@ func (r *snapshotReactor) checkContents(expectedContents []*crdv1.VolumeSnapshot
314305
return nil
315306
}
316307

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-
352308
// checkEvents compares all expectedEvents with events generated during the test
353309
// and reports differences.
354310
func checkEvents(t *testing.T, expectedEvents []string, ctrl *csiSnapshotSideCarController) error {
@@ -414,9 +370,6 @@ func (r *snapshotReactor) popChange() interface{} {
414370
case *crdv1.VolumeSnapshotContent:
415371
vol, _ := obj.(*crdv1.VolumeSnapshotContent)
416372
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)
420373
}
421374
}
422375

@@ -428,21 +381,15 @@ func (r *snapshotReactor) popChange() interface{} {
428381

429382
// syncAll simulates the controller periodic sync of contents and snapshot. It
430383
// 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
432385
// use real controller loop (ctrl.Run()) will get periodic sync automatically.
433386
func (r *snapshotReactor) syncAll() {
434387
r.lock.Lock()
435388
defer r.lock.Unlock()
436389

437-
for _, c := range r.snapshots {
438-
r.changedObjects = append(r.changedObjects, c)
439-
}
440390
for _, v := range r.contents {
441391
r.changedObjects = append(r.changedObjects, v)
442392
}
443-
for _, pvc := range r.claims {
444-
r.changedObjects = append(r.changedObjects, pvc)
445-
}
446393
r.changedSinceLastSync = 0
447394
}
448395

@@ -511,22 +458,6 @@ func (r *snapshotReactor) deleteContentEvent(content *crdv1.VolumeSnapshotConten
511458
}
512459
}
513460

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-
530461
// addContentEvent simulates that a content has been added in etcd and the
531462
// controller receives 'content added' event.
532463
func (r *snapshotReactor) addContentEvent(content *crdv1.VolumeSnapshotContent) {
@@ -555,20 +486,6 @@ func (r *snapshotReactor) modifyContentEvent(content *crdv1.VolumeSnapshotConten
555486
}
556487
}
557488

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

581498
client.AddReactor("create", "volumesnapshotcontents", reactor.React)
582499
client.AddReactor("update", "volumesnapshotcontents", reactor.React)
583-
client.AddReactor("update", "volumesnapshots", reactor.React)
584500
client.AddReactor("get", "volumesnapshotcontents", reactor.React)
585-
client.AddReactor("get", "volumesnapshots", reactor.React)
586501
client.AddReactor("delete", "volumesnapshotcontents", reactor.React)
587-
client.AddReactor("delete", "volumesnapshots", reactor.React)
588502

589503
return reactor
590504
}
@@ -765,7 +679,7 @@ func wrapTestWithInjectedOperation(toWrap testCall, injectBeforeOperation func(c
765679
klog.V(4).Infof("reactor:injecting call")
766680
injectBeforeOperation(ctrl, reactor)
767681

768-
// Run the tested function (typically syncSnapshot/syncContent) in a
682+
// Run the tested function (typically syncContent) in a
769683
// separate goroutine.
770684
var testError error
771685
var testFinished int32
@@ -798,7 +712,7 @@ func evaluateTestResults(ctrl *csiSnapshotSideCarController, reactor *snapshotRe
798712
}
799713
}
800714

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

0 commit comments

Comments
 (0)