diff --git a/src/main/java/io/github/hectorvent/floci/services/floci/duck/FlociDuckManager.java b/src/main/java/io/github/hectorvent/floci/services/floci/duck/FlociDuckManager.java index e9a62b5fa8..fc93675a69 100644 --- a/src/main/java/io/github/hectorvent/floci/services/floci/duck/FlociDuckManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/floci/duck/FlociDuckManager.java @@ -116,6 +116,7 @@ public class FlociDuckManager { ContainerSpec spec = containerBuilder.newContainer(image) .withName(containerName) + .withInit() .withEnv("FLOCI_DUCK_S3_ACCESS_KEY", "test") .withEnv("FLOCI_DUCK_S3_SECRET_KEY", "test") .withEnv("FLOCI_DUCK_S3_REGION", config.defaultRegion()) diff --git a/src/test/java/io/github/hectorvent/floci/services/floci/duck/FlociDuckManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/floci/duck/FlociDuckManagerTest.java new file mode 100644 index 0000000000..3ccc4478fa --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/floci/duck/FlociDuckManagerTest.java @@ -0,0 +1,64 @@ +package io.github.hectorvent.floci.services.floci.duck; + +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer; +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.ContainerSpec; +import io.github.hectorvent.floci.core.common.docker.DockerHostResolver; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +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.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +class FlociDuckManagerTest { + + @Test + void configuredUrlDoesNotStartAContainer() { + EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS); + when(config.services().duck().url()).thenReturn(Optional.of("http://duck.example.test")); + ContainerBuilder builder = mock(ContainerBuilder.class); + ContainerLifecycleManager lifecycle = mock(ContainerLifecycleManager.class); + FlociDuckManager manager = new FlociDuckManager( + builder, lifecycle, mock(ContainerDetector.class), config); + + assertEquals("http://duck.example.test", manager.ensureReady()); + verifyNoInteractions(builder, lifecycle); + } + + @Test + void containerUsesInitSoSigtermReachesTheServer() { + EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS); + when(config.services().duck().url()).thenReturn(Optional.empty()); + when(config.services().duck().defaultImage()).thenReturn("duck-test-image"); + when(config.services().dockerNetwork()).thenReturn(Optional.empty()); + when(config.docker().resourceNamespace()).thenReturn(Optional.empty()); + when(config.docker().logMaxSize()).thenReturn("10m"); + when(config.docker().logMaxFile()).thenReturn("3"); + ContainerBuilder builder = new ContainerBuilder( + config, mock(DockerHostResolver.class), mock(EmbeddedDnsServer.class)); + ContainerLifecycleManager lifecycle = mock(ContainerLifecycleManager.class); + IllegalStateException unavailable = new IllegalStateException("no Docker in this unit test"); + when(lifecycle.createAndStart(any())).thenThrow(unavailable); + FlociDuckManager manager = new FlociDuckManager( + builder, lifecycle, mock(ContainerDetector.class), config); + + assertSame(unavailable, assertThrows(IllegalStateException.class, manager::ensureReady)); + + ArgumentCaptor spec = ArgumentCaptor.forClass(ContainerSpec.class); + verify(lifecycle).createAndStart(spec.capture()); + assertTrue(spec.getValue().init()); + } +}