Skip to content

Commit b0fefbe

Browse files
OKCopilot
andcommitted
test(aws): cover batch termination boundaries (1, 100, 101 instances)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 2544d78 commit b0fefbe

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

cluster-autoscaler/cloudprovider/aws/aws_cloud_provider_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
462509
func TestDeleteNodesTerminatingInstances(t *testing.T) {
463510
a := &autoScalingMock{}
464511
provider := testProvider(t, newTestAwsManagerWithAsgs(t, a, nil, []string{"1:5:test-asg"}))

0 commit comments

Comments
 (0)