@@ -406,18 +406,73 @@ func TestNodeGroupIncreaseSize(t *testing.T) {
406
406
407
407
func TestNodeGroupDecreaseTargetSize (t * testing.T ) {
408
408
type testCase struct {
409
- description string
410
- delta int
411
- initial int32
412
- targetSizeIncrement int32
413
- expected int32
414
- expectedError bool
409
+ description string
410
+ delta int
411
+ initial int32
412
+ targetSizeIncrement int32
413
+ expected int32
414
+ expectedError bool
415
+ includeDeletingMachine bool
416
+ includeFailedMachine bool
417
+ includePendingMachine bool
415
418
}
416
419
417
420
test := func (t * testing.T , tc * testCase , testConfig * testConfig ) {
418
421
controller , stop := mustCreateTestController (t , testConfig )
419
422
defer stop ()
420
423
424
+ // machines in deletion should not be counted towards the active nodes when calculating a decrease in size.
425
+ if tc .includeDeletingMachine {
426
+ if tc .initial < 1 {
427
+ t .Fatal ("test cannot pass, deleted machine requires at least 1 machine in machineset" )
428
+ }
429
+
430
+ // Simulate a machine in deleting
431
+ machine := testConfig .machines [0 ].DeepCopy ()
432
+ timestamp := metav1 .Now ()
433
+ machine .SetDeletionTimestamp (& timestamp )
434
+
435
+ if err := updateResource (controller .managementClient , controller .machineInformer , controller .machineResource , machine ); err != nil {
436
+ t .Fatalf ("unexpected error updating machine, got %v" , err )
437
+ }
438
+ }
439
+
440
+ // machines that have failed should not be counted towards the active nodes when calculating a decrease in size.
441
+ if tc .includeFailedMachine {
442
+ // because we want to allow for tests that have deleted machines and failed machines, we use the second machine in the test data.
443
+ if tc .initial < 2 {
444
+ t .Fatal ("test cannot pass, failed machine requires at least 2 machine in machineset" )
445
+ }
446
+
447
+ // Simulate a failed machine
448
+ machine := testConfig .machines [1 ].DeepCopy ()
449
+
450
+ unstructured .RemoveNestedField (machine .Object , "spec" , "providerID" )
451
+ unstructured .SetNestedField (machine .Object , "FailureMessage" , "status" , "failureMessage" )
452
+
453
+ if err := updateResource (controller .managementClient , controller .machineInformer , controller .machineResource , machine ); err != nil {
454
+ t .Fatalf ("unexpected error updating machine, got %v" , err )
455
+ }
456
+ }
457
+
458
+ // machines that are in pending state should not be counted towards the active nodes when calculating a decrease in size.
459
+ if tc .includePendingMachine {
460
+ // because we want to allow for tests that have deleted, failed machines, and pending machine, we use the third machine in the test data.
461
+ if tc .initial < 3 {
462
+ t .Fatal ("test cannot pass, pending machine requires at least 3 machine in machineset" )
463
+ }
464
+
465
+ // Simulate a pending machine
466
+ machine := testConfig .machines [2 ].DeepCopy ()
467
+
468
+ unstructured .RemoveNestedField (machine .Object , "spec" , "providerID" )
469
+ unstructured .RemoveNestedField (machine .Object , "status" , "nodeRef" )
470
+
471
+ if err := updateResource (controller .managementClient , controller .machineInformer , controller .machineResource , machine ); err != nil {
472
+ t .Fatalf ("unexpected error updating machine, got %v" , err )
473
+ }
474
+ }
475
+
421
476
nodegroups , err := controller .nodeGroups ()
422
477
if err != nil {
423
478
t .Fatalf ("unexpected error: %v" , err )
@@ -522,45 +577,83 @@ func TestNodeGroupDecreaseTargetSize(t *testing.T) {
522
577
}
523
578
}
524
579
525
- annotations := map [string ]string {
526
- nodeGroupMinSizeAnnotationKey : "1" ,
527
- nodeGroupMaxSizeAnnotationKey : "10" ,
528
- }
529
-
530
- t .Run ("MachineSet" , func (t * testing.T ) {
531
- tc := testCase {
580
+ testCases := []testCase {
581
+ {
532
582
description : "Same number of existing instances and node group target size should error" ,
533
583
initial : 3 ,
534
584
targetSizeIncrement : 0 ,
535
585
expected : 3 ,
536
586
delta : - 1 ,
537
587
expectedError : true ,
538
- }
539
- test (t , & tc , createMachineSetTestConfig (RandomString (6 ), RandomString (6 ), RandomString (6 ), int (tc .initial ), annotations , nil ))
540
- })
541
-
542
- t .Run ("MachineSet" , func (t * testing.T ) {
543
- tc := testCase {
588
+ },
589
+ {
544
590
description : "A node group with target size 4 but only 3 existing instances should decrease by 1" ,
545
591
initial : 3 ,
546
592
targetSizeIncrement : 1 ,
547
593
expected : 3 ,
548
594
delta : - 1 ,
549
- }
550
- test (t , & tc , createMachineSetTestConfig (RandomString (6 ), RandomString (6 ), RandomString (6 ), int (tc .initial ), annotations , nil ))
551
- })
595
+ },
596
+ {
597
+ description : "A node group with 4 replicas with one machine in deleting state should decrease by 1" ,
598
+ initial : 4 ,
599
+ targetSizeIncrement : 0 ,
600
+ expected : 3 ,
601
+ delta : - 1 ,
602
+ includeDeletingMachine : true ,
603
+ },
604
+ {
605
+ description : "A node group with 4 replicas with one failed machine should decrease by 1" ,
606
+ initial : 4 ,
607
+ targetSizeIncrement : 0 ,
608
+ expected : 3 ,
609
+ delta : - 1 ,
610
+ includeFailedMachine : true ,
611
+ },
612
+ {
613
+ description : "A node group with 4 replicas with one pending machine should decrease by 1" ,
614
+ initial : 4 ,
615
+ targetSizeIncrement : 0 ,
616
+ expected : 3 ,
617
+ delta : - 1 ,
618
+ includePendingMachine : true ,
619
+ },
620
+ {
621
+ description : "A node group with 5 replicas with one pending and one failed machine should decrease by 2" ,
622
+ initial : 5 ,
623
+ targetSizeIncrement : 0 ,
624
+ expected : 3 ,
625
+ delta : - 2 ,
626
+ includeFailedMachine : true ,
627
+ includePendingMachine : true ,
628
+ },
629
+ {
630
+ description : "A node group with 5 replicas with one pending, one failed, and one deleting machine should decrease by 3" ,
631
+ initial : 5 ,
632
+ targetSizeIncrement : 0 ,
633
+ expected : 2 ,
634
+ delta : - 3 ,
635
+ includeFailedMachine : true ,
636
+ includePendingMachine : true ,
637
+ includeDeletingMachine : true ,
638
+ },
639
+ }
552
640
553
- t .Run ("MachineDeployment" , func (t * testing.T ) {
554
- tc := testCase {
555
- description : "Same number of existing instances and node group target size should error" ,
556
- initial : 3 ,
557
- targetSizeIncrement : 0 ,
558
- expected : 3 ,
559
- delta : - 1 ,
560
- expectedError : true ,
561
- }
562
- test (t , & tc , createMachineDeploymentTestConfig (RandomString (6 ), RandomString (6 ), RandomString (6 ), int (tc .initial ), annotations , nil ))
563
- })
641
+ annotations := map [string ]string {
642
+ nodeGroupMinSizeAnnotationKey : "1" ,
643
+ nodeGroupMaxSizeAnnotationKey : "10" ,
644
+ }
645
+
646
+ for _ , tc := range testCases {
647
+ t .Run (tc .description , func (t * testing.T ) {
648
+ test (t , & tc , createMachineSetTestConfig (RandomString (6 ), RandomString (6 ), RandomString (6 ), int (tc .initial ), annotations , nil ))
649
+ })
650
+ }
651
+
652
+ for _ , tc := range testCases {
653
+ t .Run (tc .description , func (t * testing.T ) {
654
+ test (t , & tc , createMachineDeploymentTestConfig (RandomString (6 ), RandomString (6 ), RandomString (6 ), int (tc .initial ), annotations , nil ))
655
+ })
656
+ }
564
657
}
565
658
566
659
func TestNodeGroupDecreaseSizeErrors (t * testing.T ) {
@@ -580,7 +673,7 @@ func TestNodeGroupDecreaseSizeErrors(t *testing.T) {
580
673
description : "errors because initial+delta < len(nodes)" ,
581
674
delta : - 1 ,
582
675
initial : 3 ,
583
- errorMsg : "attempt to delete existing nodes targetSize :3 delta:-1 existingNodes: 3" ,
676
+ errorMsg : "attempt to delete existing nodes currentReplicas :3 delta:-1 existingNodes: 3" ,
584
677
}}
585
678
586
679
test := func (t * testing.T , tc * testCase , testConfig * testConfig ) {
0 commit comments