Always set feature gates for ContainerVisitor test

This fixes a bug where the test was dependent on the current set of
feature gates. Since AllFeatureEnabledContainers() depends on the
feature gates it must be evaluated after the test case is initialized.
This commit is contained in:
Lee Verberne 2021-10-04 11:44:19 +02:00
parent 3e6d122ee1
commit 83a4ec90ae
2 changed files with 12 additions and 8 deletions

View File

@ -36,6 +36,7 @@ import (
) )
func TestVisitContainers(t *testing.T) { func TestVisitContainers(t *testing.T) {
setAllFeatureEnabledContainersDuringTest := ContainerType(0)
testCases := []struct { testCases := []struct {
desc string desc string
spec *api.PodSpec spec *api.PodSpec
@ -142,7 +143,7 @@ func TestVisitContainers(t *testing.T) {
}, },
}, },
wantContainers: []string{"i1", "i2", "c1", "c2"}, wantContainers: []string{"i1", "i2", "c1", "c2"},
mask: AllFeatureEnabledContainers(), mask: setAllFeatureEnabledContainersDuringTest,
}, },
{ {
desc: "all feature enabled container types with ephemeral containers enabled", desc: "all feature enabled container types with ephemeral containers enabled",
@ -161,7 +162,7 @@ func TestVisitContainers(t *testing.T) {
}, },
}, },
wantContainers: []string{"i1", "i2", "c1", "c2", "e1", "e2"}, wantContainers: []string{"i1", "i2", "c1", "c2", "e1", "e2"},
mask: AllFeatureEnabledContainers(), mask: setAllFeatureEnabledContainersDuringTest,
ephemeralContainersEnabled: true, ephemeralContainersEnabled: true,
}, },
{ {
@ -187,8 +188,9 @@ func TestVisitContainers(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) { t.Run(tc.desc, func(t *testing.T) {
if tc.ephemeralContainersEnabled { defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.EphemeralContainers, tc.ephemeralContainersEnabled)()
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.EphemeralContainers, tc.ephemeralContainersEnabled)()
if tc.mask == setAllFeatureEnabledContainersDuringTest {
tc.mask = AllFeatureEnabledContainers() tc.mask = AllFeatureEnabledContainers()
} }

View File

@ -203,6 +203,7 @@ func TestFindPort(t *testing.T) {
} }
func TestVisitContainers(t *testing.T) { func TestVisitContainers(t *testing.T) {
setAllFeatureEnabledContainersDuringTest := ContainerType(0)
testCases := []struct { testCases := []struct {
desc string desc string
spec *v1.PodSpec spec *v1.PodSpec
@ -309,7 +310,7 @@ func TestVisitContainers(t *testing.T) {
}, },
}, },
wantContainers: []string{"i1", "i2", "c1", "c2"}, wantContainers: []string{"i1", "i2", "c1", "c2"},
mask: AllFeatureEnabledContainers(), mask: setAllFeatureEnabledContainersDuringTest,
}, },
{ {
desc: "all feature enabled container types with ephemeral containers enabled", desc: "all feature enabled container types with ephemeral containers enabled",
@ -328,7 +329,7 @@ func TestVisitContainers(t *testing.T) {
}, },
}, },
wantContainers: []string{"i1", "i2", "c1", "c2", "e1", "e2"}, wantContainers: []string{"i1", "i2", "c1", "c2", "e1", "e2"},
mask: AllFeatureEnabledContainers(), mask: setAllFeatureEnabledContainersDuringTest,
ephemeralContainersEnabled: true, ephemeralContainersEnabled: true,
}, },
{ {
@ -354,8 +355,9 @@ func TestVisitContainers(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) { t.Run(tc.desc, func(t *testing.T) {
if tc.ephemeralContainersEnabled { defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.EphemeralContainers, tc.ephemeralContainersEnabled)()
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.EphemeralContainers, tc.ephemeralContainersEnabled)()
if tc.mask == setAllFeatureEnabledContainersDuringTest {
tc.mask = AllFeatureEnabledContainers() tc.mask = AllFeatureEnabledContainers()
} }