Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(emr): return AWS client-error codes instead of 500 InternalServerError (#1731)

Hector Ventura87749e46002d
diff --git a/src/main/java/io/github/hectorvent/floci/services/emr/EmrService.java b/src/main/java/io/github/hectorvent/floci/services/emr/EmrService.java
index 58fd0c5c48..44c953ca92 100644
--- a/src/main/java/io/github/hectorvent/floci/services/emr/EmrService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/emr/EmrService.java
@@ -50,8 +50,12 @@ public class EmrService {
// ──────────────────────────── Cluster lifecycle ────────────────────────────
public EmrCluster runJobFlow(EmrCluster cluster, String region) {
- if (cluster.getName() == null || cluster.getName().isBlank()) {
- throw new AwsException("InternalServerError", "Name is required.", 500);
+ // Only a missing Name fails AWS's framework validation: the member is REQUIRED but its
+ // constraint is len=[0..256], so an explicit empty string is accepted by real EMR.
+ if (cluster.getName() == null) {
+ throw new AwsException("ValidationException",
+ "1 validation error detected: Value null at 'name' failed to satisfy constraint: "
+ + "Member must not be null", 400);
}
String id = "j-" + randomId(13);
cluster.setId(id);
@@ -104,14 +108,17 @@ public class EmrService {
}
public void terminateJobFlows(List<String> ids) {
+ // AWS terminates the unprotected clusters in the request and then fails it with a
+ // ValidationException if any were termination protected.
+ boolean anyProtected = false;
for (String id : ids) {
EmrCluster cluster = clusterStore.get(id).orElse(null);
if (cluster == null) {
continue;
}
if (cluster.isTerminationProtected()) {
- throw new AwsException("InternalServerError",
- "Cluster " + id + " is protected from termination.", 500);
+ anyProtected = true;
+ continue;
}
if ("TERMINATED".equals(cluster.getState())) {
continue;
@@ -122,6 +129,10 @@ public class EmrService {
cluster.setEndDateTime(Instant.now());
clusterStore.put(id, cluster);
}
+ if (anyProtected) {
+ throw new AwsException("ValidationException",
+ "Could not shut down one or more job flows since they are termination protected.", 400);
+ }
}
public void setTerminationProtection(List<String> ids, boolean value) {
@@ -156,7 +167,7 @@ public class EmrService {
public List<String> addJobFlowSteps(String clusterId, List<EmrStep> steps) {
EmrCluster cluster = clusterStore.get(clusterId).orElseThrow(() -> new AwsException(
- "InternalServerError", "Cluster " + clusterId + " does not exist.", 500));
+ "InvalidRequestException", "Cluster id '" + clusterId + "' is not valid.", 400));
List<String> ids = new ArrayList<>();
for (EmrStep step : steps) {
initStep(step);
diff --git a/src/test/java/io/github/hectorvent/floci/services/emr/EmrIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/emr/EmrIntegrationTest.java
index 389b392f8f..c684d29ce6 100644
--- a/src/test/java/io/github/hectorvent/floci/services/emr/EmrIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/emr/EmrIntegrationTest.java
@@ -184,4 +184,65 @@ class EmrIntegrationTest {
.then().statusCode(400)
.body("__type", equalTo("InvalidRequestException"));
}
+
+ @Test
+ @Order(14)
+ void runJobFlowWithoutNameIsValidationException() {
+ call("RunJobFlow", "{\"ReleaseLabel\":\"emr-7.5.0\",\"Instances\":{}}")
+ .then().statusCode(400)
+ .body("__type", equalTo("ValidationException"))
+ .body("message", equalTo(
+ "1 validation error detected: Value null at 'name' failed to satisfy constraint: "
+ + "Member must not be null"));
+ }
+
+ @Test
+ @Order(15)
+ void runJobFlowWithEmptyNameIsAccepted() {
+ // The Name member is REQUIRED but its model constraint is len=[0..256], so real EMR
+ // accepts an explicit empty string; only a missing Name fails framework validation.
+ call("RunJobFlow", "{\"Name\":\"\",\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true}}")
+ .then().statusCode(200)
+ .body("JobFlowId", startsWith("j-"));
+ }
+
+ @Test
+ @Order(16)
+ void addStepsToUnknownClusterIsInvalidRequest() {
+ call("AddJobFlowSteps",
+ "{\"JobFlowId\":\"j-DOESNOTEXIST0\",\"Steps\":[{\"Name\":\"s\","
+ + "\"HadoopJarStep\":{\"Jar\":\"command-runner.jar\"}}]}")
+ .then().statusCode(400)
+ .body("__type", equalTo("InvalidRequestException"))
+ .body("message", equalTo("Cluster id 'j-DOESNOTEXIST0' is not valid."));
+ }
+
+ @Test
+ @Order(17)
+ void terminationProtectionIsValidationExceptionAndUnprotectedStillTerminate() {
+ String protectedId = call("RunJobFlow",
+ "{\"Name\":\"protected\",\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true}}")
+ .jsonPath().getString("JobFlowId");
+ String unprotectedId = call("RunJobFlow",
+ "{\"Name\":\"unprotected\",\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true}}")
+ .jsonPath().getString("JobFlowId");
+ call("SetTerminationProtection",
+ "{\"JobFlowIds\":[\"" + protectedId + "\"],\"TerminationProtected\":true}")
+ .then().statusCode(200);
+
+ // AWS terminates the unprotected clusters in the request and then fails it.
+ call("TerminateJobFlows",
+ "{\"JobFlowIds\":[\"" + unprotectedId + "\",\"" + protectedId + "\"]}")
+ .then().statusCode(400)
+ .body("__type", equalTo("ValidationException"))
+ .body("message", equalTo(
+ "Could not shut down one or more job flows since they are termination protected."));
+
+ call("DescribeCluster", "{\"ClusterId\":\"" + unprotectedId + "\"}")
+ .then().statusCode(200)
+ .body("Cluster.Status.State", equalTo("TERMINATED"));
+ call("DescribeCluster", "{\"ClusterId\":\"" + protectedId + "\"}")
+ .then().statusCode(200)
+ .body("Cluster.Status.State", equalTo("WAITING"));
+ }
}