fix(autoscaling): match AWS capacity target validation errors (#7344)

* fix(autoscaling): match AWS capacity target validation errors

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>

* fix(autoscaling): align sparse target error expectation

Preserve sparse-index and rejected-update state assertions while matching the capacity-target validation message.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>

* test: reuse shared clients for capacity target validation

Use AwsTestClients for both SDK clients while retaining the account, region, error matrix and rejected-update state assertions.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>

---------

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-02 22:14:15 -04:00
1 parent a93de121ae
commit e09ca1abe5
5 files changed
+130 -2

No files matched your search

+8
View File
@@ -44,6 +44,14 @@ and zero capacity. Both groups had identical configuration except for name, ARN
and creation time. The SDK regression is
`compatibility-tests/sdk-test-python/tests/test_autoscaling_subnet_whitespace.py`.
Capacity reservation targets remain limited to one reservation ID or one resource
group ARN. Multiple entries, including duplicates, return `ValidationError`;
mixing IDs and group ARNs returns a distinct validation message. These messages
match live AWS comparisons on November 2, 2026 in `us-east-1` and `eu-west-1`
for create, and `eu-west-1` for update, using zero desired capacity.
Rejected updates preserve the group's existing configuration. This does not
enable preview support for multiple reservation targets.
Scale-out follows the [AWS placement rules](https://docs.aws.amazon.com/autoscaling/ec2/userguide/auto-scaling-benefits.html):
choose the zone with the fewest active instances, then its subnet with the most
free IPv4 addresses. Declared subnets and actual EC2 placement take precedence
@@ -91,6 +91,13 @@ public class AutoScalingService implements PassedRoleLookup {
+ "capacity-reservations-only, capacity-reservations-first, none, default.";
static final String CAPACITY_RESERVATION_TARGET_WITH_NONE_MESSAGE =
"You cannot specify a CapacityReservationTarget when CapacityReservationPreference is none.";
// AWS returns "Resoure" verbatim; retain it for callers that classify service errors by message.
static final String MULTIPLE_CAPACITY_RESERVATION_TARGETS_MESSAGE =
"Specifying more than one target CapacityReservationIds or CapacityReservationResoureGroupArns "
+ "is not supported.";
static final String MIXED_CAPACITY_RESERVATION_TARGETS_MESSAGE =
"You can't specify CapacityReservationIds and CapacityReservationResourceGroupArns in the same request. "
+ "Remove either CapacityReservationIds or CapacityReservationResourceGroupArns.";
static final int MAX_SERVICE_LINKED_ROLE_ARN_LENGTH = 1600;
static final int MAX_RESOURCE_NAME_LENGTH = 1600;
private static final Pattern CAPACITY_RESERVATION_ID_PATTERN = Pattern.compile("^[A-Za-z0-9\\-_/]+$");
@@ -1939,9 +1946,13 @@ public class AutoScalingService implements PassedRoleLookup {
if ("none".equals(preference) && specification.hasTarget()) {
throw new AwsException("ValidationError", CAPACITY_RESERVATION_TARGET_WITH_NONE_MESSAGE, 400);
}
if (!specification.getCapacityReservationIds().isEmpty()
&& !specification.getCapacityReservationResourceGroupArns().isEmpty()) {
throw new AwsException("ValidationError", MIXED_CAPACITY_RESERVATION_TARGETS_MESSAGE, 400);
}
if (specification.getCapacityReservationIds().size()
+ specification.getCapacityReservationResourceGroupArns().size() > 1) {
throw new AwsException("ValidationError", "Specify only one Capacity Reservation target.", 400);
throw new AwsException("ValidationError", MULTIPLE_CAPACITY_RESERVATION_TARGETS_MESSAGE, 400);
}
for (String id : specification.getCapacityReservationIds()) {
if (id == null || id.isEmpty() || id.length() > 255
@@ -0,0 +1,105 @@
package io.github.hectorvent.floci.services.autoscaling;
import io.github.hectorvent.floci.testing.AwsTestClients;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.autoscaling.AutoScalingClient;
import software.amazon.awssdk.services.autoscaling.model.AutoScalingException;
import software.amazon.awssdk.services.autoscaling.model.AutoScalingGroup;
import software.amazon.awssdk.services.autoscaling.model.CapacityReservationSpecification;
import software.amazon.awssdk.services.autoscaling.model.CapacityReservationTarget;
import software.amazon.awssdk.services.ec2.Ec2Client;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
class AutoScalingCapacityTargetIntegrationTest {
@ParameterizedTest
@CsvSource({
"ids, capacity-reservations-first",
"duplicate-id, capacity-reservations-first",
"groups, capacity-reservations-first",
"duplicate-group, capacity-reservations-first",
"mixed, capacity-reservations-first",
"ids, capacity-reservations-only",
"duplicate-id, capacity-reservations-only",
"groups, capacity-reservations-only",
"duplicate-group, capacity-reservations-only",
"mixed, capacity-reservations-only"
})
void sdkReceivesAwsTargetErrorsWithoutCreatingOrUpdatingAGroup(String kind, String preference) {
String name = "capacity-target-" + UUID.randomUUID();
CapacityReservationTarget target = switch (kind) {
case "ids" -> CapacityReservationTarget.builder()
.capacityReservationIds("cr-0123456789abcdef0", "cr-11111111111111111").build();
case "duplicate-id" -> CapacityReservationTarget.builder()
.capacityReservationIds("cr-0123456789abcdef0", "cr-0123456789abcdef0").build();
case "groups" -> CapacityReservationTarget.builder().capacityReservationResourceGroupArns(
"arn:aws:resource-groups:us-east-1:454545454545:group/one",
"arn:aws:resource-groups:us-east-1:454545454545:group/two").build();
case "duplicate-group" -> CapacityReservationTarget.builder().capacityReservationResourceGroupArns(
"arn:aws:resource-groups:us-east-1:454545454545:group/one",
"arn:aws:resource-groups:us-east-1:454545454545:group/one").build();
default -> CapacityReservationTarget.builder().capacityReservationIds("cr-0123456789abcdef0")
.capacityReservationResourceGroupArns(
"arn:aws:resource-groups:us-east-1:454545454545:group/one").build();
};
CapacityReservationSpecification specification = CapacityReservationSpecification.builder()
.capacityReservationPreference(preference).capacityReservationTarget(target).build();
// AWS returns "Resoure" verbatim; callers may classify the service error by this message.
String expected = "mixed".equals(kind)
? "You can't specify CapacityReservationIds and CapacityReservationResourceGroupArns in the same request. "
+ "Remove either CapacityReservationIds or CapacityReservationResourceGroupArns."
: "Specifying more than one target CapacityReservationIds or CapacityReservationResoureGroupArns "
+ "is not supported.";
try (AutoScalingClient client = AwsTestClients.create(
AutoScalingClient.builder(), "454545454545", Region.US_EAST_1);
Ec2Client ec2 = AwsTestClients.create(Ec2Client.builder(), "454545454545", Region.US_EAST_1)) {
ec2.createLaunchTemplate(r -> r.launchTemplateName(name)
.launchTemplateData(t -> t.imageId("ami-amazonlinux2023").instanceType("t3.micro")));
try {
AutoScalingException create = assertThrows(AutoScalingException.class,
() -> client.createAutoScalingGroup(r -> r.autoScalingGroupName(name + "-invalid")
.launchTemplate(t -> t.launchTemplateName(name)).availabilityZones("us-east-1a")
.minSize(0).maxSize(1).desiredCapacity(0)
.capacityReservationSpecification(specification)));
assertError(expected, create);
assertTrue(client.describeAutoScalingGroups(r -> r.autoScalingGroupNames(name + "-invalid"))
.autoScalingGroups().isEmpty());
client.createAutoScalingGroup(r -> r.autoScalingGroupName(name)
.launchTemplate(t -> t.launchTemplateName(name)).availabilityZones("us-east-1a")
.minSize(0).maxSize(1).desiredCapacity(0));
AutoScalingGroup before = client.describeAutoScalingGroups(r -> r.autoScalingGroupNames(name))
.autoScalingGroups().getFirst();
AutoScalingException update = assertThrows(AutoScalingException.class,
() -> client.updateAutoScalingGroup(r -> r.autoScalingGroupName(name).maxSize(2)
.capacityReservationSpecification(specification)));
assertError(expected, update);
AutoScalingGroup after = client.describeAutoScalingGroups(r -> r.autoScalingGroupNames(name))
.autoScalingGroups().getFirst();
assertEquals(before.maxSize(), after.maxSize());
assertEquals(before.capacityReservationSpecification(), after.capacityReservationSpecification());
assertEquals(0, after.desiredCapacity());
assertTrue(after.instances().isEmpty());
} finally {
for (AutoScalingGroup group : client.describeAutoScalingGroups(
r -> r.autoScalingGroupNames(name, name + "-invalid")).autoScalingGroups()) {
client.deleteAutoScalingGroup(r -> r.autoScalingGroupName(group.autoScalingGroupName()));
}
ec2.deleteLaunchTemplate(r -> r.launchTemplateName(name));
}
}
}
private static void assertError(String expected, AutoScalingException error) {
assertEquals(400, error.statusCode());
assertEquals("ValidationError", error.awsErrorDetails().errorCode());
assertEquals(expected, error.awsErrorDetails().errorMessage());
}
}
@@ -162,7 +162,7 @@ class AutoScalingQueryHandlerTest {
params.add(prefix + field + ".member.2", target + "-other");
Response rejected = handler.handle("UpdateAutoScalingGroup", params, REGION);
assertEquals(400, rejected.getStatus());
assertTrue(rejected.getEntity().toString().contains("Specify only one Capacity Reservation target."));
assertTrue(rejected.getEntity().toString().contains(AutoScalingService.MULTIPLE_CAPACITY_RESERVATION_TARGETS_MESSAGE));
assertEquals("CapacityReservationIds".equals(field) ? List.of(target) : List.of(),
group.getCapacityReservationSpecification().getCapacityReservationIds());
assertEquals("CapacityReservationResourceGroupArns".equals(field) ? List.of(target) : List.of(),
@@ -1709,12 +1709,16 @@ class AutoScalingServiceTest {
AsgOptionalFields options = new AsgOptionalFields(null, null, null, null, null, null, null, specification);
AwsException create = assertThrows(AwsException.class, () -> createWithStructures("multi-target", options));
assertEquals("ValidationError", create.getErrorCode());
String expected = "mixed".equals(kind) ? AutoScalingService.MIXED_CAPACITY_RESERVATION_TARGETS_MESSAGE
: AutoScalingService.MULTIPLE_CAPACITY_RESERVATION_TARGETS_MESSAGE;
assertEquals(expected, create.getMessage());
assertTrue(service.describeAutoScalingGroups(REGION, List.of("multi-target")).isEmpty());
AutoScalingGroup before = service.describeAutoScalingGroups(REGION, List.of("test-asg")).getFirst();
int desired = before.getDesiredCapacity();
AwsException update = assertThrows(AwsException.class, () -> service.updateAutoScalingGroup(REGION,
"test-asg", null, null, null, null, null, null, null, 2, null, null, null, null, null, null, options));
assertEquals("ValidationError", update.getErrorCode());
assertEquals(expected, update.getMessage());
AutoScalingGroup after = service.describeAutoScalingGroups(REGION, List.of("test-asg")).getFirst();
assertEquals(desired, after.getDesiredCapacity());
assertNull(after.getCapacityReservationSpecification());