Skip to content

Commit bfd61d7

Browse files
authored
Use implicit strategy when docker.host configuration is set (#4175)
1 parent e89dce1 commit bfd61d7

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

core/src/main/java/org/testcontainers/utility/TestcontainersConfiguration.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.jetbrains.annotations.NotNull;
1414
import org.jetbrains.annotations.Nullable;
1515
import org.testcontainers.UnstableAPI;
16+
import org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy;
1617

1718
import java.io.File;
1819
import java.io.FileNotFoundException;
@@ -23,6 +24,7 @@
2324
import java.net.MalformedURLException;
2425
import java.net.URL;
2526
import java.util.Map;
27+
import java.util.Objects;
2628
import java.util.Optional;
2729
import java.util.Properties;
2830
import java.util.concurrent.atomic.AtomicReference;
@@ -172,7 +174,27 @@ public String getDockerClientStrategyClassName() {
172174
// Because of this overlap, and the desire to not change this specific TESTCONTAINERS_DOCKER_CLIENT_STRATEGY setting,
173175
// we special-case the logic here so that docker.client.strategy is used when reading properties files and
174176
// TESTCONTAINERS_DOCKER_CLIENT_STRATEGY is used when searching environment variables.
175-
return getEnvVarOrUserProperty("docker.client.strategy", environment.get("TESTCONTAINERS_DOCKER_CLIENT_STRATEGY"));
177+
178+
// looks for TESTCONTAINERS_ prefixed env var only
179+
String prefixedEnvVarStrategy = environment.get("TESTCONTAINERS_DOCKER_CLIENT_STRATEGY");
180+
if (prefixedEnvVarStrategy != null) {
181+
return prefixedEnvVarStrategy;
182+
}
183+
184+
// looks for unprefixed env var or unprefixed property
185+
String unprefixedEnvVarOrProperty = getEnvVarOrUserProperty("docker.client.strategy", null);
186+
if (unprefixedEnvVarOrProperty != null) {
187+
return unprefixedEnvVarOrProperty;
188+
}
189+
190+
// If docker.host is set then EnvironmentAndSystemPropertyClientProviderStrategy is likely to work
191+
String dockerHostProperty = getEnvVarOrUserProperty("docker.host", null);
192+
if (dockerHostProperty != null) {
193+
return EnvironmentAndSystemPropertyClientProviderStrategy.class.getCanonicalName();
194+
}
195+
196+
// No value set, and no implicit value to use either
197+
return null;
176198
}
177199

178200
public String getTransportType() {

core/src/test/java/org/testcontainers/utility/TestcontainersConfigurationTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.Before;
44
import org.junit.Test;
5+
import org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy;
56

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

159+
@Test
160+
public void shouldUseImplicitDockerClientStrategyWhenDockerHostPropertyIsSet() {
161+
userProperties.remove("docker.client.strategy");
162+
userProperties.put("docker.host", "tcp://1.2.3.4:5678");
163+
assertEquals("Docker client strategy is implicitly set when docker host property is set", EnvironmentAndSystemPropertyClientProviderStrategy.class.getCanonicalName(), newConfig().getDockerClientStrategyClassName());
164+
}
165+
166+
@Test
167+
public void shouldNotUseImplicitDockerClientStrategyWhenDockerHostAndStrategyAreBothSet() {
168+
userProperties.put("docker.client.strategy", "foo");
169+
userProperties.put("docker.host", "tcp://1.2.3.4:5678");
170+
assertEquals("Docker client strategy is can be explicitly set", "foo", newConfig().getDockerClientStrategyClassName());
171+
172+
userProperties.remove("docker.client.strategy");
173+
174+
environment.put("TESTCONTAINERS_DOCKER_CLIENT_STRATEGY", "bar");
175+
userProperties.put("docker.client.strategy", "foo");
176+
assertEquals("Docker client strategy is can be explicitly set", "bar", newConfig().getDockerClientStrategyClassName());
177+
178+
environment.put("TESTCONTAINERS_DOCKER_CLIENT_STRATEGY", "bar");
179+
userProperties.remove("docker.client.strategy");
180+
assertEquals("Docker client strategy is can be explicitly set", "bar", newConfig().getDockerClientStrategyClassName());
181+
182+
environment.remove("TESTCONTAINERS_DOCKER_CLIENT_STRATEGY");
183+
userProperties.put("docker.client.strategy", "foo");
184+
assertEquals("Docker client strategy is can be explicitly set", "foo", newConfig().getDockerClientStrategyClassName());
185+
}
186+
158187
@Test
159188
public void shouldNotReadReuseFromClasspathProperties() {
160189
assertFalse("no reuse by default", newConfig().environmentSupportsReuse());

0 commit comments

Comments
 (0)