Skip to content

Commit be6da12

Browse files
authored
Merge pull request #899 from mboersma/e2e-zones-to-seprate-file
🌱 move e2e failure domains spec to its own file
2 parents 5a9cf7b + f8844c6 commit be6da12

File tree

2 files changed

+99
-36
lines changed

2 files changed

+99
-36
lines changed

test/e2e/azure_failuredomains.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// +build e2e
2+
3+
/*
4+
Copyright 2020 The Kubernetes Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package e2e
20+
21+
import (
22+
"context"
23+
"os"
24+
25+
. "github.com/onsi/ginkgo"
26+
. "github.com/onsi/gomega"
27+
corev1 "k8s.io/api/core/v1"
28+
apimachinerytypes "k8s.io/apimachinery/pkg/types"
29+
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
30+
"sigs.k8s.io/cluster-api/test/framework"
31+
"sigs.k8s.io/controller-runtime/pkg/client"
32+
)
33+
34+
// AzureFailureDomainsSpecInput is the input for AzureFailureDomainSpec.
35+
type AzureFailureDomainsSpecInput struct {
36+
BootstrapClusterProxy framework.ClusterProxy
37+
Cluster *clusterv1.Cluster
38+
Namespace *corev1.Namespace
39+
ClusterName string
40+
}
41+
42+
// AzureFailureDomainsSpec implements a test that checks that control plane machines are spread
43+
// across Azure failure domains.
44+
func AzureFailureDomainsSpec(ctx context.Context, inputGetter func() AzureFailureDomainsSpecInput) {
45+
var (
46+
specName = "azure-failuredomains"
47+
input AzureFailureDomainsSpecInput
48+
machineType = os.Getenv("AZURE_CONTROL_PLANE_MACHINE_TYPE")
49+
location = os.Getenv("AZURE_LOCATION")
50+
zones []string
51+
)
52+
53+
input = inputGetter()
54+
Expect(input.Namespace).NotTo(BeNil(), "Invalid argument. input.Namespace can't be nil when calling %s spec", specName)
55+
Expect(input.ClusterName).NotTo(BeEmpty(), "Invalid argument. input.ClusterName can't be empty when calling %s spec", specName)
56+
57+
zones, err := getAvailabilityZonesForRegion(location, machineType)
58+
Expect(err).NotTo(HaveOccurred())
59+
60+
if zones != nil {
61+
// location supports zones for selected machine type
62+
By("Ensuring zones match CAPI failure domains")
63+
64+
// fetch updated cluster object to ensure Status.FailureDomains is up-to-date
65+
err := input.BootstrapClusterProxy.GetClient().Get(context.TODO(), apimachinerytypes.NamespacedName{
66+
Namespace: input.Namespace.Name, Name: input.ClusterName}, input.Cluster)
67+
Expect(err).NotTo(HaveOccurred())
68+
Expect(len(input.Cluster.Status.FailureDomains)).To(Equal(len(zones)))
69+
for _, z := range zones {
70+
Expect(input.Cluster.Status.FailureDomains[z]).NotTo(BeNil())
71+
}
72+
73+
// TODO: Find alternative when the number of zones is > 1 but doesn't equal to number of control plane machines.
74+
if len(input.Cluster.Status.FailureDomains) == 3 {
75+
By("Ensuring control planes are spread across availability zones.")
76+
key, err := client.ObjectKeyFromObject(input.Cluster)
77+
Expect(err).NotTo(HaveOccurred())
78+
failureDomainsInput := framework.AssertControlPlaneFailureDomainsInput{
79+
GetLister: input.BootstrapClusterProxy.GetClient(),
80+
ClusterKey: key,
81+
ExpectedFailureDomains: map[string]int{
82+
"1": 1,
83+
"2": 1,
84+
"3": 1,
85+
},
86+
}
87+
framework.AssertControlPlaneFailureDomains(ctx, failureDomainsInput)
88+
}
89+
}
90+
}

test/e2e/azure_test.go

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,10 @@ import (
2727
. "github.com/onsi/ginkgo"
2828
. "github.com/onsi/gomega"
2929
corev1 "k8s.io/api/core/v1"
30-
apimachinerytypes "k8s.io/apimachinery/pkg/types"
3130
"k8s.io/utils/pointer"
3231
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
33-
"sigs.k8s.io/cluster-api/test/framework"
3432
"sigs.k8s.io/cluster-api/test/framework/clusterctl"
3533
"sigs.k8s.io/cluster-api/util"
36-
"sigs.k8s.io/controller-runtime/pkg/client"
3734
)
3835

3936
var _ = Describe("Workload cluster creation", func() {
@@ -117,40 +114,16 @@ var _ = Describe("Workload cluster creation", func() {
117114
WaitForMachineDeployments: e2eConfig.GetIntervals(specName, "wait-worker-nodes"),
118115
})
119116

120-
machineType := os.Getenv("AZURE_CONTROL_PLANE_MACHINE_TYPE")
121-
location := os.Getenv("AZURE_LOCATION")
122-
zones, err := getAvailabilityZonesForRegion(location, machineType)
123-
Expect(err).NotTo(HaveOccurred())
124-
125-
if zones != nil {
126-
// location supports zones for selected machine type
127-
By("Ensuring zones match CAPI failure domains")
128-
129-
// fetch updated cluster object to ensure Status.FailureDomains is up-to-date
130-
err := bootstrapClusterProxy.GetClient().Get(context.TODO(), apimachinerytypes.NamespacedName{Namespace: namespace.Name, Name: clusterName}, cluster)
131-
Expect(err).NotTo(HaveOccurred())
132-
Expect(len(cluster.Status.FailureDomains)).To(Equal(len(zones)))
133-
for _, z := range zones {
134-
Expect(cluster.Status.FailureDomains[z]).ToNot(BeNil())
135-
}
136-
137-
// TODO: Find alternative when the number of zones is > 1 but doesn't equal to number of control plane machines.
138-
if len(cluster.Status.FailureDomains) == 3 {
139-
By("Ensuring control planes are spread across availability zones.")
140-
key, err := client.ObjectKeyFromObject(cluster)
141-
Expect(err).NotTo(HaveOccurred())
142-
failureDomainsInput := framework.AssertControlPlaneFailureDomainsInput{
143-
GetLister: bootstrapClusterProxy.GetClient(),
144-
ClusterKey: key,
145-
ExpectedFailureDomains: map[string]int{
146-
"1": 1,
147-
"2": 1,
148-
"3": 1,
149-
},
117+
Context("Validating failure domains", func() {
118+
AzureFailureDomainsSpec(ctx, func() AzureFailureDomainsSpecInput {
119+
return AzureFailureDomainsSpecInput{
120+
BootstrapClusterProxy: bootstrapClusterProxy,
121+
Cluster: cluster,
122+
Namespace: namespace,
123+
ClusterName: clusterName,
150124
}
151-
framework.AssertControlPlaneFailureDomains(ctx, failureDomainsInput)
152-
}
153-
}
125+
})
126+
})
154127

155128
Context("Creating a accessible load balancer", func() {
156129
AzureLBSpec(ctx, func() AzureLBSpecInput {

0 commit comments

Comments
 (0)