@@ -401,10 +401,12 @@ func TestNodeGroupIncreaseSize(t *testing.T) {
401401
402402func TestNodeGroupDecreaseTargetSize (t * testing.T ) {
403403 type testCase struct {
404- description string
405- delta int
406- initial int32
407- expected int32
404+ description string
405+ delta int
406+ initial int32
407+ targetSizeIncrement int32
408+ expected int32
409+ expectedError bool
408410 }
409411
410412 test := func (t * testing.T , tc * testCase , testConfig * testConfig ) {
@@ -421,17 +423,49 @@ func TestNodeGroupDecreaseTargetSize(t *testing.T) {
421423 }
422424
423425 ng := nodegroups [0 ]
424- currReplicas , err := ng .TargetSize ()
425- if err := controller .machineSetInformer .Informer ().GetStore ().Add (newUnstructuredFromMachineSet (testConfig .machineSet )); err != nil {
426- }
426+ // DecreaseTargetSize should only decrease the size when the current target size of the nodeGroup
427+ // is bigger than the number existing instances for that group. We force such a scenario with targetSizeIncrement.
428+ switch v := (ng .scalableResource ).(type ) {
429+ case * machineSetScalableResource :
430+ testConfig .machineSet .Spec .Replicas = int32ptr (* testConfig .machineSet .Spec .Replicas + tc .targetSizeIncrement )
431+ ms , err := ng .machineapiClient .MachineSets (ng .Namespace ()).Update (testConfig .machineSet )
432+ if err != nil {
433+ t .Fatalf ("unexpected error: %v" , err )
434+ }
427435
428- if err := controller .nodeInformer .GetStore ().Delete (testConfig .nodes [0 ]); err != nil {
429- if err := controller .machineDeploymentInformer .Informer ().GetStore ().Add (newUnstructuredFromMachineDeployment (testConfig .machineDeployment )); err != nil {
436+ if err := controller .machineSetInformer .Informer ().GetStore ().Add (ms ); err != nil {
437+ t .Fatalf ("failed to add new machine: %v" , err )
438+ }
439+ case * machineDeploymentScalableResource :
440+ testConfig .machineDeployment .Spec .Replicas = int32ptr (* testConfig .machineDeployment .Spec .Replicas + tc .targetSizeIncrement )
441+ md , err := ng .machineapiClient .MachineDeployments (ng .Namespace ()).Update (testConfig .machineDeployment )
442+ if err != nil {
443+ t .Fatalf ("unexpected error: %v" , err )
444+ }
445+ if err := controller .machineDeploymentInformer .Informer ().GetStore ().Add (md ); err != nil {
446+ t .Fatalf ("failed to add new machine: %v" , err )
447+ }
448+ default :
449+ t .Errorf ("unexpected type: %T" , v )
430450 }
451+ // A nodegroup is immutable; get a fresh copy after adding targetSizeIncrement.
452+ nodegroups , err = controller .nodeGroups ()
453+ if err != nil {
454+ t .Fatalf ("unexpected error: %v" , err )
455+ }
456+ ng = nodegroups [0 ]
431457
432- if err := ng .DecreaseTargetSize (tc .delta ); err != nil {
458+ currReplicas , err := ng .TargetSize ()
459+ if err != nil {
433460 t .Fatalf ("unexpected error: %v" , err )
434461 }
462+ if currReplicas != int (tc .initial )+ int (tc .targetSizeIncrement ) {
463+ t .Errorf ("initially expected %v, got %v" , tc .initial , currReplicas )
464+ }
465+
466+ if err := ng .DecreaseTargetSize (tc .delta ); (err != nil ) != tc .expectedError {
467+ t .Fatalf ("expected error: %v, got: %v" , tc .expectedError , err )
468+ }
435469
436470 switch v := (ng .scalableResource ).(type ) {
437471 case * machineSetScalableResource :
@@ -464,20 +498,35 @@ func TestNodeGroupDecreaseTargetSize(t *testing.T) {
464498
465499 t .Run ("MachineSet" , func (t * testing.T ) {
466500 tc := testCase {
467- description : "decrease by 1" ,
468- initial : 3 ,
469- expected : 2 ,
470- delta : - 1 ,
501+ description : "Same number of existing instances and node group target size should error" ,
502+ initial : 3 ,
503+ targetSizeIncrement : 0 ,
504+ expected : 3 ,
505+ delta : - 1 ,
506+ expectedError : true ,
507+ }
508+ test (t , & tc , createMachineSetTestConfig (testNamespace , int (tc .initial ), annotations ))
509+ })
510+
511+ t .Run ("MachineSet" , func (t * testing.T ) {
512+ tc := testCase {
513+ description : "A node group with targe size 4 but only 3 existing instances should decrease by 1" ,
514+ initial : 3 ,
515+ targetSizeIncrement : 1 ,
516+ expected : 3 ,
517+ delta : - 1 ,
471518 }
472519 test (t , & tc , createMachineSetTestConfig (testNamespace , int (tc .initial ), annotations ))
473520 })
474521
475522 t .Run ("MachineDeployment" , func (t * testing.T ) {
476523 tc := testCase {
477- description : "decrease by 1" ,
478- initial : 3 ,
479- expected : 2 ,
480- delta : - 1 ,
524+ description : "Same number of existing instances and node group target size should error" ,
525+ initial : 3 ,
526+ targetSizeIncrement : 0 ,
527+ expected : 3 ,
528+ delta : - 1 ,
529+ expectedError : true ,
481530 }
482531 test (t , & tc , createMachineDeploymentTestConfig (testNamespace , int (tc .initial ), annotations ))
483532 })
0 commit comments