diff --git a/docs/services/autoscaling.md b/docs/services/autoscaling.md index 819e97dfd6..0e67a17912 100644 --- a/docs/services/autoscaling.md +++ b/docs/services/autoscaling.md @@ -273,10 +273,18 @@ never set a field omits it from the response rather than reporting a default. | `ServiceLinkedRoleARN` | Stored and echoed as given, 1 to 1600 characters. Floci does not check that the role exists, and a group that never set it omits it rather than reporting the default `AWSServiceRoleForAutoScaling` role | | `InstanceMaintenancePolicy` | `MinHealthyPercentage` 0 to 100 and `MaxHealthyPercentage` 100 to 200, both required, at most 100 apart. `-1` on both clears a policy already set. Any other combination raises `ValidationError` | | `AvailabilityZoneDistribution` | `CapacityDistributionStrategy` is one of `balanced-only`, `balanced-best-effort`, `reservations-then-balanced` | -| `CapacityReservationSpecification` | `CapacityReservationPreference` is one of `capacity-reservations-only`, `capacity-reservations-first`, `none`, `default`. `CapacityReservationTarget` (`CapacityReservationIds`, `CapacityReservationResourceGroupArns`) round-trips, and is rejected alongside `none`. Each ID is 1 to 255 characters of `[A-Za-z0-9-_/]` and each resource group ARN is 1 to 1600 characters | +| `CapacityReservationSpecification` | `CapacityReservationPreference` is one of `capacity-reservations-only`, `capacity-reservations-first`, `none`, `default`. `CapacityReservationTarget` (`CapacityReservationIds`, `CapacityReservationResourceGroupArns`) round-trips, accepts at most one ID or resource group ARN, and is rejected alongside `none`. Each ID is 1 to 255 characters of `[A-Za-z0-9-_/]` and each resource group ARN is 1 to 1600 characters | -These fields are stored and reported only. Floci's capacity reconciler does not change how it -launches or replaces instances because of them. +Except for `CapacityReservationSpecification`, these fields are stored and reported only. + +The group capacity reservation preference applies to scale-out and replacement launches. +`default` or an omitted preference retains the launch template specification. `none` launches +without consuming reservations. `capacity-reservations-only` requires matching reserved capacity; +`capacity-reservations-first` falls back to unreserved On-Demand capacity on a capacity shortage. +An omitted group target inherits the template target; with neither target, matching open +reservations are eligible. Updating the group affects subsequent launches, not existing instances. +Missing or invalid targets do not trigger On-Demand fallback. Resource group targeting remains +unsupported, as in EC2, and reports a failed scaling activity without allocating an instance. ## Scaling Policy Compatibility diff --git a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingQueryHandler.java index 6a93e78089..611cbe6de2 100644 --- a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingQueryHandler.java @@ -1666,12 +1666,21 @@ public class AutoScalingQueryHandler { CapacityReservationSpecification specification = new CapacityReservationSpecification(); specification.setCapacityReservationPreference(p.getFirst(prefix + "CapacityReservationPreference")); specification.setCapacityReservationIds( - memberList(p, prefix + "CapacityReservationTarget.CapacityReservationIds")); + capacityTargetMembers(p, prefix + "CapacityReservationTarget.CapacityReservationIds")); specification.setCapacityReservationResourceGroupArns( - memberList(p, prefix + "CapacityReservationTarget.CapacityReservationResourceGroupArns")); + capacityTargetMembers(p, prefix + "CapacityReservationTarget.CapacityReservationResourceGroupArns")); return specification; } + private List capacityTargetMembers(MultivaluedMap params, String prefix) { + String memberPrefix = prefix + ".member."; + return params.entrySet().stream() + .filter(entry -> entry.getKey().startsWith(memberPrefix)) + .filter(entry -> entry.getKey().substring(memberPrefix.length()).matches("[1-9][0-9]*")) + .flatMap(entry -> entry.getValue().stream()) + .toList(); + } + private ParsedTags parseTags(MultivaluedMap p) { Map tags = new LinkedHashMap<>(); Map propagateAtLaunch = new LinkedHashMap<>(); diff --git a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.java b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.java index ff35cafeae..4ff5558bc0 100644 --- a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.java +++ b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.java @@ -4,6 +4,7 @@ import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RequestScopes; import io.github.hectorvent.floci.services.autoscaling.model.AsgInstance; import io.github.hectorvent.floci.services.autoscaling.model.AutoScalingGroup; +import io.github.hectorvent.floci.services.autoscaling.model.CapacityReservationSpecification; import io.github.hectorvent.floci.services.autoscaling.model.InstanceRefresh; import io.github.hectorvent.floci.services.autoscaling.model.InstanceRefreshLaunch; import io.github.hectorvent.floci.services.autoscaling.model.InstanceRefreshReplacement; @@ -538,6 +539,71 @@ public class AutoScalingReconciler { return launchedInstanceIds; } + private Reservation launchWithGroupCapacity(AutoScalingGroup asg, LaunchSource launchSource, + LaunchPlacement placement, String clientToken) { + CapacityReservationSpecification groupSpecification = asg.getCapacityReservationSpecification(); + AutoScalingService.validateCapacityReservationSpecification(groupSpecification); + String preference = groupSpecification == null ? null + : groupSpecification.getCapacityReservationPreference(); + if (preference == null || "default".equals(preference)) { + return launchWithCapacity(asg, launchSource, placement, clientToken, + launchSource.capacityReservationSpecification()); + } + LaunchTemplateData.CapacityReservationSpecification specification = + new LaunchTemplateData.CapacityReservationSpecification(); + if (!"none".equals(preference)) { + specification.setCapacityReservationTarget(capacityTarget(groupSpecification, + launchSource.capacityReservationSpecification())); + } + boolean fallback = "capacity-reservations-first".equals(preference); + specification.setCapacityReservationPreference(fallback + ? specification.getCapacityReservationTarget() == null ? "open" : "capacity-reservations-only" + : preference); + try { + return launchWithCapacity(asg, launchSource, placement, clientToken, specification); + } catch (AwsException failure) { + if (!fallback || !Set.of("InsufficientInstanceCapacity", "ReservationCapacityExceeded") + .contains(failure.getErrorCode())) { + throw failure; + } + specification.setCapacityReservationPreference("none"); + specification.setCapacityReservationTarget(null); + return launchWithCapacity(asg, launchSource, placement, clientToken, specification); + } + } + + private static LaunchTemplateData.CapacityReservationTarget capacityTarget( + CapacityReservationSpecification groupSpecification, + LaunchTemplateData.CapacityReservationSpecification templateSpecification) { + if (!groupSpecification.hasTarget()) { + return templateSpecification == null ? null : templateSpecification.getCapacityReservationTarget(); + } + LaunchTemplateData.CapacityReservationTarget target = new LaunchTemplateData.CapacityReservationTarget(); + if (!groupSpecification.getCapacityReservationIds().isEmpty()) { + target.setCapacityReservationId(groupSpecification.getCapacityReservationIds().getFirst()); + } else { + target.setCapacityReservationResourceGroupArn( + groupSpecification.getCapacityReservationResourceGroupArns().getFirst()); + } + return target; + } + + private Reservation launchWithCapacity(AutoScalingGroup asg, LaunchSource launchSource, + LaunchPlacement placement, String clientToken, + LaunchTemplateData.CapacityReservationSpecification capacitySpecification) { + return ec2Service.runInstances( + asg.getRegion(), launchSource.imageId(), launchSource.instanceType(), 1, 1, + launchSource.keyName(), launchSource.securityGroupIds(), placement.subnetId(), clientToken, + propagatedInstanceTags(asg, launchSource), launchSource.userData(), + launchSource.launchConfigurationName() == null ? launchSource.iamInstanceProfile() + : asgService.resolveLaunchConfigurationProfile(launchSource.iamInstanceProfile()), + launchSource.associatePublicIpAddress(), null, 0, null, launchSource.metadataOptions(), null, + launchSource.encodedUserData() != null ? launchSource.encodedUserData() + : encodeLegacyUserData(launchSource.userData()), + false, null, null, launchSource.blockDeviceMappings(), false, null, + capacitySpecification); + } + private List launchInstance(AutoScalingGroup asg, LaunchSource launchSource, LaunchPlacement placement, String clientToken, InstanceRefreshLaunch metadata, List existing, @@ -546,17 +612,7 @@ public class AutoScalingReconciler { if (instances.isEmpty()) { Reservation reservation; try { - reservation = ec2Service.runInstances( - asg.getRegion(), launchSource.imageId(), launchSource.instanceType(), 1, 1, - launchSource.keyName(), launchSource.securityGroupIds(), placement.subnetId(), clientToken, - propagatedInstanceTags(asg, launchSource), launchSource.userData(), - launchSource.launchConfigurationName() == null ? launchSource.iamInstanceProfile() - : asgService.resolveLaunchConfigurationProfile(launchSource.iamInstanceProfile()), - launchSource.associatePublicIpAddress(), null, 0, null, launchSource.metadataOptions(), null, - launchSource.encodedUserData() != null ? launchSource.encodedUserData() - : encodeLegacyUserData(launchSource.userData()), - false, null, null, launchSource.blockDeviceMappings(), false, null, - launchSource.capacityReservationSpecification()); + reservation = launchWithGroupCapacity(asg, launchSource, placement, clientToken); } catch (RuntimeException launchFailure) { if (!refreshLaunch) { throw launchFailure; diff --git a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingService.java b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingService.java index d5680924a9..bed21b896b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingService.java +++ b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingService.java @@ -1627,7 +1627,7 @@ public class AutoScalingService implements PassedRoleLookup { } } - private static void validateCapacityReservationSpecification(CapacityReservationSpecification specification) { + static void validateCapacityReservationSpecification(CapacityReservationSpecification specification) { if (specification == null) { return; } @@ -1638,6 +1638,10 @@ 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().size() + + specification.getCapacityReservationResourceGroupArns().size() > 1) { + throw new AwsException("ValidationError", "Specify only one Capacity Reservation target.", 400); + } for (String id : specification.getCapacityReservationIds()) { if (id == null || id.isEmpty() || id.length() > 255 || !CAPACITY_RESERVATION_ID_PATTERN.matcher(id).matches()) { diff --git a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingCapacityTargetQueryIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingCapacityTargetQueryIntegrationTest.java new file mode 100644 index 0000000000..2a09d8338a --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingCapacityTargetQueryIntegrationTest.java @@ -0,0 +1,99 @@ +package io.github.hectorvent.floci.services.autoscaling; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.ValidatableResponse; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.UUID; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; + +@QuarkusTest +class AutoScalingCapacityTargetQueryIntegrationTest { + private static final String AUTH = + "AWS4-HMAC-SHA256 Credential=test/20261023/us-east-1/autoscaling/aws4_request"; + private static final String MEMBER = + "DescribeAutoScalingGroupsResponse.DescribeAutoScalingGroupsResult.AutoScalingGroups.member"; + private static final String TARGET = "CapacityReservationSpecification.CapacityReservationTarget."; + + @ParameterizedTest + @CsvSource({"CapacityReservationIds,false", "CapacityReservationIds,true", + "CapacityReservationResourceGroupArns,false", "CapacityReservationResourceGroupArns,true"}) + void sparseMultipleTargetsCannotMutateGroup(String target, boolean update) { + String name = "sparse-target-" + UUID.randomUUID(); + createConfiguration(name); + try { + if (update) { + create(name).post("/").then().statusCode(200); + } + RequestSpecification request = update ? request("UpdateAutoScalingGroup", name) + .formParam("DesiredCapacity", "1") : create(name); + request.formParam("CapacityReservationSpecification.CapacityReservationPreference", + "capacity-reservations-only") + .formParam(TARGET + target + ".member.1", "target-one") + .formParam(TARGET + target + ".member.3", "target-three") + .post("/").then().statusCode(400).body(containsString("ValidationError")); + describe(name).body(MEMBER + ".size()", equalTo(update ? 1 : 0)); + if (update) { + describe(name).body(MEMBER + ".DesiredCapacity", equalTo("0")) + .body(MEMBER + ".CapacityReservationSpecification.size()", equalTo(0)); + } + } finally { + cleanUp(name); + } + } + + @ParameterizedTest + @ValueSource(strings = {"CapacityReservationIds", "CapacityReservationResourceGroupArns"}) + void sparseSingletonTargetIsRetained(String target) { + String name = "sparse-single-" + UUID.randomUUID(); + createConfiguration(name); + try { + create(name).formParam("CapacityReservationSpecification.CapacityReservationPreference", + "capacity-reservations-only") + .formParam(TARGET + target + ".member.3", "target-three") + .post("/").then().statusCode(200); + describe(name).body(MEMBER + ".CapacityReservationSpecification.CapacityReservationTarget." + + target + ".member", equalTo("target-three")); + } finally { + cleanUp(name); + } + } + + private static RequestSpecification request(String action, String name) { + return given().header("Authorization", AUTH).formParam("Action", action) + .formParam("AutoScalingGroupName", name); + } + + private static RequestSpecification create(String name) { + return request("CreateAutoScalingGroup", name).formParam("LaunchConfigurationName", name) + .formParam("MinSize", "0").formParam("MaxSize", "1").formParam("DesiredCapacity", "0") + .formParam("AvailabilityZones.member.1", "us-east-1a"); + } + + private static ValidatableResponse describe(String name) { + return given().header("Authorization", AUTH).formParam("Action", "DescribeAutoScalingGroups") + .formParam("AutoScalingGroupNames.member.1", name).post("/").then().statusCode(200); + } + + private static void createConfiguration(String name) { + given().header("Authorization", AUTH).formParam("Action", "CreateLaunchConfiguration") + .formParam("LaunchConfigurationName", name).formParam("ImageId", "ami-amazonlinux2023") + .formParam("InstanceType", "t3.small").post("/").then().statusCode(200); + } + + private static void cleanUp(String name) { + int count = describe(name).extract().xmlPath().getInt(MEMBER + ".size()"); + if (count > 0) { + request("DeleteAutoScalingGroup", name).formParam("ForceDelete", "true") + .post("/").then().statusCode(200); + } + given().header("Authorization", AUTH).formParam("Action", "DeleteLaunchConfiguration") + .formParam("LaunchConfigurationName", name).post("/").then().statusCode(200); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReservationOnlyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReservationOnlyIntegrationTest.java index 269ae4f3af..c5002002c9 100644 --- a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReservationOnlyIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReservationOnlyIntegrationTest.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.autoscaling; import io.github.hectorvent.floci.core.common.RequestScopes; import io.github.hectorvent.floci.services.autoscaling.model.AsgOptionalFields; import io.github.hectorvent.floci.services.autoscaling.model.AutoScalingGroup; +import io.github.hectorvent.floci.services.autoscaling.model.CapacityReservationSpecification; import io.github.hectorvent.floci.services.ec2.Ec2Service; import io.github.hectorvent.floci.services.ec2.model.CapacityReservation; import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate; @@ -10,6 +11,7 @@ import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData; import io.quarkus.test.junit.QuarkusTest; import jakarta.inject.Inject; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; import java.util.List; @@ -35,10 +37,56 @@ class AutoScalingReservationOnlyIntegrationTest { @ParameterizedTest @ValueSource(booleans = {false, true}) void templateReservationOnlyDoesNotFallBack(boolean available) { + checkReservationPreference(available, "capacity-reservations-only", null, false, available ? 1 : 0, + available, null); + } + + @ParameterizedTest + @CsvSource({ + "false,none,capacity-reservations-only,false,0,false", + "true,none,capacity-reservations-only,false,1,true", + "false,none,capacity-reservations-only,true,0,false", + "true,none,capacity-reservations-only,true,1,true", + "true,capacity-reservations-only,none,false,1,false", + "true,capacity-reservations-only,none,true,1,false", + "false,capacity-reservations-only,capacity-reservations-first,false,1,false", + "true,none,capacity-reservations-first,false,1,true", + "false,capacity-reservations-only,default,false,0,false", + "true,none,default,false,1,false" + }) + void groupPreferenceControlsLaunch(boolean available, String templatePreference, String groupPreference, + boolean update, int expectedInstances, boolean expectedReserved) { + checkReservationPreference(available, templatePreference, groupPreference, update, expectedInstances, + expectedReserved, null); + } + + @ParameterizedTest + @CsvSource({ + "capacity-reservations-only,matching,1,true", + "capacity-reservations-first,matching,1,true", + "capacity-reservations-only,inherited-mismatch,0,false", + "capacity-reservations-first,inherited-mismatch,1,false", + "capacity-reservations-only,override-template,1,true", + "none,inherited-mismatch,1,false", + "capacity-reservations-only,mismatch,0,false", + "capacity-reservations-first,mismatch,1,false", + "capacity-reservations-only,missing,0,false", + "capacity-reservations-first,missing,0,false", + "capacity-reservations-only,resource-group,0,false", + "capacity-reservations-first,resource-group,0,false" + }) + void groupTargetsDoNotConsumeUnrelatedOpenCapacity(String preference, String targets, + int expectedInstances, boolean expectedReserved) { + checkReservationPreference(true, "open", preference, false, expectedInstances, expectedReserved, targets); + } + + private void checkReservationPreference(boolean available, String templatePreference, String groupPreference, + boolean update, int expectedInstances, boolean expectedReserved, String targets) { RequestScopes.runAs(ACCOUNT, REGION, () -> { CapacityReservation capacity = ec2.createCapacityReservation(REGION, "t3.small", "Linux/UNIX", REGION + "a", null, 1, null, null, null, null, null, null, null, null); String groupName = "reservation-only-" + UUID.randomUUID(); + CapacityReservation mismatch = null; LaunchTemplate template = null; boolean groupCreated = false; try { @@ -50,25 +98,58 @@ class AutoScalingReservationOnlyIntegrationTest { data.setInstanceType("t3.small"); LaunchTemplateData.CapacityReservationSpecification specification = new LaunchTemplateData.CapacityReservationSpecification(); - specification.setCapacityReservationPreference("capacity-reservations-only"); + specification.setCapacityReservationPreference(templatePreference); data.setCapacityReservationSpecification(specification); template = ec2.createLaunchTemplate(REGION, groupName, data, List.of()); String subnet = ec2.describeSubnets(REGION, List.of(), Map.of()).stream() .filter(value -> (REGION + "a").equals(value.getAvailabilityZone())) .findFirst().orElseThrow().getSubnetId(); + CapacityReservationSpecification groupSpec = + new CapacityReservationSpecification(); + groupSpec.setCapacityReservationPreference(groupPreference); + if (targets != null) { + if ("resource-group".equals(targets)) { + groupSpec.setCapacityReservationResourceGroupArns( + List.of("arn:aws:resource-groups:us-east-1:" + ACCOUNT + ":group/test")); + } else if ("missing".equals(targets)) { + groupSpec.setCapacityReservationIds(List.of("cr-00000000000000000")); + } else { + mismatch = ec2.createCapacityReservation(REGION, "t3.micro", "Linux/UNIX", + REGION + "a", null, 1, null, null, null, null, null, null, null, null); + if ("inherited-mismatch".equals(targets) || "override-template".equals(targets)) { + LaunchTemplateData.CapacityReservationTarget templateTarget = + new LaunchTemplateData.CapacityReservationTarget(); + templateTarget.setCapacityReservationId(mismatch.getCapacityReservationId()); + specification.setCapacityReservationTarget(templateTarget); + ec2.createLaunchTemplateVersion(REGION, template.getLaunchTemplateId(), null, + "1", data, null); + } + if (!"inherited-mismatch".equals(targets)) { + groupSpec.setCapacityReservationIds(List.of( + "matching".equals(targets) || "override-template".equals(targets) + ? capacity.getCapacityReservationId() : mismatch.getCapacityReservationId())); + } + } + } + AsgOptionalFields options = groupPreference == null ? AsgOptionalFields.none() + : new AsgOptionalFields(null, null, null, null, null, null, null, groupSpec); AutoScalingGroup group = service.createAutoScalingGroup(REGION, groupName, null, - template.getLaunchTemplateId(), null, "1", null, 0, 1, 1, 300, + template.getLaunchTemplateId(), null, "$Latest", null, 0, 1, update ? 0 : 1, 300, List.of(REGION + "a"), List.of(subnet), List.of(), List.of(), "EC2", 0, - List.of("Default"), Map.of(), Map.of(), AsgOptionalFields.none()); + List.of("Default"), Map.of(), Map.of(), update ? AsgOptionalFields.none() : options); groupCreated = true; + if (update) { + service.updateAutoScalingGroup(REGION, groupName, null, null, null, null, null, + null, null, 1, null, null, null, null, null, null, options); + } reconciler.reconcile(group); AutoScalingGroup current = service.describeAutoScalingGroups(REGION, List.of(groupName)).getFirst(); - assertEquals(available ? 1 : 0, current.getInstances().size()); - assertEquals(available ? 1 : 0, ec2.describeInstances(REGION, List.of(), Map.of()).stream() + assertEquals(expectedInstances, current.getInstances().size()); + assertEquals(expectedInstances, ec2.describeInstances(REGION, List.of(), Map.of()).stream() .flatMap(value -> value.getInstances().stream()) .filter(value -> !"terminated".equals(value.getState().getName())).count()); if (available) { - assertEquals(List.of(current.getInstances().getFirst().getInstanceId()), + assertEquals(expectedReserved ? List.of(current.getInstances().getFirst().getInstanceId()) : List.of(), ec2.describeCapacityReservations(REGION, List.of(capacity.getCapacityReservationId()), Map.of()).getFirst().getInstanceIds()); } @@ -79,6 +160,9 @@ class AutoScalingReservationOnlyIntegrationTest { if (template != null) { ec2.deleteLaunchTemplate(REGION, template.getLaunchTemplateId(), null); } + if (mismatch != null) { + ec2.cancelCapacityReservation(REGION, mismatch.getCapacityReservationId()); + } if (available) { ec2.cancelCapacityReservation(REGION, capacity.getCapacityReservationId()); } diff --git a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingServiceTest.java index e4019fcb3a..8b636eb90a 100644 --- a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingServiceTest.java @@ -26,6 +26,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -1449,6 +1450,33 @@ class AutoScalingServiceTest { assertEquals(AutoScalingService.CAPACITY_RESERVATION_TARGET_WITH_NONE_MESSAGE, error.getMessage()); } + @ParameterizedTest + @ValueSource(strings = {"ids", "groups", "mixed"}) + void capacityReservationRejectsMultipleTargetsBeforeCreateOrUpdate(String kind) { + CapacityReservationSpecification specification = new CapacityReservationSpecification(); + specification.setCapacityReservationPreference("capacity-reservations-only"); + if ("ids".equals(kind)) { + specification.setCapacityReservationIds(List.of("cr-one", "cr-two")); + } else if ("groups".equals(kind)) { + specification.setCapacityReservationResourceGroupArns(List.of("group-one", "group-two")); + } else { + specification.setCapacityReservationIds(List.of("cr-one")); + specification.setCapacityReservationResourceGroupArns(List.of("group-one")); + } + 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()); + 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()); + AutoScalingGroup after = service.describeAutoScalingGroups(REGION, List.of("test-asg")).getFirst(); + assertEquals(desired, after.getDesiredCapacity()); + assertNull(after.getCapacityReservationSpecification()); + } + @Test void capacityReservationTargetMembersFollowTheirModelShapes() { CapacityReservationSpecification badId = new CapacityReservationSpecification();