-
Notifications
You must be signed in to change notification settings - Fork 379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor common controller tests to use withXYZ functions and add tests #254
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,16 +164,17 @@ type reactorError struct { | |
error error | ||
} | ||
|
||
func withSnapshotFinalizer(snapshot *crdv1.VolumeSnapshot) *crdv1.VolumeSnapshot { | ||
snapshot.ObjectMeta.Finalizers = append(snapshot.ObjectMeta.Finalizers, utils.VolumeSnapshotContentFinalizer) | ||
snapshot.ObjectMeta.Finalizers = append(snapshot.ObjectMeta.Finalizers, utils.VolumeSnapshotAsSourceFinalizer) | ||
snapshot.ObjectMeta.Finalizers = append(snapshot.ObjectMeta.Finalizers, utils.VolumeSnapshotBoundFinalizer) | ||
return snapshot | ||
func withSnapshotFinalizers(snapshots []*crdv1.VolumeSnapshot, finalizers ...string) []*crdv1.VolumeSnapshot { | ||
for i := range snapshots { | ||
for _, f := range finalizers { | ||
snapshots[i].ObjectMeta.Finalizers = append(snapshots[i].ObjectMeta.Finalizers, f) | ||
} | ||
} | ||
return snapshots | ||
} | ||
|
||
func withContentFinalizer(content *crdv1.VolumeSnapshotContent) *crdv1.VolumeSnapshotContent { | ||
content.ObjectMeta.Finalizers = append(content.ObjectMeta.Finalizers, utils.VolumeSnapshotContentFinalizer) | ||
metav1.SetMetaDataAnnotation(&content.ObjectMeta, utils.AnnVolumeSnapshotBeingDeleted, "yes") | ||
return content | ||
} | ||
|
||
|
@@ -824,6 +825,18 @@ func newContent(contentName, boundToSnapshotUID, boundToSnapshotName, snapshotHa | |
return &content | ||
} | ||
|
||
func withContentAnnotations(contents []*crdv1.VolumeSnapshotContent, annotations map[string]string) []*crdv1.VolumeSnapshotContent { | ||
for i := range contents { | ||
if contents[i].ObjectMeta.Annotations == nil { | ||
contents[i].ObjectMeta.Annotations = make(map[string]string) | ||
} | ||
for k, v := range annotations { | ||
contents[i].ObjectMeta.Annotations[k] = v | ||
} | ||
} | ||
return contents | ||
} | ||
|
||
func newContentArray(contentName, boundToSnapshotUID, boundToSnapshotName, snapshotHandle, snapshotClassName, desiredSnapshotHandle, volumeHandle string, | ||
deletionPolicy crdv1.DeletionPolicy, size, creationTime *int64, | ||
withFinalizer bool) []*crdv1.VolumeSnapshotContent { | ||
|
@@ -863,7 +876,7 @@ func newContentWithUnmatchDriverArray(contentName, boundToSnapshotUID, boundToSn | |
func newSnapshot( | ||
snapshotName, snapshotUID, pvcName, targetContentName, snapshotClassName, boundContentName string, | ||
readyToUse *bool, creationTime *metav1.Time, restoreSize *resource.Quantity, | ||
err *crdv1.VolumeSnapshotError, nilStatus bool, withFinalizer bool, deletionTimestamp *metav1.Time) *crdv1.VolumeSnapshot { | ||
err *crdv1.VolumeSnapshotError, nilStatus bool, withAllFinalizers bool, deletionTimestamp *metav1.Time) *crdv1.VolumeSnapshot { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought the test framework in pv/pvc controller will allow you to create a basic newSnapshot() without passing in the withAllFinalizers flag, and then we can have another function withAllFinalizers() which takes a created snapshot object as input and apply finalizers on top of it. So that means we shouldn't need "withAllFinalizers bool" as an input parameter for newSnapshot(). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I left "withFinalizers" as "withAllFinalizers" for the purpose of reducing the refactor required. Some tests require all finalizers while others require no finalizers, so pretty much every test would have the "withSnapFinalizers" wrapper. I mainly introduced "func withSnapshotFinalizers()" to fine-tune which finalizers are added. i.e. for cases where only one finalizer is wanted. |
||
snapshot := crdv1.VolumeSnapshot{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: snapshotName, | ||
|
@@ -904,18 +917,18 @@ func newSnapshot( | |
VolumeSnapshotContentName: &targetContentName, | ||
} | ||
} | ||
if withFinalizer { | ||
return withSnapshotFinalizer(&snapshot) | ||
if withAllFinalizers { | ||
return withSnapshotFinalizers([]*crdv1.VolumeSnapshot{&snapshot}, utils.VolumeSnapshotAsSourceFinalizer, utils.VolumeSnapshotBoundFinalizer)[0] | ||
} | ||
return &snapshot | ||
} | ||
|
||
func newSnapshotArray( | ||
snapshotName, snapshotUID, pvcName, targetContentName, snapshotClassName, boundContentName string, | ||
readyToUse *bool, creationTime *metav1.Time, restoreSize *resource.Quantity, | ||
err *crdv1.VolumeSnapshotError, nilStatus bool, withFinalizer bool, deletionTimestamp *metav1.Time) []*crdv1.VolumeSnapshot { | ||
err *crdv1.VolumeSnapshotError, nilStatus bool, withAllFinalizers bool, deletionTimestamp *metav1.Time) []*crdv1.VolumeSnapshot { | ||
return []*crdv1.VolumeSnapshot{ | ||
newSnapshot(snapshotName, snapshotUID, pvcName, targetContentName, snapshotClassName, boundContentName, readyToUse, creationTime, restoreSize, err, nilStatus, withFinalizer, deletionTimestamp), | ||
newSnapshot(snapshotName, snapshotUID, pvcName, targetContentName, snapshotClassName, boundContentName, readyToUse, creationTime, restoreSize, err, nilStatus, withAllFinalizers, deletionTimestamp), | ||
} | ||
} | ||
|
||
|
@@ -1071,6 +1084,14 @@ func testSyncContent(ctrl *csiSnapshotCommonController, reactor *snapshotReactor | |
return ctrl.syncContent(test.initialContents[0]) | ||
} | ||
|
||
func testSyncContentError(ctrl *csiSnapshotCommonController, reactor *snapshotReactor, test controllerTest) error { | ||
err := ctrl.syncContent(test.initialContents[0]) | ||
if err != nil { | ||
return nil | ||
} | ||
return fmt.Errorf("syncContent succeeded when failure was expected") | ||
} | ||
|
||
func testAddPVCFinalizer(ctrl *csiSnapshotCommonController, reactor *snapshotReactor, test controllerTest) error { | ||
return ctrl.ensurePVCFinalizer(test.initialSnapshots[0]) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I see that utils.VolumeSnapshotContentFinalizer is added to VolumeSnapshot API object here. This is wrong. We only need utils.VolumeSnapshotContentFinalizer for VolumeSnapshotContent, not for VolumeSnapshot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, removed the contentFinalizer.