Skip to content

Commit 6685a1b

Browse files
authored
Merge pull request #446 from mattcary/nil-fix
Cherrypick #381 to 2.1.
2 parents 4af0677 + ec44a61 commit 6685a1b

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

pkg/common-controller/framework_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,21 @@ type reactorError struct {
164164
error error
165165
}
166166

167+
// testError is an error returned from a test that marks a test as failed even
168+
// though the test case itself expected a common error (such as API error)
169+
type testError string
170+
171+
func (t testError) Error() string {
172+
return string(t)
173+
}
174+
175+
var _ error = testError("foo")
176+
177+
func isTestError(err error) bool {
178+
_, ok := err.(testError)
179+
return ok
180+
}
181+
167182
func withSnapshotFinalizers(snapshots []*crdv1.VolumeSnapshot, finalizers ...string) []*crdv1.VolumeSnapshot {
168183
for i := range snapshots {
169184
for _, f := range finalizers {
@@ -1111,7 +1126,11 @@ func testRemoveSnapshotFinalizer(ctrl *csiSnapshotCommonController, reactor *sna
11111126
}
11121127

11131128
func testUpdateSnapshotClass(ctrl *csiSnapshotCommonController, reactor *snapshotReactor, test controllerTest) error {
1114-
_, err := ctrl.checkAndUpdateSnapshotClass(test.initialSnapshots[0])
1129+
snap, err := ctrl.checkAndUpdateSnapshotClass(test.initialSnapshots[0])
1130+
// syncSnapshotByKey expects that checkAndUpdateSnapshotClass always returns a snapshot
1131+
if snap == nil {
1132+
return testError(fmt.Sprintf("checkAndUpdateSnapshotClass returned nil snapshot on error: %v", err))
1133+
}
11151134
return err
11161135
}
11171136

@@ -1441,6 +1460,9 @@ func runUpdateSnapshotClassTests(t *testing.T, tests []controllerTest, snapshotC
14411460

14421461
// Run the tested functions
14431462
err = test.test(ctrl, reactor, test)
1463+
if err != nil && isTestError(err) {
1464+
t.Errorf("Test %q failed: %v", test.name, err)
1465+
}
14441466
if test.expectSuccess && err != nil {
14451467
t.Errorf("Test %q failed: %v", test.name, err)
14461468
}

pkg/common-controller/snapshot_controller_base.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ func (ctrl *csiSnapshotCommonController) contentWorker() {
324324

325325
// checkAndUpdateSnapshotClass gets the VolumeSnapshotClass from VolumeSnapshot. If it is not set,
326326
// gets it from default VolumeSnapshotClass and sets it.
327+
// On error, it must return the original snapshot, not nil, because the caller syncContentByKey
328+
// needs to check snapshot's timestamp.
327329
func (ctrl *csiSnapshotCommonController) checkAndUpdateSnapshotClass(snapshot *crdv1.VolumeSnapshot) (*crdv1.VolumeSnapshot, error) {
328330
className := snapshot.Spec.VolumeSnapshotClassName
329331
var class *crdv1.VolumeSnapshotClass
@@ -344,7 +346,7 @@ func (ctrl *csiSnapshotCommonController) checkAndUpdateSnapshotClass(snapshot *c
344346
if err != nil {
345347
klog.Errorf("checkAndUpdateSnapshotClass failed to setDefaultClass %v", err)
346348
ctrl.updateSnapshotErrorStatusWithEvent(snapshot, v1.EventTypeWarning, "SetDefaultSnapshotClassFailed", fmt.Sprintf("Failed to set default snapshot class with error %v", err))
347-
return nil, err
349+
return snapshot, err
348350
}
349351
}
350352

pkg/common-controller/snapshotclass_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ func TestUpdateSnapshotClass(t *testing.T) {
8686
},
8787
test: testUpdateSnapshotClass,
8888
},
89+
{
90+
// PVC does not exist
91+
name: "1-5 - snapshot update with default class name failed because PVC was not found",
92+
initialContents: nocontents,
93+
initialSnapshots: newSnapshotArray("snap1-5", "snapuid1-5", "claim1-5", "", "", "", &True, nil, nil, nil, false, true, nil),
94+
expectedSnapshots: newSnapshotArray("snap1-5", "snapuid1-5", "claim1-5", "", "", "", &False, nil, nil, newVolumeError("Failed to set default snapshot class with error failed to retrieve PVC claim1-5 from the lister: \"persistentvolumeclaim \\\"claim1-5\\\" not found\""), false, true, nil),
95+
initialClaims: nil,
96+
initialVolumes: nil,
97+
initialStorageClasses: []*storage.StorageClass{},
98+
expectedEvents: []string{"Warning SetDefaultSnapshotClassFailed"},
99+
errors: noerrors,
100+
test: testUpdateSnapshotClass,
101+
},
89102
}
90103

91104
runUpdateSnapshotClassTests(t, tests, snapshotClasses)

0 commit comments

Comments
 (0)