From 27292f1364a561e336f290961605519cc8243ca1 Mon Sep 17 00:00:00 2001 From: Nilesh-chaudhary Date: Wed, 14 Jun 2023 23:05:52 +0530 Subject: [PATCH] add test for ValidateNetworkSpec and refactor --- .../azureclustertemplate_validation.go | 16 +++-- .../azureclustertemplate_validation_test.go | 72 +++++++++++++++++++ 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/api/v1beta1/azureclustertemplate_validation.go b/api/v1beta1/azureclustertemplate_validation.go index a3fd425ed2f..132eeabcae6 100644 --- a/api/v1beta1/azureclustertemplate_validation.go +++ b/api/v1beta1/azureclustertemplate_validation.go @@ -55,6 +55,18 @@ func (c *AzureClusterTemplate) validateClusterTemplateSpec() field.ErrorList { field.NewPath("spec").Child("template").Child("spec").Child("networkSpec").Child("apiServerLB"), )...) + allErrs = append(allErrs, c.validateNetworkSpec()...) + + allErrs = append(allErrs, c.validateControlPlaneOutboundLB()...) + + allErrs = append(allErrs, c.validatePrivateDNSZoneName()...) + + return allErrs +} + +func (c *AzureClusterTemplate) validateNetworkSpec() field.ErrorList { + var allErrs field.ErrorList + var needOutboundLB bool networkSpec := c.Spec.Template.Spec.NetworkSpec for _, subnet := range networkSpec.Subnets { @@ -67,10 +79,6 @@ func (c *AzureClusterTemplate) validateClusterTemplateSpec() field.ErrorList { allErrs = append(allErrs, c.validateNodeOutboundLB()...) } - allErrs = append(allErrs, c.validateControlPlaneOutboundLB()...) - - allErrs = append(allErrs, c.validatePrivateDNSZoneName()...) - return allErrs } diff --git a/api/v1beta1/azureclustertemplate_validation_test.go b/api/v1beta1/azureclustertemplate_validation_test.go index 0c48b2385a8..81ab0bb6f3d 100644 --- a/api/v1beta1/azureclustertemplate_validation_test.go +++ b/api/v1beta1/azureclustertemplate_validation_test.go @@ -822,3 +822,75 @@ func TestValidatePrivateDNSZoneName(t *testing.T) { }) } } +func TestValidateNetworkSpec(t *testing.T) { + cases := []struct { + name string + clusterTemplate *AzureClusterTemplate + expectValid bool + }{ + { + name: "subnet with SubnetNode role and enabled IPv6 triggers needOutboundLB and calls validateNodeOutboundLB", + clusterTemplate: &AzureClusterTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-cluster-template", + }, + Spec: AzureClusterTemplateSpec{ + Template: AzureClusterTemplateResource{ + Spec: AzureClusterTemplateResourceSpec{ + NetworkSpec: NetworkTemplateSpec{ + Subnets: SubnetTemplatesSpec{ + { + SubnetClassSpec: SubnetClassSpec{ + Role: SubnetNode, + CIDRBlocks: []string{"2001:beea::1/64"}, + }, + }, + }, + }, + }, + }, + }, + }, + expectValid: false, + }, + { + name: "subnet with non-SubnetNode role", + clusterTemplate: &AzureClusterTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-cluster-template", + }, + Spec: AzureClusterTemplateSpec{ + Template: AzureClusterTemplateResource{ + Spec: AzureClusterTemplateResourceSpec{ + NetworkSpec: NetworkTemplateSpec{ + Subnets: SubnetTemplatesSpec{ + { + SubnetClassSpec: SubnetClassSpec{ + Role: "SomeOtherRole", + CIDRBlocks: []string{"10.0.0.0/24"}, + }, + }, + }, + }, + }, + }, + }, + }, + expectValid: true, // No need for outbound LB when not SubnetNode + }, + } + + for _, c := range cases { + tc := c + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + g := NewWithT(t) + res := tc.clusterTemplate.validateNetworkSpec() + if tc.expectValid { + g.Expect(res).To(BeNil()) + } else { + g.Expect(res).NotTo(BeNil()) + } + }) + } +}