mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(core): avoid Podman host-gateway aliases in container builder (#7572)
* fix(core): avoid Podman host-gateway aliases in container builder * fix(core): share host alias parsing and log probe failures * refactor(lambda): reuse builder host alias guard and test fixture * fix(docker): register version response for native image
This commit is contained in:
1 parent
a613d7b0c3
commit
e9378bb71a
8 files changed
+376
-126
No files matched your search
+184
-1
@@ -1,23 +1,204 @@
|
||||
package io.github.hectorvent.floci.core.common.docker;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.command.VersionCmd;
|
||||
import com.github.dockerjava.api.model.DeviceRequest;
|
||||
import com.github.dockerjava.api.model.Version;
|
||||
import com.github.dockerjava.api.model.VersionComponent;
|
||||
import com.github.dockerjava.api.model.VersionPlatform;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.DockerDaemonVersionFixtures.stubDaemonVersion;
|
||||
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.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class ContainerBuilderTest {
|
||||
|
||||
@Test
|
||||
void withHostDockerInternalOnLinux_omitsAutomaticAliasForPodmanDaemon() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
when(fixture.dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion(fixture.dockerClient, "linux/amd64/fedora-42",
|
||||
List.of(new VersionComponent().withName("Podman Engine")));
|
||||
|
||||
ContainerSpec spec = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
ContainerSpec explicitSpec = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.withExtraHost("host.docker.internal", "192.0.2.10")
|
||||
.build();
|
||||
|
||||
assertEquals(List.of(), spec.extraHosts());
|
||||
assertEquals(List.of("host.docker.internal:192.0.2.10"), explicitSpec.extraHosts());
|
||||
verify(fixture.dockerClient, times(1)).versionCmd();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withHostDockerInternalOnLinux_explicitAliasSkipsPodmanProbe() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
when(fixture.dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion(fixture.dockerClient, "Podman Engine", List.of());
|
||||
|
||||
ContainerSpec spec = fixture.builder.newContainer("alpine")
|
||||
.withExtraHost("host.docker.internal", "192.0.2.10")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
|
||||
assertEquals(List.of("host.docker.internal:192.0.2.10"), spec.extraHosts());
|
||||
verify(fixture.dockerClient, never()).versionCmd();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withHostDockerInternalOnLinux_emptyExplicitAliasDoesNotSuppressGateway() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
when(fixture.dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion(fixture.dockerClient, "Docker Engine - Community", List.of());
|
||||
|
||||
ContainerSpec before = fixture.builder.newContainer("alpine")
|
||||
.withExtraHost("host.docker.internal", "")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
ContainerSpec after = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.withExtraHost("host.docker.internal", "")
|
||||
.build();
|
||||
|
||||
assertFalse(ContainerBuilder.isHostDockerInternalEntry("host.docker.internal:"));
|
||||
assertTrue(ContainerBuilder.isHostDockerInternalEntry("HOST.DOCKER.INTERNAL:192.0.2.10"));
|
||||
assertTrue(before.extraHosts().contains("host.docker.internal:host-gateway"));
|
||||
assertTrue(after.extraHosts().contains("host.docker.internal:host-gateway"));
|
||||
verify(fixture.dockerClient, times(1)).versionCmd();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"Docker Engine - Community", "Moby Engine"})
|
||||
void withHostDockerInternalOnLinux_addsAliasOnceForDockerDaemon(String engineName) {
|
||||
TestFixture fixture = new TestFixture();
|
||||
when(fixture.dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion(fixture.dockerClient, engineName, List.of());
|
||||
|
||||
ContainerSpec first = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
ContainerSpec second = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
|
||||
assertEquals(List.of("host.docker.internal:host-gateway"), first.extraHosts());
|
||||
assertEquals(first.extraHosts(), second.extraHosts());
|
||||
verify(fixture.dockerClient, times(1)).versionCmd();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withHostDockerInternalOnLinux_skipsNonLinuxHostWithoutProbingDaemon() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
|
||||
ContainerSpec spec = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
|
||||
assertEquals(List.of(), spec.extraHosts());
|
||||
verify(fixture.dockerClient, never()).versionCmd();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withHostDockerInternalOnLinux_prefersExplicitAliasInEitherOrder() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
when(fixture.dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion(fixture.dockerClient, "Docker Engine - Community", List.of());
|
||||
|
||||
ContainerSpec before = fixture.builder.newContainer("alpine")
|
||||
.withExtraHost("HOST.DOCKER.INTERNAL", "192.0.2.10")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
ContainerSpec after = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.withExtraHost("host.docker.internal", "192.0.2.11")
|
||||
.build();
|
||||
|
||||
assertEquals(List.of("HOST.DOCKER.INTERNAL:192.0.2.10"), before.extraHosts());
|
||||
assertEquals(List.of("host.docker.internal:192.0.2.11"), after.extraHosts());
|
||||
verify(fixture.dockerClient, times(1)).versionCmd();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withHostDockerInternalOnLinux_keepsAliasForUnknownDaemonAndCachesResult() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
when(fixture.dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion(fixture.dockerClient, "unidentified engine", List.of());
|
||||
|
||||
ContainerSpec first = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
ContainerSpec second = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
|
||||
assertEquals(List.of("host.docker.internal:host-gateway"), first.extraHosts());
|
||||
assertEquals(first.extraHosts(), second.extraHosts());
|
||||
verify(fixture.dockerClient, times(1)).versionCmd();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withHostDockerInternalOnLinux_retriesFailedProbeOnNextSpec() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
when(fixture.dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
VersionCmd versionCmd = mock(VersionCmd.class);
|
||||
Version podmanVersion = mock(Version.class);
|
||||
when(fixture.dockerClient.versionCmd()).thenReturn(versionCmd);
|
||||
when(versionCmd.exec()).thenThrow(new IllegalStateException("/version unavailable"))
|
||||
.thenReturn(podmanVersion);
|
||||
when(podmanVersion.getPlatform()).thenReturn(new VersionPlatform().withName("Podman Engine"));
|
||||
|
||||
ContainerSpec first = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
ContainerSpec second = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
|
||||
assertEquals(List.of("host.docker.internal:host-gateway"), first.extraHosts());
|
||||
assertEquals(List.of(), second.extraHosts());
|
||||
verify(fixture.dockerClient, times(2)).versionCmd();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withHostDockerInternalOnLinux_keepsAliasForMissingDaemonMetadata() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
when(fixture.dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
VersionCmd versionCmd = mock(VersionCmd.class);
|
||||
when(fixture.dockerClient.versionCmd()).thenReturn(versionCmd);
|
||||
when(versionCmd.exec()).thenReturn(new Version());
|
||||
|
||||
ContainerSpec first = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
ContainerSpec second = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
|
||||
assertEquals(List.of("host.docker.internal:host-gateway"), first.extraHosts());
|
||||
assertEquals(first.extraHosts(), second.extraHosts());
|
||||
verify(fixture.dockerClient, times(1)).versionCmd();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withDockerNetwork_usesExplicitServiceNetworkFirst() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
@@ -352,12 +533,14 @@ class ContainerBuilderTest {
|
||||
final EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
final EmulatorConfig.DockerConfig docker = mock(EmulatorConfig.DockerConfig.class);
|
||||
final EmulatorConfig.DnsConfig dns = mock(EmulatorConfig.DnsConfig.class);
|
||||
final DockerClient dockerClient = mock(DockerClient.class);
|
||||
final DockerHostResolver dockerHostResolver = mock(DockerHostResolver.class);
|
||||
final EmbeddedDnsServer embeddedDnsServer = mock(EmbeddedDnsServer.class);
|
||||
final CurrentContainerNetworkResolver currentContainerNetworkResolver =
|
||||
mock(CurrentContainerNetworkResolver.class);
|
||||
final ContainerBuilder builder =
|
||||
new ContainerBuilder(config, dockerHostResolver, embeddedDnsServer, currentContainerNetworkResolver);
|
||||
new ContainerBuilder(config, dockerHostResolver, embeddedDnsServer,
|
||||
currentContainerNetworkResolver, new ContainerEngineDetector(dockerClient));
|
||||
|
||||
TestFixture() {
|
||||
when(config.services()).thenReturn(services);
|
||||
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package io.github.hectorvent.floci.core.common.docker;
|
||||
|
||||
import com.github.dockerjava.api.model.Version;
|
||||
import com.github.dockerjava.api.model.VersionComponent;
|
||||
import com.github.dockerjava.api.model.VersionPlatform;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class DockerJavaNativeSupportTest {
|
||||
|
||||
@Test
|
||||
void versionCommandResponseTypesAreRegisteredForNativeReflection() {
|
||||
RegisterForReflection registration = DockerJavaNativeSupport.class.getAnnotation(RegisterForReflection.class);
|
||||
assertNotNull(registration);
|
||||
List<String> registered = Arrays.asList(registration.classNames());
|
||||
for (Class<?> type : List.of(Version.class, VersionComponent.class, VersionPlatform.class)) {
|
||||
assertTrue(registered.contains(type.getName()), type.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
-47
@@ -7,19 +7,17 @@ import com.github.dockerjava.api.command.CopyArchiveToContainerCmd;
|
||||
import com.github.dockerjava.api.command.ExecCreateCmd;
|
||||
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
|
||||
import com.github.dockerjava.api.command.ExecStartCmd;
|
||||
import com.github.dockerjava.api.command.VersionCmd;
|
||||
import com.github.dockerjava.api.exception.NotFoundException;
|
||||
import com.github.dockerjava.api.model.AccessMode;
|
||||
import com.github.dockerjava.api.model.Frame;
|
||||
import com.github.dockerjava.api.model.Mount;
|
||||
import com.github.dockerjava.api.model.MountType;
|
||||
import com.github.dockerjava.api.model.StreamType;
|
||||
import com.github.dockerjava.api.model.Version;
|
||||
import com.github.dockerjava.api.model.VersionComponent;
|
||||
import com.github.dockerjava.api.model.VersionPlatform;
|
||||
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.ContainerEngineDetector;
|
||||
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.ContainerReachableEndpoint;
|
||||
@@ -74,6 +72,7 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.DockerDaemonVersionFixtures.stubDaemonVersion;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
@@ -458,7 +457,7 @@ class ContainerLauncherTest {
|
||||
private ContainerLauncher newLauncher(ImageResolver selectedResolver,
|
||||
CurrentContainerNetworkResolver networkResolver) {
|
||||
ContainerBuilder builder = new ContainerBuilder(config, dockerHostResolver, embeddedDnsServer,
|
||||
networkResolver);
|
||||
networkResolver, new ContainerEngineDetector(dockerClient));
|
||||
ContainerReachableEndpoint endpoint =
|
||||
new ContainerReachableEndpoint(config, dockerHostResolver, embeddedDnsServer, 4566);
|
||||
return new ContainerLauncher(builder, lifecycleManager, logStreamer, selectedResolver,
|
||||
@@ -467,15 +466,6 @@ class ContainerLauncherTest {
|
||||
executionRoleCredentials);
|
||||
}
|
||||
|
||||
private void stubDaemonVersion(String platformName, List<VersionComponent> components) {
|
||||
VersionCmd versionCmd = mock(VersionCmd.class);
|
||||
Version version = mock(Version.class);
|
||||
when(dockerClient.versionCmd()).thenReturn(versionCmd);
|
||||
when(versionCmd.exec()).thenReturn(version);
|
||||
when(version.getPlatform()).thenReturn(new VersionPlatform().withName(platformName));
|
||||
when(version.getComponents()).thenReturn(components);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"arm64, true, , linux/arm64, arm64",
|
||||
@@ -654,7 +644,8 @@ class ContainerLauncherTest {
|
||||
when(dockerHostResolver.resolve()).thenReturn("10.89.0.5");
|
||||
lenient().when(dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
when(config.services().lambda().extraHosts()).thenReturn(Optional.of(List.of("db.internal:10.0.0.5")));
|
||||
stubDaemonVersion("linux/amd64/fedora-42", List.of(new VersionComponent().withName("Podman Engine")));
|
||||
stubDaemonVersion(dockerClient, "linux/amd64/fedora-42",
|
||||
List.of(new VersionComponent().withName("Podman Engine")));
|
||||
|
||||
newLauncher(imageResolver, networkResolver).launch(imageFunction());
|
||||
|
||||
@@ -671,7 +662,7 @@ class ContainerLauncherTest {
|
||||
when(networkResolver.resolveNetworkName()).thenReturn(Optional.of("docker_compose_default"));
|
||||
when(dockerHostResolver.resolve()).thenReturn("172.18.0.5");
|
||||
lenient().when(dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion("Docker Engine - Community", List.of());
|
||||
stubDaemonVersion(dockerClient, "Docker Engine - Community", List.of());
|
||||
LambdaFunction fn = imageFunction();
|
||||
fn.setEnvironment(Map.of("HOST_SERVICE_URL", "http://host.docker.internal:8080"));
|
||||
|
||||
@@ -688,7 +679,7 @@ class ContainerLauncherTest {
|
||||
void launchFunction_onNativeLinuxDockerAddsHostGatewayForCallback() {
|
||||
when(dockerHostResolver.resolve()).thenReturn("host.docker.internal");
|
||||
when(dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion("Docker Engine - Community", List.of());
|
||||
stubDaemonVersion(dockerClient, "Docker Engine - Community", List.of());
|
||||
|
||||
launcher.launch(imageFunction());
|
||||
|
||||
@@ -724,42 +715,14 @@ class ContainerLauncherTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchFunction_keepsHostAliasWhenDaemonMetadataIsUnknown() {
|
||||
VersionCmd versionCmd = mock(VersionCmd.class);
|
||||
when(dockerClient.versionCmd()).thenReturn(versionCmd);
|
||||
when(versionCmd.exec()).thenReturn(new Version());
|
||||
when(dockerHostResolver.resolve()).thenReturn("172.18.0.5");
|
||||
when(dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
|
||||
launcher.launch(imageFunction());
|
||||
launcher.launch(imageFunction());
|
||||
|
||||
assertEquals(List.of("host.docker.internal:host-gateway"), captureRealContainerSpec().extraHosts());
|
||||
verify(dockerClient, times(1)).versionCmd();
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchFunction_keepsHostAliasWhenVersionProbeFails() {
|
||||
VersionCmd versionCmd = mock(VersionCmd.class);
|
||||
when(dockerClient.versionCmd()).thenReturn(versionCmd);
|
||||
when(versionCmd.exec()).thenThrow(new IllegalStateException("/version unavailable"));
|
||||
when(dockerHostResolver.resolve()).thenReturn("172.18.0.5");
|
||||
void launchFunction_ignoresEmptyHostAliasFromExtraHosts() {
|
||||
when(config.services().lambda().extraHosts()).thenReturn(Optional.of(List.of("host.docker.internal:")));
|
||||
when(dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion(dockerClient, "Docker Engine - Community", List.of());
|
||||
|
||||
launcher.launch(imageFunction());
|
||||
|
||||
assertEquals(List.of("host.docker.internal:host-gateway"), captureRealContainerSpec().extraHosts());
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchFunction_reusesDetectedEngineAcrossInvocations() {
|
||||
when(dockerHostResolver.resolve()).thenReturn("172.18.0.5");
|
||||
when(dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion("Docker Engine - Community", List.of());
|
||||
|
||||
launcher.launch(imageFunction());
|
||||
launcher.launch(imageFunction());
|
||||
|
||||
verify(dockerClient, times(1)).versionCmd();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package io.github.hectorvent.floci.testing;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.command.VersionCmd;
|
||||
import com.github.dockerjava.api.model.Version;
|
||||
import com.github.dockerjava.api.model.VersionComponent;
|
||||
import com.github.dockerjava.api.model.VersionPlatform;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public final class DockerDaemonVersionFixtures {
|
||||
private DockerDaemonVersionFixtures() {
|
||||
}
|
||||
|
||||
public static void stubDaemonVersion(DockerClient dockerClient, String platformName,
|
||||
List<VersionComponent> components) {
|
||||
VersionCmd versionCmd = mock(VersionCmd.class);
|
||||
Version version = mock(Version.class);
|
||||
when(dockerClient.versionCmd()).thenReturn(versionCmd);
|
||||
when(versionCmd.exec()).thenReturn(version);
|
||||
when(version.getPlatform()).thenReturn(new VersionPlatform().withName(platformName));
|
||||
when(version.getComponents()).thenReturn(components);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user