|
| 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 | +} |
0 commit comments