Skip to content

Commit 827c4c5

Browse files
authored
Separate "environment" and "classpath" properties (for global things) (#1784)
* Separate "environment" and "classpath" properties (for global things) * Update TestcontainersConfiguration.java
1 parent 7d93069 commit 827c4c5

File tree

3 files changed

+99
-59
lines changed

3 files changed

+99
-59
lines changed

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,10 @@ public DockerClient getClient() {
167167
}
168168

169169
protected DockerClient getClientForConfig(DockerClientConfig config) {
170-
DockerClientBuilder clientBuilder = DockerClientBuilder
171-
.getInstance(new AuthDelegatingDockerClientConfig(config));
172-
173-
String transportType = TestcontainersConfiguration.getInstance().getTransportType();
174-
if ("okhttp".equals(transportType)) {
175-
clientBuilder
176-
.withDockerCmdExecFactory(new OkHttpDockerCmdExecFactory());
177-
} else {
178-
throw new IllegalArgumentException("Unknown transport type: " + transportType);
179-
}
180-
181-
LOGGER.info("Will use '{}' transport", transportType);
182-
183-
return clientBuilder.build();
170+
return DockerClientBuilder
171+
.getInstance(new AuthDelegatingDockerClientConfig(config))
172+
.withDockerCmdExecFactory(new OkHttpDockerCmdExecFactory())
173+
.build();
184174
}
185175

186176
protected void ping(DockerClient client, int timeoutInSeconds) {

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

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.io.*;
77
import java.net.MalformedURLException;
8+
import java.net.URL;
89
import java.util.Objects;
910
import java.util.Properties;
1011
import java.util.stream.Stream;
@@ -19,12 +20,22 @@ public class TestcontainersConfiguration {
1920

2021
private static String PROPERTIES_FILE_NAME = "testcontainers.properties";
2122

22-
private static File GLOBAL_CONFIG_FILE = new File(System.getProperty("user.home"), "." + PROPERTIES_FILE_NAME);
23+
private static File ENVIRONMENT_CONFIG_FILE = new File(System.getProperty("user.home"), "." + PROPERTIES_FILE_NAME);
2324

2425
@Getter(lazy = true)
2526
private static final TestcontainersConfiguration instance = loadConfiguration();
2627

27-
private final Properties properties;
28+
@Getter(AccessLevel.NONE)
29+
private final Properties environmentProperties;
30+
31+
private final Properties properties = new Properties();
32+
33+
TestcontainersConfiguration(Properties environmentProperties, Properties classpathProperties) {
34+
this.environmentProperties = environmentProperties;
35+
36+
this.properties.putAll(classpathProperties);
37+
this.properties.putAll(environmentProperties);
38+
}
2839

2940
public String getAmbassadorContainerImage() {
3041
return (String) properties.getOrDefault("ambassador.container.image", "richnorth/ambassador:latest");
@@ -71,13 +82,18 @@ public String getPulsarImage() {
7182
}
7283

7384
public boolean isDisableChecks() {
74-
return Boolean.parseBoolean((String) properties.getOrDefault("checks.disable", "false"));
85+
return Boolean.parseBoolean((String) environmentProperties.getOrDefault("checks.disable", "false"));
7586
}
7687

7788
public String getDockerClientStrategyClassName() {
78-
return (String) properties.get("docker.client.strategy");
89+
return (String) environmentProperties.get("docker.client.strategy");
7990
}
8091

92+
/**
93+
*
94+
* @deprecated we no longer have different transport types
95+
*/
96+
@Deprecated
8197
public String getTransportType() {
8298
return properties.getProperty("transport.type", "okhttp");
8399
}
@@ -89,64 +105,55 @@ public Integer getImagePullPauseTimeout() {
89105
@Synchronized
90106
public boolean updateGlobalConfig(@NonNull String prop, @NonNull String value) {
91107
try {
92-
Properties globalProperties = new Properties();
93-
GLOBAL_CONFIG_FILE.createNewFile();
94-
try (InputStream inputStream = new FileInputStream(GLOBAL_CONFIG_FILE)) {
95-
globalProperties.load(inputStream);
96-
}
97-
98-
if (value.equals(globalProperties.get(prop))) {
108+
if (value.equals(environmentProperties.get(prop))) {
99109
return false;
100110
}
101111

102-
globalProperties.setProperty(prop, value);
112+
environmentProperties.setProperty(prop, value);
103113

104-
try (OutputStream outputStream = new FileOutputStream(GLOBAL_CONFIG_FILE)) {
105-
globalProperties.store(outputStream, "Modified by Testcontainers");
114+
ENVIRONMENT_CONFIG_FILE.createNewFile();
115+
try (OutputStream outputStream = new FileOutputStream(ENVIRONMENT_CONFIG_FILE)) {
116+
environmentProperties.store(outputStream, "Modified by Testcontainers");
106117
}
107118

108-
// Update internal state only if global config was successfully updated
119+
// Update internal state only if environment config was successfully updated
109120
properties.setProperty(prop, value);
110121
return true;
111122
} catch (Exception e) {
112-
log.debug("Can't store global property {} in {}", prop, GLOBAL_CONFIG_FILE);
123+
log.debug("Can't store environment property {} in {}", prop, ENVIRONMENT_CONFIG_FILE);
113124
return false;
114125
}
115126
}
116127

117128
@SneakyThrows(MalformedURLException.class)
118129
private static TestcontainersConfiguration loadConfiguration() {
119-
final TestcontainersConfiguration config = new TestcontainersConfiguration(
120-
Stream
121-
.of(
122-
TestcontainersConfiguration.class.getClassLoader().getResource(PROPERTIES_FILE_NAME),
123-
Thread.currentThread().getContextClassLoader().getResource(PROPERTIES_FILE_NAME),
124-
GLOBAL_CONFIG_FILE.toURI().toURL()
125-
)
126-
.filter(Objects::nonNull)
127-
.map(it -> {
128-
log.debug("Testcontainers configuration overrides will be loaded from {}", it);
129-
130-
final Properties subProperties = new Properties();
131-
try (final InputStream inputStream = it.openStream()) {
132-
subProperties.load(inputStream);
133-
} catch (FileNotFoundException e) {
134-
log.trace("Testcontainers config override was found on " + it + " but the file was not found", e);
135-
} catch (IOException e) {
136-
log.warn("Testcontainers config override was found on " + it + " but could not be loaded", e);
137-
}
138-
return subProperties;
139-
})
140-
.reduce(new Properties(), (a, b) -> {
141-
a.putAll(b);
142-
return a;
143-
})
130+
return new TestcontainersConfiguration(
131+
readProperties(ENVIRONMENT_CONFIG_FILE.toURI().toURL()),
132+
Stream
133+
.of(
134+
TestcontainersConfiguration.class.getClassLoader(),
135+
Thread.currentThread().getContextClassLoader()
136+
)
137+
.map(it -> it.getResource(PROPERTIES_FILE_NAME))
138+
.filter(Objects::nonNull)
139+
.map(TestcontainersConfiguration::readProperties)
140+
.reduce(new Properties(), (a, b) -> {
141+
a.putAll(b);
142+
return a;
143+
})
144144
);
145+
}
145146

146-
if (!config.getProperties().isEmpty()) {
147-
log.debug("Testcontainers configuration overrides loaded from {}", config);
147+
private static Properties readProperties(URL url) {
148+
log.debug("Testcontainers configuration overrides will be loaded from {}", url);
149+
Properties properties = new Properties();
150+
try (InputStream inputStream = url.openStream()) {
151+
properties.load(inputStream);
152+
} catch (FileNotFoundException e) {
153+
log.trace("Testcontainers config override was found on {} but the file was not found", url, e);
154+
} catch (IOException e) {
155+
log.warn("Testcontainers config override was found on {} but could not be loaded", url, e);
148156
}
149-
150-
return config;
157+
return properties;
151158
}
152159
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.testcontainers.utility;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Properties;
6+
import java.util.UUID;
7+
8+
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
9+
import static org.rnorth.visibleassertions.VisibleAssertions.assertFalse;
10+
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;
11+
12+
public class TestcontainersConfigurationTest {
13+
14+
final Properties environmentProperties = new Properties();
15+
16+
final Properties classpathProperties = new Properties();
17+
18+
@Test
19+
public void shouldReadChecksFromEnvironmentOnly() {
20+
assertFalse("checks enabled by default", newConfig().isDisableChecks());
21+
22+
classpathProperties.setProperty("checks.disable", "true");
23+
assertFalse("checks are not affected by classpath properties", newConfig().isDisableChecks());
24+
25+
environmentProperties.setProperty("checks.disable", "true");
26+
assertTrue("checks disabled", newConfig().isDisableChecks());
27+
}
28+
29+
@Test
30+
public void shouldReadDockerClientStrategyFromEnvironmentOnly() {
31+
String currentValue = newConfig().getDockerClientStrategyClassName();
32+
33+
classpathProperties.setProperty("docker.client.strategy", UUID.randomUUID().toString());
34+
assertEquals("Docker client strategy is not affected by classpath properties", currentValue, newConfig().getDockerClientStrategyClassName());
35+
36+
environmentProperties.setProperty("docker.client.strategy", "foo");
37+
assertEquals("Docker client strategy is changed", "foo", newConfig().getDockerClientStrategyClassName());
38+
}
39+
40+
private TestcontainersConfiguration newConfig() {
41+
return new TestcontainersConfiguration(environmentProperties, classpathProperties);
42+
}
43+
}

0 commit comments

Comments
 (0)