Skip to content

armgit / floci

Public Imported from GitHub
0

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

refactor(redshift): deduplicate RedshiftContainerManager startup and adoption sequence (#3679)

Vũ Thế Huy631d98be643b
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 c051efd71c..eb8b81ad04 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
@@ -87,22 +87,7 @@ public class RedshiftContainerManager {
ContainerSpec spec = specBuilder.build();
ContainerInfo info = lifecycleManager.createAndStart(spec);
- EndpointInfo endpoint = info.getEndpoint(enginePort);
-
- RedshiftContainerHandle handle = new RedshiftContainerHandle(
- info.containerId(), clusterIdentifier, endpoint.host(), endpoint.port());
-
- try {
- Closeable stream = logStreamer.attach(info.containerId(), "/floci/redshift", clusterIdentifier, "us-east-1", "redshift:" + clusterIdentifier);
- handle.setLogStream(stream);
- } catch (Exception e) {
- LOG.warnv("Failed to stream logs for {0}", containerName);
- }
-
- waitForReady(containerName, info.containerId(), masterUsername, "dev");
-
- containers.put(containerKey(accountId, clusterIdentifier), handle);
- return handle;
+ return initializeAndRegisterHandle(info, accountId, clusterIdentifier, masterUsername, containerName, enginePort);
}
/**
@@ -128,12 +113,27 @@ public class RedshiftContainerManager {
containerName, clusterIdentifier);
int enginePort = 5432;
ContainerInfo info = lifecycleManager.adopt(existing.get().getId(), List.of(enginePort));
- EndpointInfo endpoint = info.getEndpoint(enginePort);
+ return initializeAndRegisterHandle(info, accountId, clusterIdentifier, masterUsername, containerName, enginePort);
+ }
+ private RedshiftContainerHandle initializeAndRegisterHandle(
+ ContainerInfo info,
+ String accountId,
+ String clusterIdentifier,
+ String masterUsername,
+ String containerName,
+ int enginePort) {
+ EndpointInfo endpoint = info.getEndpoint(enginePort);
RedshiftContainerHandle handle = new RedshiftContainerHandle(
info.containerId(), clusterIdentifier, endpoint.host(), endpoint.port());
+
try {
- Closeable stream = logStreamer.attach(info.containerId(), "/floci/redshift", clusterIdentifier, "us-east-1", "redshift:" + clusterIdentifier);
+ Closeable stream = logStreamer.attach(
+ info.containerId(),
+ "/floci/redshift",
+ clusterIdentifier,
+ "us-east-1",
+ "redshift:" + clusterIdentifier);
handle.setLogStream(stream);
} catch (Exception e) {
LOG.warnv("Failed to stream logs for {0}", containerName);
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 209b2bb62a..111b7d280d 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
@@ -36,14 +36,17 @@ import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
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.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.RETURNS_SELF;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -491,4 +494,52 @@ class RedshiftContainerManagerTest {
assertEquals("InternalFailure", ex.getErrorCode());
assertEquals(500, ex.getHttpStatus());
}
+
+ @Test
+ void testStartAttachesLogStreamerAndToleratesFailure() throws Exception {
+ ContainerBuilder.Builder specBuilder = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
+ when(containerBuilder.newContainer(anyString())).thenReturn(specBuilder);
+ ContainerInfo info = new ContainerInfo("cont-stream", Map.of(5432, new EndpointInfo("localhost", 5432)));
+ when(lifecycleManager.createAndStart(any())).thenReturn(info);
+ Closeable stream = mock(Closeable.class);
+ when(logStreamer.attach("cont-stream", "/floci/redshift", "test-cluster", "us-east-1", "redshift:test-cluster"))
+ .thenReturn(stream);
+
+ RedshiftContainerHandle handle = manager.start(ACCOUNT_ID, "test-cluster", "admin", "pass");
+ assertNotNull(handle);
+ assertEquals(stream, handle.getLogStream());
+ verify(logStreamer).attach("cont-stream", "/floci/redshift", "test-cluster", "us-east-1", "redshift:test-cluster");
+
+ // When logStreamer throws, start still completes successfully
+ doThrow(new RuntimeException("stream error"))
+ .when(logStreamer).attach(anyString(), anyString(), anyString(), anyString(), anyString());
+ RedshiftContainerHandle handleTolerated = manager.start(ACCOUNT_ID, "test-cluster-2", "admin", "pass");
+ assertNotNull(handleTolerated);
+ assertNull(handleTolerated.getLogStream());
+ }
+
+ @Test
+ void testAdoptOrStartAttachesLogStreamerAndToleratesFailure() throws Exception {
+ String containerName = "floci-redshift-" + ACCOUNT_ID + "-test-cluster";
+ Container existing = mock(Container.class);
+ when(existing.getId()).thenReturn("cont-adopt-stream");
+ when(lifecycleManager.findByName(containerName)).thenReturn(Optional.of(existing));
+ ContainerInfo adopted = new ContainerInfo("cont-adopt-stream", Map.of(5432, new EndpointInfo("localhost", 6000)));
+ when(lifecycleManager.adopt(eq("cont-adopt-stream"), any())).thenReturn(adopted);
+ Closeable stream = mock(Closeable.class);
+ when(logStreamer.attach("cont-adopt-stream", "/floci/redshift", "test-cluster", "us-east-1", "redshift:test-cluster"))
+ .thenReturn(stream);
+
+ RedshiftContainerHandle handle = manager.adoptOrStart(ACCOUNT_ID, "test-cluster", "admin", "pass");
+ assertNotNull(handle);
+ assertEquals(stream, handle.getLogStream());
+ verify(logStreamer).attach("cont-adopt-stream", "/floci/redshift", "test-cluster", "us-east-1", "redshift:test-cluster");
+
+ // When logStreamer throws, adoptOrStart still completes successfully
+ doThrow(new RuntimeException("stream error"))
+ .when(logStreamer).attach(anyString(), anyString(), anyString(), anyString(), anyString());
+ RedshiftContainerHandle handleTolerated = manager.adoptOrStart(ACCOUNT_ID, "test-cluster", "admin", "pass");
+ assertNotNull(handleTolerated);
+ assertNull(handleTolerated.getLogStream());
+ }
}