Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(ecr): release registry port when container start fails (#1010)

Juano Morelloc7317c16265a
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 ebe1374c02..ea22726c6d 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
@@ -142,28 +142,28 @@ public class EcrRegistryManager {
config.services().ecr().registryBasePort(),
config.services().ecr().registryMaxPort());
- String image = config.services().ecr().registryImage();
+ try {
+ String image = config.services().ecr().registryImage();
- // Build environment variables
- List<String> env = new ArrayList<>(List.of(
- "REGISTRY_STORAGE_DELETE_ENABLED=true",
- "REGISTRY_HTTP_ADDR=0.0.0.0:" + CONTAINER_INTERNAL_PORT
- ));
+ // Build environment variables
+ List<String> env = new ArrayList<>(List.of(
+ "REGISTRY_STORAGE_DELETE_ENABLED=true",
+ "REGISTRY_HTTP_ADDR=0.0.0.0:" + CONTAINER_INTERNAL_PORT
+ ));
- // Build container spec
- ContainerBuilder.Builder specBuilder = containerBuilder.newContainer(image)
- .withName(name)
- .withEnv(env)
- .withPortBinding(CONTAINER_INTERNAL_PORT, chosenPort)
- .withDockerNetwork(config.services().ecr().dockerNetwork())
- .withLogRotation();
+ // Build container spec
+ ContainerBuilder.Builder specBuilder = containerBuilder.newContainer(image)
+ .withName(name)
+ .withEnv(env)
+ .withPortBinding(CONTAINER_INTERNAL_PORT, chosenPort)
+ .withDockerNetwork(config.services().ecr().dockerNetwork())
+ .withLogRotation();
- // Handle persistence mounting based on storage configuration
- addPersistenceMounts(specBuilder, env);
+ // Handle persistence mounting based on storage configuration
+ addPersistenceMounts(specBuilder, env);
- ContainerSpec spec = specBuilder.build();
+ ContainerSpec spec = specBuilder.build();
- try {
ContainerInfo info = lifecycleManager.createAndStart(spec);
this.containerId = info.containerId();
this.hostPort = chosenPort;
@@ -173,6 +173,12 @@ public class EcrRegistryManager {
// Attach log streaming (new feature)
attachLogStream();
} catch (Exception e) {
+ // Release the reserved port unless the container actually started, so a
+ // failed start (e.g. Docker unreachable) does not permanently exhaust the
+ // registry port pool across retries.
+ if (!started) {
+ portAllocator.release(chosenPort);
+ }
throw new RuntimeException("Failed to start ECR backing registry container: " + e.getMessage(), e);
}
runReconcileOnce();
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
new file mode 100644
index 0000000000..ac8838e741
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java
@@ -0,0 +1,90 @@
+package io.github.hectorvent.floci.services.ecr.registry;
+
+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 io.github.hectorvent.floci.core.common.docker.PortAllocator;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import java.util.Optional;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+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.anyString;
+import static org.mockito.Mockito.when;
+
+/**
+ * Unit tests for {@link EcrRegistryManager} startup behavior. Uses a real
+ * {@link PortAllocator} and a mocked Docker layer so the failure path can be
+ * exercised without a Docker daemon.
+ */
+class EcrRegistryManagerTest {
+
+ private static final int BASE_PORT = 6100;
+ private static final int MAX_PORT = 6101; // pool of exactly two ports
+
+ private PortAllocator portAllocator;
+ private ContainerLifecycleManager lifecycleManager;
+ private EcrRegistryManager manager;
+
+ @BeforeEach
+ void setUp() {
+ portAllocator = new PortAllocator();
+
+ ContainerBuilder 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(Mockito.mock(ContainerSpec.class));
+
+ lifecycleManager = Mockito.mock(ContainerLifecycleManager.class);
+ when(lifecycleManager.findByName(anyString())).thenReturn(Optional.empty());
+
+ ContainerLogStreamer logStreamer = Mockito.mock(ContainerLogStreamer.class);
+ ContainerDetector containerDetector = Mockito.mock(ContainerDetector.class);
+ RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");
+
+ EmulatorConfig config = Mockito.mock(EmulatorConfig.class);
+ EmulatorConfig.EcrServiceConfig ecr = Mockito.mock(EmulatorConfig.EcrServiceConfig.class);
+ EmulatorConfig.StorageConfig storage = Mockito.mock(EmulatorConfig.StorageConfig.class);
+ when(config.services()).thenReturn(Mockito.mock(EmulatorConfig.ServicesConfig.class));
+ when(config.services().ecr()).thenReturn(ecr);
+ when(config.storage()).thenReturn(storage);
+ // Empty host-persistent-path selects named-volume mode (no host bind-mount logic).
+ when(storage.hostPersistentPath()).thenReturn("");
+ when(ecr.registryContainerName()).thenReturn("floci-test-ecr-registry");
+ when(ecr.registryImage()).thenReturn("registry:2");
+ when(ecr.registryBasePort()).thenReturn(BASE_PORT);
+ when(ecr.registryMaxPort()).thenReturn(MAX_PORT);
+ when(ecr.dockerNetwork()).thenReturn(Optional.empty());
+
+ manager = new EcrRegistryManager(containerBuilder, lifecycleManager, logStreamer,
+ containerDetector, portAllocator, config, regionResolver);
+ }
+
+ @Test
+ void ensureStarted_releasesPortWhenDockerStartFails_soPoolIsNotExhausted() {
+ when(lifecycleManager.createAndStart(any()))
+ .thenThrow(new RuntimeException("Cannot connect to the Docker daemon"));
+
+ // Far more attempts than the two-port pool. Every attempt must surface the
+ // real Docker failure. If the reserved port were leaked on failure, the pool
+ // would exhaust after two attempts and later calls would instead fail with
+ // "No free port available" — the symptom this test guards against.
+ for (int attempt = 0; attempt < 6; attempt++) {
+ RuntimeException ex = assertThrows(RuntimeException.class, manager::ensureStarted);
+ assertTrue(ex.getMessage().contains("Failed to start ECR backing registry container"),
+ "attempt " + attempt + " should surface the Docker failure, got: " + ex.getMessage());
+ assertFalse(ex.getMessage().contains("No free port available"),
+ "port pool leaked on attempt " + attempt + ": " + ex.getMessage());
+ }
+ }
+}