Skip to content

Commit 6cde564

Browse files
authored
Allow to disable configuration of EnvironmentAndSystemPropertyClientProviderStrategy through Testcontainers (#4387)
Adds the `dockerconfig.source` property that can be used to change the way `EnvironmentAndSystemPropertyClientProviderStrategy` is configured. Additionally, `EnvironmentAndSystemPropertyClientProviderStrategy` is not persistable anymore. Currently supports the following modes: * `auto` (which is the default), meaning that the Testcontainers configuration mechanisms can be used to configure docker-java * `autoIgnoringUserProperties`, same as `auto`, but ignoring values set in the user properties
1 parent 5e88d74 commit 6cde564

File tree

5 files changed

+65
-32
lines changed

5 files changed

+65
-32
lines changed

core/src/main/java/org/testcontainers/dockerclient/EnvironmentAndSystemPropertyClientProviderStrategy.java

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

33
import com.github.dockerjava.core.DefaultDockerClientConfig;
44
import com.github.dockerjava.core.DockerClientConfig;
5+
import lombok.Getter;
56
import org.testcontainers.utility.TestcontainersConfiguration;
67

78
import java.util.Optional;
@@ -25,13 +26,32 @@ public final class EnvironmentAndSystemPropertyClientProviderStrategy extends Do
2526

2627
private final DockerClientConfig dockerClientConfig;
2728

29+
@Getter
30+
private final boolean applicable;
31+
2832
public EnvironmentAndSystemPropertyClientProviderStrategy() {
2933
// use docker-java defaults if present, overridden if our own configuration is set
30-
DefaultDockerClientConfig.Builder configBuilder = DefaultDockerClientConfig.createDefaultConfigBuilder();
34+
this(DefaultDockerClientConfig.createDefaultConfigBuilder());
35+
}
36+
37+
EnvironmentAndSystemPropertyClientProviderStrategy(DefaultDockerClientConfig.Builder configBuilder) {
38+
String dockerConfigSource = TestcontainersConfiguration.getInstance()
39+
.getEnvVarOrProperty("dockerconfig.source", "auto");
3140

32-
getSetting("docker.host").ifPresent(configBuilder::withDockerHost);
33-
getSetting("docker.tls.verify").ifPresent(configBuilder::withDockerTlsVerify);
34-
getSetting("docker.cert.path").ifPresent(configBuilder::withDockerCertPath);
41+
switch (dockerConfigSource) {
42+
case "auto":
43+
Optional<String> dockerHost = getSetting("docker.host");
44+
dockerHost.ifPresent(configBuilder::withDockerHost);
45+
applicable = dockerHost.isPresent();
46+
getSetting("docker.tls.verify").ifPresent(configBuilder::withDockerTlsVerify);
47+
getSetting("docker.cert.path").ifPresent(configBuilder::withDockerCertPath);
48+
break;
49+
case "autoIgnoringUserProperties":
50+
applicable = configBuilder.isDockerHostSetExplicitly();
51+
break;
52+
default:
53+
throw new InvalidConfigurationException("Invalid value for dockerconfig.source: " + dockerConfigSource);
54+
}
3555

3656
dockerClientConfig = configBuilder.build();
3757
}
@@ -40,11 +60,6 @@ private Optional<String> getSetting(final String name) {
4060
return Optional.ofNullable(TestcontainersConfiguration.getInstance().getEnvVarOrUserProperty(name, null));
4161
}
4262

43-
@Override
44-
protected boolean isApplicable() {
45-
return getSetting("docker.host").isPresent();
46-
}
47-
4863
@Override
4964
public TransportConfig getTransportConfig() {
5065
return TransportConfig.builder()
@@ -62,4 +77,9 @@ protected int getPriority() {
6277
public String getDescription() {
6378
return "Environment variables, system properties and defaults. Resolved dockerHost=" + dockerClientConfig.getDockerHost();
6479
}
80+
81+
@Override
82+
protected boolean isPersistable() {
83+
return false;
84+
}
6585
}

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

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

1817
import java.io.File;
1918
import java.io.FileNotFoundException;
@@ -182,20 +181,8 @@ public String getDockerClientStrategyClassName() {
182181
return prefixedEnvVarStrategy;
183182
}
184183

185-
// looks for unprefixed env var or unprefixed property
186-
String unprefixedEnvVarOrProperty = getEnvVarOrUserProperty("docker.client.strategy", null);
187-
if (unprefixedEnvVarOrProperty != null) {
188-
return unprefixedEnvVarOrProperty;
189-
}
190-
191-
// If docker.host is set then EnvironmentAndSystemPropertyClientProviderStrategy is likely to work
192-
String dockerHostProperty = getEnvVarOrUserProperty("docker.host", null);
193-
if (dockerHostProperty != null) {
194-
return EnvironmentAndSystemPropertyClientProviderStrategy.class.getCanonicalName();
195-
}
196-
197-
// No value set, and no implicit value to use either
198-
return null;
184+
// looks for unprefixed env var or unprefixed property, or null if the strategy is not set at all
185+
return getEnvVarOrUserProperty("docker.client.strategy", null);
199186
}
200187

201188
public String getTransportType() {

core/src/test/java/org/testcontainers/dockerclient/EnvironmentAndSystemPropertyClientProviderStrategyTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616
import java.net.URI;
1717
import java.nio.file.Files;
1818
import java.nio.file.Path;
19+
import java.util.Collections;
20+
import java.util.HashMap;
21+
import java.util.Map;
1922

2023
import static org.junit.Assert.assertEquals;
24+
import static org.junit.Assert.assertFalse;
2125
import static org.junit.Assert.assertNotNull;
2226
import static org.junit.Assert.assertTrue;
27+
import static org.mockito.ArgumentMatchers.any;
28+
import static org.mockito.ArgumentMatchers.anyString;
2329
import static org.mockito.ArgumentMatchers.eq;
2430
import static org.mockito.ArgumentMatchers.isNull;
2531

@@ -45,6 +51,7 @@ public void checkEnvironmentClear() {
4551

4652
@Test
4753
public void testWhenConfigAbsent() {
54+
Mockito.doReturn("auto").when(TestcontainersConfiguration.getInstance()).getEnvVarOrProperty(eq("dockerconfig.source"), anyString());
4855
Mockito.doReturn(null).when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.host"), isNull());
4956
Mockito.doReturn(null).when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.tls.verify"), isNull());
5057
Mockito.doReturn(null).when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.cert.path"), isNull());
@@ -58,6 +65,7 @@ public void testWhenConfigAbsent() {
5865

5966
@Test
6067
public void testWhenDockerHostPresent() {
68+
Mockito.doReturn("auto").when(TestcontainersConfiguration.getInstance()).getEnvVarOrProperty(eq("dockerconfig.source"), anyString());
6169
Mockito.doReturn("tcp://1.2.3.4:2375").when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.host"), isNull());
6270
Mockito.doReturn(null).when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.tls.verify"), isNull());
6371
Mockito.doReturn(null).when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.cert.path"), isNull());
@@ -74,6 +82,7 @@ public void testWhenDockerHostAndSSLConfigPresent() throws IOException {
7482
Path tempDir = Files.createTempDirectory("testcontainers-test");
7583
String tempDirPath = tempDir.toAbsolutePath().toString();
7684

85+
Mockito.doReturn("auto").when(TestcontainersConfiguration.getInstance()).getEnvVarOrProperty(eq("dockerconfig.source"), anyString());
7786
Mockito.doReturn("tcp://1.2.3.4:2375").when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.host"), isNull());
7887
Mockito.doReturn("1").when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.tls.verify"), isNull());
7988
Mockito.doReturn(tempDirPath).when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.cert.path"), isNull());
@@ -88,4 +97,25 @@ public void testWhenDockerHostAndSSLConfigPresent() throws IOException {
8897
assertTrue(sslConfig instanceof LocalDirectorySSLConfig);
8998
assertEquals(tempDirPath, ((LocalDirectorySSLConfig) sslConfig).getDockerCertPath());
9099
}
100+
101+
@Test
102+
public void applicableWhenIgnoringUserPropertiesAndConfigured() {
103+
Mockito.doReturn("autoIgnoringUserProperties").when(TestcontainersConfiguration.getInstance()).getEnvVarOrProperty(eq("dockerconfig.source"), anyString());
104+
105+
DefaultDockerClientConfig.Builder configBuilder = DefaultDockerClientConfig.createDefaultConfigBuilder()
106+
.withDockerHost("tcp://1.2.3.4:2375");
107+
108+
EnvironmentAndSystemPropertyClientProviderStrategy strategy = new EnvironmentAndSystemPropertyClientProviderStrategy(configBuilder);
109+
110+
assertTrue(strategy.isApplicable());
111+
}
112+
113+
@Test
114+
public void notApplicableWhenIgnoringUserPropertiesAndNotConfigured() {
115+
Mockito.doReturn("autoIgnoringUserProperties").when(TestcontainersConfiguration.getInstance()).getEnvVarOrProperty(eq("dockerconfig.source"), anyString());
116+
117+
EnvironmentAndSystemPropertyClientProviderStrategy strategy = new EnvironmentAndSystemPropertyClientProviderStrategy();
118+
119+
assertFalse(strategy.isApplicable());
120+
}
91121
}

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

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

33
import org.junit.Before;
44
import org.junit.Test;
5-
import org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy;
65

76
import java.util.HashMap;
87
import java.util.Map;
@@ -156,13 +155,6 @@ public void shouldReadDockerClientStrategyFromEnvironment() {
156155
assertEquals("Docker client strategy is changed by env var", "foo", newConfig().getDockerClientStrategyClassName());
157156
}
158157

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-
166158
@Test
167159
public void shouldNotUseImplicitDockerClientStrategyWhenDockerHostAndStrategyAreBothSet() {
168160
userProperties.put("docker.client.strategy", "foo");

docs/features/configuration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,7 @@ docker.host=tcp\://my.docker.host\:1234 # Equivalent to the DOCKER_HOST envi
126126
docker.tls.verify=1 # Equivalent to the DOCKER_TLS_VERIFY environment variable
127127
docker.cert.path=/some/path # Equivalent to the DOCKER_CERT_PATH environment variable
128128
```
129+
In addition, you can deactivate this behaviour by specifying:
130+
```properties
131+
dockerconfig.source=autoIgnoringUserProperties # 'auto' by default
132+
```

0 commit comments

Comments
 (0)