Skip to content

Commit

Permalink
storage: enhance test for ValidateCSIDriverUpdate
Browse files Browse the repository at this point in the history
This revised test changes one field at a time in various ways. This
ensures that one failure reason doesn't mask the other.

An incorrect comment also gets fixed.

Suggested in kubernetes#80568 (review).
  • Loading branch information
pohly committed Aug 26, 2019
1 parent 3972485 commit c20721a
Showing 1 changed file with 60 additions and 38 deletions.
98 changes: 60 additions & 38 deletions pkg/apis/storage/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,7 @@ func TestCSIDriverValidation(t *testing.T) {
},
},
{
// AttachRequired not set
// PodInfoOnMount not set
ObjectMeta: metav1.ObjectMeta{Name: driverName},
Spec: storage.CSIDriverSpec{
AttachRequired: &attachNotRequired,
Expand Down Expand Up @@ -1856,64 +1856,86 @@ func TestCSIDriverValidationUpdate(t *testing.T) {
}
}

errorCases := []storage.CSIDriver{
// Each test case changes exactly one field. None of that is valid.
errorCases := []struct {
name string
modify func(new *storage.CSIDriver)
}{
{
ObjectMeta: metav1.ObjectMeta{Name: invalidName},
Spec: storage.CSIDriverSpec{
AttachRequired: &attachRequired,
PodInfoOnMount: &podInfoOnMount,
name: "invalid name",
modify: func(new *storage.CSIDriver) {
new.Name = invalidName
},
},
{
ObjectMeta: metav1.ObjectMeta{Name: longName},
Spec: storage.CSIDriverSpec{
AttachRequired: &attachNotRequired,
PodInfoOnMount: &notPodInfoOnMount,
name: "long name",
modify: func(new *storage.CSIDriver) {
new.Name = longName
},
},
{
// AttachRequired not set
ObjectMeta: metav1.ObjectMeta{Name: driverName},
Spec: storage.CSIDriverSpec{
AttachRequired: nil,
PodInfoOnMount: &podInfoOnMount,
name: "AttachRequired not set",
modify: func(new *storage.CSIDriver) {
new.Spec.AttachRequired = nil
},
},
{
// AttachRequired not set
ObjectMeta: metav1.ObjectMeta{Name: driverName},
Spec: storage.CSIDriverSpec{
AttachRequired: &attachNotRequired,
PodInfoOnMount: nil,
name: "PodInfoOnMount not set",
modify: func(new *storage.CSIDriver) {
new.Spec.PodInfoOnMount = nil
},
},
{
// invalid mode
ObjectMeta: metav1.ObjectMeta{Name: driverName},
Spec: storage.CSIDriverSpec{
AttachRequired: &attachNotRequired,
PodInfoOnMount: &notPodInfoOnMount,
VolumeLifecycleModes: []storage.VolumeLifecycleMode{
name: "AttachRequired changed",
modify: func(new *storage.CSIDriver) {
new.Spec.AttachRequired = &attachRequired
},
},
{
name: "PodInfoOnMount changed",
modify: func(new *storage.CSIDriver) {
new.Spec.PodInfoOnMount = &podInfoOnMount
},
},
{
name: "invalid volume lifecycle mode",
modify: func(new *storage.CSIDriver) {
new.Spec.VolumeLifecycleModes = []storage.VolumeLifecycleMode{
"no-such-mode",
},
}
},
},
{
// different modes
ObjectMeta: metav1.ObjectMeta{Name: driverName},
Spec: storage.CSIDriverSpec{
AttachRequired: &attachNotRequired,
PodInfoOnMount: &notPodInfoOnMount,
VolumeLifecycleModes: []storage.VolumeLifecycleMode{
name: "volume lifecycle modes not set",
modify: func(new *storage.CSIDriver) {
new.Spec.VolumeLifecycleModes = nil
},
},
{
name: "VolumeLifecyclePersistent removed",
modify: func(new *storage.CSIDriver) {
new.Spec.VolumeLifecycleModes = []storage.VolumeLifecycleMode{
storage.VolumeLifecycleEphemeral,
},
}
},
},
{
name: "VolumeLifecycleEphemeral removed",
modify: func(new *storage.CSIDriver) {
new.Spec.VolumeLifecycleModes = []storage.VolumeLifecycleMode{
storage.VolumeLifecyclePersistent,
}
},
},
}

for _, csiDriver := range errorCases {
if errs := ValidateCSIDriverUpdate(&csiDriver, &old); len(errs) == 0 {
t.Errorf("Expected failure for test: %v", csiDriver)
}
for _, test := range errorCases {
t.Run(test.name, func(t *testing.T) {
new := old.DeepCopy()
test.modify(new)
if errs := ValidateCSIDriverUpdate(new, &old); len(errs) == 0 {
t.Errorf("Expected failure for test: %v", new)
}
})
}
}

0 comments on commit c20721a

Please sign in to comment.