mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(emr-serverless): tag job runs and report their attempts (#6297)
TagResource, UntagResource and ListTagsForResource now take job run ARNs
(.../applications/<id>/jobruns/<id>), which the model's ResourceArn
allows ("the supported resources are Amazon EMR Serverless applications
and job runs"). They work on the tags StartJobRun stores, so GetJobRun
reports the current set, under the same rules as an application's: at
most 50 tags, no "aws:" prefix. An ARN must be the run's own, so one of
another application or account names nothing, and once an application
is deleted its runs' ARNs name nothing, as its own ARN already does. A
session ARN, which the pattern also allows, now answers
ResourceNotFoundException rather than ValidationException, as sessions
are not emulated.
ListJobRunAttempts (GET .../jobruns/{jobRunId}/attempts) and GetJobRun's
attempt parameter report attempts. Nothing runs, so no run fails and
none is retried: every run is one attempt, number 1, created and updated
with the run. GetJobRun, ListJobRuns and ListJobRunAttempts carry it.
GetJobRun with attempt 1 or none returns the run; another attempt
answers ResourceNotFoundException, in Floci's words, as no source shows
AWS's answer for an attempt a run never made, and leaves the run as it
was.
Also from the #6202 review:
- From release emr-7.11.0 a BATCH run's executionTimeoutMinutes must be
1 to 10080. The model: "the maximum value is 10080 minutes (7 days)
starting with Amazon EMR release 7.11. Setting a value of 0 to disable
the timeout is no longer supported for BATCH mode job runs." Earlier
releases and STREAMING runs keep 0 to 1,000,000. No source gives the
error; ValidationException, in Floci's words. The check runs before
auto-start, under the application's lock, so a refused run leaves the
application as it was.
- ListJobRuns refuses a state or mode that does not exist and more than
8 states, and ListApplications a state that does not exist and more
than 7, with the framework ValidationException. A typo listed nothing
before. ListApplications' maxResults outside 1 to 50 now answers the
same framework message, joined with any states violation.
- EmrServerlessRegionIntegrationTest uses EmrServerlessRequests, which
gains tagsPath, and pins a job run ARN across Regions;
EmrServerlessTagIntegrationTest moves onto the same helpers.
The enum and range violations are now SmithyValidationErrors.notInEnum,
notAllInEnum and outOfRange, which EMR Serverless uses.
Part of #6046 (part 9).
This commit is contained in:
1 parent
581cd62790
commit
63d577a245
18 files changed
+887
-137
No files matched your search
+31
@@ -17,6 +17,7 @@ import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
@@ -338,4 +339,34 @@ public class EmrServerlessIntegrationTest {
|
||||
assertEquals(0, createdAt.compareTo(updatedAt), "updatedAt " + updatedAt + " differs from createdAt " + createdAt);
|
||||
assertEquals(0, createdAt.compareTo(listed), "ListApplications reports createdAt " + listed);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void listApplicationsRefusesStatesThatDoNotExistAndPagesOutsideOneToFifty() {
|
||||
givenReq()
|
||||
.queryParam("states", "RUNNING")
|
||||
.queryParam("maxResults", 51)
|
||||
.when()
|
||||
.get("/applications")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", equalTo("2 validation errors detected: "
|
||||
+ "Value '51' at 'maxResults' failed to satisfy constraint: "
|
||||
+ "Member must have value less than or equal to 50; "
|
||||
+ "Value '[RUNNING]' at 'states' failed to satisfy constraint: Member must satisfy constraint: "
|
||||
+ "[Member must satisfy enum value set: "
|
||||
+ "[CREATING, CREATED, STARTING, STARTED, STOPPING, STOPPED, TERMINATED]]"));
|
||||
|
||||
givenReq()
|
||||
.queryParam("states", List.of("CREATING", "CREATED", "STARTING", "STARTED", "STOPPING", "STOPPED",
|
||||
"TERMINATED", "CREATED"))
|
||||
.when()
|
||||
.get("/applications")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("1 validation error detected: "
|
||||
+ "Value '[CREATING, CREATED, STARTING, STARTED, STOPPING, STOPPED, TERMINATED, CREATED]' "
|
||||
+ "at 'states' failed to satisfy constraint: Member must have length less than or equal to 7"));
|
||||
}
|
||||
}
|
||||
+144
-2
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.config.JsonConfig;
|
||||
import io.restassured.config.RestAssuredConfig;
|
||||
import io.restassured.path.json.JsonPath;
|
||||
import io.restassured.path.json.config.JsonPathConfig;
|
||||
import io.restassured.response.Response;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
@@ -44,10 +45,14 @@ class EmrServerlessJobRunIntegrationTest {
|
||||
}
|
||||
|
||||
private static String createApplication(String extra) {
|
||||
return createApplicationOn("emr-7.5.0", extra);
|
||||
}
|
||||
|
||||
private static String createApplicationOn(String releaseLabel, String extra) {
|
||||
return givenReq()
|
||||
.body("""
|
||||
{"name": "jobs-%s", "releaseLabel": "emr-7.5.0", "type": "SPARK", "clientToken": "%s"%s}
|
||||
""".formatted(UUID.randomUUID(), UUID.randomUUID(), extra))
|
||||
{"name": "jobs-%s", "releaseLabel": "%s", "type": "SPARK", "clientToken": "%s"%s}
|
||||
""".formatted(UUID.randomUUID(), releaseLabel, UUID.randomUUID(), extra))
|
||||
.when().post("/applications")
|
||||
.then().statusCode(200)
|
||||
.extract().path("applicationId");
|
||||
@@ -115,6 +120,9 @@ class EmrServerlessJobRunIntegrationTest {
|
||||
.body("jobRun.totalExecutionDurationSeconds", equalTo(0))
|
||||
.body("jobRun.startedAt", instanceOf(Number.class))
|
||||
.body("jobRun.endedAt", instanceOf(Number.class))
|
||||
.body("jobRun.attempt", equalTo(1))
|
||||
.body("jobRun.attemptCreatedAt", instanceOf(Number.class))
|
||||
.body("jobRun.attemptUpdatedAt", instanceOf(Number.class))
|
||||
.body("jobRun", not(hasKey("hive")));
|
||||
}
|
||||
|
||||
@@ -369,6 +377,9 @@ class EmrServerlessJobRunIntegrationTest {
|
||||
.body("jobRuns[0].type", equalTo("SPARK"))
|
||||
.body("jobRuns[0].releaseLabel", equalTo("emr-7.5.0"))
|
||||
.body("jobRuns[0].createdBy", equalTo("arn:aws:iam::000000000000:root"))
|
||||
.body("jobRuns[0].attempt", equalTo(1))
|
||||
.body("jobRuns[0].attemptCreatedAt", instanceOf(Number.class))
|
||||
.body("jobRuns[0].attemptUpdatedAt", instanceOf(Number.class))
|
||||
.body("jobRuns[0]", not(hasKey("jobRunId")))
|
||||
.body("jobRuns[0]", not(hasKey("jobDriver")));
|
||||
}
|
||||
@@ -465,6 +476,29 @@ class EmrServerlessJobRunIntegrationTest {
|
||||
.body("jobRuns.id", contains(streaming, batch));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listJobRunsRefusesStatesAndModesThatDoNotExist() {
|
||||
String base = "/applications/" + createApplication("") + "/jobruns";
|
||||
|
||||
givenReq().queryParam("states", List.of("RUNNING", "SUCCEEDED")).queryParam("mode", "batch")
|
||||
.when().get(base)
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", equalTo("2 validation errors detected: "
|
||||
+ "Value '[RUNNING, SUCCEEDED]' at 'states' failed to satisfy constraint: Member must "
|
||||
+ "satisfy constraint: [Member must satisfy enum value set: [SUBMITTED, PENDING, SCHEDULED, "
|
||||
+ "RUNNING, SUCCESS, FAILED, CANCELLING, CANCELLED, QUEUED]]; "
|
||||
+ "Value 'batch' at 'mode' failed to satisfy constraint: Member must satisfy enum value set: "
|
||||
+ "[BATCH, STREAMING]"));
|
||||
givenReq().queryParam("states", List.of("SUBMITTED", "PENDING", "SCHEDULED", "RUNNING", "SUCCESS", "FAILED",
|
||||
"CANCELLING", "CANCELLED", "QUEUED"))
|
||||
.when().get(base)
|
||||
.then().statusCode(400)
|
||||
.body("message", equalTo("1 validation error detected: Value '[SUBMITTED, PENDING, SCHEDULED, "
|
||||
+ "RUNNING, SUCCESS, FAILED, CANCELLING, CANCELLED, QUEUED]' at 'states' failed to satisfy "
|
||||
+ "constraint: Member must have length less than or equal to 8"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listJobRunsOfAnUnknownApplicationIsEmpty() {
|
||||
givenReq().when().get("/applications/00missing0000000/jobruns")
|
||||
@@ -472,6 +506,114 @@ class EmrServerlessJobRunIntegrationTest {
|
||||
.body("jobRuns", empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromRelease711ABatchRunsTimeoutIsOneToTenThousandEightyMinutes() {
|
||||
String current = createApplicationOn("emr-7.11.0", "");
|
||||
for (String timeout : List.of(", \"executionTimeoutMinutes\": 0",
|
||||
", \"executionTimeoutMinutes\": 10081", ", \"mode\": \"BATCH\", \"executionTimeoutMinutes\": 0")) {
|
||||
startJobRun(current, sparkRun(UUID.randomUUID().toString(), timeout))
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", equalTo("executionTimeoutMinutes must be between 1 and 10080 for a BATCH job "
|
||||
+ "run on release emr-7.11.0 or later."));
|
||||
}
|
||||
givenReq().when().get("/applications/" + current)
|
||||
.then().body("application.state", equalTo("CREATED"));
|
||||
|
||||
startJobRun(current, sparkRun(UUID.randomUUID().toString(), ", \"executionTimeoutMinutes\": 10080"))
|
||||
.then().statusCode(200);
|
||||
startJobRun(current, sparkRun(UUID.randomUUID().toString(),
|
||||
", \"mode\": \"STREAMING\", \"executionTimeoutMinutes\": 0"))
|
||||
.then().statusCode(200);
|
||||
String earlier = createApplicationOn("emr-7.10.0", "");
|
||||
for (String timeout : List.of(", \"executionTimeoutMinutes\": 0", ", \"executionTimeoutMinutes\": 1000000")) {
|
||||
startJobRun(earlier, sparkRun(UUID.randomUUID().toString(), timeout)).then().statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRunIsItsOneAttempt() {
|
||||
String applicationId = createApplication("");
|
||||
String path = "/applications/" + applicationId + "/jobruns/"
|
||||
+ startedRunId(applicationId, sparkRun(UUID.randomUUID().toString(), ""));
|
||||
JsonPath run = givenReq().config(DECIMAL_NUMBERS).when().get(path)
|
||||
.then().statusCode(200).extract().jsonPath();
|
||||
|
||||
assertEquals(1, run.getInt("jobRun.attempt"));
|
||||
assertEquals(run.<BigDecimal>get("jobRun.createdAt"), run.<BigDecimal>get("jobRun.attemptCreatedAt"));
|
||||
assertEquals(run.<BigDecimal>get("jobRun.updatedAt"), run.<BigDecimal>get("jobRun.attemptUpdatedAt"));
|
||||
|
||||
givenReq().queryParam("attempt", 1).when().get(path)
|
||||
.then().statusCode(200).body("jobRun.attempt", equalTo(1));
|
||||
givenReq().queryParam("attempt", 2).when().get(path)
|
||||
.then().statusCode(404)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
givenReq().queryParam("attempt", 0).when().get(path)
|
||||
.then().statusCode(400)
|
||||
.body("message", equalTo("1 validation error detected: Value '0' at 'attempt' failed to satisfy "
|
||||
+ "constraint: Member must have value greater than or equal to 1"));
|
||||
givenReq().queryParam("attempt", "first").when().get(path)
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"));
|
||||
// An empty value never reaches the parameter: RESTEasy Reactive binds it as null, so it reads as
|
||||
// no attempt, the latest one, as Pagination.parseMaxResults reads an empty maxResults.
|
||||
givenReq().queryParam("attempt", "").when().get(path)
|
||||
.then().statusCode(200).body("jobRun.attempt", equalTo(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listJobRunAttemptsReportsTheRunsOneAttempt() {
|
||||
String applicationId = createApplication("");
|
||||
String jobRunId = startedRunId(applicationId,
|
||||
sparkRun(UUID.randomUUID().toString(), ", \"name\": \"nightly\", \"mode\": \"BATCH\""));
|
||||
String path = "/applications/" + applicationId + "/jobruns/" + jobRunId;
|
||||
JsonPath run = givenReq().config(DECIMAL_NUMBERS).when().get(path)
|
||||
.then().statusCode(200).extract().jsonPath();
|
||||
|
||||
JsonPath attempts = givenReq().config(DECIMAL_NUMBERS).when().get(path + "/attempts")
|
||||
.then().statusCode(200)
|
||||
.body("jobRunAttempts", hasSize(1))
|
||||
.body("$", not(hasKey("nextToken")))
|
||||
.extract().jsonPath();
|
||||
assertEquals(applicationId, attempts.getString("jobRunAttempts[0].applicationId"));
|
||||
assertEquals(jobRunId, attempts.getString("jobRunAttempts[0].id"));
|
||||
assertEquals("nightly", attempts.getString("jobRunAttempts[0].name"));
|
||||
assertEquals("BATCH", attempts.getString("jobRunAttempts[0].mode"));
|
||||
assertEquals(run.getString("jobRun.arn"), attempts.getString("jobRunAttempts[0].arn"));
|
||||
assertEquals("arn:aws:iam::000000000000:root", attempts.getString("jobRunAttempts[0].createdBy"));
|
||||
assertEquals(run.<BigDecimal>get("jobRun.createdAt"),
|
||||
attempts.<BigDecimal>get("jobRunAttempts[0].jobCreatedAt"));
|
||||
assertEquals(run.<BigDecimal>get("jobRun.createdAt"), attempts.<BigDecimal>get("jobRunAttempts[0].createdAt"));
|
||||
assertEquals(run.<BigDecimal>get("jobRun.updatedAt"), attempts.<BigDecimal>get("jobRunAttempts[0].updatedAt"));
|
||||
assertEquals(ROLE, attempts.getString("jobRunAttempts[0].executionRole"));
|
||||
assertEquals("SUCCESS", attempts.getString("jobRunAttempts[0].state"));
|
||||
assertEquals("emr-7.5.0", attempts.getString("jobRunAttempts[0].releaseLabel"));
|
||||
assertEquals("SPARK", attempts.getString("jobRunAttempts[0].type"));
|
||||
assertEquals(1, attempts.getInt("jobRunAttempts[0].attempt"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listJobRunAttemptsRefusesAPageLargerThanFiftyAndAnUnknownRun() {
|
||||
String applicationId = createApplication("");
|
||||
String jobRunId = startedRunId(applicationId, sparkRun(UUID.randomUUID().toString(), ""));
|
||||
|
||||
givenReq().queryParam("maxResults", 51)
|
||||
.when().get("/applications/" + applicationId + "/jobruns/" + jobRunId + "/attempts")
|
||||
.then().statusCode(400)
|
||||
.body("message", equalTo("1 validation error detected: Value '51' at 'maxResults' failed to "
|
||||
+ "satisfy constraint: Member must have value less than or equal to 50"));
|
||||
givenReq().when().get("/applications/" + applicationId + "/jobruns/00missing0000000/attempts")
|
||||
.then().statusCode(404)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
givenReq().when().get("/applications/00missing0000000/jobruns/" + jobRunId + "/attempts")
|
||||
.then().statusCode(404)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
givenReq().queryParam("nextToken", "bm90LWEtdG9rZW4")
|
||||
.when().get("/applications/" + applicationId + "/jobruns/" + jobRunId + "/attempts")
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void jobRunsStayInTheirRegion() {
|
||||
String applicationId = createApplication("");
|
||||
|
||||
+23
-6
@@ -100,13 +100,13 @@ class EmrServerlessJobRunServiceTest {
|
||||
Instant submittedAt = started.getCreatedAt();
|
||||
|
||||
clock.advance(Duration.ofSeconds(29));
|
||||
JobRun running = service.getJobRun(applicationId, jobRunId);
|
||||
JobRun running = service.getJobRun(applicationId, jobRunId, null);
|
||||
assertEquals("RUNNING", running.getState());
|
||||
assertNull(running.getEndedAt());
|
||||
assertNull(running.getTotalExecutionDurationSeconds());
|
||||
|
||||
clock.advance(Duration.ofSeconds(1));
|
||||
JobRun finished = service.getJobRun(applicationId, jobRunId);
|
||||
JobRun finished = service.getJobRun(applicationId, jobRunId, null);
|
||||
assertEquals("SUCCESS", finished.getState());
|
||||
assertEquals(submittedAt.plusSeconds(30), finished.getEndedAt());
|
||||
assertEquals(submittedAt.plusSeconds(30), finished.getUpdatedAt());
|
||||
@@ -124,7 +124,7 @@ class EmrServerlessJobRunServiceTest {
|
||||
assertEquals("CANCELLED", cancelled.getState());
|
||||
assertEquals(10, cancelled.getTotalExecutionDurationSeconds());
|
||||
clock.advance(Duration.ofMinutes(5));
|
||||
assertEquals("CANCELLED", service.getJobRun(applicationId, jobRunId).getState());
|
||||
assertEquals("CANCELLED", service.getJobRun(applicationId, jobRunId, null).getState());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -135,14 +135,31 @@ class EmrServerlessJobRunServiceTest {
|
||||
Instant submittedAt = started.getCreatedAt();
|
||||
|
||||
clock.advance(Duration.ofMinutes(5).minusSeconds(1));
|
||||
assertEquals("RUNNING", service.getJobRun(applicationId, jobRunId).getState());
|
||||
assertEquals("RUNNING", service.getJobRun(applicationId, jobRunId, null).getState());
|
||||
|
||||
clock.advance(Duration.ofSeconds(1));
|
||||
JobRun timedOut = service.getJobRun(applicationId, jobRunId);
|
||||
JobRun timedOut = service.getJobRun(applicationId, jobRunId, null);
|
||||
assertEquals("CANCELLED", timedOut.getState());
|
||||
assertEquals(submittedAt.plus(Duration.ofMinutes(5)), timedOut.getEndedAt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAttemptTheRunNeverMadeIsRefusedBeforeTheRunSettles() {
|
||||
EmrServerlessJobRunService service = service(30);
|
||||
JobRun started = service.startJobRun(applicationId, request(null, null));
|
||||
String jobRunId = started.getJobRunId();
|
||||
clock.advance(Duration.ofSeconds(30));
|
||||
|
||||
AwsException noAttempt = assertThrows(AwsException.class,
|
||||
() -> service.getJobRun(applicationId, jobRunId, 2));
|
||||
assertEquals("ResourceNotFoundException", noAttempt.getErrorCode());
|
||||
assertEquals("RUNNING", store.get(jobRunId).orElseThrow().getJobRun().getState());
|
||||
|
||||
JobRun finished = service.getJobRun(applicationId, jobRunId, 1);
|
||||
assertEquals("SUCCESS", finished.getState());
|
||||
assertEquals(started.getCreatedAt().plusSeconds(30), finished.getEndedAt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void streamingRunIgnoresTheRunDuration() {
|
||||
EmrServerlessJobRunService service = service(0);
|
||||
@@ -150,7 +167,7 @@ class EmrServerlessJobRunServiceTest {
|
||||
|
||||
clock.advance(Duration.ofDays(2));
|
||||
|
||||
assertEquals("RUNNING", service.getJobRun(applicationId, jobRunId).getState());
|
||||
assertEquals("RUNNING", service.getJobRun(applicationId, jobRunId, null).getState());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+28
-12
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.emrserverless;
|
||||
|
||||
import io.github.hectorvent.floci.testing.EmrServerlessRequests;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
@@ -7,11 +8,9 @@ import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static io.github.hectorvent.floci.testing.EmrServerlessRequests.tagsPath;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
@@ -26,15 +25,7 @@ class EmrServerlessRegionIntegrationTest {
|
||||
}
|
||||
|
||||
private static RequestSpecification emr(String region) {
|
||||
return given()
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=TEST/20261020/" + region
|
||||
+ "/emr-serverless/aws4_request, SignedHeaders=host;x-amz-date, Signature=test")
|
||||
.contentType("application/json")
|
||||
.urlEncodingEnabled(false);
|
||||
}
|
||||
|
||||
private static String tagsPath(String arn) {
|
||||
return "/tags/" + URLEncoder.encode(arn, StandardCharsets.UTF_8);
|
||||
return EmrServerlessRequests.emrServerless(region).urlEncodingEnabled(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -89,4 +80,29 @@ class EmrServerlessRegionIntegrationTest {
|
||||
emr("us-east-1").when().get(tagsPath(eastArn))
|
||||
.then().statusCode(200).body("tags.team", equalTo("data"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void jobRunTagsStayInTheRunsRegion() {
|
||||
String applicationId = emr("us-east-1").body("""
|
||||
{"name":"regional-runs","releaseLabel":"emr-7.5.0","type":"SPARK","clientToken":"%s"}
|
||||
""".formatted(UUID.randomUUID()))
|
||||
.when().post("/applications")
|
||||
.then().statusCode(200).extract().path("applicationId");
|
||||
String runArn = emr("us-east-1").body("""
|
||||
{"clientToken":"%s","executionRoleArn":"arn:aws:iam::000000000000:role/emr-serverless-job",
|
||||
"jobDriver":{"sparkSubmit":{"entryPoint":"s3://bucket/jobs/app.jar"}},"tags":{"team":"data"}}
|
||||
""".formatted(UUID.randomUUID()))
|
||||
.when().post("/applications/" + applicationId + "/jobruns")
|
||||
.then().statusCode(200).extract().path("arn");
|
||||
|
||||
emr("eu-west-1").when().get(tagsPath(runArn))
|
||||
.then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));
|
||||
emr("eu-west-1").body("{\"tags\":{\"team\":\"wrong-region\"}}")
|
||||
.when().post(tagsPath(runArn))
|
||||
.then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));
|
||||
emr("eu-west-1").when().delete(tagsPath(runArn) + "?tagKeys=team")
|
||||
.then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));
|
||||
emr("us-east-1").when().get(tagsPath(runArn))
|
||||
.then().statusCode(200).body("tags.team", equalTo("data"));
|
||||
}
|
||||
}
|
||||
+138
-35
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.emrserverless;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.testing.EmrServerlessRequests;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.json.JsonPath;
|
||||
@@ -21,7 +22,7 @@ import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static io.github.hectorvent.floci.testing.EmrServerlessRequests.tagsPath;
|
||||
import static org.hamcrest.Matchers.aMapWithSize;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasKey;
|
||||
@@ -41,16 +42,28 @@ class EmrServerlessTagIntegrationTest {
|
||||
}
|
||||
|
||||
private static RequestSpecification emr() {
|
||||
return given()
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=TEST/20260929/us-east-1/emr-serverless/aws4_request, "
|
||||
+ "SignedHeaders=host;x-amz-date, Signature=test")
|
||||
.contentType("application/json")
|
||||
.urlEncodingEnabled(false);
|
||||
return EmrServerlessRequests.emrServerless("us-east-1").urlEncodingEnabled(false);
|
||||
}
|
||||
|
||||
/** The SDK sends the ARN as one path segment with ':' and '/' percent-encoded. */
|
||||
private static String tagsPath(String arn) {
|
||||
return "/tags/" + URLEncoder.encode(arn, StandardCharsets.UTF_8);
|
||||
/** A tags map of {@code count} distinct keys, as JSON. */
|
||||
private static String tagMap(int count) {
|
||||
StringBuilder tags = new StringBuilder("{");
|
||||
for (int i = 0; i < count; i++) {
|
||||
tags.append(i == 0 ? "" : ",").append("\"k").append(i).append("\": \"v\"");
|
||||
}
|
||||
return tags.append("}").toString();
|
||||
}
|
||||
|
||||
private static String[] startJobRun(String applicationId, String tagsJson) {
|
||||
JsonPath started = emr()
|
||||
.body("""
|
||||
{ "clientToken": "%s", "executionRoleArn": "arn:aws:iam::000000000000:role/emr-serverless-job",
|
||||
"jobDriver": { "sparkSubmit": { "entryPoint": "s3://bucket/jobs/app.jar" } }, "tags": %s }
|
||||
""".formatted(UUID.randomUUID(), tagsJson))
|
||||
.when().post("/applications/" + applicationId + "/jobruns")
|
||||
.then().statusCode(200)
|
||||
.extract().jsonPath();
|
||||
return new String[] {started.getString("jobRunId"), started.getString("arn")};
|
||||
}
|
||||
|
||||
private static String[] createApplication(String tagsJson) {
|
||||
@@ -138,12 +151,7 @@ class EmrServerlessTagIntegrationTest {
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"));
|
||||
|
||||
StringBuilder tooMany = new StringBuilder("{ \"tags\": {");
|
||||
for (int i = 0; i < 51; i++) {
|
||||
tooMany.append(i == 0 ? "" : ",").append("\"k").append(i).append("\": \"v\"");
|
||||
}
|
||||
tooMany.append("} }");
|
||||
emr().body(tooMany.toString())
|
||||
emr().body("{ \"tags\": " + tagMap(51) + " }")
|
||||
.when().post(tagsPath(arn))
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"));
|
||||
@@ -170,12 +178,7 @@ class EmrServerlessTagIntegrationTest {
|
||||
|
||||
@Test
|
||||
void createApplicationAppliesTheSameTagRules() {
|
||||
StringBuilder tooMany = new StringBuilder("{");
|
||||
for (int i = 0; i < 51; i++) {
|
||||
tooMany.append(i == 0 ? "" : ",").append("\"k").append(i).append("\": \"v\"");
|
||||
}
|
||||
tooMany.append("}");
|
||||
for (String tags : List.of(tooMany.toString(), "{ \"bad#key\": \"v\" }")) {
|
||||
for (String tags : List.of(tagMap(51), "{ \"bad#key\": \"v\" }")) {
|
||||
emr().body("""
|
||||
{ "name": "limits", "releaseLabel": "emr-7.5.0", "type": "SPARK",
|
||||
"clientToken": "%s", "tags": %s }
|
||||
@@ -193,13 +196,8 @@ class EmrServerlessTagIntegrationTest {
|
||||
@Test
|
||||
void theUserGuideLimitsApplyAndAnEmptyMapIsANoOp() {
|
||||
String arn = createApplication("{ \"team\": \"data\" }")[1];
|
||||
StringBuilder fortyNine = new StringBuilder("{ \"tags\": {");
|
||||
for (int i = 0; i < 49; i++) {
|
||||
fortyNine.append(i == 0 ? "" : ",").append("\"k").append(i).append("\": \"v\"");
|
||||
}
|
||||
fortyNine.append("} }");
|
||||
|
||||
emr().body(fortyNine.toString()).when().post(tagsPath(arn)).then().statusCode(200);
|
||||
emr().body("{ \"tags\": " + tagMap(49) + " }").when().post(tagsPath(arn)).then().statusCode(200);
|
||||
emr().when().get(tagsPath(arn)).then().statusCode(200).body("tags", aMapWithSize(50));
|
||||
emr().body("{ \"tags\": { \"one-more\": \"v\" } }")
|
||||
.when().post(tagsPath(arn))
|
||||
@@ -221,7 +219,7 @@ class EmrServerlessTagIntegrationTest {
|
||||
/** Tag writes read, change and store the whole application; concurrent ones must not lose each other. */
|
||||
@Test
|
||||
void concurrentTagWritesAreAllKept() throws Exception {
|
||||
String arn = createApplication("{}")[1];
|
||||
EmrServerlessService.ResourceArn resource = service.resourceArn(createApplication("{}")[1]);
|
||||
int threads = 16;
|
||||
CountDownLatch go = new CountDownLatch(1);
|
||||
ExecutorService pool = Executors.newFixedThreadPool(threads);
|
||||
@@ -231,7 +229,7 @@ class EmrServerlessTagIntegrationTest {
|
||||
String key = "k" + i;
|
||||
futures.add(pool.submit(() -> {
|
||||
go.await();
|
||||
service.tagResource(arn, Map.of(key, "v"));
|
||||
service.tagResource(resource, Map.of(key, "v"));
|
||||
return null;
|
||||
}));
|
||||
}
|
||||
@@ -243,20 +241,125 @@ class EmrServerlessTagIntegrationTest {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
|
||||
assertEquals(threads, service.listTags(arn).size());
|
||||
assertEquals(threads, service.listTags(resource).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anEmptyTagMapChangesNothingButMissingTagsOrKeysAreRefused() {
|
||||
String arn = createApplication("{ \"team\": \"data\" }")[1];
|
||||
EmrServerlessService.ResourceArn resource = service.resourceArn(createApplication("{ \"team\": \"data\" }")[1]);
|
||||
|
||||
service.tagResource(arn, Map.of());
|
||||
assertEquals(Map.of("team", "data"), service.listTags(arn));
|
||||
service.tagResource(resource, Map.of());
|
||||
assertEquals(Map.of("team", "data"), service.listTags(resource));
|
||||
assertEquals("ValidationException",
|
||||
assertThrows(AwsException.class,
|
||||
() -> service.tagResource(arn, null)).getErrorCode());
|
||||
() -> service.tagResource(resource, null)).getErrorCode());
|
||||
assertEquals("ValidationException",
|
||||
assertThrows(AwsException.class,
|
||||
() -> service.untagResource(arn, List.of())).getErrorCode());
|
||||
() -> service.untagResource(resource, List.of())).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagsRoundTripOnAJobRunArnAndShowInGetJobRun() {
|
||||
String[] app = createApplication("{ \"team\": \"app\" }");
|
||||
String[] run = startJobRun(app[0], "{ \"team\": \"data\" }");
|
||||
String arn = run[1];
|
||||
|
||||
emr().when().get(tagsPath(arn))
|
||||
.then().statusCode(200)
|
||||
.body("tags", aMapWithSize(1))
|
||||
.body("tags.team", equalTo("data"));
|
||||
|
||||
emr().body("{ \"tags\": { \"env\": \"dev\", \"team\": \"platform\" } }")
|
||||
.when().post(tagsPath(arn))
|
||||
.then().statusCode(200);
|
||||
emr().when().get("/applications/" + app[0] + "/jobruns/" + run[0])
|
||||
.then().statusCode(200)
|
||||
.body("jobRun.state", equalTo("SUCCESS"))
|
||||
.body("jobRun.tags.team", equalTo("platform"))
|
||||
.body("jobRun.tags.env", equalTo("dev"));
|
||||
|
||||
emr().when().delete(tagsPath(arn) + "?tagKeys=env")
|
||||
.then().statusCode(200);
|
||||
emr().when().get(tagsPath(arn))
|
||||
.then().statusCode(200)
|
||||
.body("tags", not(hasKey("env")))
|
||||
.body("tags.team", equalTo("platform"));
|
||||
emr().when().get(tagsPath(app[1]))
|
||||
.then().statusCode(200)
|
||||
.body("tags", aMapWithSize(1))
|
||||
.body("tags.team", equalTo("app"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDeletedApplicationsRunsCannotBeTagged() {
|
||||
String applicationId = createApplication("{}")[0];
|
||||
String[] run = startJobRun(applicationId, "{ \"team\": \"data\" }");
|
||||
String path = tagsPath(run[1]);
|
||||
|
||||
emr().when().post("/applications/" + applicationId + "/stop")
|
||||
.then().statusCode(200);
|
||||
emr().when().delete("/applications/" + applicationId)
|
||||
.then().statusCode(200);
|
||||
|
||||
emr().when().get(path)
|
||||
.then().statusCode(404)
|
||||
.body("__type", equalTo("ResourceNotFoundException"))
|
||||
.body("message", equalTo("Resource " + run[1] + " not found"));
|
||||
emr().body("{ \"tags\": { \"env\": \"dev\" } }")
|
||||
.when().post(path)
|
||||
.then().statusCode(404)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
emr().when().delete(path + "?tagKeys=team")
|
||||
.then().statusCode(404)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
|
||||
emr().when().get("/applications/" + applicationId + "/jobruns/" + run[0])
|
||||
.then().statusCode(200)
|
||||
.body("jobRun.tags.team", equalTo("data"))
|
||||
.body("jobRun.tags", not(hasKey("env")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aJobRunArnMustBeTheRunsOwnAndSessionArnsNameNothing() {
|
||||
String[] owner = createApplication("{}");
|
||||
String[] other = createApplication("{}");
|
||||
String[] run = startJobRun(owner[0], "{ \"team\": \"data\" }");
|
||||
|
||||
for (String arn : List.of(other[1] + "/jobruns/" + run[0],
|
||||
run[1].replace(":000000000000:", ":111111111111:"),
|
||||
owner[1] + "/sessions/00abc0000000000")) {
|
||||
emr().when().get(tagsPath(arn))
|
||||
.then().statusCode(404)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
emr().body("{ \"tags\": { \"env\": \"dev\" } }")
|
||||
.when().post(tagsPath(arn))
|
||||
.then().statusCode(404)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
}
|
||||
emr().when().get(tagsPath(run[1]))
|
||||
.then().statusCode(200)
|
||||
.body("tags", aMapWithSize(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void jobRunsKeepTheSameTagRules() {
|
||||
String applicationId = createApplication("{}")[0];
|
||||
String arn = startJobRun(applicationId, tagMap(50))[1];
|
||||
|
||||
emr().body("{ \"tags\": { \"one-more\": \"v\" } }")
|
||||
.when().post(tagsPath(arn))
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", equalTo("A resource can have at most 50 tags."));
|
||||
emr().body("{ \"tags\": { \"aws:owner\": \"x\" } }")
|
||||
.when().post(tagsPath(arn))
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"));
|
||||
emr().when().delete(tagsPath(arn) + "?tagKeys=")
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"));
|
||||
emr().when().get(tagsPath(arn))
|
||||
.then().statusCode(200)
|
||||
.body("tags", aMapWithSize(50));
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,9 @@ package io.github.hectorvent.floci.testing;
|
||||
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
|
||||
/** One EMR Serverless REST JSON request, signed for a named Region. */
|
||||
@@ -14,4 +17,12 @@ public final class EmrServerlessRequests {
|
||||
+ "/emr-serverless/aws4_request, SignedHeaders=host;x-amz-date, Signature=test")
|
||||
.contentType("application/json");
|
||||
}
|
||||
|
||||
/**
|
||||
* The tagging path for an ARN, sent as the SDK sends it: one path segment with ':' and '/'
|
||||
* percent-encoded. Send it with {@code urlEncodingEnabled(false)}, or it is encoded twice.
|
||||
*/
|
||||
public static String tagsPath(String arn) {
|
||||
return "/tags/" + URLEncoder.encode(arn, StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user