forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it possible to run TestContainers inside a container. Fixes test…
- Loading branch information
Showing
12 changed files
with
559 additions
and
65 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 45 additions & 10 deletions
55
core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,53 @@ | ||
package org.testcontainers.dockerclient; | ||
|
||
import com.github.dockerjava.core.DockerClientConfig; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.testcontainers.DockerClientFactory; | ||
|
||
import java.io.File; | ||
import java.util.Optional; | ||
|
||
@Slf4j | ||
public class DockerClientConfigUtils { | ||
|
||
// See https://github.com/docker/docker/blob/a9fa38b1edf30b23cae3eade0be48b3d4b1de14b/daemon/initlayer/setup_unix.go#L25 | ||
public static final boolean IN_A_CONTAINER = new File("/.dockerenv").exists(); | ||
|
||
@Getter(lazy = true) | ||
private static final Optional<String> detectedDockerHostIp = Optional | ||
.of(IN_A_CONTAINER) | ||
.filter(it -> it) | ||
.map(file -> DockerClientFactory.instance().runInsideDocker( | ||
cmd -> cmd.withCmd("sh", "-c", "ip route|awk '/default/ { print $3 }'"), | ||
(client, id) -> { | ||
try { | ||
return client.logContainerCmd(id) | ||
.withStdOut(true) | ||
.exec(new LogToStringContainerCallback()) | ||
.toString(); | ||
} catch (Exception e) { | ||
log.warn("Can't parse the default gateway IP", e); | ||
return null; | ||
} | ||
} | ||
)) | ||
.map(StringUtils::trimToEmpty) | ||
.filter(StringUtils::isNotBlank); | ||
|
||
public static String getDockerHostIpAddress(DockerClientConfig config) { | ||
switch (config.getDockerHost().getScheme()) { | ||
case "http": | ||
case "https": | ||
case "tcp": | ||
return config.getDockerHost().getHost(); | ||
case "unix": | ||
return "localhost"; | ||
default: | ||
return null; | ||
} | ||
return getDetectedDockerHostIp().orElseGet(() -> { | ||
switch (config.getDockerHost().getScheme()) { | ||
case "http": | ||
case "https": | ||
case "tcp": | ||
return config.getDockerHost().getHost(); | ||
case "unix": | ||
return "localhost"; | ||
default: | ||
return null; | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
core/src/main/java/org/testcontainers/dockerclient/LogToStringContainerCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.testcontainers.dockerclient; | ||
|
||
import com.github.dockerjava.api.model.Frame; | ||
import com.github.dockerjava.core.command.LogContainerResultCallback; | ||
|
||
public class LogToStringContainerCallback extends LogContainerResultCallback { | ||
private final StringBuffer log = new StringBuffer(); | ||
|
||
@Override | ||
public void onNext(Frame frame) { | ||
log.append(new String(frame.getPayload())); | ||
super.onNext(frame); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
try { | ||
awaitCompletion(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return log.toString(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
core/src/test/java/org/testcontainers/dockerclient/DockerInDockerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.testcontainers.dockerclient; | ||
|
||
import org.junit.ClassRule; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TestRule; | ||
import org.junit.rules.TestWatcher; | ||
import org.junit.runner.Description; | ||
import org.junit.runners.model.Statement; | ||
import org.testcontainers.containers.GenericContainer; | ||
|
||
import static org.junit.Assume.assumeTrue; | ||
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals; | ||
import static org.testcontainers.junit.GenericContainerRuleTest.getReaderForContainerPort80; | ||
|
||
public class DockerInDockerTest { | ||
|
||
@ClassRule | ||
public static TestRule assumption = new TestWatcher() { | ||
@Override | ||
public Statement apply(Statement base, Description description) { | ||
assumeTrue("We're inside a container", DockerClientConfigUtils.IN_A_CONTAINER); | ||
return super.apply(base, description); | ||
} | ||
}; | ||
|
||
@Rule | ||
public GenericContainer container = new GenericContainer("alpine:3.2") | ||
.withExposedPorts(80) | ||
.withCommand("/bin/sh", "-c", "while true; do echo \"hello\" | nc -l -p 80; done"); | ||
|
||
@Test | ||
public void testIpDetection() throws Exception { | ||
String line = getReaderForContainerPort80(container).readLine(); | ||
assertEquals("The container is accessible", "hello", line); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.