1
1
package mco
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"testing"
7
+ "time"
6
8
7
9
"github.com/openshift-kni/eco-goinfra/pkg/clients"
8
10
mcv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
@@ -14,10 +16,12 @@ import (
14
16
15
17
const defaultMCPName = "test-machine-config-pool"
16
18
17
- var defaultMCPCondition = mcv1.MachineConfigPoolCondition {
18
- Type : mcv1 .MachineConfigPoolBuildSuccess ,
19
- Status : corev1 .ConditionTrue ,
20
- }
19
+ var (
20
+ updatingMCPCondition = mcv1.MachineConfigPoolCondition {
21
+ Type : mcv1 .MachineConfigPoolUpdating ,
22
+ Status : corev1 .ConditionTrue ,
23
+ }
24
+ )
21
25
22
26
func TestNewMCPBuilder (t * testing.T ) {
23
27
testCases := []struct {
@@ -265,6 +269,158 @@ func TestMachineConfigPoolWithMCSelector(t *testing.T) {
265
269
}
266
270
}
267
271
272
+ func TestMachineConfigPoolWaitToBeInCondition (t * testing.T ) {
273
+ testCases := []struct {
274
+ exists bool
275
+ valid bool
276
+ hasCondition bool
277
+ expectedError error
278
+ }{
279
+ {
280
+ exists : true ,
281
+ valid : true ,
282
+ hasCondition : true ,
283
+ expectedError : nil ,
284
+ },
285
+ {
286
+ exists : false ,
287
+ valid : true ,
288
+ hasCondition : true ,
289
+ expectedError : context .DeadlineExceeded ,
290
+ },
291
+ {
292
+ exists : true ,
293
+ valid : false ,
294
+ hasCondition : true ,
295
+ expectedError : fmt .Errorf ("machineconfigpool 'name' cannot be empty" ),
296
+ },
297
+ {
298
+ exists : true ,
299
+ valid : true ,
300
+ hasCondition : false ,
301
+ expectedError : context .DeadlineExceeded ,
302
+ },
303
+ }
304
+
305
+ for _ , testCase := range testCases {
306
+ testBuilder := buildMCPBuilderWithUpdatingCondition (testCase .exists , testCase .hasCondition , testCase .valid )
307
+ err := testBuilder .WaitToBeInCondition (updatingMCPCondition .Type , updatingMCPCondition .Status , time .Second )
308
+
309
+ assert .Equal (t , testCase .expectedError , err )
310
+ }
311
+ }
312
+
313
+ func TestMachineConfigPoolWaitForUpdate (t * testing.T ) {
314
+ testCases := []struct {
315
+ valid bool
316
+ exists bool
317
+ updating bool
318
+ expectedError error
319
+ }{
320
+ {
321
+ valid : true ,
322
+ exists : true ,
323
+ updating : false ,
324
+ expectedError : nil ,
325
+ },
326
+ {
327
+ valid : true ,
328
+ exists : true ,
329
+ updating : true ,
330
+ expectedError : context .DeadlineExceeded ,
331
+ },
332
+ {
333
+ valid : false ,
334
+ exists : true ,
335
+ updating : true ,
336
+ expectedError : fmt .Errorf ("machineconfigpool 'name' cannot be empty" ),
337
+ },
338
+ {
339
+ valid : true ,
340
+ exists : false ,
341
+ updating : true ,
342
+ expectedError : fmt .Errorf (
343
+ "machineconfigpools.machineconfiguration.openshift.io \" test-machine-config-pool\" not found" ),
344
+ },
345
+ }
346
+
347
+ for _ , testCase := range testCases {
348
+ testBuilder := buildMCPBuilderWithUpdatingCondition (testCase .exists , testCase .updating , testCase .valid )
349
+ err := testBuilder .WaitForUpdate (time .Second )
350
+
351
+ if testCase .expectedError == nil {
352
+ assert .Nil (t , err )
353
+ } else {
354
+ assert .Equal (t , testCase .expectedError .Error (), err .Error ())
355
+ }
356
+ }
357
+ }
358
+
359
+ func TestMachineConfigPoolWaitToBeStableFor (t * testing.T ) {
360
+ testCases := []struct {
361
+ valid bool
362
+ exists bool
363
+ stable bool
364
+ expectedError error
365
+ }{
366
+ {
367
+ valid : true ,
368
+ exists : true ,
369
+ stable : true ,
370
+ expectedError : nil ,
371
+ },
372
+ {
373
+ valid : false ,
374
+ exists : true ,
375
+ stable : true ,
376
+ expectedError : fmt .Errorf ("machineconfigpool 'name' cannot be empty" ),
377
+ },
378
+ {
379
+ valid : true ,
380
+ exists : false ,
381
+ stable : true ,
382
+ expectedError : nil ,
383
+ },
384
+ {
385
+ valid : true ,
386
+ exists : true ,
387
+ stable : false ,
388
+ expectedError : context .DeadlineExceeded ,
389
+ },
390
+ }
391
+
392
+ for _ , testCase := range testCases {
393
+ var (
394
+ runtimeObjects []runtime.Object
395
+ testBuilder * MCPBuilder
396
+ )
397
+
398
+ if testCase .exists {
399
+ mcp := buildDummyMCP (defaultMCPName )
400
+
401
+ if ! testCase .stable {
402
+ mcp .Status .DegradedMachineCount = 1
403
+ }
404
+
405
+ runtimeObjects = append (runtimeObjects , mcp )
406
+ }
407
+
408
+ testSettings := clients .GetTestClients (clients.TestClientParams {
409
+ K8sMockObjects : runtimeObjects ,
410
+ SchemeAttachers : testSchemes ,
411
+ })
412
+
413
+ if testCase .valid {
414
+ testBuilder = buildValidMCPTestBuilder (testSettings )
415
+ } else {
416
+ testBuilder = buildInvalidMCPTestBuilder (testSettings )
417
+ }
418
+
419
+ err := testBuilder .WaitToBeStableFor (500 * time .Millisecond , time .Second )
420
+ assert .Equal (t , testCase .expectedError , err )
421
+ }
422
+ }
423
+
268
424
func TestMachineConfigPoolWithOptions (t * testing.T ) {
269
425
testCases := []struct {
270
426
testBuilder * MCPBuilder
@@ -340,33 +496,9 @@ func TestMachineConfigPoolIsInCondition(t *testing.T) {
340
496
}
341
497
342
498
for _ , testCase := range testCases {
343
- var (
344
- runtimeObjects []runtime.Object
345
- testBuilder * MCPBuilder
346
- )
347
-
348
- if testCase .exists {
349
- mcp := buildDummyMCP (defaultMCPName )
350
-
351
- if testCase .hasCondition {
352
- mcp .Status .Conditions = []mcv1.MachineConfigPoolCondition {defaultMCPCondition }
353
- }
354
-
355
- runtimeObjects = append (runtimeObjects , mcp )
356
- }
357
-
358
- testSettings := clients .GetTestClients (clients.TestClientParams {
359
- K8sMockObjects : runtimeObjects ,
360
- SchemeAttachers : testSchemes ,
361
- })
362
-
363
- if testCase .valid {
364
- testBuilder = buildValidMCPTestBuilder (testSettings )
365
- } else {
366
- testBuilder = buildInvalidMCPTestBuilder (testSettings )
367
- }
499
+ testBuilder := buildMCPBuilderWithUpdatingCondition (testCase .exists , testCase .hasCondition , testCase .valid )
500
+ isInCondition := testBuilder .IsInCondition (updatingMCPCondition .Type )
368
501
369
- isInCondition := testBuilder .IsInCondition (defaultMCPCondition .Type )
370
502
assert .Equal (t , testCase .isInCondition , isInCondition )
371
503
}
372
504
}
@@ -399,3 +531,35 @@ func buildValidMCPTestBuilder(apiClient *clients.Settings) *MCPBuilder {
399
531
func buildInvalidMCPTestBuilder (apiClient * clients.Settings ) * MCPBuilder {
400
532
return NewMCPBuilder (apiClient , "" )
401
533
}
534
+
535
+ // buildMCPBuilderWithUpdatingCondition returns an MCPBuilder for testing, with the ability to configure whether it
536
+ // exists on the test client, has the updating condition, and is valid.
537
+ func buildMCPBuilderWithUpdatingCondition (exists , hasCondition , valid bool ) * MCPBuilder {
538
+ var (
539
+ runtimeObjects []runtime.Object
540
+ testBuilder * MCPBuilder
541
+ )
542
+
543
+ if exists {
544
+ mcp := buildDummyMCP (defaultMCPName )
545
+
546
+ if hasCondition {
547
+ mcp .Status .Conditions = []mcv1.MachineConfigPoolCondition {updatingMCPCondition }
548
+ }
549
+
550
+ runtimeObjects = append (runtimeObjects , mcp )
551
+ }
552
+
553
+ testSettings := clients .GetTestClients (clients.TestClientParams {
554
+ K8sMockObjects : runtimeObjects ,
555
+ SchemeAttachers : testSchemes ,
556
+ })
557
+
558
+ if valid {
559
+ testBuilder = buildValidMCPTestBuilder (testSettings )
560
+ } else {
561
+ testBuilder = buildInvalidMCPTestBuilder (testSettings )
562
+ }
563
+
564
+ return testBuilder
565
+ }
0 commit comments