Skip to content

Commit

Permalink
Merge pull request #345 from amihaiemil/344
Browse files Browse the repository at this point in the history
Implemented docker.info()
  • Loading branch information
amihaiemil authored Apr 20, 2020
2 parents 7996e81 + d3c512b commit 301d691
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/main/java/com/amihaiemil/docker/Docker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br><br>
*
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/com/amihaiemil/docker/Info.java
Original file line number Diff line number Diff line change
@@ -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();
}
27 changes: 23 additions & 4 deletions src/main/java/com/amihaiemil/docker/RtDocker.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ abstract class RtDocker implements Docker {
* Base URI.
*/
private final URI baseUri;

/**
* Ctor.
* @param client Given HTTP Client.
Expand All @@ -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");
Expand Down Expand Up @@ -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
);
}
Expand Down Expand Up @@ -157,10 +157,29 @@ public Version version() throws IOException {
final String versionUri = this.baseUri.toString() + "/version";
return new RtVersion(
this.client,
URI.create(versionUri)
URI.create(versionUri),
this
);
}

@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;
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/com/amihaiemil/docker/RtInfo.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
19 changes: 18 additions & 1 deletion src/main/java/com/amihaiemil/docker/RtVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -134,4 +146,9 @@ public String arch() {
public boolean experimental() {
return this.getBoolean("Experimental");
}

@Override
public Docker docker() {
return this.docker;
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/amihaiemil/docker/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
37 changes: 37 additions & 0 deletions src/test/java/com/amihaiemil/docker/UnixDockerITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,43 @@ 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")
);
}

/**
* 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.
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/com/amihaiemil/docker/UnixDockerTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
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;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

import javax.json.Json;

/**
* Unit tests for LocalUnixDocker.
* @author Mihai Andronache (amihaiemil@gmail.com)
Expand Down Expand Up @@ -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()
)
);
}
}

0 comments on commit 301d691

Please sign in to comment.