Fix BlockVolume feature gate toggling in validation & defaults unit tests

This commit is contained in:
Matthew Wong 2018-11-15 23:36:42 -05:00
parent bee759b925
commit 9bbf768ad6
2 changed files with 54 additions and 100 deletions

View File

@ -825,10 +825,7 @@ func TestSetDefaultPersistentVolume(t *testing.T) {
} }
// When feature gate is enabled, field should be defaulted // When feature gate is enabled, field should be defaulted
err := utilfeature.DefaultFeatureGate.Set("BlockVolume=true") defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, true)()
if err != nil {
t.Fatalf("Failed to enable feature gate for BlockVolume: %v", err)
}
obj3 := roundTrip(t, runtime.Object(pv)).(*v1.PersistentVolume) obj3 := roundTrip(t, runtime.Object(pv)).(*v1.PersistentVolume)
outputMode3 := obj3.Spec.VolumeMode outputMode3 := obj3.Spec.VolumeMode
@ -857,10 +854,7 @@ func TestSetDefaultPersistentVolumeClaim(t *testing.T) {
} }
// When feature gate is enabled, field should be defaulted // When feature gate is enabled, field should be defaulted
err := utilfeature.DefaultFeatureGate.Set("BlockVolume=true") defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, true)()
if err != nil {
t.Fatalf("Failed to enable feature gate for BlockVolume: %v", err)
}
obj3 := roundTrip(t, runtime.Object(pvc)).(*v1.PersistentVolumeClaim) obj3 := roundTrip(t, runtime.Object(pvc)).(*v1.PersistentVolumeClaim)
outputMode3 := obj3.Spec.VolumeMode outputMode3 := obj3.Spec.VolumeMode

View File

