Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mboersma committed Apr 3, 2024
1 parent 47bd15a commit 660f858
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 95 deletions.
35 changes: 17 additions & 18 deletions api/v1beta1/azuremanagedcluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ import (
)

func TestAzureManagedCluster_ValidateUpdate(t *testing.T) {
// NOTE: AzureManagedCluster is behind AKS feature gate flag; the webhook
// must prevent creating new objects in case the feature flag is disabled.
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, true)()

tests := []struct {
name string
oldAMC *AzureManagedCluster
Expand Down Expand Up @@ -95,10 +91,6 @@ func TestAzureManagedCluster_ValidateUpdate(t *testing.T) {
}

func TestAzureManagedCluster_ValidateCreate(t *testing.T) {
// NOTE: AzureManagedCluster is behind AKS feature gate flag; the webhook
// must prevent creating new objects in case the feature flag is disabled.
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, true)()

tests := []struct {
name string
oldAMC *AzureManagedCluster
Expand Down Expand Up @@ -143,27 +135,34 @@ func TestAzureManagedCluster_ValidateCreate(t *testing.T) {

func TestAzureManagedCluster_ValidateCreateFailure(t *testing.T) {
tests := []struct {
name string
amc *AzureManagedCluster
deferFunc func()
name string
amc *AzureManagedCluster
deferFunc func()
expectError bool
}{
{
name: "feature gate explicitly disabled",
amc: getKnownValidAzureManagedCluster(),
deferFunc: utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, false),
name: "feature gate explicitly disabled",
amc: getKnownValidAzureManagedCluster(),
deferFunc: utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, false),
expectError: true,
},
{
name: "feature gate implicitly disabled",
amc: getKnownValidAzureManagedCluster(),
deferFunc: func() {},
name: "feature gate implicitly enabled",
amc: getKnownValidAzureManagedCluster(),
deferFunc: func() {},
expectError: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
defer tc.deferFunc()
g := NewWithT(t)
_, err := tc.amc.ValidateCreate()
g.Expect(err).To(HaveOccurred())
if tc.expectError {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).NotTo(HaveOccurred())
}
})
}
}
Expand Down
35 changes: 17 additions & 18 deletions api/v1beta1/azuremanagedcontrolplane_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,6 @@ func TestValidateAutoScalerProfile(t *testing.T) {
}

func TestValidatingWebhook(t *testing.T) {
// NOTE: AzureManageControlPlane is behind AKS feature gate flag; the webhook
// must prevent creating new objects in case the feature flag is disabled.
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, true)()
tests := []struct {
name string
amcp AzureManagedControlPlane
Expand Down Expand Up @@ -1379,10 +1376,6 @@ func TestValidatingWebhook(t *testing.T) {
}

func TestAzureManagedControlPlane_ValidateCreate(t *testing.T) {
// NOTE: AzureManageControlPlane is behind AKS feature gate flag; the webhook
// must prevent creating new objects in case the feature flag is disabled.
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, true)()

tests := []struct {
name string
amcp *AzureManagedControlPlane
Expand Down Expand Up @@ -1637,19 +1630,22 @@ func TestAzureManagedControlPlane_ValidateCreate(t *testing.T) {

func TestAzureManagedControlPlane_ValidateCreateFailure(t *testing.T) {
tests := []struct {
name string
amcp *AzureManagedControlPlane
deferFunc func()
name string
amcp *AzureManagedControlPlane
deferFunc func()
expectError bool
}{
{
name: "feature gate explicitly disabled",
amcp: getKnownValidAzureManagedControlPlane(),
deferFunc: utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, false),
name: "feature gate explicitly disabled",
amcp: getKnownValidAzureManagedControlPlane(),
deferFunc: utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, false),
expectError: true,
},
{
name: "feature gate implicitly disabled",
amcp: getKnownValidAzureManagedControlPlane(),
deferFunc: func() {},
name: "feature gate implicitly enabled",
amcp: getKnownValidAzureManagedControlPlane(),
deferFunc: func() {},
expectError: false,
},
}
client := mockClient{ReturnError: false}
Expand All @@ -1661,7 +1657,11 @@ func TestAzureManagedControlPlane_ValidateCreateFailure(t *testing.T) {
Client: client,
}
_, err := mcpw.ValidateCreate(context.Background(), tc.amcp)
g.Expect(err).To(HaveOccurred())
if tc.expectError {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).NotTo(HaveOccurred())
}
})
}
}
Expand Down Expand Up @@ -3219,7 +3219,6 @@ func getAMCPMetaData() metav1.ObjectMeta {
}

