From 735623aa714eaf9d6e99908f61bacbf7c86d5288 Mon Sep 17 00:00:00 2001 From: Richard North Date: Sun, 20 Mar 2016 19:47:48 +0000 Subject: [PATCH 1/2] Refs #90: Proposed solution for environments where container host cannot be determined automatically and needs to be overidden by environment variable/system property --- core/pom.xml | 7 +++- .../dockerclient/DockerClientConfigUtils.java | 6 +++ .../DockerClientConfigUtilsTest.java | 39 +++++++++++++++++-- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 5a120c3c145..6a958088ced 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -54,7 +54,12 @@ 3.0.2 test - + + com.github.stefanbirkner + system-rules + 1.16.0 + test + diff --git a/core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java b/core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java index b93070780a7..71e0ac79f29 100644 --- a/core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java +++ b/core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java @@ -4,6 +4,12 @@ public class DockerClientConfigUtils { public static String getDockerHostIpAddress(DockerClientConfig config) { + + String overrideContainerHost = System.getProperty("testcontainers.container.host", System.getenv("TESTCONTAINERS_CONTAINER_HOST")); + if (overrideContainerHost != null) { + return overrideContainerHost; + } + switch (config.getUri().getScheme()) { case "http": case "https": diff --git a/core/src/test/java/org/testcontainers/dockerclient/DockerClientConfigUtilsTest.java b/core/src/test/java/org/testcontainers/dockerclient/DockerClientConfigUtilsTest.java index 1cb406cece8..238382a67d9 100644 --- a/core/src/test/java/org/testcontainers/dockerclient/DockerClientConfigUtilsTest.java +++ b/core/src/test/java/org/testcontainers/dockerclient/DockerClientConfigUtilsTest.java @@ -1,13 +1,20 @@ package org.testcontainers.dockerclient; +import com.github.dockerjava.core.DockerClientConfig; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.EnvironmentVariables; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -import org.junit.Test; +public class DockerClientConfigUtilsTest { -import com.github.dockerjava.core.DockerClientConfig; + @Rule + public final EnvironmentVariables environmentVariables = new EnvironmentVariables(); -public class DockerClientConfigUtilsTest { + public static final String CONTAINER_HOST_SYSPROP = "testcontainers.container.host"; + public static final String CONTAINER_HOST_ENVVAR = "TESTCONTAINERS_CONTAINER_HOST"; @Test public void getDockerHostIpAddressShouldReturnLocalhostWhenUnixSocket() { @@ -43,4 +50,30 @@ public void getDockerHostIpAddressShouldReturnNullWhenUnsupportedUriScheme() { String actual = DockerClientConfigUtils.getDockerHostIpAddress(configuration); assertNull(actual); } + + @Test + public void getDockerHostIpAddressShouldReturnOverrideWhenSystemPropertyIsSet() { + DockerClientConfig configuration = DockerClientConfig.createDefaultConfigBuilder().withUri("unix:///var/run/docker.sock").build(); + try { + System.setProperty(CONTAINER_HOST_SYSPROP, "12.23.34.45"); + String actual = DockerClientConfigUtils.getDockerHostIpAddress(configuration); + assertEquals("12.23.34.45", actual); + + } finally { + System.clearProperty(CONTAINER_HOST_SYSPROP); + } + } + + @Test + public void getDockerHostIpAddressShouldReturnOverrideWhenEnvVarIsSet() { + DockerClientConfig configuration = DockerClientConfig.createDefaultConfigBuilder().withUri("unix:///var/run/docker.sock").build(); + try { + environmentVariables.set(CONTAINER_HOST_ENVVAR, "12.23.34.45"); + String actual = DockerClientConfigUtils.getDockerHostIpAddress(configuration); + assertEquals("12.23.34.45", actual); + + } finally { + environmentVariables.set(CONTAINER_HOST_ENVVAR, null); + } + } } From e5fd873cb03b1d2af6f76f93f1f1ddbccf342863 Mon Sep 17 00:00:00 2001 From: Richard North Date: Sun, 20 Mar 2016 19:49:44 +0000 Subject: [PATCH 2/2] Clarify Javadocs around use of `getContainerIpAddress()` --- .../java/org/testcontainers/containers/GenericContainer.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/src/main/java/org/testcontainers/containers/GenericContainer.java b/core/src/main/java/org/testcontainers/containers/GenericContainer.java index 50106476a63..2a5723a85ad 100644 --- a/core/src/main/java/org/testcontainers/containers/GenericContainer.java +++ b/core/src/main/java/org/testcontainers/containers/GenericContainer.java @@ -514,6 +514,9 @@ public GenericContainer withClasspathResourceMapping(String resourcePath, String /** * Get the IP address that this container may be reached on (may not be the local machine). * + * If the system property 'testcontainers.container.host' or environment variable 'TESTCONTAINERS_CONTAINER_HOST' + * are set, that value will be returned instead. + * * @return an IP address */ public String getContainerIpAddress() {