Skip to content

Commit

Permalink
Merge pull request #167 from testcontainers/1.1
Browse files Browse the repository at this point in the history
1.1
  • Loading branch information
rnorth committed Jul 5, 2016
2 parents 21b27b4 + 57ecdf2 commit e1e501c
Show file tree
Hide file tree
Showing 79 changed files with 1,757 additions and 727 deletions.
7 changes: 7 additions & 0 deletions annotations/com/google/common/base/annotations.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<root>
<item name='com.google.common.base.Preconditions void checkArgument(boolean, java.lang.Object)'>
<annotation name='org.jetbrains.annotations.Contract'>
<val val="&quot;false, _ -&gt; fail; true, _ -&gt; _&quot;"/>
</annotation>
</item>
</root>
7 changes: 7 additions & 0 deletions annotations/org/rnorth/ducttape/annotations.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<root>
<item name='org.rnorth.ducttape.Preconditions void check(java.lang.String, boolean)'>
<annotation name='org.jetbrains.annotations.Contract'>
<val val="&quot;_,true-&gt;_;_,false-&gt;fail&quot;"/>
</annotation>
</item>
</root>
44 changes: 36 additions & 8 deletions core/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand All @@ -17,8 +18,22 @@
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java</artifactId>
<version>2.2.0</version>
<version>3.0.0</version>
<exclusions>
<!-- replace with junixsocket -->
<exclusion>
<groupId>de.gesellix</groupId>
<artifactId>unix-socket-factory</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.rnorth</groupId>
<artifactId>tcp-unix-socket-proxy</artifactId>
<version>1.0.1</version>
</dependency>

<dependency>
<groupId>org.zeroturnaround</groupId>
<artifactId>zt-exec</artifactId>
Expand All @@ -37,9 +52,9 @@

<!-- Test dependencies -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>1.3.0</version>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -69,6 +84,13 @@

</dependencies>

<!-- Prevent netty version conflicts with redisson, used for testing -->
<dependencyManagement>
<dependencies>

</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
Expand All @@ -84,8 +106,10 @@
</executions>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"/>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
<relocations>
<relocation>
Expand Down Expand Up @@ -120,6 +144,10 @@
<pattern>com.github.dockerjava</pattern>
<shadedPattern>org.testcontainers.shaded.com.github.dockerjava</shadedPattern>
</relocation>
<relocation>
<pattern>jersey.repackaged</pattern>
<shadedPattern>org.testcontainers.shaded.jersey.repackaged</shadedPattern>
</relocation>
</relocations>
<artifactSet>
<includes>
Expand All @@ -141,7 +169,7 @@
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>META-INF/services/com.fasterxml.jackson.core.*</exclude>
<exclude>META-INF/services/com.fasterxml.jackson.core.*</exclude>
<exclude>META-INF/services/com.github.dockerjava.api.command.*</exclude>
<exclude>META-INF/services/javax.ws.rs.ext.*</exclude>
<exclude>META-INF/services/org.glassfish.hk2.extension.*</exclude>
Expand Down
95 changes: 64 additions & 31 deletions core/src/main/java/org/testcontainers/DockerClientFactory.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package org.testcontainers;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.InternalServerErrorException;
import com.github.dockerjava.api.NotFoundException;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.exception.InternalServerErrorException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.Image;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.api.model.Info;
import com.github.dockerjava.api.model.Version;
import com.github.dockerjava.core.command.LogContainerResultCallback;
import com.github.dockerjava.core.command.PullImageResultCallback;
import com.github.dockerjava.core.command.WaitContainerResultCallback;
import lombok.Synchronized;
import org.slf4j.Logger;
import org.testcontainers.dockerclient.DockerClientConfigUtils;
import org.testcontainers.dockerclient.DockerConfigurationStrategy;
import org.testcontainers.dockerclient.DockerMachineConfigurationStrategy;
import org.testcontainers.dockerclient.EnvironmentAndSystemPropertyConfigurationStrategy;
import org.testcontainers.dockerclient.UnixSocketConfigurationStrategy;
import org.testcontainers.dockerclient.*;

import java.util.List;

