From 244cdaa2d20c4f69c2c7751fc2e150f607c2e11f Mon Sep 17 00:00:00 2001 From: Andre Kurait Date: Fri, 6 Nov 2026 23:04:56 -0600 Subject: [PATCH] test(emr): await cluster cleanup before fixture teardown (#7714) Preserve workload assertions while awaiting terminal cluster state and released storage/bootstrap ownership. Signed-off-by: Andre Kurait --- .../EmrDockerRunnerDockerIntegrationTest.java | 7 +++++-- .../EmrHiveStepsDockerIntegrationTest.java | 8 ++++++-- .../io/floci/aws/testing/EmrRequests.java | 20 +++++++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/test/java/io/floci/aws/services/emr/EmrDockerRunnerDockerIntegrationTest.java b/src/test/java/io/floci/aws/services/emr/EmrDockerRunnerDockerIntegrationTest.java index d857409661..78da38ee4a 100644 --- a/src/test/java/io/floci/aws/services/emr/EmrDockerRunnerDockerIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/emr/EmrDockerRunnerDockerIntegrationTest.java @@ -43,6 +43,7 @@ import java.util.stream.Stream; import static io.floci.aws.testing.EmrRequests.awaitStep; import static io.floci.aws.testing.EmrRequests.emr; +import static io.floci.aws.testing.EmrRequests.terminateClusterAndAwaitCleanup; import static io.floci.aws.testing.EmrServerlessRequests.awaitJobRun; import static io.floci.aws.testing.EmrServerlessRequests.createSparkApplication; import static io.floci.aws.testing.EmrServerlessRequests.deleteApplication; @@ -90,6 +91,9 @@ class EmrDockerRunnerDockerIntegrationTest { @Inject EmulatorConfig config; + @Inject + EmrService emrService; + private S3Client s3; private String bucket; private String clusterId; @@ -308,8 +312,7 @@ class EmrDockerRunnerDockerIntegrationTest { + "\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true,\"InstanceGroups\":[{\"Name\":\"primary\"," + "\"InstanceRole\":\"MASTER\",\"InstanceType\":\"m5.xlarge\",\"InstanceCount\":1}]}}") .then().statusCode(200).extract().jsonPath().getString("JobFlowId"); - cleanup.register(() -> emr("TerminateJobFlows", "{\"JobFlowIds\":[\"" + clusterId + "\"]}") - .then().statusCode(200)); + cleanup.register(() -> terminateClusterAndAwaitCleanup(clusterId, emrService)); } /** Adds one step to the cluster and returns the state it ended in. */ diff --git a/src/test/java/io/floci/aws/services/emr/EmrHiveStepsDockerIntegrationTest.java b/src/test/java/io/floci/aws/services/emr/EmrHiveStepsDockerIntegrationTest.java index b940a8e785..b2aaf098ed 100644 --- a/src/test/java/io/floci/aws/services/emr/EmrHiveStepsDockerIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/emr/EmrHiveStepsDockerIntegrationTest.java @@ -12,6 +12,7 @@ import io.floci.aws.testing.S3TestClients; import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.TestProfile; import io.restassured.response.Response; +import jakarta.inject.Inject; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -33,6 +34,7 @@ import java.util.zip.GZIPInputStream; import static io.floci.aws.testing.EmrRequests.awaitStep; import static io.floci.aws.testing.EmrRequests.emr; +import static io.floci.aws.testing.EmrRequests.terminateClusterAndAwaitCleanup; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeTrue; @@ -56,6 +58,9 @@ class EmrHiveStepsDockerIntegrationTest { @RegisterExtension final PartitionCleanup cleanup = new PartitionCleanup(); + @Inject + EmrService emrService; + private S3Client s3; private String bucket; private String clusterId; @@ -216,8 +221,7 @@ class EmrHiveStepsDockerIntegrationTest { .put("InstanceCount", 1); clusterId = emr("RunJobFlow", request.toString()).then().statusCode(200).extract().jsonPath() .getString("JobFlowId"); - cleanup.register(() -> emr("TerminateJobFlows", "{\"JobFlowIds\":[\"" + clusterId + "\"]}") - .then().statusCode(200)); + cleanup.register(() -> terminateClusterAndAwaitCleanup(clusterId, emrService)); } /** Runs the AWS CLI's Hive step with {@code args} and returns the state it ended in. */ diff --git a/src/test/java/io/floci/aws/testing/EmrRequests.java b/src/test/java/io/floci/aws/testing/EmrRequests.java index a5ca1a0133..59be89be81 100644 --- a/src/test/java/io/floci/aws/testing/EmrRequests.java +++ b/src/test/java/io/floci/aws/testing/EmrRequests.java @@ -1,5 +1,7 @@ package io.floci.aws.testing; +import io.floci.aws.services.emr.EmrService; +import io.floci.aws.services.emr.model.EmrCluster; import io.restassured.response.Response; import java.time.Duration; @@ -12,7 +14,9 @@ import static org.awaitility.Awaitility.await; public final class EmrRequests { private static final Set UNFINISHED_STEP_STATES = Set.of("PENDING", "RUNNING"); + private static final Set TERMINATED_CLUSTER_STATES = Set.of("TERMINATED", "TERMINATED_WITH_ERRORS"); private static final Duration STEP_TIMEOUT = Duration.ofMinutes(5); + private static final Duration CLEANUP_TIMEOUT = Duration.ofSeconds(30); private static final Duration POLL_INTERVAL = Duration.ofMillis(500); private EmrRequests() {} @@ -47,4 +51,20 @@ public final class EmrRequests { response -> !UNFINISHED_STEP_STATES.contains( response.jsonPath().getString("Step.Status.State"))); } + + /** + * Ends a test's default-account/default-region cluster and waits for its owned storage and bootstrap image + * to be released. TerminateJobFlows accepts the request before that background cleanup has completed. + */ + public static void terminateClusterAndAwaitCleanup(String clusterId, EmrService service) { + emr("TerminateJobFlows", "{\"JobFlowIds\":[\"" + clusterId + "\"]}").then().statusCode(200); + await("cluster " + clusterId + " to terminate and release its storage and bootstrap image") + .atMost(CLEANUP_TIMEOUT) + .pollInterval(POLL_INTERVAL) + .until(() -> { + EmrCluster cluster = service.describeCluster(clusterId); + return TERMINATED_CLUSTER_STATES.contains(cluster.getState()) + && !cluster.isStorageCreated() && cluster.getBootstrapImage() == null; + }); + } }