Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,6 +73,9 @@ public class GenericContainer extends FailureDetectingExternalResource implement
@NonNull
private List<String> portBindings = new ArrayList<>();

@NonNull
private List<String> extraHosts = new ArrayList<>();

@NonNull
private Future<String> image;

Expand Down Expand Up @@ -111,6 +116,7 @@ public class GenericContainer extends FailureDetectingExternalResource implement
@Nullable
private InspectContainerResponse containerInfo;


private static final Set<String> AVAILABLE_IMAGE_NAME_CACHE = new HashSet<>();
private static final RateLimiter DOCKER_CLIENT_RATE_LIMITER = RateLimiterBuilder
.newBuilder()
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -494,6 +504,16 @@ public GenericContainer withCommand(String... commandParts) {
return this;
}

/**
* Add an extra host entry to be passed to the container
* @param hostname
* @param ipAddress
* @return this
*/
public GenericContainer withExtraHost(String hostname, String ipAddress) {
this.extraHosts.add(String.format("%s:%s", hostname, ipAddress));
return this;
}

/**
* Map a resource (file or directory) on the classpath to a path inside the container.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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());
}
}