diff --git a/.ci/setup_docker.sh b/.ci/setup_docker.sh index fd71ffd48f..df9cf205a9 100755 --- a/.ci/setup_docker.sh +++ b/.ci/setup_docker.sh @@ -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 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4ec27099e6..255ad5a431 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/docker-java-core/src/main/java/com/github/dockerjava/core/DefaultDockerClientConfig.java b/docker-java-core/src/main/java/com/github/dockerjava/core/DefaultDockerClientConfig.java index c22d5cd71e..9637e27aa8 100644 --- a/docker-java-core/src/main/java/com/github/dockerjava/core/DefaultDockerClientConfig.java +++ b/docker-java-core/src/main/java/com/github/dockerjava/core/DefaultDockerClientConfig.java @@ -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; @@ -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": diff --git a/docker-java-transport-jersey/src/test/java/com/github/dockerjava/transport/JerseyTests.java b/docker-java-transport-jersey/src/test/java/com/github/dockerjava/transport/JerseyTests.java index 46626d4966..64dfe39664 100644 --- a/docker-java-transport-jersey/src/test/java/com/github/dockerjava/transport/JerseyTests.java +++ b/docker-java-transport-jersey/src/test/java/com/github/dockerjava/transport/JerseyTests.java @@ -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; @@ -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(); + } } diff --git a/docker-java-transport-tck/pom.xml b/docker-java-transport-tck/pom.xml index a92a6b002f..f65fead093 100644 --- a/docker-java-transport-tck/pom.xml +++ b/docker-java-transport-tck/pom.xml @@ -16,6 +16,11 @@ Java API Client for Docker + + ${project.groupId} + docker-java-core + ${project.version} + ${project.groupId} docker-java-transport @@ -33,6 +38,18 @@ mockwebserver 3.14.9 + + + org.testcontainers + testcontainers + 1.16.3 + + + + org.slf4j + slf4j-jdk14 + 1.7.35 + diff --git a/docker-java-transport-tck/src/main/java/com/github/dockerjava/transport/DockerHttpClientTCK.java b/docker-java-transport-tck/src/main/java/com/github/dockerjava/transport/DockerHttpClientTCK.java index 344ef7f326..f90973be6b 100644 --- a/docker-java-transport-tck/src/main/java/com/github/dockerjava/transport/DockerHttpClientTCK.java +++ b/docker-java-transport-tck/src/main/java/com/github/dockerjava/transport/DockerHttpClientTCK.java @@ -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 * @@ -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) @@ -52,4 +113,36 @@ private void ping(DockerHttpClient client) { .isEqualTo(200); } } + + private static class AttachContainerTestCallback extends ResultCallback.Adapter { + + 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 { + + 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); + }); + } + } } diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/AttachContainerCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/AttachContainerCmdIT.java index 6913c21c08..0e2df4cd49 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/AttachContainerCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/AttachContainerCmdIT.java @@ -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") @@ -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. diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/CmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/CmdIT.java index 12664c4e50..b01cb1e908 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/CmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/CmdIT.java @@ -5,116 +5,34 @@ import com.github.dockerjava.core.DockerClientImpl; import com.github.dockerjava.core.DockerRule; import com.github.dockerjava.httpclient5.ApacheDockerHttpClient; -import com.github.dockerjava.jaxrs.JerseyDockerHttpClient; import com.github.dockerjava.junit.category.Integration; -import com.github.dockerjava.netty.NettyDockerCmdExecFactory; -import com.github.dockerjava.okhttp.OkDockerHttpClient; +import com.github.dockerjava.transport.DockerHttpClient; import org.junit.Rule; import org.junit.experimental.categories.Category; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.util.Arrays; /** * @author Kanstantsin Shautsou */ @Category(Integration.class) -@RunWith(Parameterized.class) public abstract class CmdIT { - public enum FactoryType { - NETTY(true) { - @Override - public DockerClientImpl createDockerClient(DockerClientConfig config) { - return (DockerClientImpl) DockerClientBuilder.getInstance(config) - .withDockerCmdExecFactory( - new NettyDockerCmdExecFactory() - .withConnectTimeout(30 * 1000) - ) - .build(); - } - }, - JERSEY(false) { - @Override - public DockerClientImpl createDockerClient(DockerClientConfig config) { - return (DockerClientImpl) DockerClientBuilder.getInstance(config) - .withDockerHttpClient( - new TrackingDockerHttpClient( - new JerseyDockerHttpClient.Builder() - .dockerHost(config.getDockerHost()) - .sslConfig(config.getSSLConfig()) - .connectTimeout(30 * 1000) - .build() - ) - ) - .build(); - } - }, - OKHTTP(true) { - @Override - public DockerClientImpl createDockerClient(DockerClientConfig config) { - return (DockerClientImpl) DockerClientBuilder.getInstance(config) - .withDockerHttpClient( - new TrackingDockerHttpClient( - new OkDockerHttpClient.Builder() - .dockerHost(config.getDockerHost()) - .sslConfig(config.getSSLConfig()) - .connectTimeout(30 * 100) - .build() - ) - ) - .build(); - } - }, - HTTPCLIENT5(true) { - @Override - public DockerClientImpl createDockerClient(DockerClientConfig config) { - return (DockerClientImpl) DockerClientBuilder.getInstance(config) - .withDockerHttpClient( - new TrackingDockerHttpClient( - new ApacheDockerHttpClient.Builder() - .dockerHost(config.getDockerHost()) - .sslConfig(config.getSSLConfig()) - .build() - ) - ) - .build(); - } - }; - - private final String subnetPrefix; - private final boolean supportsStdinAttach; - - FactoryType(boolean supportsStdinAttach) { - this.subnetPrefix = "10." + (100 + ordinal()) + "."; - this.supportsStdinAttach = supportsStdinAttach; - } - - public String getSubnetPrefix() { - return subnetPrefix; - } - public boolean supportsStdinAttach() { - return supportsStdinAttach; - } - - public abstract DockerClientImpl createDockerClient(DockerClientConfig config); - } - - @Parameterized.Parameters(name = "{index}:{0}") - public static Iterable data() { - return Arrays.asList(FactoryType.values()); + public static DockerHttpClient createDockerHttpClient(DockerClientConfig config) { + return new TrackingDockerHttpClient( + new ApacheDockerHttpClient.Builder() + .dockerHost(config.getDockerHost()) + .sslConfig(config.getSSLConfig()) + .build() + ); } - @Parameterized.Parameter - public FactoryType factoryType; - - public FactoryType getFactoryType() { - return factoryType; + public static DockerClientImpl createDockerClient(DockerClientConfig config) { + return (DockerClientImpl) DockerClientBuilder.getInstance(config) + .withDockerHttpClient(createDockerHttpClient(config)) + .build(); } @Rule - public DockerRule dockerRule = new DockerRule( this); + public DockerRule dockerRule = new DockerRule(); @Rule public DockerHttpClientLeakDetector leakDetector = new DockerHttpClientLeakDetector(); diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/ConnectToNetworkCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/ConnectToNetworkCmdIT.java index b7c28e8bcd..40b5526113 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/ConnectToNetworkCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/ConnectToNetworkCmdIT.java @@ -26,7 +26,7 @@ public class ConnectToNetworkCmdIT extends CmdIT { @Test public void connectToNetwork() throws InterruptedException { assumeNotSwarm("no network in swarm", dockerRule); - String networkName = "connectToNetwork" + dockerRule.getKind(); + String networkName = "connectToNetwork"; CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withCmd("sleep", "9999").exec(); dockerRule.getClient().startContainerCmd(container.getId()).exec(); @@ -48,8 +48,8 @@ public void connectToNetwork() throws InterruptedException { public void connectToNetworkWithContainerNetwork() throws InterruptedException { assumeNotSwarm("no network in swarm", dockerRule); - final String subnetPrefix = getFactoryType().getSubnetPrefix() + "100"; - final String networkName = "ContainerWithNetwork" + dockerRule.getKind(); + final String subnetPrefix = "10.100.100"; + final String networkName = "ContainerWithNetwork"; final String containerIp = subnetPrefix + ".100"; CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) @@ -74,7 +74,7 @@ public void connectToNetworkWithContainerNetwork() throws InterruptedException { .withNetworkId(network.getId()) .withContainerId(container.getId()) .withContainerNetwork(new ContainerNetwork() - .withAliases("aliasName" + dockerRule.getKind()) + .withAliases("aliasName") .withIpamConfig(new ContainerNetwork.Ipam() .withIpv4Address(containerIp))) .exec(); @@ -89,7 +89,7 @@ public void connectToNetworkWithContainerNetwork() throws InterruptedException { ContainerNetwork testNetwork = inspectContainerResponse.getNetworkSettings().getNetworks().get(networkName); assertNotNull(testNetwork); - assertThat(testNetwork.getAliases(), hasItem("aliasName" + dockerRule.getKind())); + assertThat(testNetwork.getAliases(), hasItem("aliasName")); assertThat(testNetwork.getGateway(), is(subnetPrefix + ".1")); assertThat(testNetwork.getIpAddress(), is(containerIp)); } diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveFromContainerCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveFromContainerCmdIT.java index 9cede72e8b..a0eb585ebc 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveFromContainerCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveFromContainerCmdIT.java @@ -33,7 +33,7 @@ public class CopyArchiveFromContainerCmdIT extends CmdIT { public void copyFromContainer() throws Exception { // TODO extract this into a shared method CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) - .withName("copyFromContainer" + dockerRule.getKind()) + .withName("copyFromContainer") .withCmd("touch", "/copyFromContainer") .exec(); @@ -59,7 +59,7 @@ public void copyFromNonExistingContainer() throws Exception { @Test public void copyFromContainerBinaryFile() throws Exception { CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) - .withName("copyFromContainerBinaryFile" + dockerRule.getKind()) + .withName("copyFromContainerBinaryFile") .exec(); LOG.info("Created container: {}", container); diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveToContainerCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveToContainerCmdIT.java index 307c1e17c2..c8bac183a8 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveToContainerCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveToContainerCmdIT.java @@ -60,7 +60,7 @@ public void copyStreamToContainerTwice() throws Exception { private CreateContainerResponse prepareContainerForCopy(String method) { CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") - .withName("docker-java-itest-copyToContainer" + method + dockerRule.getKind()) + .withName("docker-java-itest-copyToContainer" + method) .exec(); LOG.info("Created container: {}", container); assertThat(container.getId(), not(isEmptyOrNullString())); @@ -125,7 +125,7 @@ public void copyFileWithExecutePermission() throws Exception { // script to be copied to the container's home dir and then executes it String containerCmd = "sleep 3; /home/" + scriptPath.getFileName().toString(); CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") - .withName("copyFileWithExecutivePerm" + dockerRule.getKind()) + .withName("copyFileWithExecutivePerm") .withCmd("/bin/sh", "-c", containerCmd) .exec(); // start the container diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/CopyFileFromContainerCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/CopyFileFromContainerCmdIT.java index ffef0d38a7..74bbad671b 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/CopyFileFromContainerCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/CopyFileFromContainerCmdIT.java @@ -28,7 +28,7 @@ public void copyFromContainer() throws Exception { assumeNotSwarm("", dockerRule); - String containerName = "copyFileFromContainer" + dockerRule.getKind(); + String containerName = "copyFileFromContainer"; dockerRule.ensureContainerRemoved(containerName); // TODO extract this into a shared method diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java index b70d6fc085..3eb63770f0 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java @@ -364,7 +364,7 @@ public void createContainerWithHostname() throws Exception { @Test(expected = ConflictException.class) public void createContainerWithName() throws DockerException { - String containerName = "container_" + dockerRule.getKind(); + String containerName = "container_"; CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) .withName(containerName) @@ -386,8 +386,8 @@ public void createContainerWithName() throws DockerException { @Test public void createContainerWithLink() throws DockerException { - String containerName1 = "containerWithlink_" + dockerRule.getKind(); - String containerName2 = "container2Withlink_" + dockerRule.getKind(); + String containerName1 = "containerWithlink_"; + String containerName2 = "container2Withlink_"; CreateContainerResponse container1 = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withCmd("sleep", "9999") .withName(containerName1).exec(); @@ -437,9 +437,9 @@ public void createContainerWithMemorySwappiness() throws DockerException { @Test public void createContainerWithLinkInCustomNetwork() throws DockerException { - String containerName1 = "containerCustomlink_" + dockerRule.getKind(); - String containerName2 = "containerCustom2link_" + dockerRule.getKind(); - String networkName = "linkNetcustom" + dockerRule.getKind(); + String containerName1 = "containerCustomlink_"; + String containerName2 = "containerCustom2link_"; + String networkName = "linkNetcustom"; CreateNetworkResponse createNetworkResponse = dockerRule.getClient().createNetworkCmd() .withName(networkName) @@ -485,9 +485,9 @@ public void createContainerWithLinkInCustomNetwork() throws DockerException { @Test public void createContainerWithCustomIp() throws DockerException { - String containerName1 = "containerCustomIplink_" + dockerRule.getKind(); - String networkName = "customIpNet" + dockerRule.getKind(); - String subnetPrefix = getFactoryType().getSubnetPrefix() + "101"; + String containerName1 = "containerCustomIplink_"; + String networkName = "customIpNet"; + String subnetPrefix = "10.100.101"; CreateNetworkResponse createNetworkResponse = dockerRule.getClient().createNetworkCmd() .withIpam(new Network.Ipam() @@ -523,8 +523,8 @@ public void createContainerWithCustomIp() throws DockerException { @Test public void createContainerWithAlias() throws DockerException { - String containerName1 = "containerAlias_" + dockerRule.getKind(); - String networkName = "aliasNet" + dockerRule.getKind(); + String containerName1 = "containerAlias_"; + String networkName = "aliasNet"; CreateNetworkResponse createNetworkResponse = dockerRule.getClient().createNetworkCmd() .withName(networkName) @@ -538,7 +538,7 @@ public void createContainerWithAlias() throws DockerException { .withNetworkMode(networkName)) .withCmd("sleep", "9999") .withName(containerName1) - .withAliases("server" + dockerRule.getKind()) + .withAliases("server") .exec(); assertThat(container.getId(), not(is(emptyString()))); @@ -549,7 +549,7 @@ public void createContainerWithAlias() throws DockerException { .exec(); ContainerNetwork aliasNet = inspectContainerResponse.getNetworkSettings().getNetworks().get(networkName); - assertThat(aliasNet.getAliases(), hasItem("server" + dockerRule.getKind())); + assertThat(aliasNet.getAliases(), hasItem("server")); } @Test @@ -597,7 +597,7 @@ public void createContainerWithDns() throws DockerException { public void createContainerWithEntrypoint() throws DockerException { CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) - .withName("containerEntrypoint" + dockerRule.getKind()) + .withName("containerEntrypoint") .withEntrypoint("sleep", "9999").exec(); LOG.info("Created container {}", container.toString()); @@ -616,7 +616,7 @@ public void createContainerWithExtraHosts() throws DockerException { String[] extraHosts = {"dockerhost:127.0.0.1", "otherhost:10.0.0.1"}; CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) - .withName("containerextrahosts" + dockerRule.getKind()) + .withName("containerextrahosts") .withHostConfig(newHostConfig() .withExtraHosts(extraHosts)).exec(); @@ -650,7 +650,7 @@ public void createContainerWithDevices() throws DockerException { @Test public void createContainerWithPortBindings() throws DockerException { - int baseport = 10_000 + (getFactoryType().ordinal() * 1000); + int baseport = 10_000; ExposedPort tcp22 = ExposedPort.tcp(22); ExposedPort tcp23 = ExposedPort.tcp(23); @@ -687,8 +687,8 @@ public void createContainerWithPortBindings() throws DockerException { @Test public void createContainerWithLinking() throws DockerException { - String containerName1 = "containerWithlinking_" + dockerRule.getKind(); - String containerName2 = "container2Withlinking_" + dockerRule.getKind(); + String containerName1 = "containerWithlinking_"; + String containerName2 = "container2Withlinking_"; CreateContainerResponse container1 = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) .withCmd("sleep", "9999") @@ -812,7 +812,7 @@ public void createContainerWithMacAddress() throws DockerException { @Test public void createContainerWithULimits() throws DockerException { - String containerName = "containerulimit" + dockerRule.getKind(); + String containerName = "containerulimit"; Ulimit[] ulimits = {new Ulimit("nproc", 709, 1026), new Ulimit("nofile", 1024, 4096)}; CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) @@ -834,7 +834,7 @@ public void createContainerWithULimits() throws DockerException { @Test public void createContainerWithIntegerBoundsExceedingULimit() throws DockerException { - String containerName = "containercoreulimit" + dockerRule.getKind(); + String containerName = "containercoreulimit"; Ulimit[] ulimits = {new Ulimit("core", 99999999998L, 99999999999L)}; CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/CreateNetworkCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/CreateNetworkCmdIT.java index 36776bdb17..36363b0561 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/CreateNetworkCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/CreateNetworkCmdIT.java @@ -28,7 +28,7 @@ public class CreateNetworkCmdIT extends CmdIT { public void createNetwork() throws DockerException { assumeNotSwarm("no network in swarm", dockerRule); - String networkName = "createNetwork" + dockerRule.getKind(); + String networkName = "createNetwork"; CreateNetworkResponse createNetworkResponse = dockerRule.getClient().createNetworkCmd().withName(networkName).exec(); @@ -43,8 +43,8 @@ public void createNetwork() throws DockerException { public void createNetworkWithIpamConfig() throws DockerException { assumeNotSwarm("no network in swarm", dockerRule); - String networkName = "networkIpam" + dockerRule.getKind(); - String subnet = "10.67." + (79 + getFactoryType().ordinal()) + ".0/24"; + String networkName = "networkIpam"; + String subnet = "10.67.79.0/24"; Network.Ipam ipam = new Network.Ipam().withConfig(new Network.Ipam.Config().withSubnet(subnet)); CreateNetworkResponse createNetworkResponse = dockerRule.getClient().createNetworkCmd().withName(networkName).withIpam(ipam).exec(); @@ -61,7 +61,7 @@ public void createNetworkWithIpamConfig() throws DockerException { public void createAttachableNetwork() throws DockerException { assumeThat("API version should be > 1.24", dockerRule, isGreaterOrEqual(VERSION_1_25)); - String networkName = "createAttachableNetwork" + dockerRule.getKind(); + String networkName = "createAttachableNetwork"; CreateNetworkResponse createNetworkResponse = dockerRule.getClient().createNetworkCmd() .withName(networkName) .withAttachable(true) @@ -77,9 +77,9 @@ public void createNetworkWithLabel() throws DockerException { assumeNotSwarm("no network in swarm?", dockerRule); assumeThat("API version should be >= 1.21", dockerRule, isGreaterOrEqual(VERSION_1_21)); - String networkName = "createNetworkWithLabel" + dockerRule.getKind(); + String networkName = "createNetworkWithLabel"; Map labels = new HashMap<>(); - labels.put("com.example.usage" + dockerRule.getKind(), "test"); + labels.put("com.example.usage", "test"); CreateNetworkResponse createNetworkResponse = dockerRule.getClient().createNetworkCmd().withName(networkName).withLabels(labels).exec(); assertNotNull(createNetworkResponse.getId()); Network network = dockerRule.getClient().inspectNetworkCmd().withNetworkId(createNetworkResponse.getId()).exec(); diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/CustomCommandIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/CustomCommandIT.java index e7899f84a7..b36002f102 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/CustomCommandIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/CustomCommandIT.java @@ -18,8 +18,7 @@ public class CustomCommandIT extends CmdIT { @Test public void testCustomCommand() throws Exception { - DockerClientImpl dockerClient = getFactoryType().createDockerClient(DockerRule.config(null)); - DockerHttpClient httpClient = dockerClient.getHttpClient(); + DockerHttpClient httpClient = CmdIT.createDockerHttpClient(DockerRule.config(null)); Assume.assumeNotNull(httpClient); diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/DisconnectFromNetworkCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/DisconnectFromNetworkCmdIT.java index 3c9451545d..7d9591bfe1 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/DisconnectFromNetworkCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/DisconnectFromNetworkCmdIT.java @@ -19,7 +19,7 @@ public void disconnectFromNetwork() throws InterruptedException { CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999").exec(); dockerRule.getClient().startContainerCmd(container.getId()).exec(); - CreateNetworkResponse network = dockerRule.getClient().createNetworkCmd().withName("disconnectNetwork" + dockerRule.getKind()).exec(); + CreateNetworkResponse network = dockerRule.getClient().createNetworkCmd().withName("disconnectNetwork").exec(); dockerRule.getClient().connectToNetworkCmd().withNetworkId(network.getId()).withContainerId(container.getId()).exec(); @@ -38,11 +38,11 @@ public void disconnectFromNetwork() throws InterruptedException { public void forceDisconnectFromNetwork() throws InterruptedException { assumeNotSwarm("no network in swarm", dockerRule); - CreateNetworkResponse network = dockerRule.getClient().createNetworkCmd().withName("testNetwork2" + dockerRule.getKind()).exec(); + CreateNetworkResponse network = dockerRule.getClient().createNetworkCmd().withName("testNetwork2").exec(); CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") .withHostConfig(newHostConfig() - .withNetworkMode("testNetwork2" + dockerRule.getKind())) + .withNetworkMode("testNetwork2")) .withCmd("sleep", "9999") .exec(); diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/PushImageCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/PushImageCmdIT.java index 7f55d5f9e2..f98d245634 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/PushImageCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/PushImageCmdIT.java @@ -48,7 +48,7 @@ public void pushLatest() throws Exception { assertThat(container.getId(), not(is(emptyString()))); LOG.info("Committing container: {}", container.toString()); - String imgName = authConfig.getRegistryAddress() + "/" + dockerRule.getKind() + "-push-latest"; + String imgName = authConfig.getRegistryAddress() + "/push-latest"; String imageId = dockerRule.getClient().commitCmd(container.getId()) .withRepository(imgName) .exec(); diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/RemoveVolumeCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/RemoveVolumeCmdIT.java index 76037e1e37..6d0fdf9812 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/RemoveVolumeCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/RemoveVolumeCmdIT.java @@ -17,7 +17,7 @@ public class RemoveVolumeCmdIT extends CmdIT { @Test(expected = NotFoundException.class) public void removeVolume() throws DockerException { - String volumeName = "volume1" + dockerRule.getKind(); + String volumeName = "volume1"; CreateVolumeResponse createVolumeResponse = dockerRule.getClient().createVolumeCmd() .withName(volumeName) diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/RenameContainerCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/RenameContainerCmdIT.java index fed3920b79..d6b91c2a4c 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/RenameContainerCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/RenameContainerCmdIT.java @@ -31,7 +31,7 @@ public void renameContainer() throws DockerException { String name1 = inspectContainerResponse.getName(); dockerRule.getClient().renameContainerCmd(container.getId()) - .withName(dockerRule.getKind() + "renameContainer") + .withName("renameContainer") .exec(); InspectContainerResponse inspectContainerResponse2 = dockerRule.getClient().inspectContainerCmd(container.getId()).exec(); @@ -47,7 +47,7 @@ public void renameContainer() throws DockerException { @Test(expected = NotFoundException.class) public void renameExistingContainer() throws DockerException, InterruptedException { dockerRule.getClient().renameContainerCmd("non-existing") - .withName(dockerRule.getKind() + "renameExistingContainer") + .withName("renameExistingContainer") .exec(); } } diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/StartContainerCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/StartContainerCmdIT.java index 76e4fe3299..b882a88bd4 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/StartContainerCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/StartContainerCmdIT.java @@ -171,7 +171,7 @@ public void startContainerWithDnsSearch() throws DockerException { @Test public void startContainerWithPortBindings() throws DockerException { - int baseport = 20_000 + (getFactoryType().ordinal() * 1000); + int baseport = 20_000; ExposedPort tcp22 = ExposedPort.tcp(22); ExposedPort tcp23 = ExposedPort.tcp(23); @@ -267,8 +267,8 @@ public void startContainerWithConflictingPortBindings() throws DockerException { @Test public void startContainerWithLinkingDeprecated() throws DockerException { - String container1Name = "containerWithLink1" + dockerRule.getKind(); - String container2Name = "containerWithLink2" + dockerRule.getKind(); + String container1Name = "containerWithLink1"; + String container2Name = "containerWithLink2"; dockerRule.ensureContainerRemoved(container1Name); dockerRule.ensureContainerRemoved(container2Name); @@ -328,8 +328,8 @@ public void startContainerWithLinkingDeprecated() throws DockerException { @Test public void startContainerWithLinking() throws DockerException { - String container1Name = "containerWithLinking1" + dockerRule.getKind(); - String container2Name = "containerWithLinking2" + dockerRule.getKind(); + String container1Name = "containerWithLinking1"; + String container2Name = "containerWithLinking2"; dockerRule.ensureContainerRemoved(container1Name); dockerRule.ensureContainerRemoved(container2Name); diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/swarm/SwarmCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/swarm/SwarmCmdIT.java index f88cb98730..8e653b5641 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/swarm/SwarmCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/swarm/SwarmCmdIT.java @@ -136,6 +136,6 @@ private DockerClient initializeDockerClient(Ports.Binding binding) { DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder() .withRegistryUrl("https://index.docker.io/v1/") .withDockerHost("tcp://" + binding).build(); - return getFactoryType().createDockerClient(config); + return createDockerClient(config); } } diff --git a/docker-java/src/test/java/com/github/dockerjava/core/DockerRule.java b/docker-java/src/test/java/com/github/dockerjava/core/DockerRule.java index c4a66fb102..98050d9341 100644 --- a/docker-java/src/test/java/com/github/dockerjava/core/DockerRule.java +++ b/docker-java/src/test/java/com/github/dockerjava/core/DockerRule.java @@ -11,16 +11,13 @@ import com.github.dockerjava.api.exception.ConflictException; import com.github.dockerjava.api.exception.NotFoundException; import com.github.dockerjava.cmd.CmdIT; -import com.github.dockerjava.transport.DockerHttpClient; import com.github.dockerjava.utils.LogContainerTestCallback; -import lombok.experimental.Delegate; import org.junit.rules.ExternalResource; import org.junit.runner.Description; import org.junit.runners.model.Statement; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.Closeable; import java.io.File; import java.util.HashSet; import java.util.Set; @@ -34,21 +31,14 @@ public class DockerRule extends ExternalResource { private DockerClient dockerClient; - private CmdIT cmdIT; - private final Set createdContainerIds = new HashSet<>(); private final Set createdNetworkIds = new HashSet<>(); private final Set createdVolumeNames = new HashSet<>(); - public DockerRule(CmdIT cmdIT) { - this.cmdIT = cmdIT; - } - - public DockerClient newClient() { - DockerClientImpl dockerClient = cmdIT.getFactoryType().createDockerClient(config()); + DockerClientImpl dockerClient = CmdIT.createDockerClient(config()); dockerClient.withDockerCmdExecFactory( new DockerCmdExecFactoryDelegate(dockerClient.dockerCmdExecFactory) { @@ -202,10 +192,6 @@ public String containerLog(String containerId) throws Exception { .toString(); } - public String getKind() { - return cmdIT.getFactoryType().name().toLowerCase(); - } - public void ensureContainerRemoved(String container1Name) { try { getClient().removeContainerCmd(container1Name) @@ -217,55 +203,4 @@ public void ensureContainerRemoved(String container1Name) { } } - public void ensureImageRemoved(String imageId) { - try { - getClient().removeImageCmd(imageId) - .withForce(true) - .exec(); - } catch (NotFoundException ex) { - // ignore - } - } - - private static class CreateContainerCmdDelegate implements CreateContainerCmd { - @Delegate(excludes = Closeable.class) - private final CreateContainerCmd delegate; - - private CreateContainerCmdDelegate(CreateContainerCmd delegate) { - this.delegate = delegate; - } - - @Override - public void close() { - delegate.close(); - } - } - - private static class CreateNetworkCmdDelegate implements CreateNetworkCmd { - @Delegate(excludes = Closeable.class) - private final CreateNetworkCmd delegate; - - private CreateNetworkCmdDelegate(CreateNetworkCmd delegate) { - this.delegate = delegate; - } - - @Override - public void close() { - delegate.close(); - } - } - - private static class CreateVolumeCmdDelegate implements CreateVolumeCmd { - @Delegate(excludes = Closeable.class) - private final CreateVolumeCmd delegate; - - private CreateVolumeCmdDelegate(CreateVolumeCmd delegate) { - this.delegate = delegate; - } - - @Override - public void close() { - delegate.close(); - } - } } diff --git a/docker-java/src/test/java/com/github/dockerjava/junit/PrivateRegistryRule.java b/docker-java/src/test/java/com/github/dockerjava/junit/PrivateRegistryRule.java index 7aae924f9f..327bfc9415 100644 --- a/docker-java/src/test/java/com/github/dockerjava/junit/PrivateRegistryRule.java +++ b/docker-java/src/test/java/com/github/dockerjava/junit/PrivateRegistryRule.java @@ -7,8 +7,8 @@ import com.github.dockerjava.api.model.ExposedPort; import com.github.dockerjava.api.model.PortBinding; import com.github.dockerjava.api.model.Ports; +import com.github.dockerjava.cmd.CmdIT; import com.github.dockerjava.core.DockerRule; -import com.github.dockerjava.core.DockerClientBuilder; import org.junit.rules.ExternalResource; import java.io.File; @@ -29,7 +29,7 @@ public class PrivateRegistryRule extends ExternalResource { private String containerId; public PrivateRegistryRule() { - this.dockerClient = DockerClientBuilder.getInstance().build(); + this.dockerClient = CmdIT.createDockerClient(DockerRule.config(null)); } public AuthConfig getAuthConfig() {