@@ -284,26 +284,14 @@ func Test_setScalingUpCondition(t *testing.T) {
284
284
}
285
285
}
286
286
287
- func testMachine (name string , staleDeleting bool ) * clusterv1.Machine {
288
- m := & clusterv1.Machine {
289
- ObjectMeta : metav1.ObjectMeta {
290
- Name : name ,
291
- },
292
- }
293
- if staleDeleting {
294
- m .DeletionTimestamp = ptr .To (metav1.Time {Time : time .Now ().Add (- 1 * time .Hour )})
295
- }
296
- return m
297
- }
298
-
299
287
func Test_setScalingDownCondition (t * testing.T ) {
300
- defaultMachineSet := & clusterv1.MachineSet {
288
+ machineSet := & clusterv1.MachineSet {
301
289
Spec : clusterv1.MachineSetSpec {
302
290
Replicas : ptr.To [int32 ](0 ),
303
291
},
304
292
}
305
293
306
- machineSet1Replica := defaultMachineSet .DeepCopy ()
294
+ machineSet1Replica := machineSet .DeepCopy ()
307
295
machineSet1Replica .Spec .Replicas = ptr.To [int32 ](1 )
308
296
309
297
tests := []struct {
@@ -314,7 +302,7 @@ func Test_setScalingDownCondition(t *testing.T) {
314
302
}{
315
303
{
316
304
name : "getAndAdoptMachines failed" ,
317
- ms : defaultMachineSet ,
305
+ ms : machineSet ,
318
306
machines : nil ,
319
307
expectCondition : metav1.Condition {
320
308
Type : clusterv1 .MachineSetScalingDownV1Beta2Condition ,
@@ -325,7 +313,7 @@ func Test_setScalingDownCondition(t *testing.T) {
325
313
},
326
314
{
327
315
name : "not scaling down and no machines" ,
328
- ms : defaultMachineSet ,
316
+ ms : machineSet ,
329
317
machines : []* clusterv1.Machine {},
330
318
expectCondition : metav1.Condition {
331
319
Type : clusterv1 .MachineSetScalingDownV1Beta2Condition ,
@@ -345,7 +333,7 @@ func Test_setScalingDownCondition(t *testing.T) {
345
333
},
346
334
{
347
335
name : "scaling down to zero" ,
348
- ms : defaultMachineSet ,
336
+ ms : machineSet ,
349
337
machines : []* clusterv1.Machine {
350
338
testMachine ("machine-1" , false ),
351
339
},
@@ -419,19 +407,105 @@ func Test_setScalingDownCondition(t *testing.T) {
419
407
}
420
408
421
409
func Test_setMachinesReadyCondition (t * testing.T ) {
410
+ machineSet := & clusterv1.MachineSet {}
411
+
412
+ readyCondition := metav1.Condition {
413
+ Type : clusterv1 .MachineReadyV1Beta2Condition ,
414
+ Status : metav1 .ConditionTrue ,
415
+ Reason : v1beta2conditions .MultipleInfoReportedReason ,
416
+ }
417
+
422
418
tests := []struct {
423
419
name string
424
420
machineSet * clusterv1.MachineSet
425
421
machines []* clusterv1.Machine
426
422
expectCondition metav1.Condition
427
423
}{
428
- // TODO: Add test cases.
424
+ {
425
+ name : "getAndAdoptMachines failed" ,
426
+ machineSet : machineSet ,
427
+ machines : nil ,
428
+ expectCondition : metav1.Condition {
429
+ Type : clusterv1 .MachineSetMachinesReadyV1Beta2Condition ,
430
+ Status : metav1 .ConditionUnknown ,
431
+ Reason : clusterv1 .MachineSetMachinesReadyInternalErrorV1Beta2Reason ,
432
+ Message : "Please check controller logs for errors" ,
433
+ },
434
+ },
435
+ {
436
+ name : "no machines" ,
437
+ machineSet : machineSet ,
438
+ machines : []* clusterv1.Machine {},
439
+ expectCondition : metav1.Condition {
440
+ Type : clusterv1 .MachineSetMachinesReadyV1Beta2Condition ,
441
+ Status : metav1 .ConditionTrue ,
442
+ Reason : clusterv1 .MachineSetMachinesReadyNoReplicasV1Beta2Reason ,
443
+ },
444
+ },
445
+ {
446
+ name : "all machines are ready" ,
447
+ machineSet : machineSet ,
448
+ machines : []* clusterv1.Machine {
449
+ createMachine ("machine-1" , readyCondition ),
450
+ createMachine ("machine-2" , readyCondition ),
451
+ },
452
+ expectCondition : metav1.Condition {
453
+ Type : clusterv1 .MachineSetMachinesReadyV1Beta2Condition ,
454
+ Status : metav1 .ConditionTrue ,
455
+ Reason : v1beta2conditions .MultipleInfoReportedReason ,
456
+ },
457
+ },
458
+ {
459
+ name : "one ready, one has nothing reported" ,
460
+ machineSet : machineSet ,
461
+ machines : []* clusterv1.Machine {
462
+ createMachine ("machine-1" , readyCondition ),
463
+ createMachine ("machine-2" ),
464
+ },
465
+ expectCondition : metav1.Condition {
466
+ Type : clusterv1 .MachineSetMachinesReadyV1Beta2Condition ,
467
+ Status : metav1 .ConditionUnknown ,
468
+ Reason : v1beta2conditions .NotYetReportedReason ,
469
+ Message : "Condition Ready not yet reported from Machine machine-2" ,
470
+ },
471
+ },
472
+ {
473
+ name : "one ready, one reporting not ready, one reporting unknown, one reporting deleting" ,
474
+ machineSet : machineSet ,
475
+ machines : []* clusterv1.Machine {
476
+ createMachine ("machine-1" , readyCondition ),
477
+ createMachine ("machine-2" , metav1.Condition {
478
+ Type : clusterv1 .MachineReadyV1Beta2Condition ,
479
+ Status : metav1 .ConditionFalse ,
480
+ Reason : "SomeReason" ,
481
+ Message : "HealthCheckSucceeded: Some message" ,
482
+ }),
483
+ createMachine ("machine-3" , metav1.Condition {
484
+ Type : clusterv1 .MachineReadyV1Beta2Condition ,
485
+ Status : metav1 .ConditionUnknown ,
486
+ Reason : "SomeUnknownReason" ,
487
+ Message : "Some unknown message" ,
488
+ }),
489
+ createMachine ("machine-4" , metav1.Condition {
490
+ Type : clusterv1 .MachineReadyV1Beta2Condition ,
491
+ Status : metav1 .ConditionFalse ,
492
+ Reason : clusterv1 .MachineDeletingV1Beta2Reason ,
493
+ Message : "Deleting: Machine deletion in progress, stage: DrainingNode" ,
494
+ }),
495
+ },
496
+ expectCondition : metav1.Condition {
497
+ Type : clusterv1 .MachineSetMachinesReadyV1Beta2Condition ,
498
+ Status : metav1 .ConditionFalse ,
499
+ Reason : v1beta2conditions .MultipleIssuesReportedReason ,
500
+ Message : "Deleting: Machine deletion in progress, stage: DrainingNode from Machine machine-4; HealthCheckSucceeded: Some message from Machine machine-2; Some unknown message from Machine machine-3" ,
501
+ },
502
+ },
429
503
}
430
504
for _ , tt := range tests {
431
505
t .Run (tt .name , func (t * testing.T ) {
432
506
g := NewWithT (t )
433
507
434
- setMachinesReadyCondition (ctx , tt .machineSet , tt .machines , true )
508
+ setMachinesReadyCondition (ctx , tt .machineSet , tt .machines , tt . machines != nil )
435
509
436
510
condition := v1beta2conditions .Get (tt .machineSet , clusterv1 .MachineSetMachinesReadyV1Beta2Condition )
437
511
g .Expect (condition ).ToNot (BeNil ())
@@ -441,23 +515,111 @@ func Test_setMachinesReadyCondition(t *testing.T) {
441
515
}
442
516
443
517
func Test_setMachinesUpToDateCondition (t * testing.T ) {
518
+ machineSet := & clusterv1.MachineSet {}
519
+
444
520
tests := []struct {
445
521
name string
446
522
machineSet * clusterv1.MachineSet
447
523
machines []* clusterv1.Machine
448
524
expectCondition metav1.Condition
449
525
}{
450
- // TODO: Add test cases.
526
+ {
527
+ name : "getAndAdoptMachines failed" ,
528
+ machineSet : machineSet ,
529
+ machines : nil ,
530
+ expectCondition : metav1.Condition {
531
+ Type : clusterv1 .MachineSetMachinesUpToDateV1Beta2Condition ,
532
+ Status : metav1 .ConditionUnknown ,
533
+ Reason : clusterv1 .MachineSetMachinesUpToDateInternalErrorV1Beta2Reason ,
534
+ Message : "Please check controller logs for errors" ,
535
+ },
536
+ },
537
+ {
538
+ name : "One machine up-to-date" ,
539
+ machineSet : machineSet ,
540
+ machines : []* clusterv1.Machine {
541
+ createMachine ("up-to-date-1" , metav1.Condition {
542
+ Type : clusterv1 .MachineUpToDateV1Beta2Condition ,
543
+ Status : metav1 .ConditionTrue ,
544
+ Reason : "some-reason-1" ,
545
+ }),
546
+ },
547
+ expectCondition : metav1.Condition {
548
+ Type : clusterv1 .MachineSetMachinesUpToDateV1Beta2Condition ,
549
+ Status : metav1 .ConditionTrue ,
550
+ Reason : "some-reason-1" ,
551
+ Message : "" ,
552
+ },
553
+ },
554
+ {
555
+ name : "One machine unknown" ,
556
+ machineSet : machineSet ,
557
+ machines : []* clusterv1.Machine {
558
+ createMachine ("unknown-1" , metav1.Condition {
559
+ Type : clusterv1 .MachineUpToDateV1Beta2Condition ,
560
+ Status : metav1 .ConditionUnknown ,
561
+ Reason : "some-unknown-reason-1" ,
562
+ Message : "some unknown message" ,
563
+ }),
564
+ },
565
+ expectCondition : metav1.Condition {
566
+ Type : clusterv1 .MachineSetMachinesUpToDateV1Beta2Condition ,
567
+ Status : metav1 .ConditionUnknown ,
568
+ Reason : "some-unknown-reason-1" ,
569
+ Message : "some unknown message from Machine unknown-1" ,
570
+ },
571
+ },
572
+ {
573
+ name : "One machine not up-to-date" ,
574
+ machineSet : machineSet ,
575
+ machines : []* clusterv1.Machine {
576
+ createMachine ("not-up-to-date-machine-1" , metav1.Condition {
577
+ Type : clusterv1 .MachineUpToDateV1Beta2Condition ,
578
+ Status : metav1 .ConditionFalse ,
579
+ Reason : "some-not-up-to-date-reason" ,
580
+ Message : "some not up-to-date message" ,
581
+ }),
582
+ },
583
+ expectCondition : metav1.Condition {
584
+ Type : clusterv1 .MachineSetMachinesUpToDateV1Beta2Condition ,
585
+ Status : metav1 .ConditionFalse ,
586
+ Reason : "some-not-up-to-date-reason" ,
587
+ Message : "some not up-to-date message from Machine not-up-to-date-machine-1" ,
588
+ },
589
+ },
451
590
}
452
591
for _ , tt := range tests {
453
592
t .Run (tt .name , func (t * testing.T ) {
454
593
g := NewWithT (t )
455
594
456
- setMachinesUpToDateCondition (ctx , tt .machineSet , tt .machines , true )
595
+ setMachinesUpToDateCondition (ctx , tt .machineSet , tt .machines , tt . machines != nil )
457
596
458
597
condition := v1beta2conditions .Get (tt .machineSet , clusterv1 .MachineSetMachinesUpToDateV1Beta2Condition )
459
598
g .Expect (condition ).ToNot (BeNil ())
460
599
g .Expect (* condition ).To (v1beta2conditions .MatchCondition (tt .expectCondition , v1beta2conditions .IgnoreLastTransitionTime (true )))
461
600
})
462
601
}
463
602
}
603
+
604
+ func createMachine (name string , conditions ... metav1.Condition ) * clusterv1.Machine {
605
+ m := & clusterv1.Machine {
606
+ ObjectMeta : metav1.ObjectMeta {
607
+ Name : name ,
608
+ Namespace : metav1 .NamespaceDefault ,
609
+ },
610
+ }
611
+
612
+ for _ , condition := range conditions {
613
+ v1beta2conditions .Set (m , condition )
614
+ }
615
+
616
+ return m
617
+ }
618
+
619
+ func testMachine (name string , staleDeleting bool ) * clusterv1.Machine {
620
+ m := createMachine (name )
621
+ if staleDeleting {
622
+ m .DeletionTimestamp = ptr .To (metav1.Time {Time : time .Now ().Add (- 1 * time .Hour )})
623
+ }
624
+ return m
625
+ }
0 commit comments