From 9a81ed1394bea1c5c4f4ae578c6974f2c1554420 Mon Sep 17 00:00:00 2001 From: amihaiemil Date: Mon, 20 Apr 2020 15:26:52 +0300 Subject: [PATCH 1/3] #344 Docker.info() + unit test --- .../java/com/amihaiemil/docker/Docker.java | 9 ++- src/main/java/com/amihaiemil/docker/Info.java | 43 ++++++++++++++ .../java/com/amihaiemil/docker/RtDocker.java | 18 ++++++ .../java/com/amihaiemil/docker/RtInfo.java | 57 +++++++++++++++++++ .../amihaiemil/docker/UnixDockerTestCase.java | 37 ++++++++++++ 5 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/amihaiemil/docker/Info.java create mode 100644 src/main/java/com/amihaiemil/docker/RtInfo.java diff --git a/src/main/java/com/amihaiemil/docker/Docker.java b/src/main/java/com/amihaiemil/docker/Docker.java index ab4f9dc6..2c5afed3 100644 --- a/src/main/java/com/amihaiemil/docker/Docker.java +++ b/src/main/java/com/amihaiemil/docker/Docker.java @@ -98,12 +98,19 @@ public interface Docker { Plugins plugins(); /** - * Entry point for Version API. + * Version of this Docker engine. * @return Version. * @throws IOException If an I/O error occurs. */ Version version() throws IOException; + /** + * Detailed information about this Docker engine, in JSON. + * @return Info. + * @throws IOException If an I/O problem occurs. + */ + Info info() throws IOException; + /** * The underlying, immutable, Apache HttpClient.

