diff --git a/pkg/api/validation/validation_test.go b/pkg/api/validation/validation_test.go index 6f0940d03e1f5..585b10498e32b 100644 --- a/pkg/api/validation/validation_test.go +++ b/pkg/api/validation/validation_test.go @@ -789,50 +789,99 @@ func TestValidatePersistentVolumeClaimUpdate(t *testing.T) { }, VolumeName: "volume", }) + validSizeUpdate := testVolumeClaim("foo", "ns", api.PersistentVolumeClaimSpec{ + AccessModes: []api.PersistentVolumeAccessMode{ + api.ReadWriteOnce, + api.ReadOnlyMany, + }, + Resources: api.ResourceRequirements{ + Requests: api.ResourceList{ + api.ResourceName(api.ResourceStorage): resource.MustParse("15G"), + }, + }, + }) + invalidSizeUpdate := testVolumeClaim("foo", "ns", api.PersistentVolumeClaimSpec{ + AccessModes: []api.PersistentVolumeAccessMode{ + api.ReadWriteOnce, + api.ReadOnlyMany, + }, + Resources: api.ResourceRequirements{ + Requests: api.ResourceList{ + api.ResourceName(api.ResourceStorage): resource.MustParse("5G"), + }, + }, + }) scenarios := map[string]struct { isExpectedFailure bool oldClaim *api.PersistentVolumeClaim newClaim *api.PersistentVolumeClaim + enableResize bool }{ "valid-update-volumeName-only": { isExpectedFailure: false, oldClaim: validClaim, newClaim: validUpdateClaim, + enableResize: false, }, "valid-no-op-update": { isExpectedFailure: false, oldClaim: validUpdateClaim, newClaim: validUpdateClaim, + enableResize: false, }, "invalid-update-change-resources-on-bound-claim": { isExpectedFailure: true, oldClaim: validUpdateClaim, newClaim: invalidUpdateClaimResources, + enableResize: false, }, "invalid-update-change-access-modes-on-bound-claim": { isExpectedFailure: true, oldClaim: validUpdateClaim, newClaim: invalidUpdateClaimAccessModes, + enableResize: false, }, "invalid-update-change-storage-class-annotation-after-creation": { isExpectedFailure: true, oldClaim: validClaimStorageClass, newClaim: invalidUpdateClaimStorageClass, + enableResize: false, }, "valid-update-mutable-annotation": { isExpectedFailure: false, oldClaim: validClaimAnnotation, newClaim: validUpdateClaimMutableAnnotation, + enableResize: false, }, "valid-update-add-annotation": { isExpectedFailure: false, oldClaim: validClaim, newClaim: validAddClaimAnnotation, + enableResize: false, + }, + "valid-size-update-resize-disabled": { + isExpectedFailure: true, + oldClaim: validClaim, + newClaim: validSizeUpdate, + enableResize: false, + }, + "valid-size-update-resize-enabled": { + isExpectedFailure: false, + oldClaim: validClaim, + newClaim: validSizeUpdate, + enableResize: true, + }, + "invalid-size-update-resize-enabled": { + isExpectedFailure: true, + oldClaim: validClaim, + newClaim: invalidSizeUpdate, + enableResize: true, }, } for name, scenario := range scenarios { // ensure we have a resource version specified for updates + togglePVExpandFeature(scenario.enableResize, t) scenario.oldClaim.ResourceVersion = "1" scenario.newClaim.ResourceVersion = "1" errs := ValidatePersistentVolumeClaimUpdate(scenario.newClaim, scenario.oldClaim) @@ -845,6 +894,23 @@ func TestValidatePersistentVolumeClaimUpdate(t *testing.T) { } } +func togglePVExpandFeature(toggleFlag bool, t *testing.T) { + if toggleFlag { + // Enable alpha feature LocalStorageCapacityIsolation + err := utilfeature.DefaultFeatureGate.Set("ExpandPersistentVolumes=true") + if err != nil { + t.Errorf("Failed to enable feature gate for ExpandPersistentVolumes: %v", err) + return + } + } else { + err := utilfeature.DefaultFeatureGate.Set("ExpandPersistentVolumes=false") + if err != nil { + t.Errorf("Failed to disable feature gate for ExpandPersistentVolumes: %v", err) + return + } + } +} + func TestValidateKeyToPath(t *testing.T) { testCases := []struct { kp api.KeyToPath