mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(docker): let the daemon pick dynamic host ports (#5928)
A dynamic port binding sent a port Floci picked with ServerSocket(0), probed where Floci runs. On Docker Desktop the daemon binds it inside the VM, whose ephemeral range lies inside the Mac's, so a VM socket on that number failed the start with "address already in use" and CreateDBInstance returned 500. The binding now leaves the host port empty and the endpoint is read back from inspect, as before.
This commit is contained in:
1 parent
e38dba5f11
commit
8972b21e1b
16 files changed
+72
-85
No files matched your search
+1
-3
@@ -53,7 +53,6 @@ class ContainerLifecycleManagerCaBundleTest {
|
||||
@Mock DockerClient dockerClient;
|
||||
@Mock ImageCacheService imageCacheService;
|
||||
@Mock ContainerDetector containerDetector;
|
||||
@Mock PortAllocator portAllocator;
|
||||
@Mock EmulatorConfig config;
|
||||
@Mock EmulatorConfig.DockerConfig dockerConfig;
|
||||
@Mock EmulatorConfig.TlsConfig tlsConfig;
|
||||
@@ -205,7 +204,6 @@ class ContainerLifecycleManagerCaBundleTest {
|
||||
assertEquals(6, env.getAllValues().get(0).size(), "the first attempt carried the trust variables");
|
||||
assertEquals(List.of("FOO=bar"), env.getAllValues().get(1), "the retry carries only the spec's own");
|
||||
verify(dockerClient, times(1)).copyArchiveToContainerCmd(anyString());
|
||||
verify(portAllocator, never()).allocateAny();
|
||||
}
|
||||
|
||||
private Path writeBundle() throws Exception {
|
||||
@@ -214,7 +212,7 @@ class ContainerLifecycleManagerCaBundleTest {
|
||||
}
|
||||
|
||||
private ContainerLifecycleManager manager() {
|
||||
return new ContainerLifecycleManager(dockerClient, imageCacheService, containerDetector, portAllocator, config);
|
||||
return new ContainerLifecycleManager(dockerClient, imageCacheService, containerDetector, config);
|
||||
}
|
||||
|
||||
private static ContainerSpec specWithEnv(List<String> env) {
|
||||
|
||||
+1
-1
@@ -139,7 +139,7 @@ class ContainerLifecycleManagerCpuLimitTest {
|
||||
ImageCacheService imageCacheService = mock(ImageCacheService.class);
|
||||
when(imageCacheService.ensureImageExists(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
ContainerLifecycleManager manager = new ContainerLifecycleManager(
|
||||
dockerClient, imageCacheService, mock(ContainerDetector.class), mock(PortAllocator.class), config);
|
||||
dockerClient, imageCacheService, mock(ContainerDetector.class), config);
|
||||
|
||||
manager.create(spec);
|
||||
|
||||
|
||||
+1
-4
@@ -45,9 +45,6 @@ class ContainerLifecycleManagerCreateConflictRecoveryTest {
|
||||
@Mock
|
||||
ContainerDetector containerDetector;
|
||||
|
||||
@Mock
|
||||
PortAllocator portAllocator;
|
||||
|
||||
@Mock
|
||||
EmulatorConfig config;
|
||||
|
||||
@@ -221,6 +218,6 @@ class ContainerLifecycleManagerCreateConflictRecoveryTest {
|
||||
|
||||
private ContainerLifecycleManager manager() {
|
||||
return new ContainerLifecycleManager(
|
||||
dockerClient, imageCacheService, containerDetector, portAllocator, config);
|
||||
dockerClient, imageCacheService, containerDetector, config);
|
||||
}
|
||||
}
|
||||
+1
-4
@@ -48,9 +48,6 @@ class ContainerLifecycleManagerDeviceRequestTest {
|
||||
@Mock
|
||||
ContainerDetector containerDetector;
|
||||
|
||||
@Mock
|
||||
PortAllocator portAllocator;
|
||||
|
||||
@Mock
|
||||
EmulatorConfig config;
|
||||
|
||||
@@ -109,7 +106,7 @@ class ContainerLifecycleManagerDeviceRequestTest {
|
||||
|
||||
private ContainerLifecycleManager manager() {
|
||||
return new ContainerLifecycleManager(
|
||||
dockerClient, imageCacheService, containerDetector, portAllocator, config);
|
||||
dockerClient, imageCacheService, containerDetector, config);
|
||||
}
|
||||
|
||||
private static ContainerSpec specWithDeviceRequests(List<DeviceRequest> deviceRequests) {
|
||||
|
||||
+1
-5
@@ -39,15 +39,11 @@ class ContainerLifecycleManagerImageLabelsTest {
|
||||
@Mock
|
||||
ContainerDetector containerDetector;
|
||||
|
||||
@Mock
|
||||
PortAllocator portAllocator;
|
||||
|
||||
@Mock
|
||||
EmulatorConfig config;
|
||||
|
||||
private ContainerLifecycleManager manager() {
|
||||
return new ContainerLifecycleManager(dockerClient, imageCacheService, containerDetector,
|
||||
portAllocator, config);
|
||||
return new ContainerLifecycleManager(dockerClient, imageCacheService, containerDetector, config);
|
||||
}
|
||||
|
||||
private void imageInspectReturns(Map<String, String> labels) {
|
||||
|
||||
+1
-4
@@ -59,9 +59,6 @@ class ContainerLifecycleManagerLabelsTest {
|
||||
@Mock
|
||||
ContainerDetector containerDetector;
|
||||
|
||||
@Mock
|
||||
PortAllocator portAllocator;
|
||||
|
||||
@Mock
|
||||
EmulatorConfig config;
|
||||
|
||||
@@ -292,7 +289,7 @@ class ContainerLifecycleManagerLabelsTest {
|
||||
|
||||
private ContainerLifecycleManager manager() {
|
||||
return new ContainerLifecycleManager(
|
||||
dockerClient, imageCacheService, containerDetector, portAllocator, config);
|
||||
dockerClient, imageCacheService, containerDetector, config);
|
||||
}
|
||||
|
||||
private static ContainerSpec specWithLabels(Map<String, String> labels) {
|
||||
|
||||
+1
-4
@@ -33,9 +33,6 @@ class ContainerLifecycleManagerLivenessTest {
|
||||
@Mock
|
||||
private ContainerDetector containerDetector;
|
||||
|
||||
@Mock
|
||||
private PortAllocator portAllocator;
|
||||
|
||||
@Mock
|
||||
private EmulatorConfig config;
|
||||
|
||||
@@ -47,7 +44,7 @@ class ContainerLifecycleManagerLivenessTest {
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
manager = new ContainerLifecycleManager(
|
||||
dockerClient, imageCacheService, containerDetector, portAllocator, config);
|
||||
dockerClient, imageCacheService, containerDetector, config);
|
||||
when(dockerClient.inspectContainerCmd("c1")).thenReturn(inspectCmd);
|
||||
}
|
||||
|
||||
|
||||
+18
-7
@@ -12,6 +12,7 @@ import com.github.dockerjava.api.command.RemoveContainerCmd;
|
||||
import com.github.dockerjava.api.command.StartContainerCmd;
|
||||
import com.github.dockerjava.api.exception.NotFoundException;
|
||||
import com.github.dockerjava.api.model.ContainerNetwork;
|
||||
import com.github.dockerjava.api.model.ExposedPort;
|
||||
import com.github.dockerjava.api.model.HostConfig;
|
||||
import com.github.dockerjava.api.model.NetworkSettings;
|
||||
import com.github.dockerjava.api.model.Ports;
|
||||
@@ -63,9 +64,6 @@ class ContainerLifecycleManagerNetworkTest {
|
||||
@Mock
|
||||
ContainerDetector containerDetector;
|
||||
|
||||
@Mock
|
||||
PortAllocator portAllocator;
|
||||
|
||||
@Mock
|
||||
EmulatorConfig config;
|
||||
|
||||
@@ -208,10 +206,24 @@ class ContainerLifecycleManagerNetworkTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDoesNotAllocateDynamicPortsOnHostNetwork() {
|
||||
void createPublishesNoDynamicPortOnHostNetwork() {
|
||||
manager().create(spec("host", Map.of(9200, 0), List.of()));
|
||||
|
||||
verify(portAllocator, never()).allocateAny();
|
||||
assertNull(createdHostConfig().getPortBindings());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAndStartReadsTheDaemonsDynamicPortBackFromInspect() {
|
||||
when(dockerClient.startContainerCmd("container-id")).thenReturn(mock(StartContainerCmd.class));
|
||||
Ports published = new Ports();
|
||||
published.bind(ExposedPort.tcp(5432), Ports.Binding.bindPort(55037));
|
||||
NetworkSettings networks = mock(NetworkSettings.class);
|
||||
when(networks.getPorts()).thenReturn(published);
|
||||
when(inspectOf("container-id").getNetworkSettings()).thenReturn(networks);
|
||||
|
||||
ContainerInfo info = manager().createAndStart(spec(null, Map.of(5432, 0), List.of(5432)));
|
||||
|
||||
assertEquals(new EndpointInfo("localhost", 55037), info.getEndpoint(5432));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -366,8 +378,7 @@ class ContainerLifecycleManagerNetworkTest {
|
||||
}
|
||||
|
||||
private ContainerLifecycleManager manager() {
|
||||
return new ContainerLifecycleManager(dockerClient, imageCacheService, containerDetector,
|
||||
portAllocator, config);
|
||||
return new ContainerLifecycleManager(dockerClient, imageCacheService, containerDetector, config);
|
||||
}
|
||||
|
||||
private static ContainerSpec spec(String networkMode, Map<Integer, Integer> portBindings,
|
||||
|
||||
+27
-2
@@ -73,6 +73,32 @@ class ContainerLifecycleManagerPortBindingTest {
|
||||
assertNull(binding.getHostIp());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDynamicPortLeavesTheHostPortToTheDaemon() {
|
||||
Ports.Binding binding = bindingFor(builder -> builder.withDynamicPort(5432), 5432);
|
||||
|
||||
assertNull(binding.getHostIp());
|
||||
assertNull(binding.getHostPortSpec(), "Floci must not pick a port the daemon cannot see");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDynamicLoopbackPortStaysOnLoopback() {
|
||||
Ports.Binding binding = bindingFor(
|
||||
builder -> builder.withLoopbackPortBinding(5050, 0), 5050);
|
||||
|
||||
assertEquals("127.0.0.1", binding.getHostIp());
|
||||
assertNull(binding.getHostPortSpec());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDynamicPortKeepsAnExplicitHostIp() {
|
||||
Ports.Binding binding = bindingFor(
|
||||
builder -> builder.withPortBinding(4500, 0, "127.0.0.1"), 4500);
|
||||
|
||||
assertEquals("127.0.0.1", binding.getHostIp());
|
||||
assertNull(binding.getHostPortSpec());
|
||||
}
|
||||
|
||||
@Test
|
||||
void oneBoundInterfaceDoesNotLeakOntoTheOtherPorts() {
|
||||
ContainerSpec spec = specFor(builder -> builder
|
||||
@@ -114,9 +140,8 @@ class ContainerLifecycleManagerPortBindingTest {
|
||||
when(createCmd.exec()).thenReturn(response);
|
||||
ImageCacheService imageCacheService = mock(ImageCacheService.class);
|
||||
when(imageCacheService.ensureImageExists(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
PortAllocator portAllocator = mock(PortAllocator.class);
|
||||
ContainerLifecycleManager manager = new ContainerLifecycleManager(
|
||||
dockerClient, imageCacheService, mock(ContainerDetector.class), portAllocator, config);
|
||||
dockerClient, imageCacheService, mock(ContainerDetector.class), config);
|
||||
|
||||
manager.create(spec);
|
||||
|
||||
|
||||
+1
-4
@@ -44,9 +44,6 @@ class ContainerLifecycleManagerStartFailureTest {
|
||||
@Mock
|
||||
private ContainerDetector containerDetector;
|
||||
|
||||
@Mock
|
||||
private PortAllocator portAllocator;
|
||||
|
||||
@Mock
|
||||
private EmulatorConfig config;
|
||||
|
||||
@@ -56,7 +53,7 @@ class ContainerLifecycleManagerStartFailureTest {
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
manager = spy(new ContainerLifecycleManager(
|
||||
dockerClient, imageCacheService, containerDetector, portAllocator, config));
|
||||
dockerClient, imageCacheService, containerDetector, config));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+1
-4
@@ -52,9 +52,6 @@ class ContainerLifecycleManagerStartTest {
|
||||
@Mock
|
||||
private ContainerDetector containerDetector;
|
||||
|
||||
@Mock
|
||||
private PortAllocator portAllocator;
|
||||
|
||||
@Mock
|
||||
private EmulatorConfig config;
|
||||
|
||||
@@ -77,7 +74,7 @@ class ContainerLifecycleManagerStartTest {
|
||||
|
||||
private ContainerLifecycleManager manager(DockerClient docker) {
|
||||
return new ContainerLifecycleManager(
|
||||
docker, imageCacheService, containerDetector, portAllocator, config);
|
||||
docker, imageCacheService, containerDetector, config);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+2
-2
@@ -31,7 +31,7 @@ class ContainerLifecycleManagerTeardownTest {
|
||||
@Test
|
||||
void interruptedWaitersStillCleanUpWithoutReleasingAnotherOwnersSlot() throws Exception {
|
||||
DockerClient docker = mock(DockerClient.class);
|
||||
ContainerLifecycleManager manager = new ContainerLifecycleManager(docker, null, null, null, null);
|
||||
ContainerLifecycleManager manager = new ContainerLifecycleManager(docker, null, null, null);
|
||||
StopContainerCmd stop = mock(StopContainerCmd.class);
|
||||
when(docker.stopContainerCmd(anyString())).thenReturn(stop);
|
||||
when(stop.withTimeout(anyInt())).thenReturn(stop);
|
||||
@@ -120,7 +120,7 @@ class ContainerLifecycleManagerTeardownTest {
|
||||
@Test
|
||||
void removalFailuresReleaseAdmissionForLaterOwners() throws Exception {
|
||||
DockerClient docker = mock(DockerClient.class);
|
||||
ContainerLifecycleManager manager = new ContainerLifecycleManager(docker, null, null, null, null);
|
||||
ContainerLifecycleManager manager = new ContainerLifecycleManager(docker, null, null, null);
|
||||
StopContainerCmd stop = mock(StopContainerCmd.class);
|
||||
when(docker.stopContainerCmd(anyString())).thenReturn(stop);
|
||||
when(stop.withTimeout(anyInt())).thenReturn(stop);
|
||||
|
||||
+1
-4
@@ -55,9 +55,6 @@ class ContainerLifecycleManagerVolumeTest {
|
||||
@Mock
|
||||
private ContainerDetector containerDetector;
|
||||
|
||||
@Mock
|
||||
private PortAllocator portAllocator;
|
||||
|
||||
@Mock
|
||||
private EmulatorConfig config;
|
||||
|
||||
@@ -66,7 +63,7 @@ class ContainerLifecycleManagerVolumeTest {
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
manager = new ContainerLifecycleManager(
|
||||
dockerClient, imageCacheService, containerDetector, portAllocator, config);
|
||||
dockerClient, imageCacheService, containerDetector, config);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+1
-1
@@ -75,7 +75,7 @@ class ContainerLifecycleManagerVolumesFromTest {
|
||||
ImageCacheService imageCacheService = mock(ImageCacheService.class);
|
||||
when(imageCacheService.ensureImageExists(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
ContainerLifecycleManager manager = new ContainerLifecycleManager(
|
||||
dockerClient, imageCacheService, mock(ContainerDetector.class), mock(PortAllocator.class), config);
|
||||
dockerClient, imageCacheService, mock(ContainerDetector.class), config);
|
||||
|
||||
manager.create(spec);
|
||||
|
||||
|
||||
Reference in new issue
Block a user