Skip to content

Commit 5f42e0c

Browse files
authored
Merge pull request #316 from milux/version-api
Added version API
2 parents c1d0c05 + f58d1b0 commit 5f42e0c

File tree

6 files changed

+265
-0
lines changed

6 files changed

+265
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ Reader events()
103103
*/
104104
Plugins plugins();
105105

106+
/**
107+
* Entry point for Version API.
108+
* @return Version.
109+
* @throws IOException If an I/O error occurs.
110+
*/
111+
Version version() throws IOException;
112+
106113
/**
107114
* The underlying, immutable, Apache HttpClient.<br><br>
108115
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ public Plugins plugins() {
158158
);
159159
}
160160

161+
@Override
162+
public Version version() throws IOException {
163+
final String versionUri = this.baseUri.toString() + "/version";
164+
return new RtVersion(
165+
this.client,
166+
URI.create(versionUri)
167+
);
168+
}
169+
161170
@Override
162171
public HttpClient httpClient() {
163172
return this.client;
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* Copyright (c) 2018-2019, 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 org.apache.http.HttpStatus;
29+
import org.apache.http.client.HttpClient;
30+
import org.apache.http.client.methods.HttpGet;
31+
32+
import javax.json.JsonObject;
33+
import java.io.IOException;
34+
import java.net.URI;
35+
36+
/**
37+
* Runtime {@link Version}.
38+
* @author Michael Lux ([email protected])
39+
* @since 0.0.11
40+
*/
41+
final class RtVersion extends JsonResource implements Version {
42+
/**
43+
* Ctor.
44+
* @param client The http client.
45+
* @param uri The URI for this version.
46+
* @throws IOException If an I/O error occurs.
47+
*/
48+
RtVersion(final HttpClient client, final URI uri) throws IOException {
49+
super(fetch(client, uri));
50+
}
51+
52+
/**
53+
* Fetch the JsonObject resource.
54+
* @param client The Http client.
55+
* @param uri The request URL.
56+
* @return The fetched JsonObject.
57+
* @throws UnexpectedResponseException If Docker's response code is not 200.
58+
* @throws IOException If an I/O error occurs.
59+
*/
60+
private static JsonObject fetch(final HttpClient client, final URI uri)
61+
throws UnexpectedResponseException, IOException {
62+
final HttpGet version = new HttpGet(uri);
63+
try {
64+
return client.execute(
65+
version,
66+
new ReadJsonObject(
67+
new MatchStatus(version.getURI(), HttpStatus.SC_OK)
68+
)
69+
);
70+
} finally {
71+
version.releaseConnection();
72+
}
73+
}
74+
75+
/**
76+
* Returns the version of the connected docker engine.
77+
* @return Version of connected docker engine
78+
*/
79+
@Override
80+
public String version() {
81+
return this.getString("Version");
82+
}
83+
84+
/**
85+
* Returns the name of the connected docker platform.
86+
* @return Name of the docker platform
87+
*/
88+
@Override
89+
public String platformName() {
90+
return this.getJsonObject("Platform").getString("Name");
91+
}
92+
93+
/**
94+
* Returns the API version of the docker engine.
95+
* @return API version
96+
*/
97+
@Override
98+
public String apiVersion() {
99+
return this.getString("ApiVersion");
100+
}
101+
102+
/**
103+
* Returns the minimum API version of the docker engine.
104+
* @return Minimum API version
105+
*/
106+
@Override
107+
public String minApiVersion() {
108+
return this.getString("MinAPIVersion");
109+
}
110+
111+
/**
112+
* Returns the OS docker is running on.
113+
* @return Name of the OS docker is running on
114+
*/
115+
@Override
116+
public String osName() {
117+
return this.getString("Os");
118+
}
119+
120+
/**
121+
* Returns the (CPU) architecture docker is running on.
122+
* @return The (CPU) architecture docker is running on
123+
*/
124+
@Override
125+
public String arch() {
126+
return this.getString("Arch");
127+
}
128+
129+
/**
130+
* Reports whether experimental docker features are enabled.
131+
* @return Whether experimental docker features are enabled
132+
*/
133+
@Override
134+
public boolean experimental() {
135+
return this.getBoolean("Experimental");
136+
}
137+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.amihaiemil.docker;
2+
3+
import javax.json.JsonObject;
4+
5+
/**
6+
* Version API.
7+
* @author Michael Lux ([email protected])
8+
* @since 0.0.11
9+
*/
10+
public interface Version extends JsonObject {
11+
/**
12+
* Returns the version of the connected docker engine.
13+
* @return Version of connected docker engine
14+
*/
15+
String version();
16+
17+
/**
18+
* Returns the name of the connected docker platform.
19+
* @return Name of the docker platform
20+
*/
21+
String platformName();
22+
23+
/**
24+
* Returns the API version of the docker engine.
25+
* @return API version
26+
*/
27+
String apiVersion();
28+
29+
/**
30+
* Returns the minimum API version of the docker engine.
31+
* @return Minimum API version
32+
*/
33+
String minApiVersion();
34+
35+
/**
36+
* Returns the OS docker is running on.
37+
* @return Name of the OS docker is running on
38+
*/
39+
String osName();
40+
41+
/**
42+
* Returns the (CPU) architecture docker is running on.
43+
* @return The (CPU) architecture docker is running on
44+
*/
45+
String arch();
46+
47+
/**
48+
* Reports whether experimental docker features are enabled.
49+
* @return Whether experimental docker features are enabled
50+
*/
51+
boolean experimental();
52+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.amihaiemil.docker;
2+
3+
import org.apache.http.HttpEntity;
4+
import org.apache.http.HttpResponse;
5+
import org.apache.http.HttpStatus;
6+
import org.apache.http.StatusLine;
7+
import org.apache.http.client.HttpClient;
8+
import org.apache.http.client.ResponseHandler;
9+
import org.apache.http.client.methods.HttpGet;
10+
import org.junit.Test;
11+
12+
import java.io.IOException;
13+
14+
import static org.junit.Assert.assertEquals;
15+
import static org.junit.Assert.assertTrue;
16+
import static org.mockito.Matchers.any;
17+
import static org.mockito.Mockito.mock;
18+
import static org.mockito.Mockito.when;
19+
20+
/**
21+
* Unit tests for {@link RtVersion}.
22+
* @author Michael Lux ([email protected])
23+
* @since 0.0.11
24+
*/
25+
public class RtVersionTestCase {
26+
/**
27+
* Must return the same number of images as there are elements in the
28+
* json array returned by the service.
29+
* @throws IOException On I/O error.
30+
*/
31+
@Test
32+
public final void queryDockerVersion() throws IOException {
33+
HttpClient client = mock(HttpClient.class);
34+
when(client.execute(any(HttpGet.class), any(ResponseHandler.class)))
35+
.thenAnswer(invocation -> {
36+
HttpResponse response = mock(HttpResponse.class);
37+
HttpEntity entity = mock(HttpEntity.class);
38+
when(response.getEntity()).thenReturn(entity);
39+
when(entity.getContent()).thenReturn(
40+
getClass().getClassLoader()
41+
.getResourceAsStream("version.json"));
42+
StatusLine statusLine = mock(StatusLine.class);
43+
when(response.getStatusLine()).thenReturn(statusLine);
44+
when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK);
45+
return ((ReadJsonObject) invocation.getArguments()[1])
46+
.handleResponse(response);
47+
});
48+
Docker docker = new LocalDocker(client, "v1.35");
49+
Version version = docker.version();
50+
assertEquals("19.03.3", version.version());
51+
assertEquals("Docker Engine - Community", version.platformName());
52+
assertEquals("1.40", version.apiVersion());
53+
assertEquals("1.12", version.minApiVersion());
54+
assertEquals("linux", version.osName());
55+
assertEquals("amd64", version.arch());
56+
assertTrue(version.experimental());
57+
}
58+
59+
}

src/test/resources/version.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Platform":{"Name":"Docker Engine - Community"},"Components":[{"Name":"Engine","Version":"19.03.3","Details":{"ApiVersion":"1.40","Arch":"amd64","BuildTime":"2019-10-08T00:59:17.000000000+00:00","Experimental":"true","GitCommit":"a872fc2f86","GoVersion":"go1.12.10","KernelVersion":"5.3.0-40-generic","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"containerd","Version":"1.2.10","Details":{"GitCommit":"b34a5c8af56e510852c35414db4c1f4fa6172339"}},{"Name":"runc","Version":"1.0.0-rc8+dev","Details":{"GitCommit":"3e425f80a8c931f88e6d94a8c831b9d5aa481657"}},{"Name":"docker-init","Version":"0.18.0","Details":{"GitCommit":"fec3683"}}],"Version":"19.03.3","ApiVersion":"1.40","MinAPIVersion":"1.12","GitCommit":"a872fc2f86","GoVersion":"go1.12.10","Os":"linux","Arch":"amd64","KernelVersion":"5.3.0-40-generic","Experimental":true,"BuildTime":"2019-10-08T00:59:17.000000000+00:00"}

0 commit comments

Comments
 (0)