Skip to content

Commit 7a0a111

Browse files
authored
Do not parameterize command tests, use TCK (docker-java#1803)
* Do not parameterize command tests, use TCK * Cleanups * debug * cleanup * debug * debug * debug * debug * debug * Unset DOCKER_HOST if empty * always listen on unix socket
1 parent d44a7a1 commit 7a0a111

File tree

23 files changed

+193
-275
lines changed

23 files changed

+193
-275
lines changed

.ci/setup_docker.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ if [[ -n $DOCKER_HOST ]]; then
2121
echo "
2222
[Service]
2323
ExecStart=
24-
ExecStart=/usr/bin/dockerd -H $DOCKER_HOST
24+
ExecStart=/usr/bin/dockerd -H $DOCKER_HOST -H unix:///var/run/docker.sock
2525
" | sudo tee -a /etc/systemd/system/docker.service.d/override.conf
2626

2727
sudo systemctl daemon-reload

.github/workflows/ci.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ jobs:
3030
- name: Build with Maven
3131
env:
3232
DOCKER_HOST: ${{matrix.dockerHost}}
33-
run: ./mvnw --no-transfer-progress verify
34-
- name: Aggregate test reports with ciMate
35-
if: always()
36-
continue-on-error: true
37-
env:
38-
CIMATE_PROJECT_ID: lodr9d83
39-
CIMATE_CI_KEY: "CI / ${{matrix.name}}"
4033
run: |
41-
wget -q https://get.cimate.io/release/linux/cimate
42-
chmod +x cimate
43-
./cimate "**/TEST-*.xml"
34+
[[ -z "$DOCKER_HOST" ]] && unset DOCKER_HOST
35+
./mvnw --no-transfer-progress verify

docker-java-core/src/main/java/com/github/dockerjava/core/DefaultDockerClientConfig.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.net.URI;
2323
import java.util.HashSet;
2424
import java.util.Map;
25+
import java.util.Objects;
2526
import java.util.Properties;
2627
import java.util.Set;
2728

@@ -102,7 +103,10 @@ public class DefaultDockerClientConfig implements Serializable, DockerClientConf
102103
}
103104

