From db48f01012078201af79d244ef787100dcec288e Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Tue, 3 Nov 2026 00:02:08 -0400 Subject: [PATCH] test(emr): run a Glue catalog Spark step and clean up after the Docker runner tests (#7366) EmrDockerRunnerDockerIntegrationTest had no case for Spark on the Glue Data Catalog, which needed #7329's Glue fixes. A cluster whose spark-hive-site names the Glue client factory, as the Management Guide's example does, now runs a PySpark step that creates a database and a partitioned table in Floci's Glue, inserts into it, reads it back and writes what it counted to S3. The test checks that each row came back once (the client reads a table's partitions in five segments) and that Floci's Glue holds the table's partitions, so the step used the Glue catalog rather than a metastore of its own. The class also left its buckets and its EMR Serverless application in the emulator it shares with the profile's other two classes, and the new case would have left a Glue database. Each test now registers what it creates with PartitionCleanup: after the test, its cluster is terminated, its Glue database and EMR Serverless application deleted, and its bucket emptied and deleted. EmrServerlessRequests' deleteApplication stops an application, as DeleteApplication requires, then deletes it. --- .../EmrDockerRunnerDockerIntegrationTest.java | 69 +++++++++++++++---- .../floci/testing/EmrServerlessRequests.java | 6 ++ 2 files changed, 62 insertions(+), 13 deletions(-) diff --git a/src/test/java/io/github/hectorvent/floci/services/emr/EmrDockerRunnerDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/emr/EmrDockerRunnerDockerIntegrationTest.java index 4e1050947d..51d4a7905a 100644 --- a/src/test/java/io/github/hectorvent/floci/services/emr/EmrDockerRunnerDockerIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/emr/EmrDockerRunnerDockerIntegrationTest.java @@ -6,18 +6,20 @@ import io.github.hectorvent.floci.core.common.docker.RetryingTarCopier; import io.github.hectorvent.floci.testing.AwsTestClients; import io.github.hectorvent.floci.testing.ContainerDataPlaneProfile; import io.github.hectorvent.floci.testing.DockerAvailability; +import io.github.hectorvent.floci.testing.PartitionCleanup; import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.TestProfile; import io.restassured.response.Response; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; -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 org.junit.jupiter.api.extension.RegisterExtension; import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.glue.GlueClient; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.S3Object; @@ -35,6 +37,7 @@ import static io.github.hectorvent.floci.testing.EmrRequests.awaitStep; import static io.github.hectorvent.floci.testing.EmrRequests.emr; import static io.github.hectorvent.floci.testing.EmrServerlessRequests.awaitJobRun; import static io.github.hectorvent.floci.testing.EmrServerlessRequests.createSparkApplication; +import static io.github.hectorvent.floci.testing.EmrServerlessRequests.deleteApplication; import static io.github.hectorvent.floci.testing.EmrServerlessRequests.startJobRun; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assumptions.assumeTrue; @@ -42,8 +45,8 @@ import static org.junit.jupiter.api.Assumptions.assumeTrue; /** * EMR steps and EMR Serverless job runs through Floci's Docker runner for real: a cluster in the {@code docker} * runner mode runs each step, and an application each job run, in a container of the release's EMR Serverless image, - * which reaches Floci's S3 from there. The cached {@code emr-7.5.0} image keeps a local run from pulling; CI pulls it - * (3.5 GB) the first time. + * which reaches Floci's S3 and Glue from there. The cached {@code emr-7.5.0} image keeps a local run from pulling; + * CI pulls it (3.5 GB) the first time. */ @QuarkusTest @TestProfile(ContainerDataPlaneProfile.class) @@ -69,6 +72,9 @@ class EmrDockerRunnerDockerIntegrationTest { print(word + "\\t" + str(counts[word])) """; + @RegisterExtension + final PartitionCleanup cleanup = new PartitionCleanup(); + private S3Client s3; private String bucket; private String clusterId; @@ -82,23 +88,15 @@ class EmrDockerRunnerDockerIntegrationTest { void putInputs() throws InterruptedException { assumeTrue(DockerAvailability.isAvailable(), "Docker daemon must be available for EMR's Docker runner tests"); s3 = AwsTestClients.create(S3Client.builder().forcePathStyle(true), "000000000000", Region.US_EAST_1); + cleanup.register(s3::close); bucket = "emr-docker-" + UUID.randomUUID().toString().substring(0, 8); s3.createBucket(request -> request.bucket(bucket)); + cleanup.register(this::deleteBucket); put("in/words.txt", WORDS); put("side/mapper.py", MAPPER); put("side/reducer.py", REDUCER); } - @AfterEach - void terminateCluster() { - if (clusterId != null) { - emr("TerminateJobFlows", "{\"JobFlowIds\":[\"" + clusterId + "\"]}").then().statusCode(200); - } - if (s3 != null) { - s3.close(); - } - } - @Test void aStreamingWordCountStepReadsItsInputAndWritesItsCountsInFlocisS3() { startCluster("[]"); @@ -150,6 +148,41 @@ class EmrDockerRunnerDockerIntegrationTest { assertEquals("from spark-env\n", partFiles("env-out/")); } + @Test + void aSparkStepOnTheGlueCatalogWritesAndReadsBackAPartitionedTableInFlocisGlue() { + String database = bucket.replace('-', '_'); + put("side/glue.py", """ + import sys + from pyspark.sql import SparkSession + spark = SparkSession.builder.enableHiveSupport().getOrCreate() + database, warehouse, out = sys.argv[1:4] + spark.sql(f"create database {database} location '{warehouse}'") + spark.sql(f"create table {database}.words (word string, n int, d string) using parquet" + f" partitioned by (d) location '{warehouse}words/'") + spark.sql(f"insert into {database}.words values ('a', 1, 'one'), ('b', 2, 'two'), ('c', 3, 'two')") + rows = spark.sql(f"select d, count(*) from {database}.words group by d order by d").collect() + spark.createDataFrame([(" ".join(f"{d}={n}" for d, n in rows),)], ["value"]) \\ + .coalesce(1).write.text(out) + """); + GlueClient glue = AwsTestClients.create(GlueClient.builder(), "000000000000", Region.US_EAST_1); + cleanup.register(glue::close); + cleanup.register(() -> glue.deleteDatabase(request -> request.name(database))); + // The Management Guide's setting for a cluster's Spark on the Glue Data Catalog. + startCluster(""" + [{"Classification": "spark-hive-site", "Properties": {"hive.metastore.client.factory.class": + "com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory"}}] + """); + + assertEquals("COMPLETED", runStep("command-runner.jar", "spark-submit", + s3Uri("side/glue.py"), database, s3Uri("warehouse/"), s3Uri("glue-out/"))); + // Each row once, though the Glue client reads a table's partitions in five segments. + assertEquals("one=1 two=2\n", partFiles("glue-out/")); + // Floci's Glue holds the partitions, so the step's Spark used the Glue catalog, not a metastore of its own. + assertEquals(List.of("one", "two"), + glue.getPartitions(request -> request.databaseName(database).tableName("words")).partitions().stream() + .map(partition -> partition.values().get(0)).sorted().toList()); + } + @Test void anEmrServerlessRunsPythonIsTheOneInTheArchiveItsSparkArchivesUnpack() throws IOException { put("side/job.py", """ @@ -164,6 +197,7 @@ class EmrDockerRunnerDockerIntegrationTest { s3.putObject(request -> request.bucket(bucket).key("envs/pyspark_venv.tar.gz"), RequestBody.fromBytes(environmentArchive())); String applicationId = createSparkApplication(REGION, "emr-7.5.0"); + cleanup.register(() -> deleteApplication(REGION, applicationId)); // The user guide's recipe for a virtual environment: the archive, and the driver's and executors' Python in it. String python = "./environment/bin/python"; ObjectNode run = JSON.createObjectNode().put("clientToken", UUID.randomUUID().toString()) @@ -209,6 +243,8 @@ 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)); } /** Adds one step to the cluster and returns the state it ended in. */ @@ -222,6 +258,13 @@ class EmrDockerRunnerDockerIntegrationTest { return awaitStep(clusterId, stepId).jsonPath().getString("Step.Status.State"); } + /** Empties the test's bucket and deletes it. */ + private void deleteBucket() { + s3.listObjectsV2Paginator(request -> request.bucket(bucket)).contents() + .forEach(object -> s3.deleteObject(request -> request.bucket(bucket).key(object.key()))); + s3.deleteBucket(request -> request.bucket(bucket)); + } + private void put(String key, String text) { s3.putObject(request -> request.bucket(bucket).key(key), RequestBody.fromString(text)); } diff --git a/src/test/java/io/github/hectorvent/floci/testing/EmrServerlessRequests.java b/src/test/java/io/github/hectorvent/floci/testing/EmrServerlessRequests.java index 235f07ab41..00dc7ce8d5 100644 --- a/src/test/java/io/github/hectorvent/floci/testing/EmrServerlessRequests.java +++ b/src/test/java/io/github/hectorvent/floci/testing/EmrServerlessRequests.java @@ -45,6 +45,12 @@ public final class EmrServerlessRequests { .then().statusCode(200).extract().path("jobRunId"); } + /** Stops the application, which DeleteApplication requires, and deletes it. */ + public static void deleteApplication(String region, String applicationId) { + emrServerless(region).when().post("/applications/" + applicationId + "/stop").then().statusCode(200); + emrServerless(region).when().delete("/applications/" + applicationId).then().statusCode(200); + } + /** * Polls GetJobRun until the run is SUCCESS, FAILED or CANCELLED and returns that response, whatever the state: * asserting which one is the caller's business. A GetJobRun that fails ends the wait there. A run in the