diff --git a/pkg/apis/storage/validation/validation_test.go b/pkg/apis/storage/validation/validation_test.go index 81f5c0eca6a..8363b201cbf 100644 --- a/pkg/apis/storage/validation/validation_test.go +++ b/pkg/apis/storage/validation/validation_test.go @@ -1826,3 +1826,94 @@ func TestCSIDriverValidation(t *testing.T) { } } } + +func TestCSIDriverValidationUpdate(t *testing.T) { + driverName := "test-driver" + longName := "my-a-b-c-d-c-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z-ABCDEFGHIJKLMNOPQRSTUVWXYZ-driver" + invalidName := "-invalid-@#$%^&*()-" + attachRequired := true + attachNotRequired := false + podInfoOnMount := true + notPodInfoOnMount := false + old := storage.CSIDriver{ + ObjectMeta: metav1.ObjectMeta{Name: driverName}, + Spec: storage.CSIDriverSpec{ + AttachRequired: &attachNotRequired, + PodInfoOnMount: ¬PodInfoOnMount, + VolumeLifecycleModes: []storage.VolumeLifecycleMode{ + storage.VolumeLifecycleEphemeral, + storage.VolumeLifecyclePersistent, + }, + }, + } + + // Currently there is only one success case: exactly the same + // as the existing object. + successCases := []storage.CSIDriver{old} + for _, csiDriver := range successCases { + if errs := ValidateCSIDriverUpdate(&csiDriver, &old); len(errs) != 0 { + t.Errorf("expected success for %+v: %v", csiDriver, errs) + } + } + + errorCases := []storage.CSIDriver{ + { + ObjectMeta: metav1.ObjectMeta{Name: invalidName}, + Spec: storage.CSIDriverSpec{ + AttachRequired: &attachRequired, + PodInfoOnMount: &podInfoOnMount, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{Name: longName}, + Spec: storage.CSIDriverSpec{ + AttachRequired: &attachNotRequired, + PodInfoOnMount: ¬PodInfoOnMount, + }, + }, + { + // AttachRequired not set + ObjectMeta: metav1.ObjectMeta{Name: driverName}, + Spec: storage.CSIDriverSpec{ + AttachRequired: nil, + PodInfoOnMount: &podInfoOnMount, + }, + }, + { + // AttachRequired not set + ObjectMeta: metav1.ObjectMeta{Name: driverName}, + Spec: storage.CSIDriverSpec{ + AttachRequired: &attachNotRequired, + PodInfoOnMount: nil, + }, + }, + { + // invalid mode + ObjectMeta: metav1.ObjectMeta{Name: driverName}, + Spec: storage.CSIDriverSpec{ + AttachRequired: &attachNotRequired, + PodInfoOnMount: ¬PodInfoOnMount, + VolumeLifecycleModes: []storage.VolumeLifecycleMode{ + "no-such-mode", + }, + }, + }, + { + // different modes + ObjectMeta: metav1.ObjectMeta{Name: driverName}, + Spec: storage.CSIDriverSpec{ + AttachRequired: &attachNotRequired, + PodInfoOnMount: ¬PodInfoOnMount, + VolumeLifecycleModes: []storage.VolumeLifecycleMode{ + storage.VolumeLifecycleEphemeral, + }, + }, + }, + } + + for _, csiDriver := range errorCases { + if errs := ValidateCSIDriverUpdate(&csiDriver, &old); len(errs) == 0 { + t.Errorf("Expected failure for test: %v", csiDriver) + } + } +}