mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(msk): poll correct Redpanda admin readiness endpoint (#1237)
* fix(msk): poll correct Redpanda admin readiness endpoint * test(msk): pin RedpandaManager.isReady() to the /v1/status/ready admin path isReady() resolves an admin URL and polls it for a 200 to decide whether the Redpanda container is ready, but nothing asserted which path it hits. Add RedpandaManagerTest.isReadyPollsTheCorrectAdminReadinessPathInNativeMode: a local HTTP server stands in for Redpanda's admin API (200 on /v1/status/ready, 404 on /ready — the previously-polled path that always 404s), the Docker container-inspect chain is mocked to resolve the admin port binding to that server, and isReady() is asserted to report ready. Reverting ADMIN_READY_PATH to /ready makes the assertion fail, pinning the fix as a regression guard.
This commit is contained in:
1 parent
27943b35c3
commit
2b466bf980
2 files changed
+107
-3
No files matched your search
@@ -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");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user