@@ -21,15 +21,17 @@ import (
21
21
"testing"
22
22
23
23
. "github.com/onsi/gomega"
24
+ corev1 "k8s.io/api/core/v1"
24
25
"k8s.io/apimachinery/pkg/runtime"
25
26
"sigs.k8s.io/controller-runtime/pkg/client"
26
27
"sigs.k8s.io/controller-runtime/pkg/client/fake"
27
28
28
29
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
30
+ expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
29
31
"sigs.k8s.io/cluster-api/internal/test/builder"
30
32
)
31
33
32
- func TestIsUpgrading (t * testing.T ) {
34
+ func TestMDIsUpgrading (t * testing.T ) {
33
35
g := NewWithT (t )
34
36
scheme := runtime .NewScheme ()
35
37
g .Expect (clusterv1 .AddToScheme (scheme )).To (Succeed ())
@@ -115,7 +117,7 @@ func TestIsUpgrading(t *testing.T) {
115
117
}
116
118
}
117
119
118
- func TestUpgrading (t * testing.T ) {
120
+ func TestMDUpgrading (t * testing.T ) {
119
121
g := NewWithT (t )
120
122
scheme := runtime .NewScheme ()
121
123
g .Expect (clusterv1 .AddToScheme (scheme )).To (Succeed ())
@@ -155,3 +157,147 @@ func TestUpgrading(t *testing.T) {
155
157
g .Expect (got ).To (BeComparableTo (want ))
156
158
})
157
159
}
160
+
161
+ func TestMPIsUpgrading (t * testing.T ) {
162
+ g := NewWithT (t )
163
+ scheme := runtime .NewScheme ()
164
+ g .Expect (expv1 .AddToScheme (scheme )).To (Succeed ())
165
+ g .Expect (corev1 .AddToScheme (scheme )).To (Succeed ())
166
+
167
+ tests := []struct {
168
+ name string
169
+ mp * expv1.MachinePool
170
+ nodes []* corev1.Node
171
+ want bool
172
+ wantErr bool
173
+ }{
174
+ {
175
+ name : "should return false if all the nodes of MachinePool have the same version as the MachinePool" ,
176
+ mp : builder .MachinePool ("ns" , "mp1" ).
177
+ WithClusterName ("cluster1" ).
178
+ WithVersion ("v1.2.3" ).
179
+ Build (),
180
+ nodes : []* corev1.Node {
181
+ builder .Node ("node1" ).
182
+ WithStatus (corev1.NodeStatus {
183
+ NodeInfo : corev1.NodeSystemInfo {
184
+ KubeletVersion : "v1.2.3" ,
185
+ },
186
+ }).
187
+ Build (),
188
+ },
189
+ want : false ,
190
+ wantErr : false ,
191
+ },
192
+ {
193
+ name : "should return true if the MachinePool's node has a different kubelet version" ,
194
+ mp : builder .MachinePool ("ns" , "mp1" ).
195
+ WithClusterName ("cluster1" ).
196
+ WithVersion ("v1.2.3" ).
197
+ WithStatus (expv1.MachinePoolStatus {
198
+ NodeRefs : []corev1.ObjectReference {
199
+ {
200
+ Name : "node1" ,
201
+ },
202
+ },
203
+ }).
204
+ Build (),
205
+ nodes : []* corev1.Node {
206
+ builder .Node ("node1" ).
207
+ WithStatus (corev1.NodeStatus {
208
+ NodeInfo : corev1.NodeSystemInfo {
209
+ KubeletVersion : "v1.2.2" ,
210
+ },
211
+ }).
212
+ Build (),
213
+ },
214
+ want : true ,
215
+ wantErr : false ,
216
+ },
217
+ }
218
+
219
+ for _ , tt := range tests {
220
+ t .Run (tt .name , func (t * testing.T ) {
221
+ g := NewWithT (t )
222
+ ctx := context .Background ()
223
+ objs := []client.Object {}
224
+ objs = append (objs , tt .mp )
225
+ for _ , n := range tt .nodes {
226
+ objs = append (objs , n )
227
+ }
228
+ fakeClient := fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (objs ... ).Build ()
229
+ mpState := & MachinePoolState {
230
+ Object : tt .mp ,
231
+ }
232
+ got , err := mpState .IsUpgrading (ctx , fakeClient )
233
+ if tt .wantErr {
234
+ g .Expect (err ).To (HaveOccurred ())
235
+ } else {
236
+ g .Expect (err ).ToNot (HaveOccurred ())
237
+ g .Expect (got ).To (Equal (tt .want ))
238
+ }
239
+ })
240
+ }
241
+ }
242
+
243
+ func TestMPUpgrading (t * testing.T ) {
244
+ g := NewWithT (t )
245
+ scheme := runtime .NewScheme ()
246
+ g .Expect (expv1 .AddToScheme (scheme )).To (Succeed ())
247
+ g .Expect (corev1 .AddToScheme (scheme )).To (Succeed ())
248
+
249
+ ctx := context .Background ()
250
+
251
+ t .Run ("should return the names of the upgrading MachinePools" , func (t * testing.T ) {
252
+ stableMP := builder .MachinePool ("ns" , "stableMP" ).
253
+ WithClusterName ("cluster1" ).
254
+ WithVersion ("v1.2.3" ).
255
+ WithStatus (expv1.MachinePoolStatus {
256
+ NodeRefs : []corev1.ObjectReference {
257
+ {
258
+ Name : "stableMP-node1" ,
259
+ },
260
+ },
261
+ }).
262
+ Build ()
263
+ stableMPNode := builder .Node ("stableMP-node1" ).
264
+ WithStatus (corev1.NodeStatus {
265
+ NodeInfo : corev1.NodeSystemInfo {
266
+ KubeletVersion : "v1.2.3" ,
267
+ },
268
+ }).
269
+ Build ()
270
+
271
+ upgradingMP := builder .MachinePool ("ns" , "upgradingMP" ).
272
+ WithClusterName ("cluster2" ).
273
+ WithVersion ("v1.2.3" ).
274
+ WithStatus (expv1.MachinePoolStatus {
275
+ NodeRefs : []corev1.ObjectReference {
276
+ {
277
+ Name : "upgradingMP-node1" ,
278
+ },
279
+ },
280
+ }).
281
+ Build ()
282
+ upgradingMPNode := builder .Node ("upgradingMP-node1" ).
283
+ WithStatus (corev1.NodeStatus {
284
+ NodeInfo : corev1.NodeSystemInfo {
285
+ KubeletVersion : "v1.2.2" ,
286
+ },
287
+ }).
288
+ Build ()
289
+
290
+ objs := []client.Object {stableMP , stableMPNode , upgradingMP , upgradingMPNode }
291
+ fakeClient := fake .NewClientBuilder ().WithObjects (objs ... ).WithScheme (scheme ).Build ()
292
+
293
+ mpsStateMap := MachinePoolsStateMap {
294
+ "stableMP" : {Object : stableMP },
295
+ "upgradingMP" : {Object : upgradingMP },
296
+ }
297
+ want := []string {"upgradingMP" }
298
+
299
+ got , err := mpsStateMap .Upgrading (ctx , fakeClient )
300
+ g .Expect (err ).ToNot (HaveOccurred ())
301
+ g .Expect (got ).To (BeComparableTo (want ))
302
+ })
303
+ }
0 commit comments