mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(memorydb,neptune,docdb): degrade gracefully with no Docker daemon (#2386)
* fix(memorydb,neptune,docdb): degrade gracefully with no Docker daemon
Applies the daemonless degradation pattern (EcrRegistryManager/RdsContainerManager's
tryStart + isDockerReachable seam) to MemoryDB, Neptune and DocumentDB: control-plane
CreateCluster now reaches AWS's documented 'available' steady state with no backing
container when the Docker daemon is unreachable, instead of leaking a raw
SocketException as a 500. Metadata operations (describe, tag, delete) keep working;
only the data-plane connection to the cache/graph/document database stays unavailable
until a daemon appears.
Changed:
- MemoryDbContainerManager, NeptuneContainerManager, DocDbContainerManager: add
tryStart()/isDockerReachable(), mirroring RdsContainerManager. start() is untouched
for other callers.
- MemoryDbService.startBackend, NeptuneService.createDbCluster,
DocDbService.createDbCluster: use tryStart() and proceed with metadata-only state
(no proxy/container fields, no auth proxy started) when it returns null.
Already done before this change (grepped for tryEnsureStarted|tryStart across
services/ first): ElastiCache (ElastiCacheContainerManager/ElastiCacheMemcachedContainerManager
+ both services) already has this seam — the issue's mention of "elasticache" needing
the fix is stale. EC2 (Ec2ContainerManager) already has an equivalent seam under a
different name (isDockerAvailable()/markContainerlessRunning()); launch, stop, start,
terminate and reboot all already null-check the container id or probe the daemon
before touching Docker, so no EC2 changes were needed.
Left out: dedicated unit tests for EC2's existing daemonless paths (isDockerAvailable,
markContainerlessRunning) — they were already implemented and behaviorally covered by
existing integration tests, and adding manager-level unit tests for the async
executor-based EC2 launch flow is a separable effort from this rollout.
Tests: added tryStart/isDockerReachable unit tests to
{MemoryDb,Neptune,DocDb}ContainerManagerTest (new file for DocDb) covering
daemon-unreachable, genuine-failure and success paths, plus a
createWithoutDockerDaemonStillReachesAvailable service-level test per service
proving control-plane CRUD succeeds and no proxy/container is touched. Existing
tests that stubbed containerManager.start(...) were updated to stub tryStart(...)
to match the new call site.
Refs lex00/floci#44
* test(memorydb,neptune,docdb): log the tolerated teardown IOException
The three new tryStart-reachable tests caught the acceptor socket's
IOException during teardown with only a comment, no log statement,
identically in all three files. Add a debug-level log line per CLAUDE.md's
catch-block rule.
This commit is contained in:
1 parent
337dead734
commit
65b22ff01e
12 files changed
+626
-32
No files matched your search
@@ -118,4 +118,42 @@ class DocDbServiceTest {
|
||||
assertTrue(docDbService.listDbClusters(null).isEmpty());
|
||||
verify(containerManager, never()).stop(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWithoutDockerDaemonStillReachesAvailable() {
|
||||
// tryStart() returns null when no Docker daemon is reachable. The cluster record is
|
||||
// metadata, so the create still succeeds and the cluster reaches 'available' on the
|
||||
// first describe (what SDK/Terraform waiters poll).
|
||||
StorageFactory storageFactory = Mockito.mock(StorageFactory.class);
|
||||
when(storageFactory.create(anyString(), anyString(), any()))
|
||||
.thenAnswer(inv -> AccountAwareStorageBackend.inMemory("000000000000"));
|
||||
EmulatorConfig config = Mockito.mock(EmulatorConfig.class);
|
||||
var servicesConfig = Mockito.mock(EmulatorConfig.ServicesConfig.class);
|
||||
var docdbConfig = Mockito.mock(EmulatorConfig.DocDbServiceConfig.class);
|
||||
when(config.services()).thenReturn(servicesConfig);
|
||||
when(servicesConfig.docdb()).thenReturn(docdbConfig);
|
||||
when(docdbConfig.mock()).thenReturn(false);
|
||||
when(docdbConfig.defaultImage()).thenReturn("mongo:7.0");
|
||||
when(config.hostname()).thenReturn(java.util.Optional.of("localhost"));
|
||||
|
||||
DocDbContainerManager noDaemonContainerManager = Mockito.mock(DocDbContainerManager.class);
|
||||
when(noDaemonContainerManager.tryStart(anyString(), anyString(), anyString(), anyString()))
|
||||
.thenReturn(null);
|
||||
RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");
|
||||
DocDbService noDaemonService = new DocDbService(config, regionResolver, noDaemonContainerManager, storageFactory);
|
||||
|
||||
DocDbCluster created = noDaemonService.createDbCluster(
|
||||
"no-docker-cluster", null, "admin", "secret", false);
|
||||
|
||||
assertEquals("available", created.getStatus());
|
||||
assertEquals("localhost", created.getEndpoint());
|
||||
assertEquals(27017, created.getPort());
|
||||
|
||||
assertEquals("no-docker-cluster",
|
||||
noDaemonService.getDbCluster("no-docker-cluster").getDbClusterIdentifier());
|
||||
|
||||
// Delete must not reach for a container that was never created.
|
||||
noDaemonService.deleteDbCluster("no-docker-cluster");
|
||||
verify(noDaemonContainerManager, never()).stop(any());
|
||||
}
|
||||
}
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
package io.github.hectorvent.floci.services.docdb.container;
|
||||
|
||||
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.core.common.docker.ContainerSpec;
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import org.jboss.logging.Logger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class DocDbContainerManagerTest {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(DocDbContainerManagerTest.class);
|
||||
|
||||
@Test
|
||||
void tryStartReportsUnavailableInsteadOfThrowingWhenNoDockerDaemonIsReachable() {
|
||||
// Floci running inside Docker without a mounted daemon socket: the DocumentDB control
|
||||
// plane must keep working, so the failure is reported rather than propagated.
|
||||
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"));
|
||||
|
||||
DocDbContainerManager manager = newManager(lifecycleManager);
|
||||
|
||||
for (int attempt = 0; attempt < 3; attempt++) {
|
||||
assertNull(manager.tryStart("cluster1", "mongo:7.0", "admin", "secret"),
|
||||
"attempt " + attempt + " should report unavailable");
|
||||
}
|
||||
assertFalse(manager.isDockerReachable());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tryStartPropagatesFailuresRaisedWhileTheDaemonIsReachable() {
|
||||
// A reachable daemon that cannot start the container is a genuine failure, not a
|
||||
// degraded mode: CreateDBCluster must still surface it.
|
||||
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);
|
||||
|
||||
DocDbContainerManager manager = newManager(lifecycleManager);
|
||||
|
||||
RuntimeException failure = assertThrows(RuntimeException.class,
|
||||
() -> manager.tryStart("cluster1", "mongo:7.0", "admin", "secret"));
|
||||
assertEquals("no such image: mongo:7.0", failure.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tryStartReturnsTheHandleOnceADaemonIsReachable() throws IOException, InterruptedException {
|
||||
// Real loopback socket standing in for the Mongo backend's port: the manager's
|
||||
// readiness probe only needs a successful TCP connect.
|
||||
try (ServerSocket serverSocket = new ServerSocket(0)) {
|
||||
Thread acceptor = new Thread(() -> {
|
||||
try (Socket socket = serverSocket.accept()) {
|
||||
socket.getInputStream().read(new byte[1]);
|
||||
} catch (IOException e) {
|
||||
// Test teardown races the socket close; nothing left to assert on.
|
||||
LOG.debugv(e, "Acceptor socket closed during test teardown");
|
||||
}
|
||||
});
|
||||
acceptor.setDaemon(true);
|
||||
acceptor.start();
|
||||
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
when(lifecycleManager.createAndStart(any())).thenReturn(new ContainerLifecycleManager.ContainerInfo(
|
||||
"container-id", Map.of(27017, new ContainerLifecycleManager.EndpointInfo(
|
||||
"127.0.0.1", serverSocket.getLocalPort()))));
|
||||
|
||||
DocDbContainerManager manager = newManager(lifecycleManager);
|
||||
|
||||
DocDbContainerHandle handle = manager.tryStart("cluster1", "mongo:7.0", "admin", "secret");
|
||||
|
||||
assertEquals("container-id", handle.getContainerId());
|
||||
assertEquals(serverSocket.getLocalPort(), handle.getPort());
|
||||
acceptor.join(5000);
|
||||
}
|
||||
}
|
||||
|
||||
private DocDbContainerManager newManager(ContainerLifecycleManager lifecycleManager) {
|
||||
ContainerBuilder containerBuilder = mock(ContainerBuilder.class);
|
||||
ContainerBuilder.Builder builder = mock(ContainerBuilder.Builder.class, Mockito.RETURNS_SELF);
|
||||
when(containerBuilder.newContainer(anyString())).thenReturn(builder);
|
||||
when(builder.build()).thenReturn(mock(ContainerSpec.class));
|
||||
|
||||
ContainerLogStreamer logStreamer = mock(ContainerLogStreamer.class);
|
||||
lenient().when(logStreamer.generateLogStreamName(any())).thenReturn("log-stream");
|
||||
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
EmulatorConfig.DocDbServiceConfig docdb = mock(EmulatorConfig.DocDbServiceConfig.class);
|
||||
when(config.services()).thenReturn(services);
|
||||
when(services.docdb()).thenReturn(docdb);
|
||||
when(docdb.dockerNetwork()).thenReturn(Optional.empty());
|
||||
|
||||
return new DocDbContainerManager(containerBuilder, lifecycleManager, logStreamer,
|
||||
mock(ContainerDetector.class), config, new RegionResolver("us-east-1", "000000000000"));
|
||||
}
|
||||
}
|
||||
+31
-7
@@ -72,7 +72,7 @@ class MemoryDbServiceTest {
|
||||
"000000000000", inv.getArgument(2)).toString());
|
||||
|
||||
when(storageFactory.create(anyString(), anyString(), any())).thenAnswer(inv -> AccountAwareStorageBackend.inMemory("000000000000"));
|
||||
when(containerManager.start(anyString(), anyString()))
|
||||
when(containerManager.tryStart(anyString(), anyString()))
|
||||
.thenReturn(new MemoryDbContainerHandle("cid", "cluster", "localhost", 6379));
|
||||
doNothing().when(proxyManager).startProxy(anyString(), anyBoolean(), anyInt(), anyString(), anyInt(), any());
|
||||
|
||||
@@ -332,7 +332,7 @@ class MemoryDbServiceTest {
|
||||
void failedProvisioningReleasesProxyPort() {
|
||||
when(mdbConfig.proxyBasePort()).thenReturn(16400);
|
||||
when(mdbConfig.proxyMaxPort()).thenReturn(16400); // exactly one port available
|
||||
when(containerManager.start(anyString(), anyString()))
|
||||
when(containerManager.tryStart(anyString(), anyString()))
|
||||
.thenThrow(new RuntimeException("docker unavailable"))
|
||||
.thenReturn(new MemoryDbContainerHandle("cid", "c2", "localhost", 6379));
|
||||
|
||||
@@ -352,7 +352,7 @@ class MemoryDbServiceTest {
|
||||
@Test
|
||||
void failedProvisioningRollsBackContainerAndReleasesProxyPort() {
|
||||
MemoryDbContainerHandle handle = new MemoryDbContainerHandle("cid", "c1", "localhost", 6379);
|
||||
when(containerManager.start(anyString(), anyString())).thenReturn(handle);
|
||||
when(containerManager.tryStart(anyString(), anyString())).thenReturn(handle);
|
||||
|
||||
// Proxy startup blows up after the port is reserved and the container is started.
|
||||
doThrow(new RuntimeException("proxy boom"))
|
||||
@@ -387,11 +387,11 @@ class MemoryDbServiceTest {
|
||||
|
||||
@Test
|
||||
void failedContainerStartupCleansUpContainerByNameAndReleasesPort() {
|
||||
// containerManager.start(...) throws — this models both a container that never started
|
||||
// containerManager.tryStart(...) throws — this models both a container that never started
|
||||
// and (crucially) a readiness timeout, where start() created + registered the container
|
||||
// before throwing, so no handle ever reaches the service.
|
||||
doThrow(new RuntimeException("readiness boom"))
|
||||
.when(containerManager).start(eq("c1"), anyString());
|
||||
.when(containerManager).tryStart(eq("c1"), anyString());
|
||||
|
||||
Cluster spec = new Cluster();
|
||||
spec.setName("c1");
|
||||
@@ -407,7 +407,7 @@ class MemoryDbServiceTest {
|
||||
verify(containerManager).stopByClusterName("c1");
|
||||
|
||||
// The reserved proxy port was still released: a subsequent successful create reuses the base port.
|
||||
when(containerManager.start(anyString(), anyString()))
|
||||
when(containerManager.tryStart(anyString(), anyString()))
|
||||
.thenReturn(new MemoryDbContainerHandle("cid", "c2", "localhost", 6379));
|
||||
Cluster second = new Cluster();
|
||||
second.setName("c2");
|
||||
@@ -436,7 +436,7 @@ class MemoryDbServiceTest {
|
||||
void concurrentCreateForSameNameIsRejectedWhileFirstIsProvisioning() throws InterruptedException {
|
||||
CountDownLatch startedLatch = new CountDownLatch(1);
|
||||
CountDownLatch releaseLatch = new CountDownLatch(1);
|
||||
when(containerManager.start(anyString(), anyString())).thenAnswer(inv -> {
|
||||
when(containerManager.tryStart(anyString(), anyString())).thenAnswer(inv -> {
|
||||
startedLatch.countDown();
|
||||
assertTrue(releaseLatch.await(5, TimeUnit.SECONDS), "test timed out waiting for release");
|
||||
return new MemoryDbContainerHandle("cid", "c1", "localhost", 6379);
|
||||
@@ -463,4 +463,28 @@ class MemoryDbServiceTest {
|
||||
|
||||
assertEquals(ClusterStatus.AVAILABLE, service.getCluster("c1").getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWithoutDockerDaemonStillReachesAvailable() {
|
||||
// tryStart() returns null when no Docker daemon is reachable. The cluster record is
|
||||
// metadata, so the create still succeeds, the cluster reaches 'available' on the first
|
||||
// describe (what SDK/Terraform waiters poll), and no auth proxy is started.
|
||||
when(containerManager.tryStart(anyString(), anyString())).thenReturn(null);
|
||||
|
||||
Cluster spec = new Cluster();
|
||||
spec.setName("no-docker-cluster");
|
||||
spec.setAclName("open-access");
|
||||
Cluster created = service.createCluster(spec, "us-east-1");
|
||||
|
||||
assertEquals(ClusterStatus.AVAILABLE, created.getStatus());
|
||||
assertEquals("localhost", created.getClusterEndpoint().address());
|
||||
assertEquals(16400, created.getProxyPort());
|
||||
verify(proxyManager, never()).startProxy(anyString(), anyBoolean(), anyInt(), anyString(), anyInt(), any());
|
||||
|
||||
assertEquals("no-docker-cluster", service.getCluster("no-docker-cluster").getName());
|
||||
|
||||
// Delete must not reach for a container that was never created.
|
||||
service.deleteCluster("no-docker-cluster");
|
||||
verify(containerManager, never()).stop(any());
|
||||
}
|
||||
}
|
||||
+111
@@ -6,13 +6,34 @@ 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.core.common.docker.ContainerSpec;
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import org.jboss.logging.Logger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class MemoryDbContainerManagerTest {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(MemoryDbContainerManagerTest.class);
|
||||
|
||||
@Test
|
||||
void stopByClusterNameRemovesByDeterministicNameWhenNothingRegistered() {
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
@@ -26,4 +47,94 @@ class MemoryDbContainerManagerTest {
|
||||
|
||||
verify(lifecycleManager).removeIfExists("floci-memorydb-my-cluster");
|
||||
}
|
||||
|
||||
@Test
|
||||
void tryStartReportsUnavailableInsteadOfThrowingWhenNoDockerDaemonIsReachable() {
|
||||
// Floci running inside Docker without a mounted daemon socket: the MemoryDB control
|
||||
// plane must keep working, so the failure is reported rather than propagated.
|
||||
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"));
|
||||
|
||||
MemoryDbContainerManager manager = newManager(lifecycleManager);
|
||||
|
||||
for (int attempt = 0; attempt < 3; attempt++) {
|
||||
assertNull(manager.tryStart("cluster1", "valkey/valkey:8"),
|
||||
"attempt " + attempt + " should report unavailable");
|
||||
}
|
||||
assertFalse(manager.isDockerReachable());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tryStartPropagatesFailuresRaisedWhileTheDaemonIsReachable() {
|
||||
// A reachable daemon that cannot start the container is a genuine failure, not a
|
||||
// degraded mode: CreateCluster must still surface it.
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
when(lifecycleManager.createAndStart(any()))
|
||||
.thenThrow(new RuntimeException("no such image: valkey/valkey:8"));
|
||||
DockerClient dockerClient = mock(DockerClient.class, Mockito.RETURNS_DEEP_STUBS);
|
||||
when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);
|
||||
|
||||
MemoryDbContainerManager manager = newManager(lifecycleManager);
|
||||
|
||||
RuntimeException failure = assertThrows(RuntimeException.class,
|
||||
() -> manager.tryStart("cluster1", "valkey/valkey:8"));
|
||||
assertEquals("no such image: valkey/valkey:8", failure.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tryStartReturnsTheHandleOnceADaemonIsReachable() throws IOException, InterruptedException {
|
||||
// Real loopback socket standing in for the backend container's Redis port, so the
|
||||
// manager's PING/PONG readiness probe succeeds without a real container.
|
||||
try (ServerSocket serverSocket = new ServerSocket(0)) {
|
||||
Thread responder = new Thread(() -> {
|
||||
try (Socket socket = serverSocket.accept()) {
|
||||
socket.getInputStream().read(new byte[64]);
|
||||
OutputStream out = socket.getOutputStream();
|
||||
out.write("+PONG\r\n".getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
// Test teardown races the socket close; nothing left to assert on.
|
||||
LOG.debugv(e, "Acceptor socket closed during test teardown");
|
||||
}
|
||||
});
|
||||
responder.setDaemon(true);
|
||||
responder.start();
|
||||
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
when(lifecycleManager.createAndStart(any())).thenReturn(new ContainerLifecycleManager.ContainerInfo(
|
||||
"container-id", Map.of(6379, new ContainerLifecycleManager.EndpointInfo(
|
||||
"127.0.0.1", serverSocket.getLocalPort()))));
|
||||
|
||||
MemoryDbContainerManager manager = newManager(lifecycleManager);
|
||||
|
||||
MemoryDbContainerHandle handle = manager.tryStart("cluster1", "valkey/valkey:8");
|
||||
|
||||
assertEquals("container-id", handle.getContainerId());
|
||||
assertEquals(serverSocket.getLocalPort(), handle.getPort());
|
||||
responder.join(5000);
|
||||
}
|
||||
}
|
||||
|
||||
private MemoryDbContainerManager newManager(ContainerLifecycleManager lifecycleManager) {
|
||||
ContainerBuilder containerBuilder = mock(ContainerBuilder.class);
|
||||
ContainerBuilder.Builder builder = mock(ContainerBuilder.Builder.class, Mockito.RETURNS_SELF);
|
||||
when(containerBuilder.newContainer(anyString())).thenReturn(builder);
|
||||
when(builder.build()).thenReturn(mock(ContainerSpec.class));
|
||||
|
||||
ContainerLogStreamer logStreamer = mock(ContainerLogStreamer.class);
|
||||
lenient().when(logStreamer.generateLogStreamName(any())).thenReturn("log-stream");
|
||||
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
EmulatorConfig.MemoryDbServiceConfig memorydb = mock(EmulatorConfig.MemoryDbServiceConfig.class);
|
||||
when(config.services()).thenReturn(services);
|
||||
when(services.memorydb()).thenReturn(memorydb);
|
||||
when(memorydb.dockerNetwork()).thenReturn(Optional.empty());
|
||||
|
||||
return new MemoryDbContainerManager(containerBuilder, lifecycleManager, logStreamer,
|
||||
mock(ContainerDetector.class), config, new RegionResolver("us-east-1", "000000000000"));
|
||||
}
|
||||
}
|
||||
@@ -58,7 +58,7 @@ class NeptuneServiceTest {
|
||||
|
||||
when(storageFactory.create(anyString(), anyString(), any()))
|
||||
.thenAnswer(inv -> AccountAwareStorageBackend.inMemory("000000000000"));
|
||||
when(containerManager.start(anyString(), anyString(), any(NeptuneDbType.class)))
|
||||
when(containerManager.tryStart(anyString(), anyString(), any(NeptuneDbType.class)))
|
||||
.thenReturn(new NeptuneContainerHandle("cid", "c", "localhost", 8182));
|
||||
doNothing().when(proxyManager).startProxy(anyString(), anyInt(), anyString(), anyInt());
|
||||
|
||||
@@ -68,7 +68,7 @@ class NeptuneServiceTest {
|
||||
@Test
|
||||
void failedProvisioningRollsBackContainerAndReleasesProxyPort() {
|
||||
NeptuneContainerHandle handle = new NeptuneContainerHandle("cid", "c", "localhost", 8182);
|
||||
when(containerManager.start(anyString(), anyString(), any(NeptuneDbType.class)))
|
||||
when(containerManager.tryStart(anyString(), anyString(), any(NeptuneDbType.class)))
|
||||
.thenReturn(handle);
|
||||
|
||||
// Proxy startup blows up after the port is reserved and the container is started.
|
||||
@@ -94,7 +94,7 @@ class NeptuneServiceTest {
|
||||
@Test
|
||||
void jvmErrorDuringProvisioningStillRollsBack() {
|
||||
NeptuneContainerHandle handle = new NeptuneContainerHandle("cid", "c", "localhost", 8182);
|
||||
when(containerManager.start(anyString(), anyString(), any(NeptuneDbType.class)))
|
||||
when(containerManager.tryStart(anyString(), anyString(), any(NeptuneDbType.class)))
|
||||
.thenReturn(handle);
|
||||
|
||||
// A JVM Error (not a RuntimeException) escapes provisioning — a catch (RuntimeException)
|
||||
@@ -117,11 +117,11 @@ class NeptuneServiceTest {
|
||||
|
||||
@Test
|
||||
void failedContainerStartupCleansUpContainerByIdAndReleasesPort() {
|
||||
// containerManager.start(...) throws — this models both a container that never started
|
||||
// containerManager.tryStart(...) throws — this models both a container that never started
|
||||
// and (crucially) a readiness timeout, where start() created + registered the container
|
||||
// before throwing, so no handle ever reaches the service.
|
||||
doThrow(new RuntimeException("readiness boom"))
|
||||
.when(containerManager).start(eq("c"), anyString(), any(NeptuneDbType.class));
|
||||
.when(containerManager).tryStart(eq("c"), anyString(), any(NeptuneDbType.class));
|
||||
|
||||
// The original failure must propagate to the caller (we clean up, then rethrow).
|
||||
assertThrows(RuntimeException.class,
|
||||
@@ -134,7 +134,7 @@ class NeptuneServiceTest {
|
||||
verify(containerManager).stopByClusterId("c");
|
||||
|
||||
// The reserved proxy port was still released: a subsequent successful create reuses the base port.
|
||||
when(containerManager.start(anyString(), anyString(), any(NeptuneDbType.class)))
|
||||
when(containerManager.tryStart(anyString(), anyString(), any(NeptuneDbType.class)))
|
||||
.thenReturn(new NeptuneContainerHandle("cid", "c2", "localhost", 8182));
|
||||
NeptuneCluster recovered = service.createDbCluster("c2", "1.3.2.1", false);
|
||||
assertEquals(18182, recovered.getProxyPort(),
|
||||
@@ -180,4 +180,25 @@ class NeptuneServiceTest {
|
||||
assertEquals("InsufficientStorageClusterCapacity", e.getErrorCode());
|
||||
assertEquals(400, e.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWithoutDockerDaemonStillReachesAvailable() {
|
||||
// tryStart() returns null when no Docker daemon is reachable. The cluster record is
|
||||
// metadata, so the create still succeeds, the cluster reaches 'available' on the first
|
||||
// describe (what SDK/Terraform waiters poll), and no proxy is started.
|
||||
when(containerManager.tryStart(anyString(), anyString(), any(NeptuneDbType.class))).thenReturn(null);
|
||||
|
||||
NeptuneCluster created = service.createDbCluster("no-docker-cluster", "1.3.2.1", false);
|
||||
|
||||
assertEquals("available", created.getStatus());
|
||||
assertEquals("localhost", created.getEndpoint());
|
||||
assertEquals(18182, created.getProxyPort());
|
||||
verify(proxyManager, never()).startProxy(anyString(), anyInt(), anyString(), anyInt());
|
||||
|
||||
assertEquals("no-docker-cluster", service.getDbCluster("no-docker-cluster").getDbClusterIdentifier());
|
||||
|
||||
// Delete must not reach for a container that was never created.
|
||||
service.deleteDbCluster("no-docker-cluster");
|
||||
verify(containerManager, never()).stop(any());
|
||||
}
|
||||
}
|
||||
+113
@@ -6,13 +6,35 @@ 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.core.common.docker.ContainerSpec;
|
||||
import io.github.hectorvent.floci.services.neptune.model.NeptuneDbType;
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import org.jboss.logging.Logger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class NeptuneContainerManagerTest {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(NeptuneContainerManagerTest.class);
|
||||
|
||||
@Test
|
||||
void stopByClusterIdRemovesByDeterministicNameWhenNothingRegistered() {
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
@@ -26,4 +48,95 @@ class NeptuneContainerManagerTest {
|
||||
|
||||
verify(lifecycleManager).removeIfExists("floci-neptune-my-cluster");
|
||||
}
|
||||
|
||||
@Test
|
||||
void tryStartReportsUnavailableInsteadOfThrowingWhenNoDockerDaemonIsReachable() {
|
||||
// Floci running inside Docker without a mounted daemon socket: the Neptune control
|
||||
// plane must keep working, so the failure is reported rather than propagated.
|
||||
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"));
|
||||
|
||||
NeptuneContainerManager manager = newManager(lifecycleManager);
|
||||
|
||||
for (int attempt = 0; attempt < 3; attempt++) {
|
||||
assertNull(manager.tryStart("cluster1", "tinkerpop/gremlin-server:3.7.3", NeptuneDbType.GREMLIN),
|
||||
"attempt " + attempt + " should report unavailable");
|
||||
}
|
||||
assertFalse(manager.isDockerReachable());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tryStartPropagatesFailuresRaisedWhileTheDaemonIsReachable() {
|
||||
// A reachable daemon that cannot start the container is a genuine failure, not a
|
||||
// degraded mode: CreateDBCluster must still surface it.
|
||||
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);
|
||||
|
||||
NeptuneContainerManager manager = newManager(lifecycleManager);
|
||||
|
||||
RuntimeException failure = assertThrows(RuntimeException.class,
|
||||
() -> manager.tryStart("cluster1", "tinkerpop/gremlin-server:3.7.3", NeptuneDbType.GREMLIN));
|
||||
assertEquals("no such image: tinkerpop/gremlin-server:3.7.3", failure.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tryStartReturnsTheHandleOnceADaemonIsReachable() throws IOException, InterruptedException {
|
||||
// Real loopback socket standing in for the Gremlin backend's port: any reply to the
|
||||
// manager's readiness probe confirms it is listening.
|
||||
try (ServerSocket serverSocket = new ServerSocket(0)) {
|
||||
Thread responder = new Thread(() -> {
|
||||
try (Socket socket = serverSocket.accept()) {
|
||||
socket.getInputStream().read(new byte[64]);
|
||||
OutputStream out = socket.getOutputStream();
|
||||
out.write("HTTP/1.1 400 Bad Request\r\n\r\n".getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
// Test teardown races the socket close; nothing left to assert on.
|
||||
LOG.debugv(e, "Acceptor socket closed during test teardown");
|
||||
}
|
||||
});
|
||||
responder.setDaemon(true);
|
||||
responder.start();
|
||||
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
when(lifecycleManager.createAndStart(any())).thenReturn(new ContainerLifecycleManager.ContainerInfo(
|
||||
"container-id", Map.of(8182, new ContainerLifecycleManager.EndpointInfo(
|
||||
"127.0.0.1", serverSocket.getLocalPort()))));
|
||||
|
||||
NeptuneContainerManager manager = newManager(lifecycleManager);
|
||||
|
||||
NeptuneContainerHandle handle =
|
||||
manager.tryStart("cluster1", "tinkerpop/gremlin-server:3.7.3", NeptuneDbType.GREMLIN);
|
||||
|
||||
assertEquals("container-id", handle.getContainerId());
|
||||
assertEquals(serverSocket.getLocalPort(), handle.getPort());
|
||||
responder.join(5000);
|
||||
}
|
||||
}
|
||||
|
||||
private NeptuneContainerManager newManager(ContainerLifecycleManager lifecycleManager) {
|
||||
ContainerBuilder containerBuilder = mock(ContainerBuilder.class);
|
||||
ContainerBuilder.Builder builder = mock(ContainerBuilder.Builder.class, Mockito.RETURNS_SELF);
|
||||
when(containerBuilder.newContainer(anyString())).thenReturn(builder);
|
||||
when(builder.build()).thenReturn(mock(ContainerSpec.class));
|
||||
|
||||
ContainerLogStreamer logStreamer = mock(ContainerLogStreamer.class);
|
||||
lenient().when(logStreamer.generateLogStreamName(any())).thenReturn("log-stream");
|
||||
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
EmulatorConfig.NeptuneServiceConfig neptune = mock(EmulatorConfig.NeptuneServiceConfig.class);
|
||||
when(config.services()).thenReturn(services);
|
||||
when(services.neptune()).thenReturn(neptune);
|
||||
when(neptune.dockerNetwork()).thenReturn(Optional.empty());
|
||||
|
||||
return new NeptuneContainerManager(containerBuilder, lifecycleManager, logStreamer,
|
||||
mock(ContainerDetector.class), config, new RegionResolver("us-east-1", "000000000000"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user