fix(application-autoscaling): paginate describe operations (#6398)

* fix(application-autoscaling): paginate describe operations

* fix(application-autoscaling): harden pagination tokens
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-24 08:22:31 -04:00
1 parent 5a9f9cbbd6
commit 2a1d3f3cec
5 files changed
+403 -16

No files matched your search

@@ -9,6 +9,8 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
@@ -33,6 +35,7 @@ class ApplicationAutoScalingIntegrationTest {
private static final String CONTENT_TYPE = "application/x-amz-json-1.1";
private static final String TARGET_PREFIX = "AnyScaleFrontendService.";
private static final String RESOURCE_ID = "service/aas-it-cluster/aas-it-service";
private static final String PAGED_RESOURCE_ID = "service/aas-it-cluster/aas-it-service-paged";
private static final String DIMENSION = "ecs:service:DesiredCount";
private static String scalableTargetArn;
@@ -118,6 +121,37 @@ class ApplicationAutoScalingIntegrationTest {
@Test
@Order(4)
void describeScalableTargetsPaginatesWithMaxResultsAndNextToken() {
call("RegisterScalableTarget")
.body("""
{
"ServiceNamespace": "ecs",
"ResourceId": "%s",
"ScalableDimension": "%s",
"MinCapacity": 1,
"MaxCapacity": 2
}
""".formatted(PAGED_RESOURCE_ID, DIMENSION))
.when().post("/").then().statusCode(200);
String nextToken = call("DescribeScalableTargets")
.body("{ \"ServiceNamespace\": \"ecs\", \"MaxResults\": 1 }")
.when().post("/")
.then().statusCode(200)
.body("ScalableTargets", hasSize(1))
.body("NextToken", notNullValue())
.extract().path("NextToken");
call("DescribeScalableTargets")
.body("{ \"ServiceNamespace\": \"ecs\", \"MaxResults\": 1, \"NextToken\": \"%s\" }"
.formatted(nextToken))
.when().post("/").then().statusCode(200)
.body("ScalableTargets", hasSize(1))
.body("NextToken", nullValue());
}
@Test
@Order(5)
void listTagsForResourceResolvesByScalableTargetArn() {
call("ListTagsForResource")
.body("{ \"ResourceARN\": \"%s\" }".formatted(scalableTargetArn))
@@ -126,7 +160,7 @@ class ApplicationAutoScalingIntegrationTest {
}
@Test
@Order(5)
@Order(6)
void tagAndUntagResource() {
call("TagResource")
.body("""
@@ -154,7 +188,7 @@ class ApplicationAutoScalingIntegrationTest {
}
@Test
@Order(6)
@Order(7)
void putScalingPolicyReturnsPolicyArnAndAlarms() {
call("PutScalingPolicy")
.body("""
@@ -185,7 +219,7 @@ class ApplicationAutoScalingIntegrationTest {
}
@Test
@Order(7)
@Order(8)
void describeScalingPoliciesRoundTripsEveryConfiguredField() {
call("DescribeScalingPolicies")
.body("""
@@ -213,7 +247,53 @@ class ApplicationAutoScalingIntegrationTest {
}
@Test
@Order(8)
@Order(9)
void describeScalingPoliciesPaginatesWithMaxResultsAndNextToken() {
call("PutScalingPolicy")
.body("""
{
"PolicyName": "aas-it-step-policy",
"PolicyType": "StepScaling",
"ServiceNamespace": "ecs",
"ResourceId": "%s",
"ScalableDimension": "%s",
"StepScalingPolicyConfiguration": { "AdjustmentType": "ChangeInCapacity" }
}
""".formatted(RESOURCE_ID, DIMENSION))
.when().post("/").then().statusCode(200);
String nextToken = call("DescribeScalingPolicies")
.body("""
{
"ServiceNamespace": "ecs",
"ResourceId": "%s",
"ScalableDimension": "%s",
"MaxResults": 1
}
""".formatted(RESOURCE_ID, DIMENSION))
.when().post("/")
.then().statusCode(200)
.body("ScalingPolicies", hasSize(1))
.body("NextToken", notNullValue())
.extract().path("NextToken");
call("DescribeScalingPolicies")
.body("""
{
"ServiceNamespace": "ecs",
"ResourceId": "%s",
"ScalableDimension": "%s",
"MaxResults": 1,
"NextToken": "%s"
}
""".formatted(RESOURCE_ID, DIMENSION, nextToken))
.when().post("/").then().statusCode(200)
.body("ScalingPolicies", hasSize(1))
.body("NextToken", nullValue());
}
@Test
@Order(10)
void putScalingPolicyOnUnregisteredTargetThrowsObjectNotFound() {
call("PutScalingPolicy")
.body("""
@@ -236,7 +316,7 @@ class ApplicationAutoScalingIntegrationTest {
}
@Test
@Order(9)
@Order(11)
void invalidServiceNamespaceIsRejected() {
call("DescribeScalableTargets")
.body("{ \"ServiceNamespace\": \"bogus\" }")
@@ -245,7 +325,7 @@ class ApplicationAutoScalingIntegrationTest {
}
@Test
@Order(10)
@Order(12)
void tagsOnUnknownArnThrowResourceNotFound() {
call("ListTagsForResource")
.body("""
@@ -256,7 +336,7 @@ class ApplicationAutoScalingIntegrationTest {
}
@Test
@Order(11)
@Order(13)
void deregisterScalableTargetCascadesToItsPolicies() {
call("DeregisterScalableTarget")
.body("""
@@ -280,7 +360,7 @@ class ApplicationAutoScalingIntegrationTest {
}
@Test
@Order(12)
@Order(14)
void deregisteringTwiceThrowsObjectNotFound() {
call("DeregisterScalableTarget")
.body("""
@@ -291,10 +371,40 @@ class ApplicationAutoScalingIntegrationTest {
}
@Test
@Order(13)
@Order(15)
void unsupportedActionIsRejected() {
call("PutScheduledAction")
.body("{ \"ServiceNamespace\": \"ecs\" }")
.when().post("/").then().statusCode(400);
}
@Test
@Order(16)
void invalidPaginationTokensReturnTheModeledError() {
for (String action : List.of("DescribeScalableTargets", "DescribeScalingPolicies")) {
for (String token : List.of("", "not!base64")) {
call(action)
.body("{ \"ServiceNamespace\": \"ecs\", \"NextToken\": \"%s\" }".formatted(token))
.when().post("/").then().statusCode(400)
.body("__type", equalTo("InvalidNextTokenException"));
}
}
}
@Test
@Order(17)
void pageSizeLimitsAreSpecificToEachOperation() {
call("DescribeScalableTargets")
.body("{ \"ServiceNamespace\": \"ecs\", \"MaxResults\": 50 }")
.when().post("/").then().statusCode(200);
call("DescribeScalingPolicies")
.body("{ \"ServiceNamespace\": \"ecs\", \"MaxResults\": 10 }")
.when().post("/").then().statusCode(200);
call("DescribeScalingPolicies")
.body("{ \"ServiceNamespace\": \"ecs\", \"MaxResults\": 11 }")
.when().post("/").then().statusCode(400)
.body("__type", equalTo("ValidationException"));
}
}
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.applicationautoscaling;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.applicationautoscaling.model.PredefinedMetricSpecification;
@@ -11,7 +12,10 @@ import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsS
import io.github.hectorvent.floci.services.cloudwatch.metrics.model.MetricAlarm;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
@@ -150,6 +154,167 @@ class ApplicationAutoScalingServiceTest {
assertEquals(0, service.describeScalableTargets("kafka", null, null, REGION).size());
}
@Test
void describeScalableTargetsHonorsMaxResultsAndNextToken() {
service.registerScalableTarget(NAMESPACE, "service/a/one", DIMENSION, 1, 10,
null, null, Map.of(), REGION);
service.registerScalableTarget(NAMESPACE, "service/a/two", DIMENSION, 1, 10,
null, null, Map.of(), REGION);
PaginatedResult<ScalableTarget> first = service.describeScalableTargets(
NAMESPACE, null, null, 1, null, REGION);
assertEquals(1, first.items().size());
assertNotNull(first.nextToken());
service.deregisterScalableTarget(NAMESPACE, first.items().getFirst().getResourceId(), DIMENSION, REGION);
PaginatedResult<ScalableTarget> second = service.describeScalableTargets(
NAMESPACE, null, null, 1, first.nextToken(), REGION);
assertEquals(1, second.items().size());
assertNull(second.nextToken());
assertTrue(!first.items().getFirst().getResourceId()
.equals(second.items().getFirst().getResourceId()));
}
@Test
void describeScalingPoliciesHonorsMaxResultsAndNextToken() {
register();
service.putScalingPolicy("policy-a", "StepScaling", NAMESPACE, RESOURCE_ID, DIMENSION,
null, null, REGION);
service.putScalingPolicy("policy-b", "StepScaling", NAMESPACE, RESOURCE_ID, DIMENSION,
null, null, REGION);
PaginatedResult<ScalingPolicy> first = service.describeScalingPolicies(
NAMESPACE, RESOURCE_ID, DIMENSION, null, 1, null, REGION);
assertEquals(1, first.items().size());
assertNotNull(first.nextToken());
service.deleteScalingPolicy(first.items().getFirst().getPolicyName(), NAMESPACE, RESOURCE_ID, DIMENSION, REGION);
PaginatedResult<ScalingPolicy> second = service.describeScalingPolicies(
NAMESPACE, RESOURCE_ID, DIMENSION, null, 1, first.nextToken(), REGION);
assertEquals(1, second.items().size());
assertNull(second.nextToken());
assertTrue(!first.items().getFirst().getPolicyName()
.equals(second.items().getFirst().getPolicyName()));
}
@Test
void describePaginationValidatesPageSizeAndToken() {
register();
assertThrows(AwsException.class, () -> service.describeScalableTargets(
NAMESPACE, null, null, 0, null, REGION));
assertThrows(AwsException.class, () -> service.describeScalableTargets(
NAMESPACE, null, null, 51, null, REGION));
assertThrows(AwsException.class, () -> service.describeScalableTargets(
NAMESPACE, null, null, null, "", REGION));
}
@Test
void targetTokensDistinguishFilterListsWithCommas() {
String dimension = "custom-resource:ResourceType:Property";
for (String id : List.of("a, b", "x")) {
service.registerScalableTarget("custom-resource", id, dimension, 1, 10, null, null, null, REGION);
}
String token = service.describeScalableTargets("custom-resource", List.of("a, b", "x"),
null, 1, null, REGION).nextToken();
assertNotNull(token);
assertEquals("x", service.describeScalableTargets("custom-resource", List.of("x", "a, b"),
null, 1, token, REGION).items().getFirst().getResourceId());
assertInvalidToken(() -> service.describeScalableTargets("custom-resource", List.of("a", "b", "x"),
null, 1, token, REGION));
}
@Test
void policyTokensDistinguishFilterListsWithCommas() {
register();
for (String name : List.of("a, b", "x")) {
service.putScalingPolicy(name, "StepScaling", NAMESPACE, RESOURCE_ID, DIMENSION, null, null, REGION);
}
String token = service.describeScalingPolicies(NAMESPACE, RESOURCE_ID, DIMENSION,
List.of("a, b", "x"), 1, null, REGION).nextToken();
assertNotNull(token);
assertEquals("x", service.describeScalingPolicies(NAMESPACE, RESOURCE_ID, DIMENSION,
List.of("x", "a, b"), 1, token, REGION).items().getFirst().getPolicyName());
assertInvalidToken(() -> service.describeScalingPolicies(NAMESPACE, RESOURCE_ID, DIMENSION,
List.of("a", "b", "x"), 1, token, REGION));
}
@Test
void targetTokensRejectEditedCursors() {
register();
service.registerScalableTarget(NAMESPACE, "service/z/last", DIMENSION, 1, 10, null, null, null, REGION);
String token = service.describeScalableTargets(NAMESPACE, null, null, 1, null, REGION).nextToken();
assertNotNull(token);
assertInvalidToken(() -> service.describeScalableTargets(NAMESPACE, null, null, 1, editedToken(token), REGION));
assertInvalidToken(() -> service.describeScalingPolicies(NAMESPACE, null, null, null, 1, token, REGION));
}
@Test
void policyTokensRejectEditedCursors() {
register();
for (String name : List.of("first", "last")) {
service.putScalingPolicy(name, "StepScaling", NAMESPACE, RESOURCE_ID, DIMENSION, null, null, REGION);
}
String token = service.describeScalingPolicies(NAMESPACE, null, null, null, 1, null, REGION).nextToken();
assertNotNull(token);
assertInvalidToken(() -> service.describeScalingPolicies(
NAMESPACE, null, null, null, 1, editedToken(token), REGION));
assertInvalidToken(() -> service.describeScalableTargets(NAMESPACE, null, null, 1, token, REGION));
}
@Test
void malformedTokensUseTheModeledErrorCode() {
for (String token : List.of("", "not!base64")) {
assertInvalidToken(() -> service.describeScalableTargets(NAMESPACE, null, null, 1, token, REGION));
assertInvalidToken(() -> service.describeScalingPolicies(NAMESPACE, null, null, null, 1, token, REGION));
}
}
@Test
void scalingPolicyPageSizeDefaultsToTen() {
register();
for (int index = 0; index < 11; index++) {
service.putScalingPolicy("policy-" + index, "StepScaling", NAMESPACE, RESOURCE_ID, DIMENSION,
null, null, REGION);
}
PaginatedResult<ScalingPolicy> first = service.describeScalingPolicies(
NAMESPACE, null, null, null, null, null, REGION);
assertEquals(10, first.items().size());
assertNotNull(first.nextToken());
PaginatedResult<ScalingPolicy> second = service.describeScalingPolicies(
NAMESPACE, null, null, null, null, first.nextToken(), REGION);
assertEquals(1, second.items().size());
assertNull(second.nextToken());
}
@Test
void scalingPolicyPageSizeHasItsOwnMaximum() {
register();
assertEquals(1, service.describeScalableTargets(NAMESPACE, null, null, 50, null, REGION).items().size());
assertEquals(0, service.describeScalingPolicies(NAMESPACE, null, null, null, 10, null, REGION).items().size());
AwsException error = assertThrows(AwsException.class,
() -> service.describeScalingPolicies(NAMESPACE, null, null, null, 11, null, REGION));
assertEquals("ValidationException", error.getErrorCode());
}
private static String editedToken(String token) {
String decoded = new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8);
return Base64.getUrlEncoder().withoutPadding().encodeToString((decoded + "edited").getBytes(StandardCharsets.UTF_8));
}
private static void assertInvalidToken(Executable request) {
AwsException error = assertThrows(AwsException.class, request);
assertEquals("InvalidNextTokenException", error.getErrorCode());
}
@Test
void describeRejectsDimensionWithoutResourceId() {
register();