diff --git a/pkg/apis/core/validation/validation_test.go b/pkg/apis/core/validation/validation_test.go index a904927dfb5..1c53800f704 100644 --- a/pkg/apis/core/validation/validation_test.go +++ b/pkg/apis/core/validation/validation_test.go @@ -6383,8 +6383,7 @@ func TestValidatePodSpec(t *testing.T) { } } - // original value will be restored by previous defer - utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.PodShareProcessNamespace, false) + defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.PodShareProcessNamespace, false)() featuregatedCases := map[string]core.PodSpec{ "set ShareProcessNamespace": { diff --git a/pkg/controller/volume/persistentvolume/index_test.go b/pkg/controller/volume/persistentvolume/index_test.go index 8729891c2c7..8955e850768 100644 --- a/pkg/controller/volume/persistentvolume/index_test.go +++ b/pkg/controller/volume/persistentvolume/index_test.go @@ -1161,19 +1161,20 @@ func TestVolumeModeCheck(t *testing.T) { } for name, scenario := range scenarios { - recover := utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, scenario.enableBlock) - expectedMismatch, err := checkVolumeModeMismatches(&scenario.pvc.Spec, &scenario.vol.Spec) - if err != nil { - t.Errorf("Unexpected failure for checkVolumeModeMismatches: %v", err) - } - // expected to match but either got an error or no returned pvmatch - if expectedMismatch && !scenario.isExpectedMismatch { - t.Errorf("Unexpected failure for scenario, expected not to mismatch on modes but did: %s", name) - } - if !expectedMismatch && scenario.isExpectedMismatch { - t.Errorf("Unexpected failure for scenario, did not mismatch on mode when expected to mismatch: %s", name) - } - recover() + t.Run(name, func(t *testing.T) { + defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, scenario.enableBlock)() + expectedMismatch, err := checkVolumeModeMismatches(&scenario.pvc.Spec, &scenario.vol.Spec) + if err != nil { + t.Errorf("Unexpected failure for checkVolumeModeMismatches: %v", err) + } + // expected to match but either got an error or no returned pvmatch + if expectedMismatch && !scenario.isExpectedMismatch { + t.Errorf("Unexpected failure for scenario, expected not to mismatch on modes but did: %s", name) + } + if !expectedMismatch && scenario.isExpectedMismatch { + t.Errorf("Unexpected failure for scenario, did not mismatch on mode when expected to mismatch: %s", name) + } + }) } } @@ -1252,23 +1253,24 @@ func TestFilteringVolumeModes(t *testing.T) { } for name, scenario := range scenarios { - recover := utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, scenario.enableBlock) - pvmatch, err := scenario.vol.findBestMatchForClaim(scenario.pvc, false) - // expected to match but either got an error or no returned pvmatch - if pvmatch == nil && scenario.isExpectedMatch { - t.Errorf("Unexpected failure for scenario, no matching volume: %s", name) - } - if err != nil && scenario.isExpectedMatch { - t.Errorf("Unexpected failure for scenario: %s - %+v", name, err) - } - // expected to not match but either got an error or a returned pvmatch - if pvmatch != nil && !scenario.isExpectedMatch { - t.Errorf("Unexpected failure for scenario, expected no matching volume: %s", name) - } - if err != nil && !scenario.isExpectedMatch { - t.Errorf("Unexpected failure for scenario: %s - %+v", name, err) - } - recover() + t.Run(name, func(t *testing.T) { + defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, scenario.enableBlock)() + pvmatch, err := scenario.vol.findBestMatchForClaim(scenario.pvc, false) + // expected to match but either got an error or no returned pvmatch + if pvmatch == nil && scenario.isExpectedMatch { + t.Errorf("Unexpected failure for scenario, no matching volume: %s", name) + } + if err != nil && scenario.isExpectedMatch { + t.Errorf("Unexpected failure for scenario: %s - %+v", name, err) + } + // expected to not match but either got an error or a returned pvmatch + if pvmatch != nil && !scenario.isExpectedMatch { + t.Errorf("Unexpected failure for scenario, expected no matching volume: %s", name) + } + if err != nil && !scenario.isExpectedMatch { + t.Errorf("Unexpected failure for scenario: %s - %+v", name, err) + } + }) } } diff --git a/pkg/kubelet/cm/helpers_linux_test.go b/pkg/kubelet/cm/helpers_linux_test.go index e82b800bdb1..51b2f6ce4c6 100644 --- a/pkg/kubelet/cm/helpers_linux_test.go +++ b/pkg/kubelet/cm/helpers_linux_test.go @@ -274,8 +274,7 @@ func TestResourceConfigForPodWithCustomCPUCFSQuotaPeriod(t *testing.T) { tunedQuotaPeriod := uint64(5 * time.Millisecond / time.Microsecond) tunedQuota := int64(1 * time.Millisecond / time.Microsecond) - utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, pkgfeatures.CPUCFSQuotaPeriod, true) - defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, pkgfeatures.CPUCFSQuotaPeriod, false) + defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, pkgfeatures.CPUCFSQuotaPeriod, true)() minShares := uint64(MinShares) burstableShares := MilliCPUToShares(100) diff --git a/pkg/kubelet/kuberuntime/helpers_linux_test.go b/pkg/kubelet/kuberuntime/helpers_linux_test.go index 73615c61cc2..e394d727317 100644 --- a/pkg/kubelet/kuberuntime/helpers_linux_test.go +++ b/pkg/kubelet/kuberuntime/helpers_linux_test.go @@ -89,8 +89,7 @@ func TestMilliCPUToQuota(t *testing.T) { } func TestMilliCPUToQuotaWithCustomCPUCFSQuotaPeriod(t *testing.T) { - utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CPUCFSQuotaPeriod, true) - defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CPUCFSQuotaPeriod, false) + defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CPUCFSQuotaPeriod, true)() for _, testCase := range []struct { msg string diff --git a/pkg/kubelet/kuberuntime/helpers_test.go b/pkg/kubelet/kuberuntime/helpers_test.go index 0db2ad4d7ed..53a927ac05b 100644 --- a/pkg/kubelet/kuberuntime/helpers_test.go +++ b/pkg/kubelet/kuberuntime/helpers_test.go @@ -351,8 +351,7 @@ func TestNamespacesForPod(t *testing.T) { assert.Equal(t, test.expected, actual) } - // Test ShareProcessNamespace feature disabled, feature gate restored by previous defer - utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.PodShareProcessNamespace, false) + defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.PodShareProcessNamespace, false)() for desc, test := range map[string]struct { input *v1.Pod