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

@@ -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());