Skip to content

Commit

Permalink
Refactor featureGate usage in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mboersma committed Aug 7, 2024
1 parent 444f896 commit abe85cb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 39 deletions.
28 changes: 15 additions & 13 deletions api/v1beta1/azuremanagedcontrolplane_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1662,29 +1662,31 @@ func TestAzureManagedControlPlane_ValidateCreate(t *testing.T) {

func TestAzureManagedControlPlane_ValidateCreateFailure(t *testing.T) {
tests := []struct {
name string
amcp *AzureManagedControlPlane
deferFunc func()
expectError bool
name string
amcp *AzureManagedControlPlane
featureGateEnabled *bool
expectError bool
}{
{
name: "feature gate explicitly disabled",
amcp: getKnownValidAzureManagedControlPlane(),
deferFunc: utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, false),
expectError: true,
name: "feature gate explicitly disabled",
amcp: getKnownValidAzureManagedControlPlane(),
featureGateEnabled: ptr.To(false),
expectError: true,
},
{
name: "feature gate implicitly enabled",
amcp: getKnownValidAzureManagedControlPlane(),
deferFunc: func() {},
expectError: false,
name: "feature gate implicitly enabled",
amcp: getKnownValidAzureManagedControlPlane(),
featureGateEnabled: nil,
expectError: false,
},
}
client := mockClient{ReturnError: false}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
defer tc.deferFunc()
if tc.featureGateEnabled != nil {
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, *tc.featureGateEnabled)()
}
mcpw := &azureManagedControlPlaneWebhook{
Client: client,
}
Expand Down
28 changes: 15 additions & 13 deletions api/v1beta1/azuremanagedmachinepool_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1307,27 +1307,29 @@ func TestAzureManagedMachinePool_ValidateCreate(t *testing.T) {

func TestAzureManagedMachinePool_ValidateCreateFailure(t *testing.T) {
tests := []struct {
name string
ammp *AzureManagedMachinePool
deferFunc func()
expectError bool
name string
ammp *AzureManagedMachinePool
featureGateEnabled *bool
expectError bool
}{
{
name: "feature gate explicitly disabled",
ammp: getKnownValidAzureManagedMachinePool(),
deferFunc: utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, false),
expectError: true,
name: "feature gate explicitly disabled",
ammp: getKnownValidAzureManagedMachinePool(),
featureGateEnabled: ptr.To(false),
expectError: true,
},
{
name: "feature gate implicitly enabled",
ammp: getKnownValidAzureManagedMachinePool(),
deferFunc: func() {},
expectError: false,
name: "feature gate implicitly enabled",
ammp: getKnownValidAzureManagedMachinePool(),
featureGateEnabled: nil,
expectError: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
defer tc.deferFunc()
if tc.featureGateEnabled != nil {
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, *tc.featureGateEnabled)()
}
g := NewWithT(t)
mw := &azureManagedMachinePoolWebhook{}
_, err := mw.ValidateCreate(context.Background(), tc.ammp)
Expand Down
28 changes: 15 additions & 13 deletions exp/api/v1beta1/azuremachinepool_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,27 +700,29 @@ func TestAzureMachinePool_ValidateCreateFailure(t *testing.T) {
g := NewWithT(t)

tests := []struct {
name string
amp *AzureMachinePool
deferFunc func()
expectError bool
name string
amp *AzureMachinePool
featureGateEnabled *bool
expectError bool
}{
{
name: "feature gate explicitly disabled",
amp: getKnownValidAzureMachinePool(),
deferFunc: utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, false),
expectError: true,
name: "feature gate explicitly disabled",
amp: getKnownValidAzureMachinePool(),
featureGateEnabled: ptr.To(false),
expectError: true,
},
{
name: "feature gate implicitly enabled",
amp: getKnownValidAzureMachinePool(),
deferFunc: func() {},
expectError: false,
name: "feature gate implicitly enabled",
amp: getKnownValidAzureMachinePool(),
featureGateEnabled: nil,
expectError: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
defer tc.deferFunc()
if tc.featureGateEnabled != nil {
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, *tc.featureGateEnabled)()
}
ampw := &azureMachinePoolWebhook{}
_, err := ampw.ValidateCreate(context.Background(), tc.amp)
if tc.expectError {
Expand Down

0 comments on commit abe85cb

Please sign in to comment.