mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(codebuild): paginate ListBuilds and ListBuildsForProject (#6707)
* fix(codebuild): paginate build list operations * fix(codebuild): reject missing project name on paged build listing * refactor(codebuild): share pagination cursor handling
This commit is contained in:
1 parent
989f714c21
commit
fc5598c579
6 files changed
+457
-47
No files matched your search
+200
@@ -0,0 +1,200 @@
|
||||
package io.github.hectorvent.floci.services.codebuild;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
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.services.codebuild.model.ProjectArtifacts;
|
||||
import io.github.hectorvent.floci.services.codebuild.model.ProjectEnvironment;
|
||||
import io.github.hectorvent.floci.services.codebuild.model.ProjectSource;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
class CodeBuildBuildListPaginationTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String PROJECT = "pagination-builds";
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private final CodeBuildService service = new CodeBuildService(
|
||||
mock(CodeBuildRunner.class), mock(EmulatorConfig.class), null);
|
||||
private final CodeBuildJsonHandler handler = new CodeBuildJsonHandler(service, mapper);
|
||||
|
||||
@BeforeEach
|
||||
void createProject() {
|
||||
ProjectSource source = new ProjectSource();
|
||||
source.setType("NO_SOURCE");
|
||||
ProjectArtifacts artifacts = new ProjectArtifacts();
|
||||
artifacts.setType("NO_ARTIFACTS");
|
||||
service.createProject(REGION, ACCOUNT, PROJECT, null, source, null, null,
|
||||
artifacts, null, new ProjectEnvironment(), "arn:aws:iam::000000000000:role/codebuild",
|
||||
null, null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void listBuildsReturnsOneHundredIdsAndContinuation() throws Exception {
|
||||
createBuilds(101);
|
||||
JsonNode first = list("ListBuilds", Map.of());
|
||||
assertEquals(100, ids(first).size());
|
||||
assertTrue(first.path("nextToken").isTextual());
|
||||
|
||||
JsonNode last = list("ListBuilds", Map.of("nextToken", first.path("nextToken").asText()));
|
||||
assertEquals(1, ids(last).size());
|
||||
assertFalse(last.has("nextToken"));
|
||||
List<String> seen = new ArrayList<>(ids(first));
|
||||
seen.addAll(ids(last));
|
||||
assertEquals(IntStream.rangeClosed(1, 101).mapToObj(number -> PROJECT + ":" + number)
|
||||
.sorted(Comparator.reverseOrder()).toList(), seen);
|
||||
}
|
||||
|
||||
@Test
|
||||
void listBuildsForProjectReturnsOneHundredIdsAndContinuation() throws Exception {
|
||||
createBuilds(101);
|
||||
JsonNode first = list("ListBuildsForProject", Map.of("projectName", PROJECT));
|
||||
assertEquals(100, ids(first).size());
|
||||
assertTrue(first.path("nextToken").isTextual());
|
||||
|
||||
JsonNode last = list("ListBuildsForProject", Map.of(
|
||||
"projectName", PROJECT, "nextToken", first.path("nextToken").asText()));
|
||||
assertEquals(List.of(PROJECT + ":1"), ids(last));
|
||||
assertFalse(last.has("nextToken"));
|
||||
List<String> seen = new ArrayList<>(ids(first));
|
||||
seen.addAll(ids(last));
|
||||
assertEquals(IntStream.rangeClosed(1, 101).mapToObj(number -> PROJECT + ":" + number)
|
||||
.toList().reversed(), seen);
|
||||
}
|
||||
|
||||
@Test
|
||||
void exactPageOmitsContinuationAndAllowsProjectSortOrder() throws Exception {
|
||||
createBuilds(100);
|
||||
JsonNode allBuilds = list("ListBuilds", Map.of());
|
||||
assertEquals(100, ids(allBuilds).size());
|
||||
assertFalse(allBuilds.has("nextToken"));
|
||||
|
||||
JsonNode projectBuilds = list("ListBuildsForProject",
|
||||
Map.of("projectName", PROJECT, "sortOrder", "ASCENDING"));
|
||||
assertEquals(IntStream.rangeClosed(1, 100).mapToObj(number -> PROJECT + ":" + number).toList(),
|
||||
ids(projectBuilds));
|
||||
assertFalse(projectBuilds.has("nextToken"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listBuildsContinuesInAscendingBuildIdOrder() throws Exception {
|
||||
createBuilds(101);
|
||||
JsonNode first = list("ListBuilds", Map.of("sortOrder", "ASCENDING"));
|
||||
JsonNode last = list("ListBuilds", Map.of(
|
||||
"sortOrder", "ASCENDING", "nextToken", first.path("nextToken").asText()));
|
||||
List<String> seen = new ArrayList<>(ids(first));
|
||||
seen.addAll(ids(last));
|
||||
assertEquals(IntStream.rangeClosed(1, 101).mapToObj(number -> PROJECT + ":" + number)
|
||||
.sorted().toList(), seen);
|
||||
assertFalse(last.has("nextToken"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void projectBuildsContinueThroughThreePages() throws Exception {
|
||||
createBuilds(201);
|
||||
List<String> seen = new ArrayList<>();
|
||||
String token = null;
|
||||
for (int pageIndex = 0; pageIndex < 3; pageIndex++) {
|
||||
Map<String, String> request = token == null
|
||||
? Map.of("projectName", PROJECT)
|
||||
: Map.of("projectName", PROJECT, "nextToken", token);
|
||||
JsonNode page = list("ListBuildsForProject", request);
|
||||
assertEquals(pageIndex < 2 ? 100 : 1, ids(page).size());
|
||||
seen.addAll(ids(page));
|
||||
token = page.path("nextToken").isTextual() ? page.path("nextToken").asText() : null;
|
||||
}
|
||||
assertEquals(IntStream.rangeClosed(1, 201).mapToObj(number -> PROJECT + ":" + number)
|
||||
.toList().reversed(), seen);
|
||||
}
|
||||
|
||||
@Test
|
||||
void projectContinuationKeepsItsPositionWhenANewBuildStarts() throws Exception {
|
||||
createBuilds(101);
|
||||
JsonNode first = list("ListBuildsForProject", Map.of("projectName", PROJECT));
|
||||
String token = first.path("nextToken").asText();
|
||||
|
||||
createBuilds(1);
|
||||
|
||||
JsonNode last = list("ListBuildsForProject", Map.of("projectName", PROJECT, "nextToken", token));
|
||||
assertEquals(List.of(PROJECT + ":1"), ids(last));
|
||||
assertFalse(last.has("nextToken"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void projectSortOrderIsRejectedAboveOneHundredBuilds() {
|
||||
createBuilds(101);
|
||||
AwsException error = assertThrows(AwsException.class, () -> list("ListBuildsForProject",
|
||||
Map.of("projectName", PROJECT, "sortOrder", "ASCENDING")));
|
||||
assertEquals("InvalidInputException", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void projectNameIsRequiredForProjectBuildListing() {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> list("ListBuildsForProject", Map.of()));
|
||||
assertEquals("InvalidInputException", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void continuationRejectsOtherRequestScopesAndMalformedTokens() throws Exception {
|
||||
createBuilds(101);
|
||||
String globalToken = list("ListBuilds", Map.of()).path("nextToken").asText();
|
||||
String projectToken = list("ListBuildsForProject", Map.of("projectName", PROJECT))
|
||||
.path("nextToken").asText();
|
||||
assertInvalidToken("ListBuilds", Map.of("nextToken", globalToken, "sortOrder", "ASCENDING"),
|
||||
REGION, ACCOUNT);
|
||||
assertInvalidToken("ListBuilds", Map.of("nextToken", globalToken), "eu-west-1", ACCOUNT);
|
||||
assertInvalidToken("ListBuilds", Map.of("nextToken", globalToken), REGION, "111111111111");
|
||||
assertInvalidToken("ListBuilds", Map.of("nextToken", "not-a-token!"), REGION, ACCOUNT);
|
||||
assertInvalidToken("ListBuilds", Map.of("nextToken", 42), REGION, ACCOUNT);
|
||||
assertInvalidToken("ListBuildsForProject", Map.of("projectName", PROJECT, "nextToken", globalToken),
|
||||
REGION, ACCOUNT);
|
||||
assertInvalidToken("ListBuildsForProject", Map.of("projectName", "another", "nextToken", projectToken),
|
||||
REGION, ACCOUNT);
|
||||
}
|
||||
|
||||
private JsonNode list(String action, Map<String, ?> request) throws Exception {
|
||||
return list(action, request, REGION, ACCOUNT);
|
||||
}
|
||||
|
||||
private JsonNode list(String action, Map<String, ?> request, String region, String account) throws Exception {
|
||||
try (Response response = handler.handle(action, mapper.valueToTree(request), region, account)) {
|
||||
return mapper.valueToTree(response.getEntity());
|
||||
}
|
||||
}
|
||||
|
||||
private void assertInvalidToken(String action, Map<String, ?> request, String region, String account) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> list(action, request, region, account));
|
||||
assertEquals("InvalidInputException", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
}
|
||||
|
||||
private void createBuilds(int count) {
|
||||
for (int number = 0; number < count; number++) {
|
||||
service.startBuild(REGION, ACCOUNT, PROJECT, null, null, null, null, null, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> ids(JsonNode page) {
|
||||
return mapper.convertValue(page.path("ids"),
|
||||
mapper.getTypeFactory().constructCollectionType(List.class, String.class));
|
||||
}
|
||||
}
|
||||
+65
-5
@@ -7,20 +7,28 @@ import io.github.hectorvent.floci.services.codebuild.model.ProjectSource;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.json.JsonPath;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
@QuarkusTest
|
||||
class CodeBuildBuildListSortingIntegrationTest {
|
||||
@@ -84,6 +92,47 @@ class CodeBuildBuildListSortingIntegrationTest {
|
||||
assertEquals(expected.reversed(), listBuildsForProject(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildListContinuationUsesJsonWireShape() {
|
||||
String region = "eu-west-3";
|
||||
String account = String.format(Locale.ROOT, "78%010d",
|
||||
ThreadLocalRandom.current().nextLong(10_000_000_000L));
|
||||
String auth = "AWS4-HMAC-SHA256 Credential=" + account
|
||||
+ "/20261027/" + region + "/codebuild/aws4_request";
|
||||
String isolatedProject = "page-builds-" + UUID.randomUUID();
|
||||
ProjectSource source = new ProjectSource();
|
||||
source.setType("NO_SOURCE");
|
||||
ProjectArtifacts artifacts = new ProjectArtifacts();
|
||||
artifacts.setType("NO_ARTIFACTS");
|
||||
service.createProject(region, account, isolatedProject, null, source, null, null,
|
||||
artifacts, null, new ProjectEnvironment(), "arn:aws:iam::" + account + ":role/codebuild",
|
||||
null, null, null, null, null, null, null);
|
||||
try {
|
||||
for (int number = 0; number < 101; number++) {
|
||||
service.startBuild(region, account, isolatedProject, null,
|
||||
null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
JsonPath first = given().header("Authorization", auth)
|
||||
.header("X-Amz-Target", "CodeBuild_20161006.ListBuildsForProject")
|
||||
.contentType(CONTENT_TYPE).body(Map.of("projectName", isolatedProject))
|
||||
.post("/").then().statusCode(200).extract().jsonPath();
|
||||
assertEquals(100, first.getList("ids", String.class).size());
|
||||
String token = first.getString("nextToken");
|
||||
assertNotNull(token);
|
||||
|
||||
JsonPath last = given().header("Authorization", auth)
|
||||
.header("X-Amz-Target", "CodeBuild_20161006.ListBuildsForProject")
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body(Map.of("projectName", isolatedProject, "nextToken", token))
|
||||
.post("/").then().statusCode(200).extract().jsonPath();
|
||||
assertEquals(List.of(isolatedProject + ":1"), last.getList("ids", String.class));
|
||||
assertNull(last.getString("nextToken"));
|
||||
} finally {
|
||||
service.deleteProject(region, isolatedProject);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidSortOrderIsRejectedWithoutChangingBuilds() {
|
||||
List<String> allBuilds = listBuilds("DESCENDING");
|
||||
@@ -108,11 +157,22 @@ class CodeBuildBuildListSortingIntegrationTest {
|
||||
}
|
||||
|
||||
private List<String> listBuilds(String sortOrder) {
|
||||
List<String> ids = given().header("X-Amz-Target", "CodeBuild_20161006.ListBuilds")
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body(sortOrder == null ? "{}" : "{\"sortOrder\":\"" + sortOrder + "\"}")
|
||||
.post("/").then().statusCode(200)
|
||||
.extract().jsonPath().getList("ids", String.class);
|
||||
List<String> ids = new ArrayList<>();
|
||||
String token = null;
|
||||
do {
|
||||
Map<String, String> request = new HashMap<>();
|
||||
if (sortOrder != null) {
|
||||
request.put("sortOrder", sortOrder);
|
||||
}
|
||||
if (token != null) {
|
||||
request.put("nextToken", token);
|
||||
}
|
||||
JsonPath page = given().header("X-Amz-Target", "CodeBuild_20161006.ListBuilds")
|
||||
.contentType(CONTENT_TYPE).body(request)
|
||||
.post("/").then().statusCode(200).extract().jsonPath();
|
||||
ids.addAll(page.getList("ids", String.class));
|
||||
token = page.getString("nextToken");
|
||||
} while (token != null);
|
||||
return ids.stream().filter(id -> id.startsWith(projectName + ":")).toList();
|
||||
}
|
||||
|
||||
|
||||
@@ -9,10 +9,12 @@ import io.github.hectorvent.floci.services.codebuild.model.ProjectSource;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -24,6 +26,21 @@ class CodeBuildServiceTest {
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
|
||||
@Test
|
||||
void sharedPaginationResumesAfterDeletingTheDescendingAnchor() {
|
||||
CodeBuildPagination.Page<Integer> first = CodeBuildPagination.page(
|
||||
List.of(5, 4, 3, 2), Comparator.reverseOrder(), 2, null,
|
||||
Integer::valueOf, String::valueOf);
|
||||
assertEquals(List.of(5, 4), first.items());
|
||||
assertEquals("4", first.nextToken());
|
||||
|
||||
CodeBuildPagination.Page<Integer> second = CodeBuildPagination.page(
|
||||
List.of(5, 3, 2), Comparator.reverseOrder(), 2, first.nextToken(),
|
||||
Integer::valueOf, String::valueOf);
|
||||
assertEquals(List.of(3, 2), second.items());
|
||||
assertNull(second.nextToken());
|
||||
}
|
||||
|
||||
@Test
|
||||
void retryBuildRetainsOriginalBuildspecOverride() {
|
||||
CodeBuildRunner runner = mock(CodeBuildRunner.class);
|
||||
|
||||
Reference in new issue
Block a user