@@ -17,6 +17,7 @@ limitations under the License.
17
17
package internal
18
18
19
19
import (
20
+ "github.com/onsi/gomega/types"
20
21
"testing"
21
22
22
23
. "github.com/onsi/gomega"
@@ -97,6 +98,111 @@ func TestNewFailureDomainPicker(t *testing.T) {
97
98
}
98
99
}
99
100
101
+ func TestNewFailureDomainPickNFewest (t * testing.T ) {
102
+ a := pointer .StringPtr ("us-west-1a" )
103
+ b := pointer .StringPtr ("us-west-1b" )
104
+ c := pointer .StringPtr ("us-west-1c" )
105
+ fds := clusterv1.FailureDomains {
106
+ * a : clusterv1.FailureDomainSpec {},
107
+ * b : clusterv1.FailureDomainSpec {},
108
+ * c : clusterv1.FailureDomainSpec {},
109
+ }
110
+ machinea := & clusterv1.Machine {Spec : clusterv1.MachineSpec {FailureDomain : a }}
111
+ machineb := & clusterv1.Machine {Spec : clusterv1.MachineSpec {FailureDomain : b }}
112
+ machinec := & clusterv1.Machine {Spec : clusterv1.MachineSpec {FailureDomain : c }}
113
+ machinenil := & clusterv1.Machine {Spec : clusterv1.MachineSpec {FailureDomain : nil }}
114
+
115
+ testcases := []struct {
116
+ name string
117
+ fds clusterv1.FailureDomains
118
+ machines FilterableMachineCollection
119
+ n int
120
+ expected types.GomegaMatcher
121
+ }{
122
+ {
123
+ name : "simple" ,
124
+ n : 1 ,
125
+ expected : Equal ([]* string {nil }),
126
+ },
127
+ {
128
+ name : "simple 3" ,
129
+ n : 3 ,
130
+ expected : Equal ([]* string {nil , nil , nil }),
131
+ },
132
+ {
133
+ name : "machines and no failure domain" ,
134
+ machines : NewFilterableMachineCollection (
135
+ machinea .DeepCopy (),
136
+ machineb .DeepCopy (),
137
+ machinec .DeepCopy ()),
138
+ n : 3 ,
139
+ expected : Equal ([]* string {nil , nil , nil }),
140
+ },
141
+ {
142
+ name : "failure domains and no machines" ,
143
+ fds : fds ,
144
+ n : 2 ,
145
+ expected : Or (
146
+ ConsistOf ([]* string {a , b }),
147
+ ConsistOf ([]* string {b , c }),
148
+ ConsistOf ([]* string {c , a }),
149
+ ),
150
+ },
151
+ {
152
+ name : "machines in all but one failure domain" ,
153
+ fds : fds ,
154
+ machines : NewFilterableMachineCollection (
155
+ machinea .DeepCopy (),
156
+ machineb .DeepCopy ()),
157
+ n : 2 ,
158
+ expected : Or (
159
+ ConsistOf ([]* string {c , b }),
160
+ ConsistOf ([]* string {c , a }),
161
+ ),
162
+ },
163
+ {
164
+ name : "n greater than number of failure domains" ,
165
+ fds : fds ,
166
+ n : 6 ,
167
+ expected : ConsistOf ([]* string {a , b , c , a , b , c }),
168
+ },
169
+ {
170
+ name : "no failure domain specified on machine" ,
171
+ fds : clusterv1.FailureDomains {
172
+ * a : clusterv1.FailureDomainSpec {},
173
+ },
174
+ machines : NewFilterableMachineCollection (machinenil .DeepCopy ()),
175
+ n : 3 ,
176
+ expected : Equal ([]* string {a , a , a }),
177
+ },
178
+ {
179
+ name : "mismatched failure domain on machine" ,
180
+ fds : clusterv1.FailureDomains {
181
+ * a : clusterv1.FailureDomainSpec {},
182
+ },
183
+ machines : NewFilterableMachineCollection (
184
+ machineb .DeepCopy (),
185
+ machinec .DeepCopy (),
186
+ ),
187
+ n : 3 ,
188
+ expected : Equal ([]* string {a , a , a }),
189
+ },
190
+ }
191
+ for _ , tc := range testcases {
192
+ t .Run (tc .name , func (t * testing.T ) {
193
+ g := NewWithT (t )
194
+
195
+ fds := PickNFewest (tc .fds , tc .machines , tc .n )
196
+ if tc .expected == nil {
197
+ g .Expect (fds ).To (BeNil ())
198
+ } else {
199
+ g .Expect (fds ).To (tc .expected )
200
+ g .Expect (len (fds )).To (Equal (tc .n ))
201
+ }
202
+ })
203
+ }
204
+ }
205
+
100
206
func TestNewFailureDomainPickMost (t * testing.T ) {
101
207
a := pointer .StringPtr ("us-west-1a" )
102
208
b := pointer .StringPtr ("us-west-1b" )
0 commit comments