Light, fluffy, and always free - The AWS Local Emulator alternative
fix(msk): poll correct Redpanda admin readiness endpoint (#1237)
Fábio Silva2b466bf980b0
diff --git a/src/main/java/io/github/hectorvent/floci/services/msk/RedpandaManager.java b/src/main/java/io/github/hectorvent/floci/services/msk/RedpandaManager.javaindex d49fe2ef64..4b18541d4d 100644--- a/src/main/java/io/github/hectorvent/floci/services/msk/RedpandaManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/msk/RedpandaManager.java@@ -29,7 +29,10 @@ public class RedpandaManager {private static final Logger LOG = Logger.getLogger(RedpandaManager.class);private static final int KAFKA_PORT = 9092;- private static final int ADMIN_PORT = 9644;+ static final int ADMIN_PORT = 9644;++ // Redpanda's Admin API exposes readiness at /v1/status/ready. /ready always returns 404+ private static final String ADMIN_READY_PATH = "/v1/status/ready";private final ContainerBuilder containerBuilder;private final ContainerLifecycleManager lifecycleManager;@@ -137,13 +140,13 @@ public class RedpandaManager {var bindings = inspect.getNetworkSettings().getPorts().getBindings();var binding = bindings.get(com.github.dockerjava.api.model.ExposedPort.tcp(ADMIN_PORT));if (binding != null && binding.length > 0) {- adminUrl = "http://localhost:" + binding[0].getHostPortSpec() + "/ready";+ adminUrl = "http://localhost:" + binding[0].getHostPortSpec() + ADMIN_READY_PATH;} else {return false;}} else {String containerIp = bootstrap.split(":")[0];- adminUrl = "http://" + containerIp + ":" + ADMIN_PORT + "/ready";+ adminUrl = "http://" + containerIp + ":" + ADMIN_PORT + ADMIN_READY_PATH;}try {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.javanew file mode 100644index 0000000000..d53bb56e4a--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/msk/RedpandaManagerTest.java@@ -0,0 +1,101 @@+package io.github.hectorvent.floci.services.msk;++import com.github.dockerjava.api.DockerClient;+import com.github.dockerjava.api.command.InspectContainerCmd;+import com.github.dockerjava.api.command.InspectContainerResponse;+import com.github.dockerjava.api.model.ExposedPort;+import com.github.dockerjava.api.model.Ports;+import com.sun.net.httpserver.HttpServer;+import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.core.common.RegionResolver;+import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;+import io.github.hectorvent.floci.core.common.docker.ContainerDetector;+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;+import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;+import io.github.hectorvent.floci.services.msk.model.MskCluster;+import org.junit.jupiter.api.AfterEach;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.extension.ExtendWith;+import org.mockito.Mock;+import org.mockito.junit.jupiter.MockitoExtension;++import java.net.InetSocketAddress;+import java.util.Map;++import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.when;++@ExtendWith(MockitoExtension.class)+class RedpandaManagerTest {++ @Mock+ private ContainerBuilder containerBuilder;+ @Mock+ private ContainerLifecycleManager lifecycleManager;+ @Mock+ private ContainerLogStreamer logStreamer;+ @Mock+ private ContainerDetector containerDetector;+ @Mock+ private EmulatorConfig config;+ @Mock+ private RegionResolver regionResolver;++ private HttpServer adminServer;++ @AfterEach+ void tearDown() {+ if (adminServer != null) {+ adminServer.stop(0);+ }+ }++ private static MskCluster newCluster() {+ return new MskCluster("arn:aws:kafka:us-east-1:000000000000:cluster/test-cluster/abc", "test-cluster");+ }++ private int startFakeAdminServer() throws Exception {+ adminServer = HttpServer.create(new InetSocketAddress("localhost", 0), 0);+ adminServer.createContext("/v1/status/ready", exchange -> {+ exchange.sendResponseHeaders(200, -1);+ exchange.close();+ });+ adminServer.createContext("/ready", exchange -> {+ exchange.sendResponseHeaders(404, -1);+ exchange.close();+ });+ adminServer.start();+ return adminServer.getAddress().getPort();+ }++ @Test+ void isReadyPollsTheCorrectAdminReadinessPathInNativeMode() throws Exception {+ int adminHostPort = startFakeAdminServer();++ when(containerDetector.isRunningInContainer()).thenReturn(false);++ DockerClient dockerClient = mock(DockerClient.class);+ InspectContainerCmd inspectCmd = mock(InspectContainerCmd.class);+ InspectContainerResponse inspect = mock(InspectContainerResponse.class, RETURNS_DEEP_STUBS);++ when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);+ when(dockerClient.inspectContainerCmd("container-id")).thenReturn(inspectCmd);+ when(inspectCmd.exec()).thenReturn(inspect);+ when(inspect.getNetworkSettings().getPorts().getBindings()).thenReturn(Map.of(+ ExposedPort.tcp(RedpandaManager.ADMIN_PORT),+ new Ports.Binding[] { new Ports.Binding("0.0.0.0", String.valueOf(adminHostPort)) }));++ RedpandaManager manager = new RedpandaManager(+ containerBuilder, lifecycleManager, logStreamer, containerDetector, config, regionResolver);++ MskCluster cluster = newCluster();+ cluster.setContainerId("container-id");+ cluster.setBootstrapBrokers("localhost:19092");++ assertTrue(manager.isReady(cluster),+ "isReady() should report ready once /v1/status/ready answers 200; "+ + "if it regresses to polling /ready (which always 404s), this assertion fails");+ }+}