func TestAzureManagedClusterSecurityProfileValidateCreate(t *testing.T) {
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, true)()
testsCreate := []struct {
name string
amcp *AzureManagedControlPlane
Expand Down
30 changes: 17 additions & 13 deletions api/v1beta1/azuremanagedmachinepool_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,6 @@ func TestAzureManagedMachinePoolUpdatingWebhook(t *testing.T) {
}

func TestAzureManagedMachinePool_ValidateCreate(t *testing.T) {
// NOTE: AzureManagedMachinePool is behind AKS feature gate flag; the webhook
// must prevent creating new objects in case the feature flag is disabled.
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, true)()
tests := []struct {
name string
ammp *AzureManagedMachinePool
Expand Down Expand Up @@ -1310,19 +1307,22 @@ func TestAzureManagedMachinePool_ValidateCreate(t *testing.T) {

func TestAzureManagedMachinePool_ValidateCreateFailure(t *testing.T) {
tests := []struct {
name string
ammp *AzureManagedMachinePool
deferFunc func()
name string
ammp *AzureManagedMachinePool
deferFunc func()
expectError bool
}{
{
name: "feature gate explicitly disabled",
ammp: getKnownValidAzureManagedMachinePool(),
deferFunc: utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, false),
name: "feature gate explicitly disabled",
ammp: getKnownValidAzureManagedMachinePool(),
deferFunc: utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, false),
expectError: true,
},
{
name: "feature gate implicitly disabled",
ammp: getKnownValidAzureManagedMachinePool(),
deferFunc: func() {},
name: "feature gate implicitly enabled",
ammp: getKnownValidAzureManagedMachinePool(),
deferFunc: func() {},
expectError: false,
},
}
for _, tc := range tests {
Expand All @@ -1331,7 +1331,11 @@ func TestAzureManagedMachinePool_ValidateCreateFailure(t *testing.T) {
g := NewWithT(t)
mw := &azureManagedMachinePoolWebhook{}
_, err := mw.ValidateCreate(context.Background(), tc.ammp)
g.Expect(err).To(HaveOccurred())
if tc.expectError {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).NotTo(HaveOccurred())
}
})
}
}
Expand Down
30 changes: 17 additions & 13 deletions controllers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
utilfeature "k8s.io/component-base/featuregate/testing"
"k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure/scope"
"sigs.k8s.io/cluster-api-provider-azure/internal/test/mock_log"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
capifeature "sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/event"
Expand Down Expand Up @@ -110,7 +108,6 @@ func TestGetCloudProviderConfig(t *testing.T) {
azureCluster *infrav1.AzureCluster
identityType infrav1.VMIdentity
identityID string
machinePoolFeature bool
expectedControlPlaneConfig string
expectedWorkerNodeConfig string
}{
Expand Down Expand Up @@ -161,7 +158,6 @@ func TestGetCloudProviderConfig(t *testing.T) {
cluster: cluster,
azureCluster: azureCluster,
identityType: infrav1.VMIdentityNone,
machinePoolFeature: true,
expectedControlPlaneConfig: vmssCloudConfig,
expectedWorkerNodeConfig: vmssCloudConfig,
},
Expand All @@ -173,9 +169,6 @@ func TestGetCloudProviderConfig(t *testing.T) {

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
if tc.machinePoolFeature {
defer utilfeature.SetFeatureGateDuringTest(t, capifeature.Gates, capifeature.MachinePool, true)()
}
fakeIdentity := &infrav1.AzureClusterIdentity{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-identity",
Expand Down Expand Up @@ -532,7 +525,8 @@ const (
"loadBalancerName": "",
"maximumLoadBalancerRuleCount": 250,
"useManagedIdentityExtension": false,
"useInstanceMetadata": true
"useInstanceMetadata": true,
"enableVmssFlexNodes": true
}`
//nolint:gosec // Ignore "G101: Potential hardcoded credentials" check.
spWorkerNodeCloudConfig = `{
Expand All @@ -554,7 +548,8 @@ const (
"loadBalancerName": "",
"maximumLoadBalancerRuleCount": 250,
"useManagedIdentityExtension": false,
"useInstanceMetadata": true
"useInstanceMetadata": true,
"enableVmssFlexNodes": true
}`

systemAssignedControlPlaneCloudConfig = `{
Expand All @@ -574,7 +569,8 @@ const (
"loadBalancerName": "",
"maximumLoadBalancerRuleCount": 250,
"useManagedIdentityExtension": true,
"useInstanceMetadata": true
"useInstanceMetadata": true,
"enableVmssFlexNodes": true
}`
systemAssignedWorkerNodeCloudConfig = `{
"cloud": "AzurePublicCloud",
Expand All @@ -593,7 +589,8 @@ const (
"loadBalancerName": "",
"maximumLoadBalancerRuleCount": 250,
"useManagedIdentityExtension": true,
"useInstanceMetadata": true
"useInstanceMetadata": true,
"enableVmssFlexNodes": true
}`

userAssignedControlPlaneCloudConfig = `{
Expand All @@ -614,6 +611,7 @@ const (
"maximumLoadBalancerRuleCount": 250,
"useManagedIdentityExtension": true,
"useInstanceMetadata": true,
"enableVmssFlexNodes": true,
"userAssignedIdentityID": "foobar"
}`
userAssignedWorkerNodeCloudConfig = `{
Expand All @@ -634,6 +632,7 @@ const (
"maximumLoadBalancerRuleCount": 250,
"useManagedIdentityExtension": true,
"useInstanceMetadata": true,
"enableVmssFlexNodes": true,
"userAssignedIdentityID": "foobar"
}`
spCustomVnetControlPlaneCloudConfig = `{
Expand All @@ -655,7 +654,8 @@ const (
"loadBalancerName": "",
"maximumLoadBalancerRuleCount": 250,
"useManagedIdentityExtension": false,
"useInstanceMetadata": true
"useInstanceMetadata": true,
"enableVmssFlexNodes": true
}`
spCustomVnetWorkerNodeCloudConfig = `{
"cloud": "AzurePublicCloud",
Expand All @@ -676,7 +676,8 @@ const (
"loadBalancerName": "",
"maximumLoadBalancerRuleCount": 250,
"useManagedIdentityExtension": false,
"useInstanceMetadata": true
"useInstanceMetadata": true,
"enableVmssFlexNodes": true
}`
rateLimitsControlPlaneCloudConfig = `{
"cloud": "AzurePublicCloud",
Expand All @@ -698,6 +699,7 @@ const (
"maximumLoadBalancerRuleCount": 250,
"useManagedIdentityExtension": false,
"useInstanceMetadata": true,
"enableVmssFlexNodes": true,
"cloudProviderRateLimit": true,
"cloudProviderRateLimitQPS": 1.2,
"loadBalancerRateLimit": {
Expand All @@ -724,6 +726,7 @@ const (
"maximumLoadBalancerRuleCount": 250,
"useManagedIdentityExtension": false,
"useInstanceMetadata": true,
"enableVmssFlexNodes": true,
"cloudProviderRateLimit": true,
"cloudProviderRateLimitQPS": 1.2,
"loadBalancerRateLimit": {
Expand All @@ -750,6 +753,7 @@ const (
"maximumLoadBalancerRuleCount": 250,
"useManagedIdentityExtension": false,
"useInstanceMetadata": true,
"enableVmssFlexNodes": true,
"cloudProviderBackoff": true,
"cloudProviderBackoffRetries": 1,
"cloudProviderBackoffExponent": 1.2000000000000002,
Expand Down
6 changes: 0 additions & 6 deletions exp/api/v1beta1/azuremachinepool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ import (
"testing"

"github.com/onsi/gomega"
utilfeature "k8s.io/component-base/featuregate/testing"
"k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/feature"
capifeature "sigs.k8s.io/cluster-api/feature"
)

func TestAzureMachinePool_Validate(t *testing.T) {
Expand Down Expand Up @@ -213,9 +210,6 @@ func TestAzureMachinePool_Validate(t *testing.T) {
c := c
t.Run(c.Name, func(t *testing.T) {
// Don't add t.Parallel() here or the test will fail.
// NOTE: AzureMachinePool is behind MachinePool feature gate flag; the webhook
// must prevent creating new objects in case the feature flag is disabled.
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, true)()
g := gomega.NewGomegaWithT(t)
amp := c.Factory(g)
actualErr := amp.Validate(nil, nil)
Expand Down

0 comments on commit 660f858

Please sign in to comment.