Skip to content

Commit

Permalink
Use inspectNetworkCmd to get the gateway's IP (#2151)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsideup committed Dec 19, 2019
1 parent 639cacd commit b0d1c9b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 30 deletions.
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

0 comments on commit b0d1c9b

Please sign in to comment.