refactor(docker): share one Docker reachability probe across the container managers (#8951)

This commit is contained in:
Hector Ventura authored and GitHub committed 2026-11-19 07:02:21 -04:00
1 parent 3b695e25d2
commit b388616273
19 files changed
+73 -104

No files matched your search

@@ -0,0 +1,33 @@
package io.floci.aws.core.common.docker;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.PingCmd;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class ContainerLifecycleManagerReachabilityTest {
@Test
void aDaemonThatAnswersThePingIsReachable() {
DockerClient docker = mock(DockerClient.class);
when(docker.pingCmd()).thenReturn(mock(PingCmd.class));
assertTrue(new ContainerLifecycleManager(docker, null, null, null).isDockerReachable());
}
// Catches: a missing daemon surfacing as an exception from the probe, which callers use to tell a
// missing daemon apart from a container that failed for its own reasons.
@Test
void aPingThatFailsReportsTheDaemonUnreachableInsteadOfThrowing() {
DockerClient docker = mock(DockerClient.class);
PingCmd ping = mock(PingCmd.class);
when(ping.exec()).thenThrow(new RuntimeException("java.net.SocketException: No such file or directory"));
when(docker.pingCmd()).thenReturn(ping);
assertFalse(new ContainerLifecycleManager(docker, null, null, null).isDockerReachable());
}
}
@@ -1,6 +1,5 @@
package io.floci.aws.services.docdb.container;
import com.github.dockerjava.api.DockerClient;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.docker.ContainerBuilder;
@@ -47,8 +46,7 @@ class DocDbContainerManagerTest {
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
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"));
when(lifecycleManager.isDockerReachable()).thenReturn(false);
DocDbContainerManager manager = newManager(lifecycleManager);
@@ -66,8 +64,7 @@ class DocDbContainerManagerTest {
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
when(lifecycleManager.createAndStart(any()))
.thenThrow(new RuntimeException("no such image: mongo:7.0"));
DockerClient dockerClient = mock(DockerClient.class, Mockito.RETURNS_DEEP_STUBS);
when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);
when(lifecycleManager.isDockerReachable()).thenReturn(true);
DocDbContainerManager manager = newManager(lifecycleManager);
@@ -104,7 +104,7 @@ class ElastiCacheContainerManagerTest {
when(lifecycleManager.createAndStart(any())).thenReturn(new ContainerLifecycleManager.ContainerInfo(
"container-id", Map.of(6379,
new ContainerLifecycleManager.EndpointInfo("127.0.0.1", 6379))));
when(lifecycleManager.getDockerClient()).thenThrow(new IllegalStateException("no daemon"));
when(lifecycleManager.isDockerReachable()).thenReturn(false);
ContainerLogStreamer logStreamer = mock(ContainerLogStreamer.class);
when(logStreamer.attach(any(), any(), any(), any(), any()))
.thenThrow(new IllegalStateException("log attach failed after daemon went away"));
@@ -88,7 +88,7 @@ class ElastiCacheMemcachedContainerManagerTest {
when(lifecycleManager.createAndStart(any())).thenReturn(new ContainerLifecycleManager.ContainerInfo(
"container-id", Map.of(11211,
new ContainerLifecycleManager.EndpointInfo("127.0.0.1", 11211))));
when(lifecycleManager.getDockerClient()).thenThrow(new IllegalStateException("no daemon"));
when(lifecycleManager.isDockerReachable()).thenReturn(false);
ContainerLogStreamer logStreamer = mock(ContainerLogStreamer.class);
when(logStreamer.attach(any(), any(), any(), any(), any()))
.thenThrow(new IllegalStateException("log attach failed after daemon went away"));
@@ -1,6 +1,5 @@
package io.floci.aws.services.memorydb.container;
import com.github.dockerjava.api.DockerClient;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.docker.ContainerBuilder;
@@ -134,8 +133,7 @@ class MemoryDbContainerManagerTest {
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
when(lifecycleManager.createAndStartEphemeral(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"));
when(lifecycleManager.isDockerReachable()).thenReturn(false);
MemoryDbContainerManager manager = newManager(lifecycleManager);
@@ -153,8 +151,7 @@ class MemoryDbContainerManagerTest {
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
when(lifecycleManager.createAndStartEphemeral(any()))
.thenThrow(new RuntimeException("no such image: valkey/valkey:8"));
DockerClient dockerClient = mock(DockerClient.class, Mockito.RETURNS_DEEP_STUBS);
when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);
when(lifecycleManager.isDockerReachable()).thenReturn(true);
MemoryDbContainerManager manager = newManager(lifecycleManager);
@@ -1,7 +1,6 @@
package io.floci.aws.services.msk;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.exception.NotFoundException;
import com.sun.net.httpserver.HttpServer;
import io.floci.aws.config.EmulatorConfig;
@@ -42,7 +41,6 @@ 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.eq;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
@@ -548,8 +546,7 @@ class RedpandaManagerTest {
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"));
when(lifecycleManager.isDockerReachable()).thenReturn(false);
assertFalse(manager.tryStartContainer(newCluster()));
}
@@ -558,7 +555,7 @@ class RedpandaManagerTest {
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));
when(lifecycleManager.isDockerReachable()).thenReturn(true);
RuntimeException failure = assertThrows(RuntimeException.class,
() -> manager.tryStartContainer(newCluster()));
@@ -1,6 +1,5 @@
package io.floci.aws.services.neptune.container;
import com.github.dockerjava.api.DockerClient;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.docker.ContainerBuilder;
@@ -57,8 +56,7 @@ class NeptuneContainerManagerTest {
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
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"));
when(lifecycleManager.isDockerReachable()).thenReturn(false);
NeptuneContainerManager manager = newManager(lifecycleManager);
@@ -77,8 +75,7 @@ class NeptuneContainerManagerTest {
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
when(lifecycleManager.createAndStart(any()))
.thenThrow(new RuntimeException("no such image: tinkerpop/gremlin-server:3.7.3"));
DockerClient dockerClient = mock(DockerClient.class, Mockito.RETURNS_DEEP_STUBS);
when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);
when(lifecycleManager.isDockerReachable()).thenReturn(true);
NeptuneContainerManager manager = newManager(lifecycleManager);
@@ -815,8 +815,7 @@ class RdsContainerManagerTest {
.when(lifecycleManager).removeIfExistsStrict(any());
doThrow(new IllegalStateException("Failed to remove container floci-rds-db1"))
.when(lifecycleManager).stopAndRemoveStrict(any(), any());
when(lifecycleManager.getDockerClient()).thenThrow(
new RuntimeException("java.net.SocketException: No such file or directory"));
when(lifecycleManager.isDockerReachable()).thenReturn(false);
RdsContainerManager manager = daemonlessCapableManager(lifecycleManager);
String runtimeId = "arn:aws:rds:us-east-1:000000000000:db:db1";
@@ -837,8 +836,7 @@ class RdsContainerManagerTest {
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
when(lifecycleManager.create(any()))
.thenThrow(new IllegalStateException("no such image: mysql:8.0"));
DockerClient dockerClient = mock(DockerClient.class, Mockito.RETURNS_DEEP_STUBS);
when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);
when(lifecycleManager.isDockerReachable()).thenReturn(true);
RdsContainerManager manager = daemonlessCapableManager(lifecycleManager);
IllegalStateException failure = assertThrows(IllegalStateException.class,
@@ -856,8 +854,7 @@ class RdsContainerManagerTest {
.when(lifecycleManager).removeIfExistsStrict(any());
doThrow(new IllegalStateException("Failed to remove container floci-rds-db1"))
.when(lifecycleManager).stopAndRemoveStrict(any(), any());
when(lifecycleManager.getDockerClient()).thenThrow(
new RuntimeException("java.net.SocketException: No such file or directory"));
when(lifecycleManager.isDockerReachable()).thenReturn(false);
RdsContainerManager manager = daemonlessCapableManager(lifecycleManager);
String runtimeId = "arn:aws:rds:us-east-1:000000000000:db:db1";
@@ -868,8 +865,7 @@ class RdsContainerManagerTest {
Mockito.reset(lifecycleManager);
stubStarts(lifecycleManager, new ContainerLifecycleManager.ContainerInfo(
"late-container", Map.of(3306, new ContainerLifecycleManager.EndpointInfo("db1", 3306))));
DockerClient dockerClient = mock(DockerClient.class, Mockito.RETURNS_DEEP_STUBS);
when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);
when(lifecycleManager.isDockerReachable()).thenReturn(true);
RdsContainerHandle handle = manager.tryStart(runtimeId, "db1", "db1", "floci-rds-db1",
DatabaseEngine.MYSQL, "mysql:8.0", "root", "password", "db", Map.of());
@@ -889,8 +885,7 @@ class RdsContainerManagerTest {
.thenThrow(new IllegalStateException("java.net.SocketException: Connection reset"));
doThrow(new IllegalStateException("Failed to remove container created-container"))
.when(lifecycleManager).stopAndRemoveStrict(any(), any());
when(lifecycleManager.getDockerClient()).thenThrow(
new RuntimeException("java.net.SocketException: No such file or directory"));
when(lifecycleManager.isDockerReachable()).thenReturn(false);
RdsContainerManager manager = daemonlessCapableManager(lifecycleManager);
String runtimeId = "arn:aws:rds:us-east-1:000000000000:db:db1";
@@ -925,8 +920,7 @@ class RdsContainerManagerTest {
.thenThrow(new IllegalStateException("connection reset after Docker create"));
doThrow(new IllegalStateException("fixed-name cleanup unavailable"))
.when(lifecycleManager).stopAndRemoveStrict(any(), any());
when(lifecycleManager.getDockerClient()).thenThrow(
new RuntimeException("java.net.SocketException: No such file or directory"));
when(lifecycleManager.isDockerReachable()).thenReturn(false);
RdsContainerManager manager = daemonlessCapableManager(lifecycleManager);
String runtimeId = "arn:aws:rds:us-east-1:000000000000:db:db1";