Skip to content

Commit

Permalink
Use implicit strategy when docker.host configuration is set (#4175)
Browse files Browse the repository at this point in the history
  • Loading branch information
rnorth committed Jul 18, 2021
1 parent e89dce1 commit bfd61d7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.testcontainers.UnstableAPI;
import org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -23,6 +24,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -172,7 +174,27 @@ public String getDockerClientStrategyClassName() {
// Because of this overlap, and the desire to not change this specific TESTCONTAINERS_DOCKER_CLIENT_STRATEGY setting,
// we special-case the logic here so that docker.client.strategy is used when reading properties files and
// TESTCONTAINERS_DOCKER_CLIENT_STRATEGY is used when searching environment variables.
return getEnvVarOrUserProperty("docker.client.strategy", environment.get("TESTCONTAINERS_DOCKER_CLIENT_STRATEGY"));

// looks for TESTCONTAINERS_ prefixed env var only
String prefixedEnvVarStrategy = environment.get("TESTCONTAINERS_DOCKER_CLIENT_STRATEGY");
if (prefixedEnvVarStrategy != null) {
return prefixedEnvVarStrategy;
}

// looks for unprefixed env var or unprefixed property
String unprefixedEnvVarOrProperty = getEnvVarOrUserProperty("docker.client.strategy", null);
if (unprefixedEnvVarOrProperty != null) {
return unprefixedEnvVarOrProperty;
}

// If docker.host is set then EnvironmentAndSystemPropertyClientProviderStrategy is likely to work
String dockerHostProperty = getEnvVarOrUserProperty("docker.host", null);
if (dockerHostProperty != null) {
return EnvironmentAndSystemPropertyClientProviderStrategy.class.getCanonicalName();
}

// No value set, and no implicit value to use either
return null;
}

public String getTransportType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.junit.Before;
import org.junit.Test;
import org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -155,6 +156,34 @@ public void shouldReadDockerClientStrategyFromEnvironment() {
assertEquals("Docker client strategy is changed by env var", "foo", newConfig().getDockerClientStrategyClassName());
}

@Test
public void shouldUseImplicitDockerClientStrategyWhenDockerHostPropertyIsSet() {
userProperties.remove("docker.client.strategy");
userProperties.put("docker.host", "tcp://1.2.3.4:5678");
assertEquals("Docker client strategy is implicitly set when docker host property is set", EnvironmentAndSystemPropertyClientProviderStrategy.class.getCanonicalName(), newConfig().getDockerClientStrategyClassName());
}

@Test
public void shouldNotUseImplicitDockerClientStrategyWhenDockerHostAndStrategyAreBothSet() {
userProperties.put("docker.client.strategy", "foo");
userProperties.put("docker.host", "tcp://1.2.3.4:5678");
assertEquals("Docker client strategy is can be explicitly set", "foo", newConfig().getDockerClientStrategyClassName());

userProperties.remove("docker.client.strategy");

environment.put("TESTCONTAINERS_DOCKER_CLIENT_STRATEGY", "bar");
userProperties.put("docker.client.strategy", "foo");
assertEquals("Docker client strategy is can be explicitly set", "bar", newConfig().getDockerClientStrategyClassName());

environment.put("TESTCONTAINERS_DOCKER_CLIENT_STRATEGY", "bar");
userProperties.remove("docker.client.strategy");
assertEquals("Docker client strategy is can be explicitly set", "bar", newConfig().getDockerClientStrategyClassName());

environment.remove("TESTCONTAINERS_DOCKER_CLIENT_STRATEGY");
userProperties.put("docker.client.strategy", "foo");
assertEquals("Docker client strategy is can be explicitly set", "foo", newConfig().getDockerClientStrategyClassName());
}

@Test
public void shouldNotReadReuseFromClasspathProperties() {
assertFalse("no reuse by default", newConfig().environmentSupportsReuse());
Expand Down

0 comments on commit bfd61d7

Please sign in to comment.