@ -70,7 +70,7 @@ func TestValidatePersistentVolumes(t *testing.T) {
scenarios := map[string]struct { scenarios := map[string]struct {
isExpectedFailure bool isExpectedFailure bool
volume *core.PersistentVolume volume *core.PersistentVolume
disableBlockVolume bool disableBlock bool
}{ }{
"good-volume": { "good-volume": {
isExpectedFailure: false, isExpectedFailure: false,
@ -371,7 +371,7 @@ func TestValidatePersistentVolumes(t *testing.T) {
}), }),
}, },
"feature disabled valid volume mode": { "feature disabled valid volume mode": {
disableBlockVolume: true, disableBlock: true,
isExpectedFailure: true, isExpectedFailure: true,
volume: testVolume("foo", "", core.PersistentVolumeSpec{ volume: testVolume("foo", "", core.PersistentVolumeSpec{
Capacity: core.ResourceList{ Capacity: core.ResourceList{
@ -433,10 +433,8 @@ func TestValidatePersistentVolumes(t *testing.T) {
} }
for name, scenario := range scenarios { for name, scenario := range scenarios {
var restore func() t.Run(name, func(t *testing.T) {
if scenario.disableBlockVolume { defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, !scenario.disableBlock)()
restore = utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, false)
}
errs := ValidatePersistentVolume(scenario.volume) errs := ValidatePersistentVolume(scenario.volume)
if len(errs) == 0 && scenario.isExpectedFailure { if len(errs) == 0 && scenario.isExpectedFailure {
t.Errorf("Unexpected success for scenario: %s", name) t.Errorf("Unexpected success for scenario: %s", name)
@ -444,9 +442,7 @@ func TestValidatePersistentVolumes(t *testing.T) {
if len(errs) > 0 && !scenario.isExpectedFailure { if len(errs) > 0 && !scenario.isExpectedFailure {
t.Errorf("Unexpected failure for scenario: %s - %+v", name, errs) t.Errorf("Unexpected failure for scenario: %s - %+v", name, errs)
} }
if scenario.disableBlockVolume { })
restore()
}
} }
} }
@ -836,7 +832,7 @@ func TestValidatePersistentVolumeClaim(t *testing.T) {
scenarios := map[string]struct { scenarios := map[string]struct {
isExpectedFailure bool isExpectedFailure bool
claim *core.PersistentVolumeClaim claim *core.PersistentVolumeClaim
disableBlockVolume bool disableBlock bool
}{ }{
"good-claim": { "good-claim": {
isExpectedFailure: false, isExpectedFailure: false,
@ -1032,7 +1028,7 @@ func TestValidatePersistentVolumeClaim(t *testing.T) {
}), }),
}, },
"feature disabled valid volume mode": { "feature disabled valid volume mode": {
disableBlockVolume: true, disableBlock: true,
isExpectedFailure: true, isExpectedFailure: true,
claim: testVolumeClaim("foo", "ns", core.PersistentVolumeClaimSpec{ claim: testVolumeClaim("foo", "ns", core.PersistentVolumeClaimSpec{
Selector: &metav1.LabelSelector{ Selector: &metav1.LabelSelector{
@ -1074,10 +1070,8 @@ func TestValidatePersistentVolumeClaim(t *testing.T) {
} }
for name, scenario := range scenarios { for name, scenario := range scenarios {
var restore func() t.Run(name, func(t *testing.T) {
if scenario.disableBlockVolume { defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, !scenario.disableBlock)()
restore = utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, false)
}
errs := ValidatePersistentVolumeClaim(scenario.claim) errs := ValidatePersistentVolumeClaim(scenario.claim)
if len(errs) == 0 && scenario.isExpectedFailure { if len(errs) == 0 && scenario.isExpectedFailure {
t.Errorf("Unexpected success for scenario: %s", name) t.Errorf("Unexpected success for scenario: %s", name)
@ -1085,9 +1079,7 @@ func TestValidatePersistentVolumeClaim(t *testing.T) {
if len(errs) > 0 && !scenario.isExpectedFailure { if len(errs) > 0 && !scenario.isExpectedFailure {
t.Errorf("Unexpected failure for scenario: %s - %+v", name, errs) t.Errorf("Unexpected failure for scenario: %s - %+v", name, errs)
} }
if scenario.disableBlockVolume { })
restore()
}
} }
} }
@ -1170,8 +1162,9 @@ func TestAlphaPVVolumeModeUpdate(t *testing.T) {
} }
for name, scenario := range scenarios { for name, scenario := range scenarios {
t.Run(name, func(t *testing.T) {
// ensure we have a resource version specified for updates // ensure we have a resource version specified for updates
toggleBlockVolumeFeature(scenario.enableBlock, t) defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, scenario.enableBlock)()
errs := ValidatePersistentVolumeUpdate(scenario.newPV, scenario.oldPV) errs := ValidatePersistentVolumeUpdate(scenario.newPV, scenario.oldPV)
if len(errs) == 0 && scenario.isExpectedFailure { if len(errs) == 0 && scenario.isExpectedFailure {
t.Errorf("Unexpected success for scenario: %s", name) t.Errorf("Unexpected success for scenario: %s", name)
@ -1179,6 +1172,7 @@ func TestAlphaPVVolumeModeUpdate(t *testing.T) {
if len(errs) > 0 && !scenario.isExpectedFailure { if len(errs) > 0 && !scenario.isExpectedFailure {
t.Errorf("Unexpected failure for scenario: %s - %+v", name, errs) t.Errorf("Unexpected failure for scenario: %s - %+v", name, errs)
} }
})
} }
} }
@ -1622,8 +1616,7 @@ func TestValidatePersistentVolumeClaimUpdate(t *testing.T) {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
// ensure we have a resource version specified for updates // ensure we have a resource version specified for updates
defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ExpandPersistentVolumes, scenario.enableResize)() defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ExpandPersistentVolumes, scenario.enableResize)()
defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, scenario.enableBlock)()
toggleBlockVolumeFeature(scenario.enableBlock, t)
scenario.oldClaim.ResourceVersion = "1" scenario.oldClaim.ResourceVersion = "1"
scenario.newClaim.ResourceVersion = "1" scenario.newClaim.ResourceVersion = "1"
errs := ValidatePersistentVolumeClaimUpdate(scenario.newClaim, scenario.oldClaim) errs := ValidatePersistentVolumeClaimUpdate(scenario.newClaim, scenario.oldClaim)
@ -1637,23 +1630,6 @@ func TestValidatePersistentVolumeClaimUpdate(t *testing.T) {
} }
} }
func toggleBlockVolumeFeature(toggleFlag bool, t *testing.T) {
if toggleFlag {
// Enable alpha feature BlockVolume
err := utilfeature.DefaultFeatureGate.Set("BlockVolume=true")
if err != nil {
t.Errorf("Failed to enable feature gate for BlockVolume: %v", err)
return
}
} else {
err := utilfeature.DefaultFeatureGate.Set("BlockVolume=false")
if err != nil {
t.Errorf("Failed to disable feature gate for BlockVolume: %v", err)
return
}
}
}
func TestValidateKeyToPath(t *testing.T) { func TestValidateKeyToPath(t *testing.T) {
testCases := []struct { testCases := []struct {
kp core.KeyToPath kp core.KeyToPath
@ -3944,13 +3920,9 @@ func TestAlphaHugePagesIsolation(t *testing.T) {
} }
} }
func TestAlphaPVCVolumeMode(t *testing.T) { func TestPVCVolumeMode(t *testing.T) {
// Enable alpha feature BlockVolume for PVC // Enable feature BlockVolume for PVC
err := utilfeature.DefaultFeatureGate.Set("BlockVolume=true") defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, true)()
if err != nil {
t.Errorf("Failed to enable feature gate for BlockVolume: %v", err)
return
}
block := core.PersistentVolumeBlock block := core.PersistentVolumeBlock
file := core.PersistentVolumeFilesystem file := core.PersistentVolumeFilesystem
@ -3981,13 +3953,9 @@ func TestAlphaPVCVolumeMode(t *testing.T) {
} }
} }
func TestAlphaPVVolumeMode(t *testing.T) { func TestPVVolumeMode(t *testing.T) {
// Enable alpha feature BlockVolume for PV // Enable feature BlockVolume for PVC
err := utilfeature.DefaultFeatureGate.Set("BlockVolume=true") defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, true)()
if err != nil {
t.Errorf("Failed to enable feature gate for BlockVolume: %v", err)
return
}
block := core.PersistentVolumeBlock block := core.PersistentVolumeBlock
file := core.PersistentVolumeFilesystem file := core.PersistentVolumeFilesystem
@ -5152,12 +5120,8 @@ func TestAlphaValidateVolumeDevices(t *testing.T) {
{Name: "abc-123", MountPath: "/this/path/exists"}, {Name: "abc-123", MountPath: "/this/path/exists"},
} }
// enable Alpha BlockVolume // enable BlockVolume
err1 := utilfeature.DefaultFeatureGate.Set("BlockVolume=true") defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, true)()
if err1 != nil {
t.Errorf("Failed to enable feature gate for BlockVolume: %v", err1)
return
}
// Success Cases: // Success Cases:
// Validate normal success cases - only PVC volumeSource // Validate normal success cases - only PVC volumeSource
if errs := ValidateVolumeDevices(successCase, GetVolumeMountMap(goodVolumeMounts), vols, field.NewPath("field")); len(errs) != 0 { if errs := ValidateVolumeDevices(successCase, GetVolumeMountMap(goodVolumeMounts), vols, field.NewPath("field")); len(errs) != 0 {
@ -5172,12 +5136,8 @@ func TestAlphaValidateVolumeDevices(t *testing.T) {
} }
} }
// disable Alpha BlockVolume // disable BlockVolume
err2 := utilfeature.DefaultFeatureGate.Set("BlockVolume=false") defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, false)()
if err2 != nil {
t.Errorf("Failed to disable feature gate for BlockVolume: %v", err2)
return
}
if errs := ValidateVolumeDevices(disabledAlphaVolDevice, GetVolumeMountMap(goodVolumeMounts), vols, field.NewPath("field")); len(errs) == 0 { if errs := ValidateVolumeDevices(disabledAlphaVolDevice, GetVolumeMountMap(goodVolumeMounts), vols, field.NewPath("field")); len(errs) == 0 {
t.Errorf("expected failure: %v", errs) t.Errorf("expected failure: %v", errs)
} }