* diff --git a/src/main/java/com/amihaiemil/docker/Info.java b/src/main/java/com/amihaiemil/docker/Info.java new file mode 100644 index 00000000..42535c42 --- /dev/null +++ b/src/main/java/com/amihaiemil/docker/Info.java @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2018-2020, Mihai Emil Andronache + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1)Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2)Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3)Neither the name of docker-java-api nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package com.amihaiemil.docker; + +import javax.json.JsonObject; + +/** + * General information about the Docker engine. + * @author Mihai Andronache (amihaiemil@gmail.com) + * @version $Id$ + * @since 0.0.13 + */ +public interface Info extends JsonObject { + + /** + * The Docker engine to which this Info refers to. + * @return Docker. + */ + Docker docker(); +} diff --git a/src/main/java/com/amihaiemil/docker/RtDocker.java b/src/main/java/com/amihaiemil/docker/RtDocker.java index 0c3992a7..8b3ea11e 100644 --- a/src/main/java/com/amihaiemil/docker/RtDocker.java +++ b/src/main/java/com/amihaiemil/docker/RtDocker.java @@ -161,6 +161,24 @@ public Version version() throws IOException { ); } + @Override + public Info info() throws IOException { + final HttpGet info = new HttpGet(this.baseUri.toString() + "/info"); + try { + return new RtInfo( + this.client.execute( + info, + new ReadJsonObject( + new MatchStatus(info.getURI(), HttpStatus.SC_OK) + ) + ), + this + ); + } finally { + info.releaseConnection(); + } + } + @Override public HttpClient httpClient() { return this.client; diff --git a/src/main/java/com/amihaiemil/docker/RtInfo.java b/src/main/java/com/amihaiemil/docker/RtInfo.java new file mode 100644 index 00000000..65db46bc --- /dev/null +++ b/src/main/java/com/amihaiemil/docker/RtInfo.java @@ -0,0 +1,57 @@ +/** + * Copyright (c) 2018-2020, Mihai Emil Andronache + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1)Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2)Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3)Neither the name of docker-java-api nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package com.amihaiemil.docker; + +import javax.json.JsonObject; + +/** + * Info about the Docker engine, in JSON format. + * @author Mihai Andronache (amihaiemil@gmail.com) + * @version $Id$ + * @since 0.0.13 + */ +final class RtInfo extends JsonResource implements Info { + + /** + * Docker to which iot refers to. + */ + private final Docker docker; + + /** + * Constructor. + * @param rep This info in JSON format. + * @param docker The Docker to which it refers to. + */ + RtInfo(final JsonObject rep, final Docker docker) { + super(rep); + this.docker = docker; + } + + @Override + public Docker docker() { + return this.docker; + } +} diff --git a/src/test/java/com/amihaiemil/docker/UnixDockerTestCase.java b/src/test/java/com/amihaiemil/docker/UnixDockerTestCase.java index db64e11c..b21cb98f 100644 --- a/src/test/java/com/amihaiemil/docker/UnixDockerTestCase.java +++ b/src/test/java/com/amihaiemil/docker/UnixDockerTestCase.java @@ -26,6 +26,7 @@ package com.amihaiemil.docker; import com.amihaiemil.docker.mock.AssertRequest; +import com.amihaiemil.docker.mock.Condition; import com.amihaiemil.docker.mock.Response; import java.io.File; import org.apache.http.HttpStatus; @@ -33,6 +34,8 @@ import org.hamcrest.Matchers; import org.junit.Test; +import javax.json.Json; + /** * Unit tests for LocalUnixDocker. * @author Mihai Andronache (amihaiemil@gmail.com) @@ -191,4 +194,38 @@ public void unsupportedOperationPlugins() { new File("/var/run/docker.sock") ).plugins(); } + + /** + * RtDocker can return info about itself. + * @throws Exception If something goes wrong. + */ + @Test + public void returnsInfo() throws Exception { + final Docker docker = new UnixDocker( + new AssertRequest( + new Response( + HttpStatus.SC_OK, + "{\"info\": \"running\"}" + ), + new Condition( + "info() must send a GET request", + req -> "GET".equals(req.getRequestLine().getMethod()) + ), + new Condition( + "info() resource URL must be unix://localhost:80/1.40/info", + req -> req.getRequestLine() + .getUri().equals("unix://localhost:80/1.40/info") + ) + ), + "1.40" + ); + MatcherAssert.assertThat( + docker.info(), + Matchers.equalTo( + Json.createObjectBuilder() + .add("info", "running") + .build() + ) + ); + } } From 7d7f17a47bd41704231245b8f8e1d258013655b0 Mon Sep 17 00:00:00 2001 From: amihaiemil Date: Mon, 20 Apr 2020 19:55:50 +0300 Subject: [PATCH 2/3] #344 Integration test for docker.info() --- .../amihaiemil/docker/UnixDockerITCase.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/test/java/com/amihaiemil/docker/UnixDockerITCase.java b/src/test/java/com/amihaiemil/docker/UnixDockerITCase.java index 7ad6bf46..53339353 100644 --- a/src/test/java/com/amihaiemil/docker/UnixDockerITCase.java +++ b/src/test/java/com/amihaiemil/docker/UnixDockerITCase.java @@ -62,6 +62,24 @@ public void pingsDocker() throws Exception { MatcherAssert.assertThat(docker.ping(), Matchers.is(Boolean.TRUE)); } + /** + * UnixDocker can return Info about Docker. + * @throws Exception If something goes wrong. + */ + @Test + public void returnsInfo() throws Exception { + final Docker docker = new UnixDocker( + new File("/var/run/docker.sock") + ); + final Info info = docker.info(); + MatcherAssert.assertThat(info, Matchers.notNullValue()); + MatcherAssert.assertThat(info.docker(), Matchers.is(docker)); + MatcherAssert.assertThat( + info.getString("OSType"), + Matchers.equalTo("linux") + ); + } + /** * Docker can return its Events unfiltered. * @throws Exception If something goes wrong. From d3c512b279b8a0e990594af6c3d4fc60818d0055 Mon Sep 17 00:00:00 2001 From: amihaiemil Date: Mon, 20 Apr 2020 20:03:09 +0300 Subject: [PATCH 3/3] Integration test for docker.version() --- .../java/com/amihaiemil/docker/RtDocker.java | 9 +++++---- .../java/com/amihaiemil/docker/RtVersion.java | 19 ++++++++++++++++++- .../java/com/amihaiemil/docker/Version.java | 6 ++++++ .../amihaiemil/docker/UnixDockerITCase.java | 19 +++++++++++++++++++ 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/amihaiemil/docker/RtDocker.java b/src/main/java/com/amihaiemil/docker/RtDocker.java index 8b3ea11e..57f5234b 100644 --- a/src/main/java/com/amihaiemil/docker/RtDocker.java +++ b/src/main/java/com/amihaiemil/docker/RtDocker.java @@ -49,7 +49,7 @@ abstract class RtDocker implements Docker { * Base URI. */ private final URI baseUri; - + /** * Ctor. * @param client Given HTTP Client. @@ -59,7 +59,7 @@ abstract class RtDocker implements Docker { this.client = client; this.baseUri = baseUri; } - + @Override public final boolean ping() throws IOException { final HttpGet ping = new HttpGet(this.baseUri.toString() + "/_ping"); @@ -126,7 +126,7 @@ public final Execs execs() { public final Swarm swarm() { return new RtSwarm( this.client, - URI.create(this.baseUri.toString().concat("/swarm")), + URI.create(this.baseUri.toString().concat("/swarm")), this ); } @@ -157,7 +157,8 @@ public Version version() throws IOException { final String versionUri = this.baseUri.toString() + "/version"; return new RtVersion( this.client, - URI.create(versionUri) + URI.create(versionUri), + this ); } diff --git a/src/main/java/com/amihaiemil/docker/RtVersion.java b/src/main/java/com/amihaiemil/docker/RtVersion.java index fc9718df..42fb3176 100644 --- a/src/main/java/com/amihaiemil/docker/RtVersion.java +++ b/src/main/java/com/amihaiemil/docker/RtVersion.java @@ -39,14 +39,26 @@ * @since 0.0.11 */ final class RtVersion extends JsonResource implements Version { + + /** + * Docker to which this Version belongs. + */ + private final Docker docker; + /** * Ctor. * @param client The http client. * @param uri The URI for this version. + * @param dkr Parent Docker. * @throws IOException If an I/O error occurs. */ - RtVersion(final HttpClient client, final URI uri) throws IOException { + RtVersion( + final HttpClient client, + final URI uri, + final Docker dkr + ) throws IOException { super(fetch(client, uri)); + this.docker = dkr; } /** @@ -134,4 +146,9 @@ public String arch() { public boolean experimental() { return this.getBoolean("Experimental"); } + + @Override + public Docker docker() { + return this.docker; + } } diff --git a/src/main/java/com/amihaiemil/docker/Version.java b/src/main/java/com/amihaiemil/docker/Version.java index 09601a13..5b6c35eb 100644 --- a/src/main/java/com/amihaiemil/docker/Version.java +++ b/src/main/java/com/amihaiemil/docker/Version.java @@ -49,4 +49,10 @@ public interface Version extends JsonObject { * @return Whether experimental docker features are enabled */ boolean experimental(); + + /** + * The Docker engine to which this Version belongs. + * @return Docker. + */ + Docker docker(); } diff --git a/src/test/java/com/amihaiemil/docker/UnixDockerITCase.java b/src/test/java/com/amihaiemil/docker/UnixDockerITCase.java index 53339353..da348fa7 100644 --- a/src/test/java/com/amihaiemil/docker/UnixDockerITCase.java +++ b/src/test/java/com/amihaiemil/docker/UnixDockerITCase.java @@ -80,6 +80,25 @@ public void returnsInfo() throws Exception { ); } + /** + * UnixDocker can return the Version of Docker. + * @throws Exception If something goes wrong. + */ + @Test + public void returnsVersion() throws Exception { + final Docker docker = new UnixDocker( + new File("/var/run/docker.sock") + ); + final Version version = docker.version(); + MatcherAssert.assertThat(version, Matchers.notNullValue()); + MatcherAssert.assertThat(version.docker(), Matchers.is(docker)); + MatcherAssert.assertThat( + version.platformName(), + Matchers.equalTo("Docker Engine - Community") + ); + } + + /** * Docker can return its Events unfiltered. * @throws Exception If something goes wrong.