Skip to content

Commit

Permalink
add test for ValidateNetworkSpec and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Nilesh-chaudhary committed Jun 14, 2023
1 parent af8668a commit 27292f1
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
16 changes: 12 additions & 4 deletions api/v1beta1/azureclustertemplate_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
72 changes: 72 additions & 0 deletions api/v1beta1/azureclustertemplate_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
})
}
}

0 comments on commit 27292f1

Please sign in to comment.