Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(redshift): remove a stale cluster container before creating it (#4537)

Kenshin Okinaka5fc38e33cbb4
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java
index 25a9b4a03a..6dbd4a0acf 100644
--- a/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java
+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java
@@ -67,6 +67,9 @@ public class RedshiftContainerManager {
public RedshiftContainerHandle start(String accountId, String clusterIdentifier, String masterUsername, String masterPassword) {
String image = config.services().redshift().imageVersion();
String containerName = containerName(accountId, clusterIdentifier);
+ // Only the current name: no volume backs this container, so removing a legacy-named survivor
+ // would destroy cluster data that adoptOrStart does not look for.
+ lifecycleManager.removeIfExists(containerName);
List<String> envVars = List.of(
"POSTGRES_USER=" + masterUsername,
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.java
index e72b0062e9..961de85241 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
@@ -1,5 +1,16 @@
package io.github.hectorvent.floci.services.redshift;
+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.RestAssured;
+import io.restassured.config.HttpClientConfig;
+import io.restassured.config.RestAssuredConfig;
+import io.restassured.response.Response;
+import io.restassured.specification.RequestSpecification;
+import org.awaitility.Awaitility;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
@@ -10,24 +21,14 @@ import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import org.awaitility.Awaitility;
-import io.restassured.RestAssured;
-import io.restassured.response.Response;
-import io.quarkus.test.junit.QuarkusTest;
-import io.restassured.config.HttpClientConfig;
-import io.restassured.config.RestAssuredConfig;
-import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
-import io.restassured.specification.RequestSpecification;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Redshift zero-ETL integrations.
@@ -94,11 +95,12 @@ class RedshiftIntegrationsIntegrationTest {
""".formatted(tableName))
.when().post("/");
if (response.statusCode() != 200) {
- response = given()
+ return given()
.header("X-Amz-Target", "DynamoDB_20120810.DescribeTable")
.contentType("application/x-amz-json-1.0")
.body("{\"TableName\":\"%s\"}".formatted(tableName))
- .when().post("/");
+ .when().post("/")
+ .then().statusCode(200).extract().path("Table.LatestStreamArn");
}
return response.then().statusCode(200).extract().path("TableDescription.LatestStreamArn");
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManagerTest.java
index 45f02896b7..3575ec2a3b 100644
--- a/src/test/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManagerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManagerTest.java
@@ -22,6 +22,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
import io.github.hectorvent.floci.services.redshift.model.Cluster;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.mockito.InOrder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -43,7 +44,9 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.RETURNS_SELF;
@@ -308,11 +311,29 @@ class RedshiftContainerManagerTest {
manager.start(ACCOUNT_ID, "test-cluster", "admin", "pass");
assertTrue(manager.getContainer(ACCOUNT_ID, "test-cluster").isPresent());
+ clearInvocations(lifecycleManager);
manager.stop(ACCOUNT_ID, "test-cluster");
assertTrue(manager.getContainer(ACCOUNT_ID, "test-cluster").isEmpty());
verify(lifecycleManager).removeIfExists("floci-aws-redshift-" + ACCOUNT_ID + "-test-cluster");
}
+ @Test
+ void startRemovesStaleContainerBeforeCreating() {
+ ContainerBuilder.Builder specBuilder = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
+ when(containerBuilder.newContainer(anyString())).thenReturn(specBuilder);
+ ContainerInfo info = new ContainerInfo("cont-123", Map.of(5432, new EndpointInfo("localhost", 5432)));
+ when(lifecycleManager.createAndStart(any())).thenReturn(info);
+
+ manager.start(ACCOUNT_ID, "test-cluster", "admin", "pass");
+
+ // A container left under the fixed name by an earlier run would make createAndStart fail with a name conflict
+ InOrder order = inOrder(lifecycleManager);
+ order.verify(lifecycleManager).removeIfExists("floci-aws-redshift-" + ACCOUNT_ID + "-test-cluster");
+ order.verify(lifecycleManager).createAndStart(any());
+ // A legacy-named container may still hold a cluster's data, and no volume backs it
+ verify(lifecycleManager, never()).removeIfExists("floci-redshift-" + ACCOUNT_ID + "-test-cluster");
+ }
+
@Test
void sameClusterIdentifierAcrossAccountsDoesNotCollide() {
ContainerBuilder.Builder specBuilder = mock(ContainerBuilder.Builder.class, org.mockito.Mockito.RETURNS_SELF);
@@ -349,6 +370,7 @@ class RedshiftContainerManagerTest {
// Data lives only in the container's writable layer (no volume): recreating it
// would silently discard it, so a container found by name must never be recreated.
verify(lifecycleManager, never()).createAndStart(any());
+ verify(lifecycleManager, never()).removeIfExists(anyString());
}
@Test