From dfeb05a4004490013c179a63f728012da9de36f0 Mon Sep 17 00:00:00 2001 From: Robin Baeckman Date: Fri, 16 Oct 2026 17:50:07 +0200 Subject: [PATCH] fix(docker): adopt a surviving container on the network it was created on (#5811) When Floci runs in Docker, a sidecar it creates that publishes ports (a k3s node, the ECR registry, a Redshift engine, the Floci UI, the AppSync JS runtime) starts on the default bridge and is then connected to its service's network. createAndStart resolves the endpoint on that network. Adopting a surviving container after a restart took the first address in Docker's inspect response instead, which in practice is usually the bridge, and Floci is not on the bridge. Every probe of the adopted container failed; an EKS cluster stayed CREATING. ContainerLifecycleManager.adopt gains an overload that takes the network to prefer and resolves each endpoint with resolveEndpoint(inspect, port, network). Each caller passes the network it creates its container on, through ContainerBuilder.resolveDockerNetwork with the argument it gives withDockerNetwork. A survivor that is not on that network falls back to the first address, as before; the two-argument adopt is unchanged. Co-authored-by: Robin Baeckman <27041454+RobinBaeckman@users.noreply.github.com> --- .../docker/ContainerLifecycleManager.java | 11 ++- .../graphql/js/NodeAppSyncJsRuntime.java | 3 +- .../ecr/registry/EcrRegistryManager.java | 3 +- .../floci/services/eks/EksClusterManager.java | 3 +- .../services/floci/ui/FlociUiManager.java | 3 +- .../container/RedshiftContainerManager.java | 3 +- .../ContainerLifecycleManagerNetworkTest.java | 69 ++++++++++++++++++ .../graphql/js/NodeAppSyncJsRuntimeTest.java | 30 ++++++++ .../ecr/registry/EcrRegistryManagerTest.java | 15 ++-- .../services/eks/EksClusterManagerTest.java | 70 ++++++++++++++----- .../services/floci/ui/FlociUiManagerTest.java | 8 ++- .../RedshiftContainerManagerTest.java | 9 ++- 12 files changed, 192 insertions(+), 35 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java index beb2cb4a3d..e0f66ba33d 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java @@ -939,6 +939,15 @@ public class ContainerLifecycleManager { * @return information about the adopted container */ public ContainerInfo adopt(String containerId, List ports) { + return adopt(containerId, ports, Optional.empty()); + } + + /** + * Adopts a container and resolves its endpoints on {@code preferredNetwork} when it is attached + * there, as {@link #createAndStart} does with the spec's network. A container that is on the + * default bridge as well as that network is otherwise reached at whichever address comes first. + */ + public ContainerInfo adopt(String containerId, List ports, Optional preferredNetwork) { LOG.infov("Adopting existing container {0}", containerId); InspectContainerResponse inspect = dockerClient.inspectContainerCmd(containerId).exec(); @@ -953,7 +962,7 @@ public class ContainerLifecycleManager { Map endpoints = new HashMap<>(); Map publishedHostPorts = new HashMap<>(); for (int port : ports) { - endpoints.put(port, resolveEndpoint(inspect, port)); + endpoints.put(port, resolveEndpoint(inspect, port, preferredNetwork.orElse(null))); OptionalInt published = readPublishedHostPort(inspect, port); if (published.isPresent()) { publishedHostPorts.put(port, published.getAsInt()); diff --git a/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/js/NodeAppSyncJsRuntime.java b/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/js/NodeAppSyncJsRuntime.java index 643267aa80..a0da5bf5d0 100644 --- a/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/js/NodeAppSyncJsRuntime.java +++ b/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/js/NodeAppSyncJsRuntime.java @@ -215,7 +215,8 @@ public class NodeAppSyncJsRuntime implements AppSyncJsRuntime, ContainerTeardown } try { this.containerId = existing.getId(); - ContainerInfo info = lifecycleManager.adopt(containerId, List.of(CONTAINER_INTERNAL_PORT)); + ContainerInfo info = lifecycleManager.adopt(containerId, List.of(CONTAINER_INTERNAL_PORT), + containerBuilder.resolveDockerNetwork(jsRuntimeConfig().dockerNetwork())); this.baseUrl = endpointUrl(info.getEndpoint(CONTAINER_INTERNAL_PORT)); awaitHealthy(ADOPT_PROBE_SECONDS); this.started = true; diff --git a/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java b/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java index fbdafcb026..c41c79abc3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java @@ -628,7 +628,8 @@ public class EcrRegistryManager { private void adoptExisting(Container existing) { this.containerId = existing.getId(); try { - ContainerInfo info = lifecycleManager.adopt(containerId, List.of(CONTAINER_INTERNAL_PORT)); + ContainerInfo info = lifecycleManager.adopt(containerId, List.of(CONTAINER_INTERNAL_PORT), + containerBuilder.resolveDockerNetwork(resolveRegistryDockerNetwork())); // The control plane reaches the backing registry through this published loopback // binding when Floci runs on the host. In Docker, httpClient() uses container DNS. OptionalInt published = info.publishedHostPort(CONTAINER_INTERNAL_PORT); diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java index ea40c10a01..00a34801f3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java @@ -807,7 +807,8 @@ public class EksClusterManager ContainerInfo info; try { - info = lifecycleManager.adopt(containerId, List.of(K3S_API_SERVER_PORT)); + info = lifecycleManager.adopt(containerId, List.of(K3S_API_SERVER_PORT), + containerBuilder.resolveDockerNetwork(config.services().eks().dockerNetwork())); } catch (Exception e) { LOG.warnv("Could not adopt surviving k3s container {0} for EKS cluster {1} ({2}); recreating it", cluster.getDockerName(), cluster.getName(), e.getMessage()); diff --git a/src/main/java/io/github/hectorvent/floci/services/floci/ui/FlociUiManager.java b/src/main/java/io/github/hectorvent/floci/services/floci/ui/FlociUiManager.java index df45ea42e6..2b90810237 100644 --- a/src/main/java/io/github/hectorvent/floci/services/floci/ui/FlociUiManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/floci/ui/FlociUiManager.java @@ -779,7 +779,8 @@ public class FlociUiManager { this.containerId = existing.getId(); try { int internalPort = profile.internalPort(); - ContainerInfo info = lifecycleManager.adopt(containerId, List.of(internalPort)); + ContainerInfo info = lifecycleManager.adopt(containerId, List.of(internalPort), + containerBuilder.resolveDockerNetwork(resolveDockerNetwork())); EndpointInfo endpoint = info.getEndpoint(internalPort); this.hostPort = resolveHostPort(endpoint, config.services().ui().port()); this.probeUrl = resolveProbeUrl(profile, endpoint, hostPort); diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java index 6de5c284e1..bdfab83d37 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java @@ -127,7 +127,8 @@ public class RedshiftContainerManager { LOG.infov("Adopting existing container {0} for cluster {1} to avoid discarding its data", containerName, clusterIdentifier); int enginePort = 5432; - ContainerInfo info = lifecycleManager.adopt(existing.get().getId(), List.of(enginePort)); + ContainerInfo info = lifecycleManager.adopt(existing.get().getId(), List.of(enginePort), + containerBuilder.resolveDockerNetwork(config.services().redshift().dockerNetwork())); return initializeAndRegisterHandle(info, accountId, clusterIdentifier, masterUsername, containerName, enginePort, databaseName); } diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManagerNetworkTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManagerNetworkTest.java index 2d6b0d8fd8..50a77d9594 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManagerNetworkTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManagerNetworkTest.java @@ -13,6 +13,7 @@ import com.github.dockerjava.api.command.StartContainerCmd; import com.github.dockerjava.api.model.ContainerNetwork; import com.github.dockerjava.api.model.HostConfig; import com.github.dockerjava.api.model.NetworkSettings; +import com.github.dockerjava.api.model.Ports; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo; import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo; @@ -253,6 +254,74 @@ class ContainerLifecycleManagerNetworkTest { manager().resolveEndpoint("container-id", 9644, "floci-net")); } + @Test + void adoptReachesASurvivorOnSeveralNetworksThroughThePreferredOne() { + // A container created on the default bridge and then connected to Floci's network lists + // both; only the one Floci shares is reachable from Floci. + when(containerDetector.isRunningInContainer()).thenReturn(true); + InspectContainerResponse.ContainerState running = runningState(); + InspectContainerResponse survivor = inspectOf("survivor-id"); + when(survivor.getState()).thenReturn(running); + NetworkSettings networks = mock(NetworkSettings.class); + when(survivor.getNetworkSettings()).thenReturn(networks); + when(survivor.getHostConfig()).thenReturn(HostConfig.newHostConfig().withNetworkMode("bridge")); + Map attached = new LinkedHashMap<>(); + attached.put("bridge", new ContainerNetwork().withIpv4Address("172.17.0.3")); + attached.put("floci_default", new ContainerNetwork().withIpv4Address("172.18.0.3")); + when(networks.getNetworks()).thenReturn(attached); + when(networks.getPorts()).thenReturn(new Ports()); + + ContainerInfo info = manager().adopt("survivor-id", List.of(6443), Optional.of("floci_default")); + + assertEquals(new EndpointInfo("172.18.0.3", 6443), info.getEndpoint(6443)); + } + + @Test + void adoptWithoutAPreferredNetworkKeepsTakingTheFirstAddress() { + when(containerDetector.isRunningInContainer()).thenReturn(true); + InspectContainerResponse.ContainerState running = runningState(); + InspectContainerResponse survivor = inspectOf("survivor-id"); + when(survivor.getState()).thenReturn(running); + NetworkSettings networks = mock(NetworkSettings.class); + when(survivor.getNetworkSettings()).thenReturn(networks); + when(survivor.getHostConfig()).thenReturn(HostConfig.newHostConfig().withNetworkMode("bridge")); + Map attached = new LinkedHashMap<>(); + attached.put("bridge", new ContainerNetwork().withIpv4Address("172.17.0.3")); + attached.put("floci_default", new ContainerNetwork().withIpv4Address("172.18.0.3")); + when(networks.getNetworks()).thenReturn(attached); + when(networks.getPorts()).thenReturn(new Ports()); + + ContainerInfo info = manager().adopt("survivor-id", List.of(6443)); + + assertEquals(new EndpointInfo("172.17.0.3", 6443), info.getEndpoint(6443)); + } + + @Test + void adoptFallsBackToTheFirstAddressWhenTheSurvivorIsNotOnThePreferredNetwork() { + when(containerDetector.isRunningInContainer()).thenReturn(true); + InspectContainerResponse.ContainerState running = runningState(); + InspectContainerResponse survivor = inspectOf("survivor-id"); + when(survivor.getState()).thenReturn(running); + NetworkSettings networks = mock(NetworkSettings.class); + when(survivor.getNetworkSettings()).thenReturn(networks); + when(survivor.getHostConfig()).thenReturn(HostConfig.newHostConfig().withNetworkMode("bridge")); + Map attached = new LinkedHashMap<>(); + attached.put("bridge", new ContainerNetwork().withIpv4Address("172.17.0.3")); + attached.put("other", new ContainerNetwork().withIpv4Address("172.19.0.3")); + when(networks.getNetworks()).thenReturn(attached); + when(networks.getPorts()).thenReturn(new Ports()); + + ContainerInfo info = manager().adopt("survivor-id", List.of(6443), Optional.of("floci_default")); + + assertEquals(new EndpointInfo("172.17.0.3", 6443), info.getEndpoint(6443)); + } + + private static InspectContainerResponse.ContainerState runningState() { + InspectContainerResponse.ContainerState state = mock(InspectContainerResponse.ContainerState.class); + when(state.getRunning()).thenReturn(true); + return state; + } + private InspectContainerResponse inspectOf(String containerId) { InspectContainerCmd inspectCmd = mock(InspectContainerCmd.class, RETURNS_SELF); InspectContainerResponse inspect = mock(InspectContainerResponse.class); diff --git a/src/test/java/io/github/hectorvent/floci/services/appsync/graphql/js/NodeAppSyncJsRuntimeTest.java b/src/test/java/io/github/hectorvent/floci/services/appsync/graphql/js/NodeAppSyncJsRuntimeTest.java index 23f80b1ab3..07eb5b8aa2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appsync/graphql/js/NodeAppSyncJsRuntimeTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appsync/graphql/js/NodeAppSyncJsRuntimeTest.java @@ -1,8 +1,10 @@ package io.github.hectorvent.floci.services.appsync.graphql.js; import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.dockerjava.api.model.Container; import com.sun.net.httpserver.HttpServer; import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.Digests; 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; @@ -16,6 +18,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; @@ -36,6 +39,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; import static org.mockito.Mockito.withSettings; @@ -115,6 +120,31 @@ class NodeAppSyncJsRuntimeTest { assertTrue(requestedPaths.contains("/evaluate"), "the evaluation goes to the configured URL"); } + @Test + void aSurvivingSidecarIsAdoptedThroughTheNetworkItWasCreatedOn() throws IOException { + when(jsRuntime.url()).thenReturn(Optional.empty()); + when(jsRuntime.containerName()).thenReturn("appsync-js-runtime-test"); + when(jsRuntime.dockerNetwork()).thenReturn(Optional.of("js-net")); + when(containerBuilder.resolveDockerNetwork(Optional.of("js-net"))).thenReturn(Optional.of("js-net")); + String digest; + try (InputStream in = Thread.currentThread().getContextClassLoader() + .getResourceAsStream("appsync/appsync-js-runtime.mjs")) { + digest = Digests.sha256Hex(new String(in.readAllBytes(), StandardCharsets.UTF_8)).substring(0, 16); + } + Container survivor = mock(Container.class); + when(survivor.getId()).thenReturn("survivor"); + when(survivor.getLabels()).thenReturn(Map.of("io.floci.appsync.js-runtime.script", digest)); + when(lifecycleManager.findByName(anyString())).thenReturn(Optional.of(survivor)); + when(lifecycleManager.adopt("survivor", List.of(4600), Optional.of("js-net"))).thenReturn( + new ContainerInfo("survivor", Map.of(4600, + new EndpointInfo("127.0.0.1", server.getAddress().getPort())))); + + runtime.ensureStarted(); + + verify(lifecycleManager).adopt("survivor", List.of(4600), Optional.of("js-net")); + verify(lifecycleManager, never()).createAndStart(any()); + } + /** * The window the concurrency fix closes: a failing evaluation used to clear containerId without diff --git a/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java index ce21f37c56..8fe8056a6b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java @@ -87,6 +87,8 @@ class EcrRegistryManagerTest { builder = Mockito.mock(ContainerBuilder.Builder.class, Mockito.RETURNS_SELF); when(containerBuilder.newContainer(anyString())).thenReturn(builder); when(containerBuilder.resolveImage(anyString())).thenAnswer(inv -> inv.getArgument(0)); + // Echoes its input, so a test sees which network the manager asked to resolve. + when(containerBuilder.resolveDockerNetwork(any())).thenAnswer(inv -> inv.>getArgument(0).map(n -> "resolved:" + n)); when(builder.build()).thenReturn(new ContainerSpec("test-image")); lifecycleManager = Mockito.mock(ContainerLifecycleManager.class); @@ -256,12 +258,15 @@ class EcrRegistryManagerTest { }); when(lifecycleManager.presenceOf("container-id")).thenReturn(ContainerPresence.STOPPED); when(lifecycleManager.findByName(REGISTRY_NAME)).thenReturn(Optional.of(existing)); - when(lifecycleManager.adopt("container-id", List.of(5000))) + // Floci in a container with no configured network: the registry was created on Floci's own. + when(containerDetector.isRunningInContainer()).thenReturn(true); + when(currentContainerNetworkResolver.resolveNetworkName()).thenReturn(Optional.of("floci_net")); + when(lifecycleManager.adopt("container-id", List.of(5000), Optional.of("resolved:floci_net"))) .thenReturn(new ContainerLifecycleManager.ContainerInfo("container-id", Map.of())); manager.ensureStarted(); - verify(lifecycleManager).adopt("container-id", List.of(5000)); + verify(lifecycleManager).adopt("container-id", List.of(5000), Optional.of("resolved:floci_net")); verify(lifecycleManager).createAndStart(any()); assertTrue(manager.isStarted()); } @@ -306,7 +311,7 @@ class EcrRegistryManagerTest { new ContainerPort().withIp("127.0.0.1").withPrivatePort(5000).withPublicPort(BASE_PORT + 1) }); when(lifecycleManager.findByName(REGISTRY_NAME)).thenReturn(Optional.of(existing)); - when(lifecycleManager.adopt("0123456789abcdef", List.of(5000))) + when(lifecycleManager.adopt("0123456789abcdef", List.of(5000), Optional.empty())) .thenReturn(new ContainerLifecycleManager.ContainerInfo("0123456789abcdef", Map.of(5000, new ContainerLifecycleManager.EndpointInfo("172.17.0.5", 5000)), Map.of(5000, BASE_PORT + 1))); @@ -328,7 +333,7 @@ class EcrRegistryManagerTest { new ContainerPort().withIp("127.0.0.1").withPrivatePort(5000).withPublicPort(BASE_PORT + 1) }); when(lifecycleManager.findByName(REGISTRY_NAME)).thenReturn(Optional.of(existing)); - when(lifecycleManager.adopt("0123456789abcdef", List.of(5000))) + when(lifecycleManager.adopt("0123456789abcdef", List.of(5000), Optional.empty())) .thenReturn(new ContainerLifecycleManager.ContainerInfo("0123456789abcdef", Map.of(5000, new ContainerLifecycleManager.EndpointInfo("172.17.0.5", 5000)), Map.of(5000, BASE_PORT + 1))); @@ -347,7 +352,7 @@ class EcrRegistryManagerTest { new ContainerPort().withIp("127.0.0.1").withPrivatePort(5000).withPublicPort(BASE_PORT) }); when(lifecycleManager.findByName(REGISTRY_NAME)).thenReturn(Optional.of(existing)); - when(lifecycleManager.adopt("0123456789abcdef", List.of(5000))) + when(lifecycleManager.adopt("0123456789abcdef", List.of(5000), Optional.empty())) .thenReturn(new ContainerLifecycleManager.ContainerInfo("0123456789abcdef", Map.of())); manager.ensureStarted(); diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java index 8806b77bd5..561e2af9be 100644 --- a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java @@ -489,6 +489,7 @@ class EksClusterManagerTest { private ContainerLifecycleManager lifecycleManager; private PortAllocator portAllocator; private DockerClient dockerClient; + private ContainerBuilder containerBuilder; private EksClusterManager manager; @BeforeEach @@ -510,7 +511,7 @@ class EksClusterManagerTest { when(eks.ecrRegistryMirror()).thenReturn(false); lifecycleManager = Mockito.mock(ContainerLifecycleManager.class); - ContainerBuilder containerBuilder = Mockito.mock(ContainerBuilder.class); + containerBuilder = Mockito.mock(ContainerBuilder.class); ContainerBuilder.Builder builder = Mockito.mock(ContainerBuilder.Builder.class, Mockito.RETURNS_SELF); when(containerBuilder.newContainer(anyString())).thenReturn(builder); when(builder.build()).thenReturn(new ContainerSpec("test-image")); @@ -557,12 +558,45 @@ class EksClusterManagerTest { when(portAllocator.allocate(6440, 6499)).thenReturn(allocatedPort); } + @Test + void adoptsASurvivorThroughTheNetworkClustersAreCreatedOn() { + // k3s starts on the default bridge and is then connected to Floci's network, so the + // survivor is on both and only the second is reachable from Floci. + when(containerBuilder.resolveDockerNetwork(Optional.empty())).thenReturn(Optional.of("floci_default")); + when(lifecycleManager.findByName("floci-eks-demo")) + .thenReturn(Optional.of(survivingContainer("cid-1"))); + when(lifecycleManager.adopt("cid-1", List.of(6443), Optional.of("floci_default"))) + .thenReturn(new ContainerInfo("cid-1", Map.of(), Map.of(6443, 6512))); + + Cluster cluster = cluster(); + manager.restoreCluster(cluster); + + assertEquals("cid-1", cluster.getContainerId()); + verify(lifecycleManager).adopt("cid-1", List.of(6443), Optional.of("floci_default")); + verify(lifecycleManager, never()).create(any()); + } + + @Test + void adoptsASurvivorThroughTheConfiguredEksNetwork() { + when(config.services().eks().dockerNetwork()).thenReturn(Optional.of("eks-net")); + when(containerBuilder.resolveDockerNetwork(Optional.of("eks-net"))).thenReturn(Optional.of("eks-net")); + when(lifecycleManager.findByName("floci-eks-demo")) + .thenReturn(Optional.of(survivingContainer("cid-1"))); + when(lifecycleManager.adopt("cid-1", List.of(6443), Optional.of("eks-net"))) + .thenReturn(new ContainerInfo("cid-1", Map.of(), Map.of(6443, 6512))); + + Cluster cluster = cluster(); + manager.restoreCluster(cluster); + + verify(lifecycleManager).adopt("cid-1", List.of(6443), Optional.of("eks-net")); + } + @Test void adoptsASurvivingContainerAndKeepsItsPublishedPort() { when(lifecycleManager.findByName("floci-eks-demo")) .thenReturn(Optional.of(survivingContainer("cid-1"))); // adopt() starts a stopped container — the Docker-reboot case from #2609. - when(lifecycleManager.adopt("cid-1", List.of(6443))) + when(lifecycleManager.adopt("cid-1", List.of(6443), Optional.empty())) .thenReturn(new ContainerInfo("cid-1", Map.of(), Map.of(6443, 6512))); Cluster cluster = cluster(); @@ -581,7 +615,7 @@ class EksClusterManagerTest { void restoreClusterPreservesPreUpgradeLocalStorageWhenContainerSurvives() { when(lifecycleManager.findByName("floci-eks-demo")) .thenReturn(Optional.of(survivingContainer("cid-pre-upgrade"))); - when(lifecycleManager.adopt("cid-pre-upgrade", List.of(6443))) + when(lifecycleManager.adopt("cid-pre-upgrade", List.of(6443), Optional.empty())) .thenReturn(new ContainerInfo("cid-pre-upgrade", Map.of(), Map.of(6443, 6512))); Cluster cluster = cluster(); @@ -597,7 +631,7 @@ class EksClusterManagerTest { + "\"io.floci.eks.default-storage-class\":\"false\"}}"); when(lifecycleManager.findByName("floci-eks-demo")) .thenReturn(Optional.of(container)); - when(lifecycleManager.adopt("cid-new", List.of(6443))) + when(lifecycleManager.adopt("cid-new", List.of(6443), Optional.empty())) .thenReturn(new ContainerInfo("cid-new", Map.of(), Map.of(6443, 6512))); Cluster cluster = cluster(); @@ -610,7 +644,7 @@ class EksClusterManagerTest { void recreatesAnOldSurvivorWithoutCapacityLimits() { when(lifecycleManager.findByName("floci-eks-demo")) .thenReturn(Optional.of(containerFromJson("{\"Id\":\"cid-old\"}"))); - when(lifecycleManager.adopt("cid-old", List.of(6443))) + when(lifecycleManager.adopt("cid-old", List.of(6443), Optional.empty())) .thenReturn(new ContainerInfo("cid-old", Map.of(), Map.of(6443, 6440))); stubFreshStart("cid-new", 6440); @@ -627,7 +661,7 @@ class EksClusterManagerTest { void restoresOldSurvivorWhenCapacityReplacementFails() { when(lifecycleManager.findByName("floci-eks-demo")) .thenReturn(Optional.of(containerFromJson("{\"Id\":\"cid-old\"}"))); - when(lifecycleManager.adopt("cid-old", List.of(6443))) + when(lifecycleManager.adopt("cid-old", List.of(6443), Optional.empty())) .thenReturn(new ContainerInfo("cid-old", Map.of(), Map.of(6443, 6440))); when(lifecycleManager.create(any())).thenReturn("cid-new"); when(lifecycleManager.startCreated(any(), any())) @@ -835,7 +869,7 @@ class EksClusterManagerTest { when(lifecycleManager.create(any())).thenReturn("cid-new"); when(lifecycleManager.startCreated(any(), any())) .thenThrow(new IllegalStateException("replacement failed")); - when(lifecycleManager.adopt("cid-old", List.of(6443))) + when(lifecycleManager.adopt("cid-old", List.of(6443), Optional.empty())) .thenReturn(new ContainerInfo("cid-old", Map.of(), Map.of(6443, 6441))); Cluster cluster = cluster(); cluster.setDockerName("floci-eks-demo"); @@ -858,7 +892,7 @@ class EksClusterManagerTest { void backupCleanupFailureKeepsTheNewNodeRunning() { when(lifecycleManager.findByName("floci-eks-demo")) .thenReturn(Optional.of(containerFromJson("{\"Id\":\"cid-old\"}"))); - when(lifecycleManager.adopt("cid-old", List.of(6443))) + when(lifecycleManager.adopt("cid-old", List.of(6443), Optional.empty())) .thenReturn(new ContainerInfo("cid-old", Map.of(), Map.of(6443, 6440))); stubFreshStart("cid-new", 6440); Mockito.doNothing().doThrow(new IllegalStateException("Docker cleanup failed")) @@ -868,7 +902,7 @@ class EksClusterManagerTest { manager.restoreCluster(cluster); assertEquals("cid-new", cluster.getContainerId()); - verify(lifecycleManager, Mockito.times(1)).adopt("cid-old", List.of(6443)); + verify(lifecycleManager, Mockito.times(1)).adopt("cid-old", List.of(6443), Optional.empty()); verify(lifecycleManager, never()).removeIfExistsStrict("floci-eks-demo"); } @@ -883,14 +917,14 @@ class EksClusterManagerTest { assertEquals("cid-new", cluster.getContainerId()); assertEquals(6440, cluster.getHostPort()); verify(lifecycleManager).create(any()); - verify(lifecycleManager, never()).adopt(anyString(), any()); + verify(lifecycleManager, never()).adopt(anyString(), any(), any()); } @Test void recreatesWhenTheSurvivingContainerPublishesNoPort() { when(lifecycleManager.findByName("floci-eks-demo")) .thenReturn(Optional.of(survivingContainer("cid-1"))); - when(lifecycleManager.adopt("cid-1", List.of(6443))) + when(lifecycleManager.adopt("cid-1", List.of(6443), Optional.empty())) .thenReturn(new ContainerInfo("cid-1", Map.of())); stubFreshStart("cid-new", 6440); @@ -905,7 +939,7 @@ class EksClusterManagerTest { void recreatesWhenAdoptionFails() { when(lifecycleManager.findByName("floci-eks-demo")) .thenReturn(Optional.of(survivingContainer("cid-1"))); - when(lifecycleManager.adopt(anyString(), any())).thenThrow(new RuntimeException("broken")); + when(lifecycleManager.adopt(anyString(), any(), any())).thenThrow(new RuntimeException("broken")); stubFreshStart("cid-new", 6440); Cluster cluster = cluster(); @@ -964,7 +998,7 @@ class EksClusterManagerTest { when(lifecycleManager.findByName("floci-eks-demo")).thenReturn(Optional.empty()); when(lifecycleManager.findByName("floci-eks-999999999999.demo")) .thenReturn(Optional.of(survivingContainer("cid-9"))); - when(lifecycleManager.adopt("cid-9", List.of(6443))) + when(lifecycleManager.adopt("cid-9", List.of(6443), Optional.empty())) .thenReturn(new ContainerInfo("cid-9", Map.of(), Map.of(6443, 6520))); Cluster cluster = cluster(); @@ -993,7 +1027,7 @@ class EksClusterManagerTest { when(config.defaultAccountId()).thenReturn("000000000000"); when(lifecycleManager.findByName("floci-eks-demo")) .thenReturn(Optional.of(survivingContainerOwnedBy("cid-legacy", "999999999999"))); - when(lifecycleManager.adopt("cid-legacy", List.of(6443))) + when(lifecycleManager.adopt("cid-legacy", List.of(6443), Optional.empty())) .thenReturn(new ContainerInfo("cid-legacy", Map.of(), Map.of(6443, 6512))); Cluster cluster = cluster(); @@ -1022,7 +1056,7 @@ class EksClusterManagerTest { assertEquals("floci-aws-eks-999999999999.demo", cluster.getDockerName()); assertEquals("cid-new", cluster.getContainerId()); - verify(lifecycleManager, never()).adopt(anyString(), any()); + verify(lifecycleManager, never()).adopt(anyString(), any(), any()); verify(lifecycleManager, never()).removeIfExists("floci-eks-demo"); } @@ -1042,7 +1076,7 @@ class EksClusterManagerTest { manager.restoreCluster(cluster); assertEquals("floci-aws-eks-999999999999.demo", cluster.getDockerName()); - verify(lifecycleManager, never()).adopt(anyString(), any()); + verify(lifecycleManager, never()).adopt(anyString(), any(), any()); verify(lifecycleManager, never()).removeIfExists("floci-eks-demo"); } @@ -2209,7 +2243,7 @@ class EksClusterManagerTest { when(container.getLabels()).thenReturn(Map.of( "io.floci.eks.node-capacity", "m5.large:unbounded")); when(lifecycleManager.findByName("floci-eks-my-cluster")).thenReturn(Optional.of(container)); - when(lifecycleManager.adopt(anyString(), any())).thenReturn( + when(lifecycleManager.adopt(anyString(), any(), any())).thenReturn( new ContainerInfo("surviving-container-id", Map.of(6443, new ContainerLifecycleManager.EndpointInfo("localhost", 6500)), Map.of(6443, 6500))); Cluster cluster = new Cluster(); @@ -3499,7 +3533,7 @@ class EksClusterManagerTest { when(container.getLabels()).thenReturn(Map.of( "io.floci.eks.node-capacity", "m5.large:unbounded")); when(lifecycleManager.findByName("floci-eks-prod-cluster")).thenReturn(Optional.of(container)); - when(lifecycleManager.adopt(anyString(), any())).thenReturn( + when(lifecycleManager.adopt(anyString(), any(), any())).thenReturn( new ContainerInfo("adopted-container-id-12345678901234567890", Map.of(6443, new ContainerLifecycleManager.EndpointInfo("localhost", 6500)), Map.of(6443, 6500))); diff --git a/src/test/java/io/github/hectorvent/floci/services/floci/ui/FlociUiManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/floci/ui/FlociUiManagerTest.java index 2c138c00b6..cab06bfee9 100644 --- a/src/test/java/io/github/hectorvent/floci/services/floci/ui/FlociUiManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/floci/ui/FlociUiManagerTest.java @@ -515,7 +515,7 @@ class FlociUiManagerTest { when(lifecycleManager.findByName("floci-ui")).thenReturn(Optional.of(existing)); when(lifecycleManager.containerEnv("abc123")).thenReturn(Optional.empty()); EndpointInfo endpoint = new EndpointInfo("localhost", 4500); - when(lifecycleManager.adopt("abc123", List.of(4500))) + when(lifecycleManager.adopt("abc123", List.of(4500), Optional.empty())) .thenReturn(new ContainerLifecycleManager.ContainerInfo("abc123", Map.of(4500, endpoint))); // Running when it is first adopted, gone by the time status() checks on it. when(lifecycleManager.presenceOf("abc123")) @@ -535,7 +535,7 @@ class FlociUiManagerTest { manager.ensureStartedAsync(); await().atMost(2, TimeUnit.SECONDS) - .untilAsserted(() -> verify(lifecycleManager, times(1)).adopt("abc123", List.of(4500))); + .untilAsserted(() -> verify(lifecycleManager, times(1)).adopt("abc123", List.of(4500), Optional.empty())); // The sidecar is gone (ABSENT) and unreachable (nothing listens on localhost:4500), so // status() must trigger a second async restart. That restart no longer re-adopts the dead @@ -1033,13 +1033,15 @@ class FlociUiManagerTest { ContainerBuilder containerBuilder = mock(ContainerBuilder.class); when(containerBuilder.resolveImage(image)).thenReturn(image); + when(containerBuilder.resolveDockerNetwork(any())).thenAnswer(inv -> inv.>getArgument(0).map(n -> "resolved:" + n)); + when(ui.dockerNetwork()).thenReturn(Optional.of("ui-net")); Container existing = mock(Container.class); when(existing.getId()).thenReturn("ui-container"); when(lifecycleManager.findByName("floci-ui")).thenReturn(Optional.of(existing)); when(lifecycleManager.containerEnv("ui-container")) .thenReturn(Optional.of(List.of("AWS_ENDPOINT_URL=http://custom:4566"))); - when(lifecycleManager.adopt("ui-container", List.of(4500))).thenReturn( + when(lifecycleManager.adopt("ui-container", List.of(4500), Optional.of("resolved:ui-net"))).thenReturn( new ContainerInfo("ui-container", Map.of(4500, new EndpointInfo("127.0.0.1", port)))); FlociUiManager manager = newManager(containerBuilder); diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManagerTest.java index 8ca23d80fc..ef9ae44083 100644 --- a/src/test/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManagerTest.java @@ -66,6 +66,8 @@ class RedshiftContainerManagerTest { @BeforeEach void setUp() { containerBuilder = mock(ContainerBuilder.class); + // Echoes its input, so a test sees which network the manager asked to resolve. + when(containerBuilder.resolveDockerNetwork(any())).thenAnswer(inv -> inv.>getArgument(0).map(n -> "resolved:" + n)); lifecycleManager = mock(ContainerLifecycleManager.class); logStreamer = mock(ContainerLogStreamer.class); containerDetector = mock(ContainerDetector.class); @@ -360,7 +362,8 @@ class RedshiftContainerManagerTest { when(existing.getId()).thenReturn("cont-existing"); when(lifecycleManager.findByName(containerName)).thenReturn(Optional.of(existing)); ContainerInfo adopted = new ContainerInfo("cont-existing", Map.of(5432, new EndpointInfo("localhost", 6000))); - when(lifecycleManager.adopt(eq("cont-existing"), any())).thenReturn(adopted); + when(config.services().redshift().dockerNetwork()).thenReturn(Optional.of("redshift-net")); + when(lifecycleManager.adopt(eq("cont-existing"), any(), eq(Optional.of("resolved:redshift-net")))).thenReturn(adopted); RedshiftContainerHandle handle = manager.adoptOrStart(ACCOUNT_ID, "test-cluster", "admin", "pass"); @@ -385,7 +388,7 @@ class RedshiftContainerManagerTest { RedshiftContainerHandle handle = manager.adoptOrStart(ACCOUNT_ID, "test-cluster", "admin", "pass"); assertEquals("cont-fresh", handle.getContainerId()); - verify(lifecycleManager, never()).adopt(any(), any()); + verify(lifecycleManager, never()).adopt(any(), any(), any()); } @Test @@ -551,7 +554,7 @@ class RedshiftContainerManagerTest { when(existing.getId()).thenReturn("cont-adopt-stream"); when(lifecycleManager.findByName(containerName)).thenReturn(Optional.of(existing)); ContainerInfo adopted = new ContainerInfo("cont-adopt-stream", Map.of(5432, new EndpointInfo("localhost", 6000))); - when(lifecycleManager.adopt(eq("cont-adopt-stream"), any())).thenReturn(adopted); + when(lifecycleManager.adopt(eq("cont-adopt-stream"), any(), eq(Optional.empty()))).thenReturn(adopted); Closeable stream = mock(Closeable.class); when(logStreamer.attach("cont-adopt-stream", "/floci/redshift", "test-cluster", "us-east-1", "redshift:test-cluster")) .thenReturn(stream);