Light, fluffy, and always free - The AWS Local Emulator alternative
test(redshift): clean up the zero-ETL cluster and cover the stale-container path on its own (#4548)
Kenshin Okinakace48b9f91ba8
diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftIntegrationsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftIntegrationsIntegrationTest.javaindex 961de85241..83b8c157ef 100644--- a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftIntegrationsIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftIntegrationsIntegrationTest.java@@ -7,9 +7,12 @@ import io.restassured.config.HttpClientConfig;import io.restassured.config.RestAssuredConfig;import io.restassured.response.Response;import io.restassured.specification.RequestSpecification;+import jakarta.inject.Inject;import org.awaitility.Awaitility;+import org.junit.jupiter.api.AfterAll;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.TestInstance;import java.sql.Connection;import java.sql.DriverManager;@@ -39,10 +42,14 @@ import static org.junit.jupiter.api.Assertions.assertTrue;* integrations returns an empty list rather than an error.*/@QuarkusTest+@TestInstance(TestInstance.Lifecycle.PER_CLASS)class RedshiftIntegrationsIntegrationTest {- private static String source;- private static String otherSource;+ @Inject+ RedshiftService service;++ private String source;+ private String otherSource;private static final String TARGET = "arn:aws:redshift:us-east-1:000000000000:cluster:zero-etl-cluster";/**@@ -80,6 +87,13 @@ class RedshiftIntegrationsIntegrationTest {otherSource = newOtherSource;}+ @AfterAll+ void deleteZeroEtlCluster() {+ if (source != null) {+ service.deleteCluster("zero-etl-cluster");+ }+ }+private static String createDynamoTable(String tableName) {Response response = given().header("X-Amz-Target", "DynamoDB_20120810.CreateTable")@@ -105,7 +119,7 @@ class RedshiftIntegrationsIntegrationTest {return response.then().statusCode(200).extract().path("TableDescription.LatestStreamArn");}- private static String createIntegration(String name) {+ private String createIntegration(String name) {return query("Action", "CreateIntegration", "IntegrationName", name,"SourceArn", source, "TargetArn", TARGET).then().statusCode(200)diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftStaleContainerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftStaleContainerIntegrationTest.javanew file mode 100644index 0000000000..3d8f3f7efe--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftStaleContainerIntegrationTest.java@@ -0,0 +1,104 @@+package io.github.hectorvent.floci.services.redshift;++import com.github.dockerjava.api.DockerClient;+import com.github.dockerjava.api.model.Container;+import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;+import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;+import io.github.hectorvent.floci.services.lambda.launcher.ImageCacheService;+import io.quarkus.test.junit.QuarkusTest;+import io.restassured.response.Response;+import io.restassured.specification.RequestSpecification;+import jakarta.inject.Inject;+import org.junit.jupiter.api.Assumptions;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;++import java.util.Optional;++import static io.restassured.RestAssured.given;+import static org.junit.jupiter.api.Assertions.assertNotEquals;+import static org.junit.jupiter.api.Assertions.assertTrue;++/**+ * A container left under a cluster's fixed name by an earlier Floci process must not make+ * CreateCluster fail with a Docker name conflict.+ */+@QuarkusTest+class RedshiftStaleContainerIntegrationTest {++ private static final String CLUSTER_ID = "stale-container-cluster";++ private static final String AUTH_HEADER =+ "AWS4-HMAC-SHA256 Credential=test/20260822/us-east-1/redshift/aws4_request";++ @Inject+ DockerClient dockerClient;++ @Inject+ ImageCacheService imageCacheService;++ @Inject+ ContainerLifecycleManager lifecycleManager;++ @Inject+ EmulatorConfig config;++ @BeforeEach+ void requireDocker() {+ boolean available;+ try {+ dockerClient.pingCmd().exec();+ available = true;+ } catch (RuntimeException ignored) {+ // An unreachable daemon only means this test is skipped; the assumption message says so.+ available = false;+ }+ Assumptions.assumeTrue(available, "Docker daemon must be available for Redshift container tests");+ }++ @Test+ void createClusterReplacesAContainerLeftUnderItsName() {+ String containerName = ContainerStorageHelper.dockerName(+ config, "redshift-" + config.defaultAccountId() + "-" + CLUSTER_ID);+ String image = imageCacheService.ensureImageExists(config.services().redshift().imageVersion());+ // An interrupted earlier run may have left its fixture under the same name+ lifecycleManager.removeIfExists(containerName);+ String staleId = dockerClient.createContainerCmd(image)+ .withName(containerName)+ .exec()+ .getId();+ Response created = null;+ try {+ created = query("Action", "CreateCluster", "ClusterIdentifier", CLUSTER_ID,+ "NodeType", "dc2.large", "MasterUsername", "admin", "MasterUserPassword", "Password123");+ created.then().statusCode(200);++ Optional<Container> running = lifecycleManager.findByName(containerName);+ assertTrue(running.isPresent());+ assertNotEquals(staleId, running.get().getId());+ } finally {+ try {+ // Only a created cluster can be deleted; asserting a 404 here would hide the original failure+ if (created != null && created.statusCode() == 200) {+ query("Action", "DeleteCluster", "ClusterIdentifier", CLUSTER_ID,+ "SkipFinalClusterSnapshot", "true")+ .then().statusCode(200);+ }+ } finally {+ lifecycleManager.removeIfExists(containerName);+ }+ }+ }++ private static Response query(String... formParams) {+ RequestSpecification spec = given()+ .contentType("application/x-www-form-urlencoded")+ .header("Authorization", AUTH_HEADER)+ .formParam("Version", "2012-12-01");+ for (int i = 0; i < formParams.length; i += 2) {+ spec = spec.formParam(formParams[i], formParams[i + 1]);+ }+ return spec.when().post("/");+ }+}