mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(autoscaling): preserve indexed standby request members (#7205)
Preserve every positive indexed standby member for validation before mutation. Reuse the existing capacity-target parser and sort numeric indexes without integer conversion. Preserve accepted namespace assertions and current main behavior. Sparse AWS index acceptance is not claimed. Signed-off-by: Andre Kurait <AndreKurait@gmail.com> Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
1 parent
24b152a274
commit
dae514b6a2
3 files changed
+133
-5
No files matched your search
+7
-5
@@ -941,7 +941,7 @@ public class AutoScalingQueryHandler {
|
||||
boolean shouldDecrement = requiredBoolParam(p, "ShouldDecrementDesiredCapacity",
|
||||
"shouldDecrementDesiredCapacity");
|
||||
return ok(activitiesResponse("EnterStandby",
|
||||
service.enterStandby(region, groupName, memberList(p, "InstanceIds"), shouldDecrement)));
|
||||
service.enterStandby(region, groupName, indexedMembers(p, "InstanceIds"), shouldDecrement)));
|
||||
}
|
||||
|
||||
private Response handleExitStandby(MultivaluedMap<String, String> p, String region) {
|
||||
@@ -950,7 +950,7 @@ public class AutoScalingQueryHandler {
|
||||
throw SmithyValidationErrors.validationError(SmithyValidationErrors.notNull("autoScalingGroupName"));
|
||||
}
|
||||
return ok(activitiesResponse("ExitStandby",
|
||||
service.exitStandby(region, groupName, memberList(p, "InstanceIds"))));
|
||||
service.exitStandby(region, groupName, indexedMembers(p, "InstanceIds"))));
|
||||
}
|
||||
|
||||
private static String activitiesResponse(String action, List<ScalingActivity> activities) {
|
||||
@@ -1720,17 +1720,19 @@ public class AutoScalingQueryHandler {
|
||||
CapacityReservationSpecification specification = new CapacityReservationSpecification();
|
||||
specification.setCapacityReservationPreference(p.getFirst(prefix + "CapacityReservationPreference"));
|
||||
specification.setCapacityReservationIds(
|
||||
capacityTargetMembers(p, prefix + "CapacityReservationTarget.CapacityReservationIds"));
|
||||
indexedMembers(p, prefix + "CapacityReservationTarget.CapacityReservationIds"));
|
||||
specification.setCapacityReservationResourceGroupArns(
|
||||
capacityTargetMembers(p, prefix + "CapacityReservationTarget.CapacityReservationResourceGroupArns"));
|
||||
indexedMembers(p, prefix + "CapacityReservationTarget.CapacityReservationResourceGroupArns"));
|
||||
return specification;
|
||||
}
|
||||
|
||||
private List<String> capacityTargetMembers(MultivaluedMap<String, String> params, String prefix) {
|
||||
private List<String> indexedMembers(MultivaluedMap<String, String> 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]*"))
|
||||
.sorted(Comparator.comparingInt((Map.Entry<String, List<String>> entry) -> entry.getKey().length())
|
||||
.thenComparing(Map.Entry::getKey))
|
||||
.flatMap(entry -> entry.getValue().stream())
|
||||
.toList();
|
||||
}
|
||||
|
||||
+75
@@ -3,12 +3,17 @@ package io.github.hectorvent.floci.services.autoscaling;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.AsgInstance;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.AsgOptionalFields;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.AutoScalingGroup;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -17,11 +22,81 @@ import static io.github.hectorvent.floci.services.autoscaling.AutoScalingXmlAsse
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class AutoScalingQueryHandlerTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"EnterStandby", "ExitStandby"})
|
||||
void standbyMembersRetainNumericOrderAndRepeatedParameterValues(String action) {
|
||||
AutoScalingService service = mock(AutoScalingService.class);
|
||||
AutoScalingQueryHandler handler = new AutoScalingQueryHandler(service);
|
||||
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.add("AutoScalingGroupName", "indexed-standby");
|
||||
params.add("ShouldDecrementDesiredCapacity", "true");
|
||||
for (int index = 12; index >= 1; index--) {
|
||||
params.add("InstanceIds.member." + index, "i-" + index);
|
||||
}
|
||||
params.add("InstanceIds.member.2", "i-repeated");
|
||||
List<String> expected = new ArrayList<>();
|
||||
for (int index = 1; index <= 12; index++) {
|
||||
expected.add("i-" + index);
|
||||
if (index == 2) {
|
||||
expected.add("i-repeated");
|
||||
}
|
||||
}
|
||||
when(service.enterStandby(eq(REGION), eq("indexed-standby"), anyList(), eq(true)))
|
||||
.thenReturn(List.of());
|
||||
when(service.exitStandby(eq(REGION), eq("indexed-standby"), anyList())).thenReturn(List.of());
|
||||
|
||||
assertEquals(200, handler.handle(action, params, REGION).getStatus());
|
||||
|
||||
if ("EnterStandby".equals(action)) {
|
||||
verify(service).enterStandby(REGION, "indexed-standby", expected, true);
|
||||
} else {
|
||||
verify(service).exitStandby(REGION, "indexed-standby", expected);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"CapacityReservationIds,cr-0123456789abcdef0",
|
||||
"CapacityReservationResourceGroupArns,arn:aws:resource-groups:us-east-1:000000000000:group/capacity"
|
||||
})
|
||||
void capacityReservationMembersRetainSparseTargetsAndRejectMultipleTargets(String field, String target) {
|
||||
AutoScalingService service = new AutoScalingService();
|
||||
service.regionResolver = new RegionResolver(REGION, "000000000000");
|
||||
AutoScalingQueryHandler handler = new AutoScalingQueryHandler(service);
|
||||
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.add("AutoScalingGroupName", "indexed-capacity");
|
||||
params.add("LaunchTemplate.LaunchTemplateId", "lt-original");
|
||||
params.add("MinSize", "0");
|
||||
params.add("MaxSize", "3");
|
||||
String prefix = "CapacityReservationSpecification.CapacityReservationTarget.";
|
||||
params.add(prefix + field + ".member.999999999999999999999999", target);
|
||||
params.add(prefix + field + ".member.01", "ignored");
|
||||
params.add(prefix + field + ".member.3.Other", "ignored");
|
||||
|
||||
assertEquals(200, handler.handle("CreateAutoScalingGroup", params, REGION).getStatus());
|
||||
|
||||
AutoScalingGroup group = service.describeAutoScalingGroups(REGION, List.of("indexed-capacity")).getFirst();
|
||||
assertEquals("CapacityReservationIds".equals(field) ? List.of(target) : List.of(),
|
||||
group.getCapacityReservationSpecification().getCapacityReservationIds());
|
||||
assertEquals("CapacityReservationResourceGroupArns".equals(field) ? List.of(target) : List.of(),
|
||||
group.getCapacityReservationSpecification().getCapacityReservationResourceGroupArns());
|
||||
|
||||
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."));
|
||||
assertEquals("CapacityReservationIds".equals(field) ? List.of(target) : List.of(),
|
||||
group.getCapacityReservationSpecification().getCapacityReservationIds());
|
||||
assertEquals("CapacityReservationResourceGroupArns".equals(field) ? List.of(target) : List.of(),
|
||||
group.getCapacityReservationSpecification().getCapacityReservationResourceGroupArns());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeAutoScalingInstancesEmptyUsesModeledNamespace() throws Exception {
|
||||
AutoScalingService service = new AutoScalingService();
|
||||
|
||||
+51
@@ -589,6 +589,57 @@ class AutoScalingStandbyTest {
|
||||
assertEquals("Cancelled", activity(describeScalingActivities(), activityId, "StatusCode"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"EnterStandby", "ExitStandby"})
|
||||
void sparseUnknownMemberRejectsTheWholeStandbyRequest(String action) {
|
||||
if ("ExitStandby".equals(action)) {
|
||||
group().getInstances().forEach(instance -> instance.setLifecycleState("Standby"));
|
||||
group().setDesiredCapacity(0);
|
||||
}
|
||||
String before = lifecycle("i-a");
|
||||
int capacity = group().getDesiredCapacity();
|
||||
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.add("AutoScalingGroupName", GROUP);
|
||||
params.add("ShouldDecrementDesiredCapacity", "true");
|
||||
params.add("InstanceIds.member.1", "i-a");
|
||||
params.add("InstanceIds.member.3", "i-unknown");
|
||||
|
||||
Response response = handler.handle(action, params, REGION);
|
||||
|
||||
assertEquals(400, response.getStatus());
|
||||
assertEquals(before, lifecycle("i-a"));
|
||||
assertEquals(capacity, group().getDesiredCapacity());
|
||||
assertTrue(service.describeScalingActivities(REGION, GROUP).isEmpty());
|
||||
verifyNoInteractions(elbV2, elbClassic);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"EnterStandby", "ExitStandby"})
|
||||
void sparseStandbyMembersUseNumericOrderAndIgnoreUnrelatedKeys(String action) {
|
||||
if ("ExitStandby".equals(action)) {
|
||||
group().getInstances().forEach(instance -> instance.setLifecycleState("Standby"));
|
||||
group().setDesiredCapacity(0);
|
||||
}
|
||||
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.add("AutoScalingGroupName", GROUP);
|
||||
params.add("ShouldDecrementDesiredCapacity", "true");
|
||||
params.add("InstanceIds.member.999999999999999999999999", "i-b");
|
||||
params.add("InstanceIds.member.2", "i-a");
|
||||
for (String key : List.of("InstanceIds.member.0", "InstanceIds.member.-1", "InstanceIds.member.01",
|
||||
"InstanceIds.member.3.Other", "Other.member.3")) {
|
||||
params.add(key, "i-unknown");
|
||||
}
|
||||
|
||||
XmlPath response = ok(handler.handle(action, params, REGION));
|
||||
|
||||
List<String> descriptions = response.getList(("EnterStandby".equals(action) ? ENTERED : EXITED)
|
||||
+ ".Description");
|
||||
assertEquals(2, descriptions.size());
|
||||
assertTrue(descriptions.get(0).contains("i-a"));
|
||||
assertTrue(descriptions.get(1).contains("i-b"));
|
||||
assertEquals("EnterStandby".equals(action) ? 0 : 2, group().getDesiredCapacity());
|
||||
}
|
||||
|
||||
private Response enterStandby(boolean decrement, String... instanceIds) {
|
||||
MultivaluedHashMap<String, String> params = params(GROUP);
|
||||
for (int index = 0; index < instanceIds.length; index++) {
|
||||
|
||||
Reference in new issue
Block a user