sigs.k8s.io/cluster-api@v1.7.1/util/failuredomains/failure_domains_test.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package failuredomains 18 19 import ( 20 "testing" 21 22 . "github.com/onsi/gomega" 23 "k8s.io/utils/ptr" 24 ctrl "sigs.k8s.io/controller-runtime" 25 26 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 27 "sigs.k8s.io/cluster-api/util/collections" 28 ) 29 30 var ( 31 ctx = ctrl.SetupSignalHandler() 32 ) 33 34 func TestNewFailureDomainPicker(t *testing.T) { 35 a := ptr.To("us-west-1a") 36 b := ptr.To("us-west-1b") 37 38 fds := clusterv1.FailureDomains{ 39 *a: clusterv1.FailureDomainSpec{}, 40 *b: clusterv1.FailureDomainSpec{}, 41 } 42 machinea := &clusterv1.Machine{Spec: clusterv1.MachineSpec{FailureDomain: a}} 43 machineb := &clusterv1.Machine{Spec: clusterv1.MachineSpec{FailureDomain: b}} 44 machinenil := &clusterv1.Machine{Spec: clusterv1.MachineSpec{FailureDomain: nil}} 45 46 testcases := []struct { 47 name string 48 fds clusterv1.FailureDomains 49 machines collections.Machines 50 expected []*string 51 }{ 52 { 53 name: "simple", 54 expected: nil, 55 }, 56 { 57 name: "no machines", 58 fds: clusterv1.FailureDomains{ 59 *a: clusterv1.FailureDomainSpec{}, 60 }, 61 expected: []*string{a}, 62 }, 63 { 64 name: "one machine in a failure domain", 65 fds: fds, 66 machines: collections.FromMachines(machinea.DeepCopy()), 67 expected: []*string{b}, 68 }, 69 { 70 name: "no failure domain specified on machine", 71 fds: clusterv1.FailureDomains{ 72 *a: clusterv1.FailureDomainSpec{}, 73 }, 74 machines: collections.FromMachines(machinenil.DeepCopy()), 75 expected: []*string{a}, 76 }, 77 { 78 name: "mismatched failure domain on machine", 79 fds: clusterv1.FailureDomains{ 80 *a: clusterv1.FailureDomainSpec{}, 81 }, 82 machines: collections.FromMachines(machineb.DeepCopy()), 83 expected: []*string{a}, 84 }, 85 { 86 name: "failure domains and no machines should return a valid failure domain", 87 fds: fds, 88 expected: []*string{a, b}, 89 }, 90 } 91 for _, tc := range testcases { 92 t.Run(tc.name, func(t *testing.T) { 93 g := NewWithT(t) 94 95 fd := PickFewest(ctx, tc.fds, tc.machines) 96 if tc.expected == nil { 97 g.Expect(fd).To(BeNil()) 98 } else { 99 g.Expect(fd).To(BeElementOf(tc.expected)) 100 } 101 }) 102 } 103 } 104 105 func TestNewFailureDomainPickMost(t *testing.T) { 106 a := ptr.To("us-west-1a") 107 b := ptr.To("us-west-1b") 108 109 fds := clusterv1.FailureDomains{ 110 *a: clusterv1.FailureDomainSpec{ControlPlane: true}, 111 *b: clusterv1.FailureDomainSpec{ControlPlane: true}, 112 } 113 machinea := &clusterv1.Machine{Spec: clusterv1.MachineSpec{FailureDomain: a}} 114 machineb := &clusterv1.Machine{Spec: clusterv1.MachineSpec{FailureDomain: b}} 115 machinenil := &clusterv1.Machine{Spec: clusterv1.MachineSpec{FailureDomain: nil}} 116 117 testcases := []struct { 118 name string 119 fds clusterv1.FailureDomains 120 machines collections.Machines 121 expected []*string 122 }{ 123 { 124 name: "simple", 125 expected: nil, 126 }, 127 { 128 name: "no machines should return nil", 129 fds: clusterv1.FailureDomains{ 130 *a: clusterv1.FailureDomainSpec{}, 131 }, 132 expected: nil, 133 }, 134 { 135 name: "one machine in a failure domain", 136 fds: fds, 137 machines: collections.FromMachines(machinea.DeepCopy()), 138 expected: []*string{a}, 139 }, 140 { 141 name: "no failure domain specified on machine", 142 fds: clusterv1.FailureDomains{ 143 *a: clusterv1.FailureDomainSpec{ControlPlane: true}, 144 }, 145 machines: collections.FromMachines(machinenil.DeepCopy()), 146 expected: nil, 147 }, 148 { 149 name: "mismatched failure domain on machine should return nil", 150 fds: clusterv1.FailureDomains{ 151 *a: clusterv1.FailureDomainSpec{ControlPlane: true}, 152 }, 153 machines: collections.FromMachines(machineb.DeepCopy()), 154 expected: nil, 155 }, 156 { 157 name: "failure domains and no machines should return nil", 158 fds: fds, 159 expected: nil, 160 }, 161 { 162 name: "nil failure domains with machines", 163 machines: collections.FromMachines(machineb.DeepCopy()), 164 expected: nil, 165 }, 166 { 167 name: "nil failure domains with no machines", 168 expected: nil, 169 }, 170 } 171 for _, tc := range testcases { 172 t.Run(tc.name, func(t *testing.T) { 173 g := NewWithT(t) 174 175 fd := PickMost(ctx, tc.fds, tc.machines, tc.machines) 176 if tc.expected == nil { 177 g.Expect(fd).To(BeNil()) 178 } else { 179 g.Expect(fd).To(BeElementOf(tc.expected)) 180 } 181 }) 182 } 183 }