Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ci/setup_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if [[ -n $DOCKER_HOST ]]; then
echo "
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H $DOCKER_HOST
ExecStart=/usr/bin/dockerd -H $DOCKER_HOST -H unix:///var/run/docker.sock
" | sudo tee -a /etc/systemd/system/docker.service.d/override.conf

sudo systemctl daemon-reload
Expand Down
12 changes: 2 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ jobs:
- name: Build with Maven
env:
DOCKER_HOST: ${{matrix.dockerHost}}
run: ./mvnw --no-transfer-progress verify
- name: Aggregate test reports with ciMate
if: always()
continue-on-error: true
env:
CIMATE_PROJECT_ID: lodr9d83
CIMATE_CI_KEY: "CI / ${{matrix.name}}"
run: |
wget -q https://get.cimate.io/release/linux/cimate
chmod +x cimate
./cimate "**/TEST-*.xml"
[[ -z "$DOCKER_HOST" ]] && unset DOCKER_HOST
./mvnw --no-transfer-progress verify
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.net.URI;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;

Expand Down Expand Up @@ -102,7 +103,10 @@ public class DefaultDockerClientConfig implements Serializable, DockerClientConf
}

private URI checkDockerHostScheme(URI dockerHost) {
switch (dockerHost.getScheme()) {
if (dockerHost == null) {
throw new DockerClientException("'dockerHost' is null");
}
switch (Objects.toString(dockerHost.getScheme())) {
case "tcp":
case "unix":
case "npipe":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.dockerjava.transport;

import com.github.dockerjava.jaxrs.JerseyDockerHttpClient;
import org.junit.Ignore;
import org.junit.Test;

import java.net.URI;

Expand All @@ -14,4 +16,11 @@ protected DockerHttpClient createDockerHttpClient(URI dockerHost, SSLConfig sslC
.connectTimeout(30 * 1000)
.build();
}

@Test
@Ignore("does not support hijacking")
@Override
public void testHijacking() throws Exception {
super.testHijacking();
}
}
17 changes: 17 additions & 0 deletions docker-java-transport-tck/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
<description>Java API Client for Docker</description>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>docker-java-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>docker-java-transport</artifactId>
Expand All @@ -33,6 +38,18 @@
<artifactId>mockwebserver</artifactId>
<version>3.14.9</version>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.16.3</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.35</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,64 @@
package com.github.dockerjava.transport;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientImpl;
import com.github.dockerjava.transport.DockerHttpClient.Request;
import com.github.dockerjava.transport.DockerHttpClient.Request.Method;
import com.github.dockerjava.transport.DockerHttpClient.Response;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.Test;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.dockerclient.TransportConfig;

import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.net.URI;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;

public abstract class DockerHttpClientTCK {

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

@Test
public void testHijacking() throws Exception {
try (
DockerClient client = createDockerClient();

PipedOutputStream out = new PipedOutputStream();
PipedInputStream in = new PipedInputStream(out);

AttachContainerTestCallback callback = new AttachContainerTestCallback();

AttacheableContainer container = new AttacheableContainer() {
@Override
protected void containerIsCreated(String containerId) {
client.attachContainerCmd(containerId)
.withStdOut(true)
.withFollowStream(true)
.withStdIn(in)
.exec(callback);
}
};
) {
container.start();
assertThat(callback.awaitStarted(5, SECONDS)).as("attached").isTrue();

String snippet = "hello world";
out.write((snippet + "\n").getBytes());
out.flush();

assertThat(callback.awaitCompletion(15, SECONDS)).as("completed").isTrue();
assertThat(callback.toString()).contains("STDOUT: " + snippet);
}
}

/**
* Test that docker-java supports path in DOCKER_HOST
*
Expand All @@ -36,10 +80,27 @@ public final void testPath() throws Exception {
}
}

private DockerHttpClient createDockerHttpClient() {
// Use Testcontainers to detect Docker environment
TransportConfig transportConfig = DockerClientFactory.instance().getTransportConfig();
return createDockerHttpClient(transportConfig.getDockerHost(), transportConfig.getSslConfig());
}

private DockerHttpClient createDockerHttpClient(String dockerHost) {
return createDockerHttpClient(URI.create(dockerHost), null);
}

private DockerClient createDockerClient() {
return createDockerClient(createDockerHttpClient());
}

private DockerClient createDockerClient(DockerHttpClient dockerHttpClient) {
return DockerClientImpl.getInstance(
DefaultDockerClientConfig.createDefaultConfigBuilder().build(),
dockerHttpClient
);
}

private void ping(DockerHttpClient client) {
Request pingRequest = Request.builder()
.method(Method.GET)
Expand All @@ -52,4 +113,36 @@ private void ping(DockerHttpClient client) {
.isEqualTo(200);
}
}

private static class AttachContainerTestCallback extends ResultCallback.Adapter<Frame> {

private final StringBuffer log = new StringBuffer();

@Override
public void onNext(Frame item) {
log.append(item.toString());
super.onNext(item);
}

@Override
public String toString() {
return log.toString();
}
}

private static class AttacheableContainer extends GenericContainer<AttacheableContainer> {

private AttacheableContainer() {
super("busybox:1.35.0");

withCommand("/bin/sh", "-c", "read line && echo $line");
withCreateContainerCmdModifier(it -> {
it.withTty(false);
it.withAttachStdin(true);
it.withAttachStdout(true);
it.withAttachStderr(true);
it.withStdinOpen(true);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public class AttachContainerCmdIT extends CmdIT {
public void attachContainerWithStdin() throws Exception {
DockerClient dockerClient = dockerRule.getClient();

Assume.assumeTrue("supports stdin attach", getFactoryType().supportsStdinAttach());

String snippet = "hello world";

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

@Test
public void attachContainerStdinUnsupported() throws Exception {

DockerClient dockerClient = dockerRule.getClient();
Assume.assumeFalse("does not support stdin attach", getFactoryType().supportsStdinAttach());
expectedException.expect(UnsupportedOperationException.class);

String snippet = "hello world";

CreateContainerResponse container = dockerClient.createContainerCmd(DEFAULT_IMAGE)
.withCmd("echo", snippet)
.withTty(false)
.withAttachStdin(true)
.withAttachStdout(true)
.withAttachStderr(true)
.exec();

LOG.info("Created container: {}", container.toString());
assertThat(container.getId(), not(is(emptyString())));

AttachContainerTestCallback callback = new AttachContainerTestCallback() {
@Override
public void onNext(Frame frame) {
assertThat(frame.getStreamType(), equalTo(StreamType.STDOUT));
super.onNext(frame);
}
};

InputStream stdin = new ByteArrayInputStream("".getBytes());

dockerClient.attachContainerCmd(container.getId())
.withStdErr(true)
.withStdOut(true)
.withFollowStream(true)
.withLogs(true)
.withStdIn(stdin)
.exec(callback);

assertFalse("Processing of the response is not expected to be started" +
" because `attachContainerCmd` with stdin is not supported", callback.awaitStarted(5, SECONDS));

dockerClient.startContainerCmd(container.getId()).exec();

callback.awaitCompletion(30, TimeUnit.SECONDS);
callback.close();
}

/**
* {@link ResultCallback#onComplete()} should be called immediately after
* container exit. It was broken for Netty and TLS connection.
Expand Down
Loading