Expand All @@ -34,13 +31,16 @@ public class DockerClientFactory {
private static final Logger LOGGER = getLogger(DockerClientFactory.class);

// Cached client configuration
private DockerClientConfig config;
private DockerClientProviderStrategy strategy;
private boolean preconditionsChecked = false;

private static final List<DockerConfigurationStrategy> CONFIGURATION_STRATEGIES =
asList(new EnvironmentAndSystemPropertyConfigurationStrategy(),
new DockerMachineConfigurationStrategy(),
new UnixSocketConfigurationStrategy());
private static final List<DockerClientProviderStrategy> CONFIGURATION_STRATEGIES =
asList(new EnvironmentAndSystemPropertyClientProviderStrategy(),
new ProxiedUnixSocketClientProviderStrategy(),
new UnixSocketClientProviderStrategy(),
new DockerMachineClientProviderStrategy());
private String activeApiVersion;
private String activeExecutionDriver;

/**
* Private constructor
Expand Down Expand Up @@ -77,15 +77,25 @@ public DockerClient client() {
*/
@Synchronized
public DockerClient client(boolean failFast) {
if (config == null) {
config = DockerConfigurationStrategy.getFirstValidConfig(CONFIGURATION_STRATEGIES);

if (strategy == null) {
strategy = DockerClientProviderStrategy.getFirstValidStrategy(CONFIGURATION_STRATEGIES);
}

DockerClient client = DockerClientBuilder.getInstance(config).build();
DockerClient client = strategy.getClient();

if (!preconditionsChecked) {
String version = client.versionCmd().exec().getVersion();
checkVersion(version);
Info dockerInfo = client.infoCmd().exec();
Version version = client.versionCmd().exec();
activeApiVersion = version.getApiVersion();
activeExecutionDriver = dockerInfo.getExecutionDriver();
LOGGER.info("Connected to docker: \n" +
" Server Version: " + dockerInfo.getServerVersion() + "\n" +
" API Version: " + activeApiVersion + "\n" +
" Operating System: " + dockerInfo.getOperatingSystem() + "\n" +
" Total Memory: " + dockerInfo.getMemTotal() / (1024 * 1024) + " MB");

checkVersion(version.getVersion());
checkDiskSpaceAndHandleExceptions(client);
preconditionsChecked = true;
}
Expand All @@ -98,19 +108,11 @@ public DockerClient client(boolean failFast) {
return client;
}

/**
* @param config docker client configuration to extract the host IP address from
* @return the IP address of the host running Docker
*/
private String dockerHostIpAddress(DockerClientConfig config) {
return DockerClientConfigUtils.getDockerHostIpAddress(config);
}

/**
* @return the IP address of the host running Docker
*/
public String dockerHostIpAddress() {
return dockerHostIpAddress(config);
return strategy.getDockerHostIpAddress();
}

private void checkVersion(String version) {
Expand Down Expand Up @@ -147,10 +149,13 @@ private void checkDiskSpace(DockerClient client) {

client.startContainerCmd(id).exec();

client.waitContainerCmd(id).exec();

LogContainerResultCallback callback = client.logContainerCmd(id).withStdOut().exec(new LogContainerCallback());
LogContainerResultCallback callback = client.logContainerCmd(id).withStdOut(true).exec(new LogContainerCallback());
try {

WaitContainerResultCallback waitCallback = new WaitContainerResultCallback();
client.waitContainerCmd(id).exec(waitCallback);
waitCallback.awaitStarted();

callback.awaitCompletion();
String logResults = callback.toString();

Expand Down Expand Up @@ -183,6 +188,34 @@ private void checkDiskSpace(DockerClient client) {
}
}

/**
* @return the docker API version of the daemon that we have connected to
*/
public String getActiveApiVersion() {
if (!preconditionsChecked) {
client(true);
}
return activeApiVersion;
}

/**
* @return the docker execution driver of the daemon that we have connected to
*/
public String getActiveExecutionDriver() {
if (!preconditionsChecked) {
client(true);
}
return activeExecutionDriver;
}

/**
* @param providerStrategyClass a class that extends {@link DockerMachineClientProviderStrategy}
* @return whether or not the currently active strategy is of the provided type
*/
public boolean isUsing(Class<? extends DockerClientProviderStrategy> providerStrategyClass) {
return providerStrategyClass.isAssignableFrom(this.strategy.getClass());
}

private static class NotEnoughDiskSpaceException extends RuntimeException {
NotEnoughDiskSpaceException(String message) {
super(message);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.testcontainers.containers;

import com.github.dockerjava.api.DockerException;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.model.Container;
import lombok.Data;
import lombok.EqualsAndHashCode;
Expand All @@ -26,9 +26,9 @@ public class AmbassadorContainer<SELF extends AmbassadorContainer<SELF>> extends
public AmbassadorContainer(LinkableContainer otherContainer, String serviceName, int servicePort) {
super("richnorth/ambassador:latest");

/**
* Use the unique 'identifierPrefix' (random compose project name) so that the ambassador can see
* the container it's supposed to be proxying.
/*
Use the unique 'identifierPrefix' (random compose project name) so that the ambassador can see
the container it's supposed to be proxying.
*/
this.otherContainerName = otherContainer.getContainerName();
this.serviceName = serviceName;
Expand Down
21 changes: 13 additions & 8 deletions core/src/main/java/org/testcontainers/containers/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public String getStderr() {
/**
* Add a link to another container.
*
* @param otherContainer
* @param alias
* @param otherContainer the other container object to link to
* @param alias the alias (for the other container) that this container should be able to use
*/
void addLink(LinkableContainer otherContainer, String alias);

Expand Down Expand Up @@ -171,12 +171,21 @@ public String getStderr() {

/**
* Add an extra host entry to be passed to the container
* @param hostname
* @param ipAddress
* @param hostname hostname to use for this hosts file entry
* @param ipAddress IP address to use for this hosts file entry
* @return this
*/
SELF withExtraHost(String hostname, String ipAddress);

/**
* Set the network mode for this container, similar to the <code>--net &lt;name&gt;</code>
* option on the docker CLI.
*
* @param networkMode network mode, e.g. including 'host', 'bridge', 'none' or the name of an existing named network.
* @return this
*/
SELF withNetworkMode(String networkMode);

/**
* Map a resource (file or directory) on the classpath to a path inside the container.
* This will only work if you are running your tests outside a Docker container.
Expand Down Expand Up @@ -311,8 +320,6 @@ ExecResult execInContainer(Charset outputCharset, String... command)

Map<String, LinkableContainer> getLinkedContainers();

Duration getMinimumRunningDuration();

DockerClient getDockerClient();

Info getDockerDaemonInfo();
Expand All @@ -339,8 +346,6 @@ ExecResult execInContainer(Charset outputCharset, String... command)

void setLinkedContainers(Map<String, LinkableContainer> linkedContainers);

void setMinimumRunningDuration(Duration minimumRunningDuration);

void setDockerClient(DockerClient dockerClient);

void setDockerDaemonInfo(Info dockerDaemonInfo);
Expand Down
Loading

0 comments on commit e1e501c

Please sign in to comment.