Skip to content

Commit 301d691

Browse files
authored
Merge pull request #345 from amihaiemil/344
Implemented docker.info()
2 parents 7996e81 + d3c512b commit 301d691

File tree

8 files changed

+229
-6
lines changed

8 files changed

+229
-6
lines changed

src/main/java/com/amihaiemil/docker/Docker.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,19 @@ public interface Docker {
9898
Plugins plugins();
9999

100100
/**
101-
* Entry point for Version API.
101+
* Version of this Docker engine.
102102
* @return Version.
103103
* @throws IOException If an I/O error occurs.
104104
*/
105105
Version version() throws IOException;
106106

107+
/**
108+
* Detailed information about this Docker engine, in JSON.
109+
* @return Info.
110+
* @throws IOException If an I/O problem occurs.
111+
*/
112+
Info info() throws IOException;
113+
107114
/**
108115
* The underlying, immutable, Apache HttpClient.<br><br>
109116
*
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright (c) 2018-2020, Mihai Emil Andronache
3+
* All rights reserved.
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
* 1)Redistributions of source code must retain the above copyright notice,
7+
* this list of conditions and the following disclaimer.
8+
* 2)Redistributions in binary form must reproduce the above copyright notice,
9+
* this list of conditions and the following disclaimer in the documentation
10+
* and/or other materials provided with the distribution.
11+
* 3)Neither the name of docker-java-api nor the names of its
12+
* contributors may be used to endorse or promote products derived from
13+
* this software without specific prior written permission.
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
* POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
package com.amihaiemil.docker;
27+
28+
import javax.json.JsonObject;
29+
30+
/**
31+
* General information about the Docker engine.
32+
* @author Mihai Andronache (amihaiemil@gmail.com)
33+
* @version $Id$
34+
* @since 0.0.13
35+
*/
36+
public interface Info extends JsonObject {
37+
38+
/**
39+
* The Docker engine to which this Info refers to.
40+
* @return Docker.
41+
*/
42+
Docker docker();
43+
}

src/main/java/com/amihaiemil/docker/RtDocker.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ abstract class RtDocker implements Docker {
4949
* Base URI.
5050
*/
5151
private final URI baseUri;
52-
52+
5353
/**
5454
* Ctor.
5555
* @param client Given HTTP Client.
@@ -59,7 +59,7 @@ abstract class RtDocker implements Docker {
5959
this.client = client;
6060
this.baseUri = baseUri;
6161
}
62-
62+
6363
@Override
6464
public final boolean ping() throws IOException {
6565
final HttpGet ping = new HttpGet(this.baseUri.toString() + "/_ping");
@@ -126,7 +126,7 @@ public final Execs execs() {
126126
public final Swarm swarm() {
127127
return new RtSwarm(
128128
this.client,
129-
URI.create(this.baseUri.toString().concat("/swarm")),
129+
URI.create(this.baseUri.toString().concat("/swarm")),
130130
this
131131
);
132132
}
@@ -157,10 +157,29 @@ public Version version() throws IOException {
157157
final String versionUri = this.baseUri.toString() + "/version";
158158
return new RtVersion(
159159
this.client,
160-
URI.create(versionUri)
160+
URI.create(versionUri),
161+
this
161162
);
162163
}
163164

165+
@Override
166+
public Info info() throws IOException {
167+
final HttpGet info = new HttpGet(this.baseUri.toString() + "/info");
168+
try {
169+
return new RtInfo(
170+
this.client.execute(
171+
info,
172+
new ReadJsonObject(
173+
new MatchStatus(info.getURI(), HttpStatus.SC_OK)
174+
)
175+
),
176+
this
177+
);
178+
} finally {
179+
info.releaseConnection();
180+
}
181+
}
182+
164183
@Override
165184
public HttpClient httpClient() {
166185
return this.client;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright (c) 2018-2020, Mihai Emil Andronache
3+
* All rights reserved.
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
* 1)Redistributions of source code must retain the above copyright notice,
7+
* this list of conditions and the following disclaimer.
8+
* 2)Redistributions in binary form must reproduce the above copyright notice,
9+
* this list of conditions and the following disclaimer in the documentation
10+
* and/or other materials provided with the distribution.
11+
* 3)Neither the name of docker-java-api nor the names of its
12+
* contributors may be used to endorse or promote products derived from
13+
* this software without specific prior written permission.
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
* POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
package com.amihaiemil.docker;
27+
28+
import javax.json.JsonObject;
29+
30+
/**
31+
* Info about the Docker engine, in JSON format.
32+
* @author Mihai Andronache (amihaiemil@gmail.com)
33+
* @version $Id$
34+
* @since 0.0.13
35+
*/
36+
final class RtInfo extends JsonResource implements Info {
37+
38+
/**
39+
* Docker to which iot refers to.
40+
*/
41+
private final Docker docker;
42+
43+
/**
44+
* Constructor.
45+
* @param rep This info in JSON format.
46+
* @param docker The Docker to which it refers to.
47+
*/
48+
RtInfo(final JsonObject rep, final Docker docker) {
49+
super(rep);
50+
this.docker = docker;
51+
}
52+
53+
@Override
54+
public Docker docker() {
55+
return this.docker;
56+
}
57+
}

src/main/java/com/amihaiemil/docker/RtVersion.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,26 @@
3939
* @since 0.0.11
4040
*/
4141
final class RtVersion extends JsonResource implements Version {
42+
43+
/**
44+
* Docker to which this Version belongs.
45+
*/
46+
private final Docker docker;
47+
4248
/**
4349
* Ctor.
4450
* @param client The http client.
4551
* @param uri The URI for this version.
52+
* @param dkr Parent Docker.
4653
* @throws IOException If an I/O error occurs.
4754
*/
48-
RtVersion(final HttpClient client, final URI uri) throws IOException {
55+
RtVersion(
56+
final HttpClient client,
57+
final URI uri,
58+
final Docker dkr
59+
) throws IOException {
4960
super(fetch(client, uri));
61+
this.docker = dkr;
5062
}
5163

5264
/**
@@ -134,4 +146,9 @@ public String arch() {
134146
public boolean experimental() {
135147
return this.getBoolean("Experimental");
136148
}
149+
150+
@Override
151+
public Docker docker() {
152+
return this.docker;
153+
}
137154
}

src/main/java/com/amihaiemil/docker/Version.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ public interface Version extends JsonObject {
4949
* @return Whether experimental docker features are enabled
5050
*/
5151
boolean experimental();
52+
53+
/**
54+
* The Docker engine to which this Version belongs.
55+
* @return Docker.
56+
*/
57+
Docker docker();
5258
}

src/test/java/com/amihaiemil/docker/UnixDockerITCase.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,43 @@ public void pingsDocker() throws Exception {
6262
MatcherAssert.assertThat(docker.ping(), Matchers.is(Boolean.TRUE));
6363
}
6464

65+
/**
66+
* UnixDocker can return Info about Docker.
67+
* @throws Exception If something goes wrong.
68+
*/
69+
@Test
70+
public void returnsInfo() throws Exception {
71+
final Docker docker = new UnixDocker(
72+
new File("/var/run/docker.sock")
73+
);
74+
final Info info = docker.info();
75+
MatcherAssert.assertThat(info, Matchers.notNullValue());
76+
MatcherAssert.assertThat(info.docker(), Matchers.is(docker));
77+
MatcherAssert.assertThat(
78+
info.getString("OSType"),
79+
Matchers.equalTo("linux")
80+
);
81+
}
82+
83+
/**
84+
* UnixDocker can return the Version of Docker.
85+
* @throws Exception If something goes wrong.
86+
*/
87+
@Test
88+
public void returnsVersion() throws Exception {
89+
final Docker docker = new UnixDocker(
90+
new File("/var/run/docker.sock")
91+
);
92+
final Version version = docker.version();
93+
MatcherAssert.assertThat(version, Matchers.notNullValue());
94+
MatcherAssert.assertThat(version.docker(), Matchers.is(docker));
95+
MatcherAssert.assertThat(
96+
version.platformName(),
97+
Matchers.equalTo("Docker Engine - Community")
98+
);
99+
}
100+
101+
65102
/**
66103
* Docker can return its Events unfiltered.
67104
* @throws Exception If something goes wrong.

src/test/java/com/amihaiemil/docker/UnixDockerTestCase.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@
2626
package com.amihaiemil.docker;
2727

2828
import com.amihaiemil.docker.mock.AssertRequest;
29+
import com.amihaiemil.docker.mock.Condition;
2930
import com.amihaiemil.docker.mock.Response;
3031
import java.io.File;
3132
import org.apache.http.HttpStatus;
3233
import org.hamcrest.MatcherAssert;
3334
import org.hamcrest.Matchers;
3435
import org.junit.Test;
3536

37+
import javax.json.Json;
38+
3639
/**
3740
* Unit tests for LocalUnixDocker.
3841
* @author Mihai Andronache (amihaiemil@gmail.com)
@@ -191,4 +194,38 @@ public void unsupportedOperationPlugins() {
191194
new File("/var/run/docker.sock")
192195
).plugins();
193196
}
197+
198+
/**
199+
* RtDocker can return info about itself.
200+
* @throws Exception If something goes wrong.
201+
*/
202+
@Test
203+
public void returnsInfo() throws Exception {
204+
final Docker docker = new UnixDocker(
205+
new AssertRequest(
206+
new Response(
207+
HttpStatus.SC_OK,
208+
"{\"info\": \"running\"}"
209+
),
210+
new Condition(
211+
"info() must send a GET request",
212+
req -> "GET".equals(req.getRequestLine().getMethod())
213+
),
214+
new Condition(
215+
"info() resource URL must be unix://localhost:80/1.40/info",
216+
req -> req.getRequestLine()
217+
.getUri().equals("unix://localhost:80/1.40/info")
218+
)
219+
),
220+
"1.40"
221+
);
222+
MatcherAssert.assertThat(
223+
docker.info(),
224+
Matchers.equalTo(
225+
Json.createObjectBuilder()
226+
.add("info", "running")
227+
.build()
228+
)
229+
);
230+
}
194231
}

0 commit comments

Comments
 (0)