fix(lambda): paginate ListFunctions responses (#6700)

* fix(lambda): paginate ListFunctions responses

* test(lambda): isolate ListFunctions pagination fixture

* refactor(lambda): share signed list pagination
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-26 21:36:27 -04:00
1 parent e9e2b38da6
commit a30e484dae
8 files changed
+263 -67

No files matched your search

@@ -67,6 +67,34 @@ class PaginationTest {
assertThrows(IllegalArgumentException.class, () -> page(0, null, "ns"));
}
@Test
void signedPaging_resumesWithAnIssuedMarkerAndRejectsForeignOrChangedMarkers() {
SignedMarkers markers = new SignedMarkers();
PaginatedResult<String> first = signedPage(markers, 1, null, "account:region:");
assertThat(first.items(), contains("a"));
assertEquals("account:region:a", markers.cursorOf(first.nextToken()));
PaginatedResult<String> second = signedPage(markers, 2, first.nextToken(), "account:region:");
assertThat(second.items(), contains("b", "c"));
assertNull(second.nextToken());
assertSame(INVALID, assertThrows(AwsException.class,
() -> signedPage(markers, 1, first.nextToken(), "other:region:")));
assertSame(INVALID, assertThrows(AwsException.class,
() -> signedPage(new SignedMarkers(), 1, first.nextToken(), "account:region:")));
assertSame(INVALID, assertThrows(AwsException.class,
() -> signedPage(markers, 1, first.nextToken() + "x", "account:region:")));
}
@Test
void signedPaging_rejectsAnEmptyOrMalformedMarker() {
SignedMarkers markers = new SignedMarkers();
assertSame(INVALID, assertThrows(AwsException.class,
() -> signedPage(markers, 1, "", "")));
assertSame(INVALID, assertThrows(AwsException.class,
() -> signedPage(markers, 1, "!!!", "")));
}
@Test
void paging_aLimitNearIntMaxDoesNotReportAFurtherPage() {
String afterFirst = page(1, null, "ns").nextToken();
@@ -196,6 +224,11 @@ class PaginationTest {
return Pagination.paginate(ITEMS, Function.identity(), limit, token, namespace, INVALID_TOKEN);
}
private static PaginatedResult<String> signedPage(SignedMarkers markers, int limit,
String marker, String scope) {
return Pagination.paginate(ITEMS, Function.identity(), limit, marker, markers, scope, INVALID_TOKEN);
}
private static String decode(String token) {
return new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8);
}
@@ -0,0 +1,109 @@
package io.github.hectorvent.floci.services.lambda;
import io.github.hectorvent.floci.core.common.RequestScopes;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.specification.RequestSpecification;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
@QuarkusTest
class LambdaListFunctionsPaginationIntegrationTest {
private static final String ACCOUNT = "000000006652";
private static final String DEFAULT_ACCOUNT = "000000000000";
private static final String REGION = "us-east-1";
private static final String AUTHORIZATION =
"AWS4-HMAC-SHA256 Credential=" + ACCOUNT + "/20260101/us-east-1/lambda/aws4_request";
@Inject LambdaService service;
@Test
void listFunctionsHonorsMaxItemsAndMarker() {
List<String> names = new ArrayList<>();
String suffix = UUID.randomUUID().toString();
String unrelatedName = "000-page-" + suffix;
boolean unrelatedCreated = false;
try {
RequestScopes.runAs(DEFAULT_ACCOUNT, REGION, () -> service.createFunction(REGION, Map.of(
"FunctionName", unrelatedName,
"Runtime", "nodejs20.x",
"Role", "arn:aws:iam::000000000000:role/lambda-role",
"Handler", "index.handler")));
unrelatedCreated = true;
for (String letter : List.of("b", "a", "c")) {
String name = "issue-page-" + suffix + "-" + letter;
lambdaRequest().contentType("application/json")
.body("""
{"FunctionName":"%s","Runtime":"nodejs20.x",
"Role":"arn:aws:iam::%s:role/lambda-role",
"Handler":"index.handler"}
""".formatted(name, ACCOUNT))
.post("/2015-03-31/functions").then().statusCode(201);
names.add(name);
}
String firstMarker = lambdaRequest().queryParam("MaxItems", 1)
.get("/2015-03-31/functions").then().statusCode(200)
.body("Functions.FunctionName", contains(names.get(1)))
.body("NextMarker", notNullValue())
.extract().path("NextMarker");
String secondMarker = lambdaRequest().queryParam("MaxItems", 1).queryParam("Marker", firstMarker)
.get("/2015-03-31/functions").then().statusCode(200)
.body("Functions.FunctionName", contains(names.get(0)))
.body("NextMarker", notNullValue())
.extract().path("NextMarker");
lambdaRequest().queryParam("MaxItems", 1).queryParam("Marker", secondMarker)
.get("/2015-03-31/functions").then().statusCode(200)
.body("Functions.FunctionName", contains(names.get(2)))
.body("$", not(hasKey("NextMarker")));
} finally {
for (String name : names) {
RequestScopes.runAs(ACCOUNT, REGION, () -> service.deleteFunction(REGION, name));
}
if (unrelatedCreated) {
RequestScopes.runAs(DEFAULT_ACCOUNT, REGION,
() -> service.deleteFunction(REGION, unrelatedName));
}
}
}
@Test
void listFunctionsRejectsAnUnknownMarker() {
lambdaRequest().queryParam("Marker", "not-an-issued-marker")
.get("/2015-03-31/functions").then().statusCode(400)
.body("__type", equalTo("InvalidParameterValueException"))
.body("message", equalTo("Invalid pagination key."));
}
@Test
void listFunctionsRejectsOutOfRangeMaxItems() {
for (int maxItems : List.of(0, 10001)) {
lambdaRequest().queryParam("MaxItems", maxItems)
.get("/2015-03-31/functions").then().statusCode(400)
.body("__type", equalTo("ValidationException"));
}
}
@Test
void listFunctionsReportsSerializationFailureBeforeMarkerValidation() {
lambdaRequest().queryParam("MaxItems", "abc").queryParam("Marker", "not-an-issued-marker")
.get("/2015-03-31/functions").then().statusCode(400)
.body("__type", equalTo("SerializationException"))
.body("message", equalTo("'abc' can not be converted to Integer"));
}
private static RequestSpecification lambdaRequest() {
return given().header("Authorization", AUTHORIZATION);
}
}
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.lambda;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.config.EmulatorConfig;
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.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
@@ -514,6 +515,29 @@ class LambdaServiceTest {
assertTrue(functions.stream().anyMatch(f -> f.getFunctionName().equals("fn-2")));
}
@Test
void listFunctionsCapsPagesAtFiftyAndScopesMarkersToRegion() {
for (int i = 51; i >= 0; i--) {
service.createFunction(REGION, baseRequest("page-fn-%03d".formatted(i)));
}
service.createFunction("eu-west-1", baseRequest("other-region-fn"));
PaginatedResult<LambdaFunction> first = service.listFunctions(REGION, "10000", null);
assertEquals(50, first.items().size());
assertEquals("page-fn-000", first.items().getFirst().getFunctionName());
assertEquals("page-fn-049", first.items().getLast().getFunctionName());
assertNotNull(first.nextToken());
PaginatedResult<LambdaFunction> second = service.listFunctions(REGION, "10000", first.nextToken());
assertEquals(List.of("page-fn-050", "page-fn-051"),
second.items().stream().map(LambdaFunction::getFunctionName).toList());
assertNull(second.nextToken());
AwsException invalid = assertThrows(AwsException.class,
() -> service.listFunctions("eu-west-1", "1", first.nextToken()));
assertEquals("InvalidParameterValueException", invalid.getErrorCode());
}
@Test
void deleteFunctionRemovesIt() {
service.createFunction(REGION, baseRequest("del-fn"));