Skip to content

Commit

Permalink
Merge branch 'master' into fix/use-bash-for-bashisms
Browse files Browse the repository at this point in the history
  • Loading branch information
perlun committed Dec 25, 2019
2 parents 02e189b + 08234f8 commit 0cd81b9
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 57 deletions.
6 changes: 3 additions & 3 deletions core/build.gradle
Expand Up @@ -57,7 +57,7 @@ project.tasks.check.dependsOn(jarFileTest)

dependencies {
compile 'junit:junit:4.12'
compile 'org.slf4j:slf4j-api:1.7.29'
compile 'org.slf4j:slf4j-api:1.7.30'
compile 'org.jetbrains:annotations:17.0.0'
compile 'javax.annotation:javax.annotation-api:1.3.2'
compile 'org.apache.commons:commons-compress:1.19'
Expand Down Expand Up @@ -106,10 +106,10 @@ dependencies {
}

testCompile 'org.apache.httpcomponents:httpclient:4.5.9'
testCompile 'redis.clients:jedis:3.1.0'
testCompile 'redis.clients:jedis:3.2.0'
testCompile 'com.rabbitmq:amqp-client:5.8.0'
testCompile 'org.mongodb:mongo-java-driver:3.12.0'
testCompile ('org.mockito:mockito-core:3.2.0') {
testCompile ('org.mockito:mockito-core:3.2.4') {
exclude(module: 'hamcrest-core')
}
// Synthetic JAR used for MountableFileTest and DirectoryTarResourceTest
Expand Down
Expand Up @@ -43,8 +43,10 @@ public class DockerClientConfigUtils {
.map(StringUtils::trimToEmpty)
.filter(StringUtils::isNotBlank);



/**
* Use {@link DockerClientFactory#dockerHostIpAddress()}
*/
@Deprecated
public static String getDockerHostIpAddress(DockerClientConfig config) {
switch (config.getDockerHost().getScheme()) {
case "http":
Expand Down
@@ -1,8 +1,10 @@
package org.testcontainers.dockerclient;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.model.Network;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Throwables;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
Expand All @@ -17,6 +19,7 @@
import org.testcontainers.dockerclient.transport.okhttp.OkHttpDockerCmdExecFactory;
import org.testcontainers.utility.TestcontainersConfiguration;

import java.net.URI;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
Expand All @@ -33,6 +36,8 @@ public abstract class DockerClientProviderStrategy {
protected DockerClient client;
protected DockerClientConfig config;

private String dockerHostIpAddress;

private static final RateLimiter PING_RATE_LIMITER = RateLimiterBuilder.newBuilder()
.withRate(2, TimeUnit.SECONDS)
.withConstantThroughput()
Expand Down Expand Up @@ -188,8 +193,38 @@ protected void ping(DockerClient client, int timeoutInSeconds) {
}
}

public String getDockerHostIpAddress() {
return DockerClientConfigUtils.getDockerHostIpAddress(this.config);
public synchronized String getDockerHostIpAddress() {
if (dockerHostIpAddress == null) {
dockerHostIpAddress = resolveDockerHostIpAddress(client, config.getDockerHost());
}
return dockerHostIpAddress;
}

@VisibleForTesting
static String resolveDockerHostIpAddress(DockerClient client, URI dockerHost) {
switch (dockerHost.getScheme()) {
case "http":
case "https":
case "tcp":
return dockerHost.getHost();
case "unix":
case "npipe":
if (DockerClientConfigUtils.IN_A_CONTAINER) {
return client.inspectNetworkCmd()
.withNetworkId("bridge")
.exec()
.getIpam()
.getConfig()
.stream()
.filter(it -> it.getGateway() != null)
.findAny()
.map(Network.Ipam.Config::getGateway)
.orElse("localhost");
}
return "localhost";
default:
return null;
}
}

protected void checkOSType() {
Expand Down
@@ -1,54 +1,40 @@
package org.testcontainers.dockerclient;

import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientConfig;
import org.junit.Ignore;
import com.github.dockerjava.api.DockerClient;
import org.junit.Test;
import org.testcontainers.DockerClientFactory;

import java.net.URI;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

public class DockerClientConfigUtilsTest {

DockerClient client = DockerClientFactory.lazyClient();

@Test
public void getDockerHostIpAddressShouldReturnLocalhostWhenUnixSocket() {
DockerClientConfig configuration = DefaultDockerClientConfig.createDefaultConfigBuilder()
.withDockerHost("unix:///var/run/docker.sock")
.withDockerTlsVerify(false) // TODO - check wrt. https://github.com/docker-java/docker-java/issues/588
.build();
String actual = DockerClientConfigUtils.getDockerHostIpAddress(configuration);
String actual = DockerClientProviderStrategy.resolveDockerHostIpAddress(client, URI.create("unix:///var/run/docker.sock"));
assertEquals("localhost", actual);
}

@Test @Ignore
public void getDockerHostIpAddressShouldReturnDockerHostIpWhenHttpUri() {
DockerClientConfig configuration = DefaultDockerClientConfig.createDefaultConfigBuilder().withDockerHost("http://12.23.34.45").build();
String actual = DockerClientConfigUtils.getDockerHostIpAddress(configuration);
assertEquals("12.23.34.45", actual);
}

@Test @Ignore
@Test
public void getDockerHostIpAddressShouldReturnDockerHostIpWhenHttpsUri() {
DockerClientConfig configuration = DefaultDockerClientConfig.createDefaultConfigBuilder().withDockerHost("https://12.23.34.45").build();
String actual = DockerClientConfigUtils.getDockerHostIpAddress(configuration);
String actual = DockerClientProviderStrategy.resolveDockerHostIpAddress(client, URI.create("http://12.23.34.45"));
assertEquals("12.23.34.45", actual);
}

@Test
public void getDockerHostIpAddressShouldReturnDockerHostIpWhenTcpUri() {
DockerClientConfig configuration = DefaultDockerClientConfig.createDefaultConfigBuilder()
.withDockerHost("tcp://12.23.34.45")
.withDockerTlsVerify(false) // TODO - check wrt. https://github.com/docker-java/docker-java/issues/588
.build();
String actual = DockerClientConfigUtils.getDockerHostIpAddress(configuration);
String actual = DockerClientProviderStrategy.resolveDockerHostIpAddress(client, URI.create("tcp://12.23.34.45"));
assertEquals("12.23.34.45", actual);
}

@Test @Ignore
@Test
public void getDockerHostIpAddressShouldReturnNullWhenUnsupportedUriScheme() {
DockerClientConfig configuration = DefaultDockerClientConfig.createDefaultConfigBuilder().withDockerHost("gopher://12.23.34.45").build();
String actual = DockerClientConfigUtils.getDockerHostIpAddress(configuration);
String actual = DockerClientProviderStrategy.resolveDockerHostIpAddress(client, URI.create("gopher://12.23.34.45"));
assertNull(actual);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/linked-container/build.gradle
Expand Up @@ -6,7 +6,7 @@ repositories {
jcenter()
}
dependencies {
compileOnly 'org.slf4j:slf4j-api:1.7.29'
compileOnly 'org.slf4j:slf4j-api:1.7.30'
implementation 'com.squareup.okhttp3:okhttp:3.14.4'
implementation 'org.json:json:20180813'
testImplementation 'org.postgresql:postgresql:42.2.9'
Expand Down
2 changes: 1 addition & 1 deletion examples/redis-backed-cache-testng/build.gradle
Expand Up @@ -7,7 +7,7 @@ repositories {
}

dependencies {
compileOnly 'org.slf4j:slf4j-api:1.7.29'
compileOnly 'org.slf4j:slf4j-api:1.7.30'
implementation 'redis.clients:jedis:3.2.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.google.guava:guava:23.0'
Expand Down
2 changes: 1 addition & 1 deletion examples/redis-backed-cache/build.gradle
Expand Up @@ -7,7 +7,7 @@ repositories {
}

dependencies {
compileOnly 'org.slf4j:slf4j-api:1.7.29'
compileOnly 'org.slf4j:slf4j-api:1.7.30'
implementation 'redis.clients:jedis:3.2.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.google.guava:guava:23.0'
Expand Down
2 changes: 1 addition & 1 deletion examples/singleton-container/build.gradle
Expand Up @@ -11,7 +11,7 @@ dependencies {
implementation 'redis.clients:jedis:3.2.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.google.guava:guava:23.0'
compileOnly 'org.slf4j:slf4j-api:1.7.29'
compileOnly 'org.slf4j:slf4j-api:1.7.30'

testImplementation 'ch.qos.logback:logback-classic:1.2.3'
testImplementation 'org.testcontainers:testcontainers'
Expand Down
2 changes: 1 addition & 1 deletion modules/elasticsearch/build.gradle
Expand Up @@ -2,6 +2,6 @@ description = "TestContainers :: elasticsearch"

dependencies {
compile project(':testcontainers')
testCompile "org.elasticsearch.client:elasticsearch-rest-client:7.5.0"
testCompile "org.elasticsearch.client:elasticsearch-rest-client:7.5.1"
testCompile "org.elasticsearch.client:transport:6.7.1"
}
4 changes: 2 additions & 2 deletions modules/junit-jupiter/build.gradle
Expand Up @@ -7,9 +7,9 @@ dependencies {
testCompile project(':mysql')
testCompile project(':postgresql')
testCompile 'com.zaxxer:HikariCP:3.4.1'
testCompile 'redis.clients:jedis:3.1.0'
testCompile 'redis.clients:jedis:3.2.0'
testCompile 'org.apache.httpcomponents:httpclient:4.5.10'
testCompile ('org.mockito:mockito-core:3.2.0') {
testCompile ('org.mockito:mockito-core:3.2.4') {
exclude(module: 'hamcrest-core')
}

Expand Down
2 changes: 1 addition & 1 deletion modules/kafka/build.gradle
Expand Up @@ -3,7 +3,7 @@ description = "Testcontainers :: Kafka"
dependencies {
compile project(':testcontainers')

testCompile 'org.apache.kafka:kafka-clients:2.3.1'
testCompile 'org.apache.kafka:kafka-clients:2.4.0'
testCompile 'org.assertj:assertj-core:3.14.0'
testCompile 'com.google.guava:guava:23.0'
}
Expand Up @@ -263,6 +263,15 @@ public RabbitMQContainer withPolicy(String name, String pattern, Map<String, Obj
return self();
}

public RabbitMQContainer withPolicy(String vhost, String name, String pattern, Map<String, Object> definition) {
values.add(asList("rabbitmqadmin", "declare", "policy",
"--vhost=" + vhost,
"name=" + name,
"pattern=" + pattern,
"definition=" + toJson(definition)));
return self();
}

public RabbitMQContainer withPolicy(String name, String pattern, Map<String, Object> definition, int priority, String applyTo) {
values.add(asList("rabbitmqadmin", "declare", "policy",
"name=" + name,
Expand Down
Expand Up @@ -154,21 +154,22 @@ public void shouldStartTheWholeEnchilada() throws IOException, InterruptedExcept
{
try (RabbitMQContainer container = new RabbitMQContainer()) {
container
.withVhost("vhost1")
.withVhostLimit("vhost1", "max-connections", 1)
.withVhost("vhost2", true)
.withExchange("direct-exchange", "direct")
.withExchange("topic-exchange", "topic")
.withQueue("queue1")
.withQueue("queue2", true, false, ImmutableMap.of("x-message-ttl", 1000))
.withBinding("direct-exchange", "queue1")
.withUser("user1", "password1")
.withUser("user2", "password2", ImmutableSet.of("administrator"))
.withPermission("vhost1", "user1", ".*", ".*", ".*")
.withPolicy("max length policy", "^dog", ImmutableMap.of("max-length", 1), 1, "queues")
.withPolicy("alternate exchange policy", "^direct-exchange", ImmutableMap.of("alternate-exchange", "amq.direct"))
.withOperatorPolicy("operator policy 1", "^queue1", ImmutableMap.of("message-ttl", 1000), 1, "queues")
.withPluginsEnabled("rabbitmq_shovel", "rabbitmq_random_exchange");
.withVhost("vhost1")
.withVhostLimit("vhost1", "max-connections", 1)
.withVhost("vhost2", true)
.withExchange("direct-exchange", "direct")
.withExchange("topic-exchange", "topic")
.withQueue("queue1")
.withQueue("queue2", true, false, ImmutableMap.of("x-message-ttl", 1000))
.withBinding("direct-exchange", "queue1")
.withUser("user1", "password1")
.withUser("user2", "password2", ImmutableSet.of("administrator"))
.withPermission("vhost1", "user1", ".*", ".*", ".*")
.withPolicy("max length policy", "^dog", ImmutableMap.of("max-length", 1), 1, "queues")
.withPolicy("alternate exchange policy", "^direct-exchange", ImmutableMap.of("alternate-exchange", "amq.direct"))
.withPolicy("vhost2", "ha-all", ".*", ImmutableMap.of("ha-mode", "all", "ha-sync-mode", "automatic"))
.withOperatorPolicy("operator policy 1", "^queue1", ImmutableMap.of("message-ttl", 1000), 1, "queues")
.withPluginsEnabled("rabbitmq_shovel", "rabbitmq_random_exchange");

container.start();

Expand All @@ -192,6 +193,10 @@ public void shouldStartTheWholeEnchilada() throws IOException, InterruptedExcept
.getStdout())
.contains("max length policy", "alternate exchange policy");

assertThat(container.execInContainer("rabbitmqadmin", "list", "policies", "--vhost=vhost2")
.getStdout())
.contains("ha-all", "ha-sync-mode");

assertThat(container.execInContainer("rabbitmqadmin", "list", "operator_policies")
.getStdout())
.contains("operator policy 1");
Expand Down
2 changes: 1 addition & 1 deletion modules/vault/build.gradle
Expand Up @@ -3,7 +3,7 @@ description = "Testcontainers :: Vault"
dependencies {
compile project(':testcontainers')

testCompile 'com.bettercloud:vault-java-driver:5.0.0'
testCompile 'com.bettercloud:vault-java-driver:5.1.0'
testCompile 'io.rest-assured:rest-assured:4.1.2'
testCompile 'org.assertj:assertj-core:3.14.0'

Expand Down

0 comments on commit 0cd81b9

Please sign in to comment.