104105
private URI checkDockerHostScheme(URI dockerHost) {
105-
switch (dockerHost.getScheme()) {
106+
if (dockerHost == null) {
107+
throw new DockerClientException("'dockerHost' is null");
108+
}
109+
switch (Objects.toString(dockerHost.getScheme())) {
106110
case "tcp":
107111
case "unix":
108112
case "npipe":

docker-java-transport-jersey/src/test/java/com/github/dockerjava/transport/JerseyTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.github.dockerjava.transport;
22

33
import com.github.dockerjava.jaxrs.JerseyDockerHttpClient;
4+
import org.junit.Ignore;
5+
import org.junit.Test;
46

57
import java.net.URI;
68

@@ -14,4 +16,11 @@ protected DockerHttpClient createDockerHttpClient(URI dockerHost, SSLConfig sslC
1416
.connectTimeout(30 * 1000)
1517
.build();
1618
}
19+
20+
@Test
21+
@Ignore("does not support hijacking")
22+
@Override
23+
public void testHijacking() throws Exception {
24+
super.testHijacking();
25+
}
1726
}

docker-java-transport-tck/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
<description>Java API Client for Docker</description>
1717

1818
<dependencies>
19+
<dependency>
20+
<groupId>${project.groupId}</groupId>
21+
<artifactId>docker-java-core</artifactId>
22+
<version>${project.version}</version>
23+
</dependency>
1924
<dependency>
2025
<groupId>${project.groupId}</groupId>
2126
<artifactId>docker-java-transport</artifactId>
@@ -33,6 +38,18 @@
3338
<artifactId>mockwebserver</artifactId>
3439
<version>3.14.9</version>
3540
</dependency>
41+
42+
<dependency>
43+
<groupId>org.testcontainers</groupId>
44+
<artifactId>testcontainers</artifactId>
45+
<version>1.16.3</version>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.slf4j</groupId>
50+
<artifactId>slf4j-jdk14</artifactId>
51+
<version>1.7.35</version>
52+
</dependency>
3653
</dependencies>
3754

3855
<build>

docker-java-transport-tck/src/main/java/com/github/dockerjava/transport/DockerHttpClientTCK.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,64 @@
11
package com.github.dockerjava.transport;
22

3+
import com.github.dockerjava.api.DockerClient;
4+
import com.github.dockerjava.api.async.ResultCallback;
5+
import com.github.dockerjava.api.model.Frame;
6+
import com.github.dockerjava.core.DefaultDockerClientConfig;
7+
import com.github.dockerjava.core.DockerClientImpl;
38
import com.github.dockerjava.transport.DockerHttpClient.Request;
49
import com.github.dockerjava.transport.DockerHttpClient.Request.Method;
510
import com.github.dockerjava.transport.DockerHttpClient.Response;
611
import okhttp3.mockwebserver.MockResponse;
712
import okhttp3.mockwebserver.MockWebServer;
813
import org.junit.Test;
14+
import org.testcontainers.DockerClientFactory;
15+
import org.testcontainers.containers.GenericContainer;
16+
import org.testcontainers.dockerclient.TransportConfig;
917

18+
import java.io.PipedInputStream;
19+
import java.io.PipedOutputStream;
1020
import java.net.URI;
1121

22+
import static java.util.concurrent.TimeUnit.SECONDS;
1223
import static org.assertj.core.api.Assertions.assertThat;
1324

1425
public abstract class DockerHttpClientTCK {
1526

1627
protected abstract DockerHttpClient createDockerHttpClient(URI dockerHost, SSLConfig sslConfig);
1728

29+
@Test
30+
public void testHijacking() throws Exception {
31+
try (
32+
DockerClient client = createDockerClient();
33+
34+
PipedOutputStream out = new PipedOutputStream();
35+
PipedInputStream in = new PipedInputStream(out);
36+
37+
AttachContainerTestCallback callback = new AttachContainerTestCallback();
38+
39+
AttacheableContainer container = new AttacheableContainer() {
40+
@Override
41+
protected void containerIsCreated(String containerId) {
42+
client.attachContainerCmd(containerId)
43+
.withStdOut(true)
44+
.withFollowStream(true)
45+
.withStdIn(in)
46+
.exec(callback);
47+
}
48+
};
49+
) {
50+
container.start();
51+
assertThat(callback.awaitStarted(5, SECONDS)).as("attached").isTrue();
52+
53+
String snippet = "hello world";
54+
out.write((snippet + "\n").getBytes());
55+
out.flush();
56+
57+
assertThat(callback.awaitCompletion(15, SECONDS)).as("completed").isTrue();
58+
assertThat(callback.toString()).contains("STDOUT: " + snippet);
59+
}
60+
}
61+
1862
/**
1963
* Test that docker-java supports path in DOCKER_HOST
2064
*
@@ -36,10 +80,27 @@ public final void testPath() throws Exception {
3680
}
3781
}
3882

83+
private DockerHttpClient createDockerHttpClient() {
84+
// Use Testcontainers to detect Docker environment
85+
TransportConfig transportConfig = DockerClientFactory.instance().getTransportConfig();
86+
return createDockerHttpClient(transportConfig.getDockerHost(), transportConfig.getSslConfig());
87+
}
88+
3989
private DockerHttpClient createDockerHttpClient(String dockerHost) {
4090
return createDockerHttpClient(URI.create(dockerHost), null);
4191
}
4292

93+
private DockerClient createDockerClient() {
94+
return createDockerClient(createDockerHttpClient());
95+
}
96+
97+
private DockerClient createDockerClient(DockerHttpClient dockerHttpClient) {
98+
return DockerClientImpl.getInstance(
99+
DefaultDockerClientConfig.createDefaultConfigBuilder().build(),
100+
dockerHttpClient
101+
);
102+
}
103+
43104
private void ping(DockerHttpClient client) {
44105
Request pingRequest = Request.builder()
45106
.method(Method.GET)
@@ -52,4 +113,36 @@ private void ping(DockerHttpClient client) {
52113
.isEqualTo(200);
53114
}
54115
}
116+
117+
private static class AttachContainerTestCallback extends ResultCallback.Adapter<Frame> {
118+
119+
private final StringBuffer log = new StringBuffer();
120+
121+
@Override
122+
public void onNext(Frame item) {
123+
log.append(item.toString());
124+
super.onNext(item);
125+
}
126+
127+
@Override
128+
public String toString() {
129+
return log.toString();
130+
}
131+
}
132+
133+
private static class AttacheableContainer extends GenericContainer<AttacheableContainer> {
134+
135+
private AttacheableContainer() {
136+
super("busybox:1.35.0");
137+
138+
withCommand("/bin/sh", "-c", "read line && echo $line");
139+
withCreateContainerCmdModifier(it -> {
140+
it.withTty(false);
141+
it.withAttachStdin(true);
142+
it.withAttachStdout(true);
143+
it.withAttachStderr(true);
144+
it.withStdinOpen(true);
145+
});
146+
}
147+
}
55148
}

docker-java/src/test/java/com/github/dockerjava/cmd/AttachContainerCmdIT.java

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ public class AttachContainerCmdIT extends CmdIT {
4444
public void attachContainerWithStdin() throws Exception {
4545
DockerClient dockerClient = dockerRule.getClient();
4646

47-
Assume.assumeTrue("supports stdin attach", getFactoryType().supportsStdinAttach());
48-
4947
String snippet = "hello world";
5048

5149
CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
@@ -183,53 +181,6 @@ public void onNext(Frame frame) {
183181
assertThat(callback.toString(), containsString("stdout\r\nstderr"));
184182
}
185183

186-
@Test
187-
public void attachContainerStdinUnsupported() throws Exception {
188-
189-
DockerClient dockerClient = dockerRule.getClient();
190-
Assume.assumeFalse("does not support stdin attach", getFactoryType().supportsStdinAttach());
191-
expectedException.expect(UnsupportedOperationException.class);
192-
193-
String snippet = "hello world";
194-
195-
CreateContainerResponse container = dockerClient.createContainerCmd(DEFAULT_IMAGE)
196-
.withCmd("echo", snippet)
197-
.withTty(false)
198-
.withAttachStdin(true)
199-
.withAttachStdout(true)
200-
.withAttachStderr(true)
201-
.exec();
202-
203-
LOG.info("Created container: {}", container.toString());
204-
assertThat(container.getId(), not(is(emptyString())));
205-
206-
AttachContainerTestCallback callback = new AttachContainerTestCallback() {
207-
@Override
208-
public void onNext(Frame frame) {
209-
assertThat(frame.getStreamType(), equalTo(StreamType.STDOUT));
210-
super.onNext(frame);
211-
}
212-
};
213-
214-
InputStream stdin = new ByteArrayInputStream("".getBytes());
215-
216-
dockerClient.attachContainerCmd(container.getId())
217-
.withStdErr(true)
218-
.withStdOut(true)
219-
.withFollowStream(true)
220-
.withLogs(true)
221-
.withStdIn(stdin)
222-
.exec(callback);
223-
224-
assertFalse("Processing of the response is not expected to be started" +
225-
" because `attachContainerCmd` with stdin is not supported", callback.awaitStarted(5, SECONDS));
226-
227-
dockerClient.startContainerCmd(container.getId()).exec();
228-
229-
callback.awaitCompletion(30, TimeUnit.SECONDS);
230-
callback.close();
231-
}
232-
233184
/**
234185
* {@link ResultCallback#onComplete()} should be called immediately after
235186
* container exit. It was broken for Netty and TLS connection.

0 commit comments

Comments
 (0)