Skip to content

Commit 780b8e3

Browse files
authored
Upgrade to docker-java 3.2.2, add Apache HttpClient5 option (#2803)
1 parent 5566597 commit 780b8e3

File tree

8 files changed

+79
-12
lines changed

8 files changed

+79
-12
lines changed

.github/workflows/ci.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,21 @@ jobs:
6666
wget -q https://get.cimate.io/release/linux/cimate
6767
chmod +x cimate
6868
./cimate "**/TEST-*.xml"
69+
httpclient5_test:
70+
runs-on: ubuntu-18.04
71+
steps:
72+
- uses: actions/checkout@v2
73+
- name: Build with Gradle
74+
run: |
75+
echo "transport.type=httpclient5" > $HOME/.testcontainers.properties
76+
cat $HOME/.testcontainers.properties
77+
./gradlew --no-daemon --scan testcontainers:test --tests '*EventStreamTest'
78+
- name: aggregate test reports with ciMate
79+
if: always()
80+
env:
81+
CIMATE_PROJECT_ID: 2348n4vl
82+
CIMATE_CI_KEY: "CI / Apache HttpClient5"
83+
run: |
84+
wget -q https://get.cimate.io/release/linux/cimate
85+
chmod +x cimate
86+
./cimate "**/TEST-*.xml"

core/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,18 @@ dependencies {
157157

158158
compile "net.java.dev.jna:jna-platform:5.5.0"
159159

160-
shaded ('com.github.docker-java:docker-java-transport-okhttp:3.2.1') {
160+
shaded ('com.github.docker-java:docker-java-transport-okhttp:3.2.2') {
161161
exclude(group: 'net.java.dev.jna')
162162
exclude(group: 'com.google.code.findbug')
163163
exclude(group: 'org.slf4j')
164164
exclude(group: 'org.apache.commons', module: 'commons-compress')
165165
}
166166

167+
shaded ('com.github.docker-java:docker-java-transport-zerodep:3.2.2') {
168+
exclude(group: 'net.java.dev.jna')
169+
exclude(group: 'org.slf4j')
170+
}
171+
167172
shaded "org.yaml:snakeyaml:1.25"
168173

169174
shaded 'org.glassfish.main.external:trilead-ssh2-repackaged:4.1.2'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.testcontainers;
2+
3+
import com.github.dockerjava.api.DockerClient;
4+
import lombok.RequiredArgsConstructor;
5+
import lombok.experimental.Delegate;
6+
7+
@RequiredArgsConstructor
8+
class DelegatingDockerClient implements DockerClient {
9+
10+
@Delegate
11+
private final DockerClient dockerClient;
12+
}

core/src/main/java/org/testcontainers/DockerClientFactory.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,12 @@ public DockerClient client() {
148148

149149
String hostIpAddress = strategy.getDockerHostIpAddress();
150150
log.info("Docker host IP address is {}", hostIpAddress);
151-
final DockerClient client = strategy.getClient();
151+
final DockerClient client = new DelegatingDockerClient(strategy.getClient()) {
152+
@Override
153+
public void close() {
154+
throw new IllegalStateException("You should never close the global DockerClient!");
155+
}
156+
};
152157

153158
Info dockerInfo = client.infoCmd().exec();
154159
Version version = client.versionCmd().exec();

core/src/main/java/org/testcontainers/containers/ContainerState.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ default <T> T copyFileFromContainer(String containerPath, ThrowingFunction<Inpu
310310
throw new IllegalStateException("copyFileFromContainer can only be used when the Container is created.");
311311
}
312312

313+
DockerClient dockerClient = DockerClientFactory.instance().client();
313314
try (
314-
DockerClient dockerClient = DockerClientFactory.instance().client();
315315
InputStream inputStream = dockerClient.copyArchiveFromContainerCmd(getContainerId(), containerPath).exec();
316316
TarArchiveInputStream tarInputStream = new TarArchiveInputStream(inputStream)
317317
) {

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import com.github.dockerjava.api.model.Network;
55
import com.github.dockerjava.core.DockerClientConfig;
66
import com.github.dockerjava.core.DockerClientImpl;
7-
import com.github.dockerjava.okhttp.OkHttpDockerCmdExecFactory;
7+
import com.github.dockerjava.okhttp.OkDockerHttpClient;
8+
import com.github.dockerjava.transport.DockerHttpClient;
9+
import com.github.dockerjava.zerodep.ZerodepDockerHttpClient;
810
import com.google.common.annotations.VisibleForTesting;
911
import com.google.common.base.Throwables;
1012
import org.apache.commons.io.IOUtils;
@@ -116,6 +118,13 @@ public static DockerClientProviderStrategy getFirstValidStrategy(List<DockerClie
116118
try {
117119
strategy.test();
118120
LOGGER.info("Found Docker environment with {}", strategy.getDescription());
121+
LOGGER.debug(
122+
"Transport type: '{}', Docker host: '{}'",
123+
TestcontainersConfiguration.getInstance().getTransportType(),
124+
strategy.config != null
125+
? strategy.config.getDockerHost()
126+
: "<unknown>"
127+
);
119128
strategy.checkOSType();
120129

121130
if (strategy.isPersistable()) {
@@ -171,9 +180,28 @@ public DockerClient getClient() {
171180
}
172181

173182
protected DockerClient getClientForConfig(DockerClientConfig config) {
174-
return DockerClientImpl
175-
.getInstance(new AuthDelegatingDockerClientConfig(config))
176-
.withDockerCmdExecFactory(new OkHttpDockerCmdExecFactory());
183+
config = new AuthDelegatingDockerClientConfig(config);
184+
final DockerHttpClient dockerHttpClient;
185+
186+
String transportType = TestcontainersConfiguration.getInstance().getTransportType();
187+
switch (transportType) {
188+
case "okhttp":
189+
dockerHttpClient = new OkDockerHttpClient.Builder()
190+
.dockerHost(config.getDockerHost())
191+
.sslConfig(config.getSSLConfig())
192+
.build();
193+
break;
194+
case "httpclient5":
195+
dockerHttpClient = new ZerodepDockerHttpClient.Builder()
196+
.dockerHost(config.getDockerHost())
197+
.sslConfig(config.getSSLConfig())
198+
.build();
199+
break;
200+
default:
201+
throw new IllegalArgumentException("Unknown transport type '" + transportType + "'");
202+
}
203+
204+
return DockerClientImpl.getInstance(config, dockerHttpClient);
177205
}
178206

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

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,6 @@ public String getDockerClientStrategyClassName() {
120120
return (String) environmentProperties.get("docker.client.strategy");
121121
}
122122

123-
/**
124-
*
125-
* @deprecated we no longer have different transport types
126-
*/
127-
@Deprecated
128123
public String getTransportType() {
129124
return properties.getProperty("transport.type", "okhttp");
130125
}

examples/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ apply from: "$rootDir/../gradle/ci-support.gradle"
33

44
subprojects {
55
apply plugin:"java"
6+
7+
repositories {
8+
jcenter()
9+
}
610
}

0 commit comments

Comments
 (0)