Skip to content

Commit abe85cb

Browse files
committed
Refactor featureGate usage in unit tests
1 parent 444f896 commit abe85cb

File tree

3 files changed

+45
-39
lines changed

3 files changed

+45
-39
lines changed

api/v1beta1/azuremanagedcontrolplane_webhook_test.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,29 +1662,31 @@ func TestAzureManagedControlPlane_ValidateCreate(t *testing.T) {
16621662

16631663
func TestAzureManagedControlPlane_ValidateCreateFailure(t *testing.T) {
16641664
tests := []struct {
1665-
name string
1666-
amcp *AzureManagedControlPlane
1667-
deferFunc func()
1668-
expectError bool
1665+
name string
1666+
amcp *AzureManagedControlPlane
1667+
featureGateEnabled *bool
1668+
expectError bool
16691669
}{
16701670
{
1671-
name: "feature gate explicitly disabled",
1672-
amcp: getKnownValidAzureManagedControlPlane(),
1673-
deferFunc: utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, false),
1674-
expectError: true,
1671+
name: "feature gate explicitly disabled",
1672+
amcp: getKnownValidAzureManagedControlPlane(),
1673+
featureGateEnabled: ptr.To(false),
1674+
expectError: true,
16751675
},
16761676
{
1677-
name: "feature gate implicitly enabled",
1678-
amcp: getKnownValidAzureManagedControlPlane(),
1679-
deferFunc: func() {},
1680-
expectError: false,
1677+
name: "feature gate implicitly enabled",
1678+
amcp: getKnownValidAzureManagedControlPlane(),
1679+
featureGateEnabled: nil,
1680+
expectError: false,
16811681
},
16821682
}
16831683
client := mockClient{ReturnError: false}
16841684
for _, tc := range tests {
16851685
t.Run(tc.name, func(t *testing.T) {
16861686
g := NewWithT(t)
1687-
defer tc.deferFunc()
1687+
if tc.featureGateEnabled != nil {
1688+
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, *tc.featureGateEnabled)()
1689+
}
16881690
mcpw := &azureManagedControlPlaneWebhook{
16891691
Client: client,
16901692
}

api/v1beta1/azuremanagedmachinepool_webhook_test.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,27 +1307,29 @@ func TestAzureManagedMachinePool_ValidateCreate(t *testing.T) {
13071307

13081308
func TestAzureManagedMachinePool_ValidateCreateFailure(t *testing.T) {
13091309
tests := []struct {
1310-
name string
1311-
ammp *AzureManagedMachinePool
1312-
deferFunc func()
1313-
expectError bool
1310+
name string
1311+
ammp *AzureManagedMachinePool
1312+
featureGateEnabled *bool
1313+
expectError bool
13141314
}{
13151315
{
1316-
name: "feature gate explicitly disabled",
1317-
ammp: getKnownValidAzureManagedMachinePool(),
1318-
deferFunc: utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, false),
1319-
expectError: true,
1316+
name: "feature gate explicitly disabled",
1317+
ammp: getKnownValidAzureManagedMachinePool(),
1318+
featureGateEnabled: ptr.To(false),
1319+
expectError: true,
13201320
},
13211321
{
1322-
name: "feature gate implicitly enabled",
1323-
ammp: getKnownValidAzureManagedMachinePool(),
1324-
deferFunc: func() {},
1325-
expectError: false,
1322+
name: "feature gate implicitly enabled",
1323+
ammp: getKnownValidAzureManagedMachinePool(),
1324+
featureGateEnabled: nil,
1325+
expectError: false,
13261326
},
13271327
}
13281328
for _, tc := range tests {
13291329
t.Run(tc.name, func(t *testing.T) {
1330-
defer tc.deferFunc()
1330+
if tc.featureGateEnabled != nil {
1331+
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, *tc.featureGateEnabled)()
1332+
}
13311333
g := NewWithT(t)
13321334
mw := &azureManagedMachinePoolWebhook{}
13331335
_, err := mw.ValidateCreate(context.Background(), tc.ammp)

exp/api/v1beta1/azuremachinepool_webhook_test.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -700,27 +700,29 @@ func TestAzureMachinePool_ValidateCreateFailure(t *testing.T) {
700700
g := NewWithT(t)
701701

702702
tests := []struct {
703-
name string
704-
amp *AzureMachinePool
705-
deferFunc func()
706-
expectError bool
703+
name string
704+
amp *AzureMachinePool
705+
featureGateEnabled *bool
706+
expectError bool
707707
}{
708708
{
709-
name: "feature gate explicitly disabled",
710-
amp: getKnownValidAzureMachinePool(),
711-
deferFunc: utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, false),
712-
expectError: true,
709+
name: "feature gate explicitly disabled",
710+
amp: getKnownValidAzureMachinePool(),
711+
featureGateEnabled: ptr.To(false),
712+
expectError: true,
713713
},
714714
{
715-
name: "feature gate implicitly enabled",
716-
amp: getKnownValidAzureMachinePool(),
717-
deferFunc: func() {},
718-
expectError: false,
715+
name: "feature gate implicitly enabled",
716+
amp: getKnownValidAzureMachinePool(),
717+
featureGateEnabled: nil,
718+
expectError: false,
719719
},
720720
}
721721
for _, tc := range tests {
722722
t.Run(tc.name, func(t *testing.T) {
723-
defer tc.deferFunc()
723+
if tc.featureGateEnabled != nil {
724+
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, *tc.featureGateEnabled)()
725+
}
724726
ampw := &azureMachinePoolWebhook{}
725727
_, err := ampw.ValidateCreate(context.Background(), tc.amp)
726728
if tc.expectError {

0 commit comments

Comments
 (0)