diff --git a/src/main/java/io/github/hectorvent/floci/services/msk/MskService.java b/src/main/java/io/github/hectorvent/floci/services/msk/MskService.java index d2a92286d7..530c1c1d40 100644 --- a/src/main/java/io/github/hectorvent/floci/services/msk/MskService.java +++ b/src/main/java/io/github/hectorvent/floci/services/msk/MskService.java @@ -167,16 +167,7 @@ public class MskService implements ResourceProvider { cluster.getCurrentBrokerSoftwareInfo().setConfigurationRevision(configurationInfo.getRevision()); } - if (config.services().msk().mock()) { - cluster.setState(ClusterState.ACTIVE); - cluster.setBootstrapBrokers("localhost:9092"); - } else if (!redpandaManager.tryStartContainer(cluster)) { - LOG.warnv("MSK cluster {0} created without a backing Kafka broker: no Docker daemon " - + "is reachable. Metadata operations work; producing and consuming do not " - + "until a daemon appears.", clusterName); - cluster.setState(ClusterState.ACTIVE); - cluster.setBootstrapBrokers("localhost:9092"); - } + startBroker(cluster); storage.put(clusterArn, cluster); return cluster; @@ -220,6 +211,19 @@ public class MskService implements ResourceProvider { return createCluster(merged); } + private void startBroker(MskCluster cluster) { + if (config.services().msk().mock()) { + cluster.setState(ClusterState.ACTIVE); + cluster.setBootstrapBrokers("localhost:9092"); + } else if (!redpandaManager.tryStartContainer(cluster)) { + LOG.warnv("MSK cluster {0} created without a backing Kafka broker: no Docker daemon " + + "is reachable. Metadata operations work; producing and consuming do not " + + "until a daemon appears.", cluster.getClusterName()); + cluster.setState(ClusterState.ACTIVE); + cluster.setBootstrapBrokers("localhost:9092"); + } + } + /** * Rejects the CreateCluster input AWS rejects. * @@ -360,12 +364,7 @@ public class MskService implements ResourceProvider { cluster.setZookeeperConnectString(null); cluster.setCurrentBrokerSoftwareInfo(null); - if (config.services().msk().mock()) { - cluster.setState(ClusterState.ACTIVE); - cluster.setBootstrapBrokers("localhost:9092"); - } else { - redpandaManager.startContainer(cluster); - } + startBroker(cluster); storage.put(clusterArn, cluster); return cluster; diff --git a/src/test/java/io/github/hectorvent/floci/services/msk/MskServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/msk/MskServiceTest.java index 23739e37c9..941c4eb64a 100644 --- a/src/test/java/io/github/hectorvent/floci/services/msk/MskServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/msk/MskServiceTest.java @@ -1024,4 +1024,25 @@ class MskServiceTest { assertThrows(AwsException.class, () -> mskService.createCluster(request)); } + + @Test + void createServerlessClusterWithoutDockerDaemonStillReachesActive() { + when(config.services().msk().mock()).thenReturn(false); + Mockito.doThrow(new RuntimeException("Cannot connect to the Docker daemon")) + .when(redpandaManager).startContainer(Mockito.any()); + when(redpandaManager.tryStartContainer(Mockito.any())).thenReturn(false); + + Serverless serverless = new Serverless(); + serverless.setVpcConfigs(List.of(new VpcConfig())); + CreateClusterV2Request request = new CreateClusterV2Request(); + request.setClusterName("no-docker-serverless"); + request.setServerless(serverless); + + MskCluster cluster = mskService.createCluster(request); + + assertEquals(ClusterState.ACTIVE, cluster.getState()); + assertEquals("localhost:9092", cluster.getBootstrapBrokers()); + assertEquals(ClusterState.ACTIVE, + mskService.describeCluster(cluster.getClusterArn()).getState()); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/msk/RedpandaManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/msk/RedpandaManagerTest.java index 95f421386e..124c4b69b4 100644 --- a/src/test/java/io/github/hectorvent/floci/services/msk/RedpandaManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/msk/RedpandaManagerTest.java @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.services.msk; +import com.github.dockerjava.api.DockerClient; import com.github.dockerjava.api.exception.NotFoundException; import com.sun.net.httpserver.HttpServer; import io.github.hectorvent.floci.config.EmulatorConfig; @@ -358,6 +359,27 @@ class RedpandaManagerTest { verify(lifecycleManager, never()).createAndStart(any()); } + @Test + void tryStartContainerReportsUnavailableWhenNoDaemonAnswers() { + when(lifecycleManager.createAndStart(any())) + .thenThrow(new RuntimeException("java.net.SocketException: No such file or directory")); + when(lifecycleManager.getDockerClient()) + .thenThrow(new RuntimeException("java.net.SocketException: No such file or directory")); + + assertFalse(manager.tryStartContainer(newCluster())); + } + + @Test + void tryStartContainerPropagatesFailuresWhileTheDaemonIsReachable() { + when(lifecycleManager.createAndStart(any())) + .thenThrow(new RuntimeException("no such image: redpandadata/redpanda:latest")); + when(lifecycleManager.getDockerClient()).thenReturn(mock(DockerClient.class, RETURNS_DEEP_STUBS)); + + RuntimeException failure = assertThrows(RuntimeException.class, + () -> manager.tryStartContainer(newCluster())); + assertEquals("no such image: redpandadata/redpanda:latest", failure.getMessage()); + } + @Test void movesToTheNextHostPortWhenDockerReportsTheChosenOneInUse() { // From inside a container, allocate() cannot see a host process holding a port, so the