@@ -459,6 +459,53 @@ func TestDeleteNodes(t *testing.T) {
459459 assert .Equal (t , 1 , newSize )
460460}
461461
462+ func TestDeleteInstancesBatchBoundaries (t * testing.T ) {
463+ for _ , count := range []int {1 , 100 , 101 } {
464+ t .Run (fmt .Sprintf ("%d instances" , count ), func (t * testing.T ) {
465+ a := & autoScalingMock {}
466+ awsService := awsWrapper {a , nil , nil }
467+ group := & asg {AwsRef : AwsRef {Name : "test-asg" }, curSize : count }
468+ cache := & asgCache {
469+ awsService : & awsService ,
470+ instanceToAsg : make (map [AwsInstanceRef ]* asg , count ),
471+ instanceLifecycle : make (map [AwsInstanceRef ]autoscalingtypes.LifecycleState , count ),
472+ }
473+ instances := make ([]* AwsInstanceRef , count )
474+ instanceIDs := make ([]string , count )
475+ for i := range instances {
476+ id := fmt .Sprintf ("i-%03d" , i )
477+ ref := & AwsInstanceRef {Name : id }
478+ instances [i ] = ref
479+ instanceIDs [i ] = id
480+ cache .instanceToAsg [* ref ] = group
481+ cache .instanceLifecycle [* ref ] = autoscalingtypes .LifecycleStateInService
482+ }
483+
484+ for start := 0 ; start < count ; start += 100 {
485+ end := start + 100
486+ if end > count {
487+ end = count
488+ }
489+ input := & autoscaling.TerminateInstanceInAutoScalingGroupInput {
490+ ShouldDecrementDesiredCapacity : aws .Bool (true ),
491+ }
492+ if end - start == 1 {
493+ input .InstanceId = aws .String (instanceIDs [start ])
494+ } else {
495+ input .AutoScalingGroupName = aws .String (group .Name )
496+ input .InstanceIds = instanceIDs [start :end ]
497+ }
498+ a .On ("TerminateInstanceInAutoScalingGroup" , mock .Anything , input ).
499+ Return (& autoscaling.TerminateInstanceInAutoScalingGroupOutput {}, nil )
500+ }
501+
502+ assert .NoError (t , cache .DeleteInstances (instances ))
503+ assert .Equal (t , 0 , group .curSize )
504+ a .AssertNumberOfCalls (t , "TerminateInstanceInAutoScalingGroup" , (count + 99 )/ 100 )
505+ })
506+ }
507+ }
508+
462509func TestDeleteNodesTerminatingInstances (t * testing.T ) {
463510 a := & autoScalingMock {}
464511 provider := testProvider (t , newTestAwsManagerWithAsgs (t , a , nil , []string {"1:5:test-asg" }))
0 commit comments