fix(emr): terminate transient clusters after steps (#6085)

* fix(emr): terminate transient clusters after steps

* fix(emr): I preserve terminal state on later requests
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-20 13:21:02 -04:00
1 parent 5aaa37284c
commit 67e4bfec1c
3 files changed
+162 -6

No files matched your search

@@ -0,0 +1,131 @@
package io.github.hectorvent.floci.services.emr;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static io.github.hectorvent.floci.testing.EmrRequests.emr;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
@QuarkusTest
class EmrTransientIntegrationTest {
@BeforeAll
static void configure() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void runJobFlowWithStepsTerminatesDespiteTerminationProtection() {
String clusterId = emr("RunJobFlow",
"{\"Name\":\"transient-initial\",\"Instances\":{\"TerminationProtected\":true},"
+ "\"Steps\":[{\"Name\":\"initial\","
+ "\"HadoopJarStep\":{\"Jar\":\"command-runner.jar\"}}]}")
.then().statusCode(200).extract().path("JobFlowId");
emr("DescribeCluster", "{\"ClusterId\":\"" + clusterId + "\"}")
.then().statusCode(200)
.body("Cluster.AutoTerminate", equalTo(true))
.body("Cluster.TerminationProtected", equalTo(true))
.body("Cluster.Status.State", equalTo("TERMINATED"))
.body("Cluster.Status.StateChangeReason.Code", equalTo("ALL_STEPS_COMPLETED"))
.body("Cluster.Status.Timeline.EndDateTime", notNullValue());
}
@Test
void addJobFlowStepsTerminatesTransientCluster() {
String clusterId = emr("RunJobFlow", "{\"Name\":\"transient-later\",\"Instances\":{}}")
.then().statusCode(200).extract().path("JobFlowId");
emr("DescribeCluster", "{\"ClusterId\":\"" + clusterId + "\"}")
.then().statusCode(200)
.body("Cluster.Status.State", equalTo("WAITING"));
emr("AddJobFlowSteps",
"{\"JobFlowId\":\"" + clusterId + "\",\"Steps\":[{\"Name\":\"later\","
+ "\"HadoopJarStep\":{\"Jar\":\"command-runner.jar\"}}]}")
.then().statusCode(200);
emr("DescribeCluster", "{\"ClusterId\":\"" + clusterId + "\"}")
.then().statusCode(200)
.body("Cluster.Status.State", equalTo("TERMINATED"))
.body("Cluster.Status.StateChangeReason.Code", equalTo("ALL_STEPS_COMPLETED"))
.body("Cluster.Status.Timeline.EndDateTime", notNullValue());
}
@Test
void keepAliveClusterWaitsAfterItsSteps() {
String clusterId = emr("RunJobFlow",
"{\"Name\":\"keep-alive\",\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true},"
+ "\"Steps\":[{\"Name\":\"initial\","
+ "\"HadoopJarStep\":{\"Jar\":\"command-runner.jar\"}}]}")
.then().statusCode(200).extract().path("JobFlowId");
emr("DescribeCluster", "{\"ClusterId\":\"" + clusterId + "\"}")
.then().statusCode(200)
.body("Cluster.AutoTerminate", equalTo(false))
.body("Cluster.Status.State", equalTo("WAITING"))
.body("Cluster.Status.Timeline.ReadyDateTime", notNullValue())
.body("Cluster.Status.Timeline.EndDateTime", nullValue());
}
@Test
void transientClusterWithoutStepsRemainsWaiting() {
String clusterId = emr("RunJobFlow", "{\"Name\":\"no-steps\",\"Instances\":{}}")
.then().statusCode(200).extract().path("JobFlowId");
emr("DescribeCluster", "{\"ClusterId\":\"" + clusterId + "\"}")
.then().statusCode(200)
.body("Cluster.AutoTerminate", equalTo(true))
.body("Cluster.Status.State", equalTo("WAITING"))
.body("Cluster.Status.Timeline.EndDateTime", nullValue());
}
@Test
void addingStepsAfterUserTerminationPreservesReasonAndEndTime() {
String clusterId = emr("RunJobFlow", "{\"Name\":\"manually-ended-transient\",\"Instances\":{}}")
.then().statusCode(200).extract().path("JobFlowId");
String clusterRequest = "{\"ClusterId\":\"" + clusterId + "\"}";
emr("TerminateJobFlows", "{\"JobFlowIds\":[\"" + clusterId + "\"]}")
.then().statusCode(200);
Object endTime = emr("DescribeCluster", clusterRequest).then().statusCode(200)
.body("Cluster.Status.StateChangeReason.Code", equalTo("USER_REQUEST"))
.extract().path("Cluster.Status.Timeline.EndDateTime");
emr("AddJobFlowSteps",
"{\"JobFlowId\":\"" + clusterId + "\",\"Steps\":[{\"Name\":\"late\","
+ "\"HadoopJarStep\":{\"Jar\":\"command-runner.jar\"}}]}")
.then().statusCode(200);
emr("ListSteps", clusterRequest).then().statusCode(200).body("Steps", hasSize(1));
emr("DescribeCluster", clusterRequest).then().statusCode(200)
.body("Cluster.Status.State", equalTo("TERMINATED"))
.body("Cluster.Status.StateChangeReason.Code", equalTo("USER_REQUEST"))
.body("Cluster.Status.Timeline.EndDateTime", equalTo(endTime));
}
@Test
void protectedCompletedClusterIsSkippedDuringMixedTermination() {
String completedId = emr("RunJobFlow",
"{\"Name\":\"protected-completed\",\"Instances\":{\"TerminationProtected\":true},"
+ "\"Steps\":[{\"Name\":\"initial\",\"HadoopJarStep\":{\"Jar\":\"command-runner.jar\"}}]}")
.then().statusCode(200).extract().path("JobFlowId");
String activeId = emr("RunJobFlow",
"{\"Name\":\"active\",\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true}}")
.then().statusCode(200).extract().path("JobFlowId");
String completedRequest = "{\"ClusterId\":\"" + completedId + "\"}";
Object endTime = emr("DescribeCluster", completedRequest).then().statusCode(200)
.extract().path("Cluster.Status.Timeline.EndDateTime");
emr("TerminateJobFlows", "{\"JobFlowIds\":[\"" + completedId + "\",\"" + activeId + "\"]}")
.then().statusCode(200);
emr("DescribeCluster", completedRequest).then().statusCode(200)
.body("Cluster.Status.State", equalTo("TERMINATED"))
.body("Cluster.Status.StateChangeReason.Code", equalTo("ALL_STEPS_COMPLETED"))
.body("Cluster.Status.Timeline.EndDateTime", equalTo(endTime));
emr("DescribeCluster", "{\"ClusterId\":\"" + activeId + "\"}").then().statusCode(200)
.body("Cluster.Status.State", equalTo("TERMINATED"))
.body("Cluster.Status.StateChangeReason.Code", equalTo("USER_REQUEST"));
}
}