From 8dbe348c44f3e1e4f53365917f8c7072feb61000 Mon Sep 17 00:00:00 2001 From: Thorsten Binias Date: Fri, 22 Apr 2016 14:53:58 +0200 Subject: [PATCH 1/2] add support for extra hosts to GenericContainer --- .../containers/GenericContainer.java | 20 +++++++++++ .../junit/GenericContainerRuleTest.java | 33 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/core/src/main/java/org/testcontainers/containers/GenericContainer.java b/core/src/main/java/org/testcontainers/containers/GenericContainer.java index e3c46b6008a..6841be54db0 100644 --- a/core/src/main/java/org/testcontainers/containers/GenericContainer.java +++ b/core/src/main/java/org/testcontainers/containers/GenericContainer.java @@ -9,9 +9,11 @@ import com.github.dockerjava.api.model.*; import com.google.common.base.Preconditions; import com.google.common.base.Strings; + import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; + import org.jetbrains.annotations.Nullable; import org.junit.runner.Description; import org.rnorth.ducttape.TimeoutException; @@ -71,6 +73,9 @@ public class GenericContainer extends FailureDetectingExternalResource implement @NonNull private List portBindings = new ArrayList<>(); + @NonNull + private List extraHosts = new ArrayList<>(); + @NonNull private Future image; @@ -111,6 +116,7 @@ public class GenericContainer extends FailureDetectingExternalResource implement @Nullable private InspectContainerResponse containerInfo; + private static final Set AVAILABLE_IMAGE_NAME_CACHE = new HashSet<>(); private static final RateLimiter DOCKER_CLIENT_RATE_LIMITER = RateLimiterBuilder .newBuilder() @@ -336,6 +342,10 @@ private void applyConfiguration(CreateContainerCmd createCommand) { createCommand.withLinks(linksArray); createCommand.withPublishAllPorts(true); + + String[] extraHostsArray = extraHosts.stream() + .toArray(String[]::new); + createCommand.withExtraHosts(extraHostsArray); } /** @@ -494,6 +504,16 @@ public GenericContainer withCommand(String... commandParts) { return this; } + /** + * Add an extra host entry to be passed to the container + * + * @param hostEntry for e.g. "somehost:192.168.47.11" + * @return this + */ + public GenericContainer withExtraHost(String hostEntry) { + this.extraHosts.add(hostEntry); + return this; + } /** * Map a resource (file or directory) on the classpath to a path inside the container. diff --git a/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java b/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java index a0ad5d7fc92..b3b3042ebd1 100644 --- a/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java +++ b/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java @@ -5,6 +5,7 @@ import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.rabbitmq.client.*; + import org.bson.Document; import org.junit.BeforeClass; import org.junit.ClassRule; @@ -22,6 +23,8 @@ import java.util.List; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals; import static org.rnorth.visibleassertions.VisibleAssertions.assertThrows; @@ -90,6 +93,14 @@ public static void setupContent() throws FileNotFoundException { .withClasspathResourceMapping("mappable-resource/test-resource.txt", "/content.txt", READ_ONLY) .withCommand("/bin/sh", "-c", "while true; do cat /content.txt | nc -l -p 80; done"); + /** + * Create a container with an extra host entry and expose the content of /etc/hosts for testing. + */ + @ClassRule + public static GenericContainer alpineExtrahost = new GenericContainer("alpine:3.2") + .withExposedPorts(80) + .withExtraHost("somehost:192.168.1.10") + .withCommand("/bin/sh", "-c", "while true; do cat /etc/hosts | nc -l -p 80; done"); @Test public void simpleRedisTest() { @@ -255,4 +266,26 @@ public void testExecInContainer() throws Exception { // Once they resolve the issue, this clause can be removed. } } + + @Test + public void extraHostTest() throws IOException { + BufferedReader br = Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> { + Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); + + Socket socket = new Socket(alpineExtrahost.getContainerIpAddress(), alpineExtrahost.getMappedPort(80)); + return new BufferedReader(new InputStreamReader(socket.getInputStream())); + }); + + // read hosts file from container + StringBuffer hosts = new StringBuffer(); + String line = br.readLine(); + while (line != null) { + hosts.append(line); + hosts.append("\n"); + line = br.readLine(); + } + + Matcher matcher = Pattern.compile("^192.168.1.10\\s.*somehost", Pattern.MULTILINE).matcher(hosts.toString()); + assertTrue("The hosts file of container contains extra host", matcher.find()); + } } From 0d7224437eb3e187d9a3082614cd20b02bed28a1 Mon Sep 17 00:00:00 2001 From: Thorsten Binias Date: Sun, 24 Apr 2016 18:07:36 +0200 Subject: [PATCH 2/2] split parameter for extra host in 'hostname' and 'ipAddress', fix indent --- .../testcontainers/containers/GenericContainer.java | 12 ++++++------ .../junit/GenericContainerRuleTest.java | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/org/testcontainers/containers/GenericContainer.java b/core/src/main/java/org/testcontainers/containers/GenericContainer.java index 6841be54db0..cfc76f3ed5f 100644 --- a/core/src/main/java/org/testcontainers/containers/GenericContainer.java +++ b/core/src/main/java/org/testcontainers/containers/GenericContainer.java @@ -74,7 +74,7 @@ public class GenericContainer extends FailureDetectingExternalResource implement private List portBindings = new ArrayList<>(); @NonNull - private List extraHosts = new ArrayList<>(); + private List extraHosts = new ArrayList<>(); @NonNull private Future image; @@ -506,13 +506,13 @@ public GenericContainer withCommand(String... commandParts) { /** * Add an extra host entry to be passed to the container - * - * @param hostEntry for e.g. "somehost:192.168.47.11" + * @param hostname + * @param ipAddress * @return this */ - public GenericContainer withExtraHost(String hostEntry) { - this.extraHosts.add(hostEntry); - return this; + public GenericContainer withExtraHost(String hostname, String ipAddress) { + this.extraHosts.add(String.format("%s:%s", hostname, ipAddress)); + return this; } /** diff --git a/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java b/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java index b3b3042ebd1..a1d3b2f5800 100644 --- a/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java +++ b/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java @@ -99,7 +99,7 @@ public static void setupContent() throws FileNotFoundException { @ClassRule public static GenericContainer alpineExtrahost = new GenericContainer("alpine:3.2") .withExposedPorts(80) - .withExtraHost("somehost:192.168.1.10") + .withExtraHost("somehost", "192.168.1.10") .withCommand("/bin/sh", "-c", "while true; do cat /etc/hosts | nc -l -p 80; done"); @Test @@ -280,9 +280,9 @@ public void extraHostTest() throws IOException { StringBuffer hosts = new StringBuffer(); String line = br.readLine(); while (line != null) { - hosts.append(line); - hosts.append("\n"); - line = br.readLine(); + hosts.append(line); + hosts.append("\n"); + line = br.readLine(); } Matcher matcher = Pattern.compile("^192.168.1.10\\s.*somehost", Pattern.MULTILINE).matcher(hosts.toString());