mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(kinesisanalytics): paginate ListApplicationSnapshots (#7185)
* fix(kinesisanalytics): paginate ListApplicationSnapshots * test(kinesisanalytics): cover ListApplicationSnapshots pagination and bounds * test(kinesisanalytics): pin the snapshot cursor contract in tests Assert the delete in the resume-after-deletion test, and the surviving list length, so a no-op delete cannot let the case pass vacuously. Accept a token purely as a cursor up to the modeled 512 characters, since this NextToken declares no pattern. Reuse the issued token instead of retyping the expected cursor, and assert the page-size message on the wire.
This commit is contained in:
1 parent
d2d9ed6ca2
commit
6b3f482510
4 files changed
+211
-1
No files matched your search
+7
-1
@@ -214,11 +214,17 @@ public class KinesisAnalyticsV2JsonHandler {
|
||||
|
||||
private Response handleListApplicationSnapshots(JsonNode request, String region) {
|
||||
String applicationName = request.path("ApplicationName").asText(null);
|
||||
Integer limit = Pagination.parseMaxResults(request, "Limit", "InvalidArgumentException");
|
||||
String nextToken = Pagination.parseNextToken(request, "NextToken", "InvalidArgumentException");
|
||||
PaginatedResult<Snapshot> page = service.listApplicationSnapshots(applicationName, region, limit, nextToken);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode summaries = response.putArray("SnapshotSummaries");
|
||||
for (Snapshot snapshot : service.listApplicationSnapshots(applicationName, region)) {
|
||||
for (Snapshot snapshot : page.items()) {
|
||||
summaries.add(snapshotDetailNode(snapshot));
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
response.put("NextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
|
||||
+8
@@ -561,6 +561,14 @@ public class KinesisAnalyticsV2Service {
|
||||
return List.copyOf(describeApplication(applicationName, region).getSnapshots().values());
|
||||
}
|
||||
|
||||
public PaginatedResult<Snapshot> listApplicationSnapshots(String applicationName, String region,
|
||||
Integer limit, String nextToken) {
|
||||
// AWS: ListSnapshotsInputLimit is 1 to 50 and NextToken is 1 to 512 with no pattern, so
|
||||
// unlike ListApplications the token is not checked against a resource name here.
|
||||
return Pagination.paginateUnencoded(listApplicationSnapshots(applicationName, region),
|
||||
Snapshot::getSnapshotName, limit, nextToken, 50, 50, 512, "InvalidArgumentException");
|
||||
}
|
||||
|
||||
public void deleteApplicationSnapshot(String applicationName, String snapshotName,
|
||||
Instant snapshotCreationTimestamp) {
|
||||
deleteApplicationSnapshot(applicationName, snapshotName, snapshotCreationTimestamp,
|
||||
|
||||
+82
@@ -17,6 +17,7 @@ import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
/**
|
||||
* Drives the full JSON-1.1 dispatch path (AwsJson11Controller → ResolvedServiceCatalog →
|
||||
@@ -391,6 +392,87 @@ class KinesisAnalyticsV2IntegrationTest {
|
||||
.body("SnapshotSummaries", equalTo(List.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listApplicationSnapshotsPaginatesThroughJsonProtocol() {
|
||||
String applicationName = "it-snapshot-page-" + System.nanoTime();
|
||||
// A snapshot needs a RUNNING app with a deployed job, so the app must carry code config.
|
||||
given()
|
||||
.header("X-Amz-Target", "KinesisAnalytics_20180523.CreateApplication")
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body("""
|
||||
{"ApplicationName": "%s", "RuntimeEnvironment": "FLINK-1_18",
|
||||
"ServiceExecutionRole": "%s",
|
||||
"ApplicationConfiguration": {"ApplicationCodeConfiguration": {
|
||||
"CodeContent": {"S3ContentLocation": {
|
||||
"BucketARN": "arn:aws:s3:::flink-code", "FileKey": "app.jar"}},
|
||||
"CodeContentType": "ZIPFILE"}}}
|
||||
""".formatted(applicationName, ROLE))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "KinesisAnalytics_20180523.StartApplication")
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body("{\"ApplicationName\": \"" + applicationName + "\", \"RunConfiguration\": {}}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
for (String snapshotName : List.of("snap-a", "snap-b")) {
|
||||
given()
|
||||
.header("X-Amz-Target", "KinesisAnalytics_20180523.CreateApplicationSnapshot")
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body("{\"ApplicationName\": \"" + applicationName + "\","
|
||||
+ " \"SnapshotName\": \"" + snapshotName + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
Response firstPage = given()
|
||||
.header("X-Amz-Target", "KinesisAnalytics_20180523.ListApplicationSnapshots")
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body("{\"ApplicationName\": \"" + applicationName + "\", \"Limit\": 1}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("SnapshotSummaries.size()", equalTo(1))
|
||||
.body("SnapshotSummaries[0].SnapshotName", equalTo("snap-a"))
|
||||
.extract().response();
|
||||
String token = firstPage.path("NextToken");
|
||||
assertNotNull(token);
|
||||
|
||||
Response secondPage = given()
|
||||
.header("X-Amz-Target", "KinesisAnalytics_20180523.ListApplicationSnapshots")
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body("{\"ApplicationName\": \"" + applicationName + "\", \"Limit\": 1,"
|
||||
+ " \"NextToken\": \"" + token + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("SnapshotSummaries.size()", equalTo(1))
|
||||
.body("SnapshotSummaries[0].SnapshotName", equalTo("snap-b"))
|
||||
.extract().response();
|
||||
assertNull(secondPage.path("NextToken"));
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "KinesisAnalytics_20180523.ListApplicationSnapshots")
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body("{\"ApplicationName\": \"" + applicationName + "\", \"Limit\": 60}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidArgumentException"))
|
||||
.body("message", equalTo("Page size must be between 1 and 50."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createApplicationWithSnapshotsDisabledRejectsCreateApplicationSnapshot() {
|
||||
given()
|
||||
|
||||
+114
@@ -453,6 +453,114 @@ class KinesisAnalyticsV2JsonHandlerTest {
|
||||
assertEquals(0, afterDelete.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listApplicationSnapshotsHonorsLimitAndReturnsNextToken() {
|
||||
createRunningCodedApplication("streaming");
|
||||
createSnapshot("streaming", "snap-a");
|
||||
createSnapshot("streaming", "snap-b");
|
||||
|
||||
ObjectNode firstPage = entity(handler.handle("ListApplicationSnapshots",
|
||||
MAPPER.createObjectNode().put("ApplicationName", "streaming").put("Limit", 1), REGION));
|
||||
JsonNode summaries = firstPage.get("SnapshotSummaries");
|
||||
assertEquals(1, summaries.size());
|
||||
assertEquals("snap-a", summaries.get(0).get("SnapshotName").asText());
|
||||
assertThat(firstPage.hasNonNull("NextToken"), is(true));
|
||||
assertEquals("snap-a", firstPage.get("NextToken").asText());
|
||||
|
||||
ObjectNode secondPage = entity(handler.handle("ListApplicationSnapshots",
|
||||
MAPPER.createObjectNode().put("ApplicationName", "streaming")
|
||||
.put("Limit", 1).put("NextToken", "snap-a"), REGION));
|
||||
assertEquals("snap-b",
|
||||
secondPage.get("SnapshotSummaries").get(0).get("SnapshotName").asText());
|
||||
assertThat(secondPage.has("NextToken"), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listApplicationSnapshotsResumesAfterTheNamedSnapshotIsDeleted() {
|
||||
createRunningCodedApplication("streaming");
|
||||
createSnapshot("streaming", "snap-a");
|
||||
createSnapshot("streaming", "snap-b");
|
||||
createSnapshot("streaming", "snap-c");
|
||||
|
||||
ObjectNode firstPage = entity(handler.handle("ListApplicationSnapshots",
|
||||
MAPPER.createObjectNode().put("ApplicationName", "streaming").put("Limit", 1), REGION));
|
||||
String token = firstPage.get("NextToken").asText();
|
||||
double creationTimestamp = firstPage.get("SnapshotSummaries").get(0)
|
||||
.get("SnapshotCreationTimestamp").asDouble();
|
||||
|
||||
assertThat(handler.handle("DeleteApplicationSnapshot", MAPPER.createObjectNode()
|
||||
.put("ApplicationName", "streaming").put("SnapshotName", token)
|
||||
.put("SnapshotCreationTimestamp", creationTimestamp), REGION).getStatus(), is(200));
|
||||
|
||||
ObjectNode secondPage = entity(handler.handle("ListApplicationSnapshots",
|
||||
MAPPER.createObjectNode().put("ApplicationName", "streaming")
|
||||
.put("Limit", 1).put("NextToken", token), REGION));
|
||||
assertEquals("snap-b",
|
||||
secondPage.get("SnapshotSummaries").get(0).get("SnapshotName").asText());
|
||||
|
||||
// The delete is what makes this a cursor-vs-offset proof, so confirm it really happened:
|
||||
// offset semantics would resume at snap-c once snap-a is gone.
|
||||
ObjectNode allPages = entity(handler.handle("ListApplicationSnapshots",
|
||||
MAPPER.createObjectNode().put("ApplicationName", "streaming"), REGION));
|
||||
assertEquals(2, allPages.get("SnapshotSummaries").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listApplicationSnapshotsUsesDefaultPageSizeAndEndsWithoutToken() {
|
||||
createRunningCodedApplication("streaming");
|
||||
for (int i = 0; i < 51; i++) {
|
||||
createSnapshot("streaming", String.format("snap-%02d", i));
|
||||
}
|
||||
|
||||
ObjectNode firstPage = entity(handler.handle("ListApplicationSnapshots",
|
||||
MAPPER.createObjectNode().put("ApplicationName", "streaming"), REGION));
|
||||
assertEquals(50, firstPage.get("SnapshotSummaries").size());
|
||||
assertEquals("snap-49", firstPage.get("NextToken").asText());
|
||||
|
||||
ObjectNode secondPage = entity(handler.handle("ListApplicationSnapshots",
|
||||
MAPPER.createObjectNode().put("ApplicationName", "streaming")
|
||||
.put("NextToken", firstPage.get("NextToken").asText()), REGION));
|
||||
assertEquals(1, secondPage.get("SnapshotSummaries").size());
|
||||
assertThat(secondPage.has("NextToken"), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listApplicationSnapshotsAcceptsAnyTokenWithinTheModeledLengthAsACursor() {
|
||||
createRunningCodedApplication("streaming");
|
||||
createSnapshot("streaming", "snap-a");
|
||||
|
||||
// AWS declares no pattern for this NextToken (unlike ListApplications, whose token reuses the
|
||||
// ApplicationName shape), so a token is only a cursor: any string up to 512 characters is
|
||||
// accepted, including one that names no snapshot.
|
||||
ObjectNode fromPunctuation = entity(handler.handle("ListApplicationSnapshots",
|
||||
MAPPER.createObjectNode().put("ApplicationName", "streaming").put("NextToken", "!!!"), REGION));
|
||||
assertEquals("snap-a",
|
||||
fromPunctuation.get("SnapshotSummaries").get(0).get("SnapshotName").asText());
|
||||
|
||||
ObjectNode pastTheEnd = entity(handler.handle("ListApplicationSnapshots",
|
||||
MAPPER.createObjectNode().put("ApplicationName", "streaming")
|
||||
.put("NextToken", "z".repeat(512)), REGION));
|
||||
assertEquals(0, pastTheEnd.get("SnapshotSummaries").size());
|
||||
assertThat(pastTheEnd.has("NextToken"), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listApplicationSnapshotsRejectsInvalidLimitAndToken() {
|
||||
createRunningCodedApplication("streaming");
|
||||
createSnapshot("streaming", "snap-a");
|
||||
|
||||
for (int limit : new int[]{0, 51}) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> handler.handle("ListApplicationSnapshots",
|
||||
MAPPER.createObjectNode().put("ApplicationName", "streaming").put("Limit", limit), REGION));
|
||||
assertEquals("InvalidArgumentException", error.getErrorCode());
|
||||
}
|
||||
for (String token : new String[]{"", "a".repeat(513)}) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> handler.handle("ListApplicationSnapshots",
|
||||
MAPPER.createObjectNode().put("ApplicationName", "streaming").put("NextToken", token), REGION));
|
||||
assertEquals("InvalidArgumentException", error.getErrorCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void createApplicationSnapshotFailsWhenApplicationNotRunning() {
|
||||
createApplication("demo");
|
||||
@@ -485,6 +593,12 @@ class KinesisAnalyticsV2JsonHandlerTest {
|
||||
assertThat(handler.handle("StartApplication", start, REGION).getStatus(), is(200));
|
||||
}
|
||||
|
||||
private void createSnapshot(String applicationName, String snapshotName) {
|
||||
assertThat(handler.handle("CreateApplicationSnapshot", MAPPER.createObjectNode()
|
||||
.put("ApplicationName", applicationName)
|
||||
.put("SnapshotName", snapshotName), REGION).getStatus(), is(200));
|
||||
}
|
||||
|
||||
private String describeArn(String name) {
|
||||
ObjectNode describe = MAPPER.createObjectNode();
|
||||
describe.put("ApplicationName", name);
|
||||
|
||||
Reference in new issue
Block a user