Skip to content

Commit 047bab9

Browse files
committed
Merge branch '__rultor'
2 parents 1e57be1 + 88f7c21 commit 047bab9

File tree

9 files changed

+305
-3
lines changed

9 files changed

+305
-3
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.amihaiemil.docker;
2+
3+
import javax.json.JsonObject;
4+
5+
/**
6+
* Info about Docker disk space usage in bytes.
7+
* @author Boris Kuzmic ([email protected])
8+
* @since 0.0.6
9+
*/
10+
public interface DiskSpaceInfo extends JsonObject {
11+
12+
/**
13+
* Images disk usage.
14+
* @return Disk usage for images in bytes
15+
*/
16+
Long images();
17+
18+
/**
19+
* Containers disk usage.
20+
* @return Disk usage for containers in bytes
21+
*/
22+
Long containers();
23+
24+
/**
25+
* Volumes disk usage.
26+
* @return Disk usage for volumes in bytes
27+
*/
28+
Long volumes();
29+
30+
/**
31+
* Total disk usage.
32+
* @return Total disk usage in bytes
33+
*/
34+
Long totalSpace();
35+
36+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ public interface Docker {
8282
*/
8383
Swarm swarm();
8484

85+
/**
86+
* Entry point for the DockerSystem API.
87+
* @return DockerSystem.
88+
*/
89+
DockerSystem system();
90+
8591
/**
8692
* The underlying, immutable, Apache HttpClient.<br><br>
8793
*
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.amihaiemil.docker;
2+
3+
import java.io.IOException;
4+
5+
/**
6+
* Docker System API entry point.
7+
* @author Boris Kuzmic ([email protected])
8+
* @see <a href="https://docs.docker.com/engine/reference/commandline/system/">Docker DockerSystem API</a>
9+
* @since 0.0.6
10+
*/
11+
public interface DockerSystem {
12+
13+
/**
14+
* Show docker disk usage.
15+
* @return The created {@link DiskSpaceInfo}.
16+
* @throws IOException If an I/O error occurs.
17+
* @throws UnexpectedResponseException If the API responds with an
18+
* unexpected status.
19+
*/
20+
DiskSpaceInfo diskUsage()
21+
throws IOException, UnexpectedResponseException;
22+
23+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ public final Swarm swarm() {
120120
);
121121
}
122122

123+
@Override
124+
public DockerSystem system() {
125+
return new RtDockerSystem(
126+
this.client,
127+
URI.create(this.baseUri.toString().concat("/system")),
128+
this
129+
);
130+
}
131+
123132
@Override
124133
public HttpClient httpClient() {
125134
return this.client;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.amihaiemil.docker;
2+
3+
import org.apache.http.HttpStatus;
4+
import org.apache.http.client.HttpClient;
5+
import org.apache.http.client.methods.HttpGet;
6+
7+
import java.io.IOException;
8+
import java.net.URI;
9+
10+
/**
11+
* System API.
12+
*
13+
* @author Boris Kuzmic ([email protected])
14+
* @since 0.0.6
15+
*/
16+
final class RtDockerSystem implements DockerSystem {
17+
18+
/**
19+
* Apache HttpClient which sends the requests.
20+
*/
21+
private final HttpClient client;
22+
23+
/**
24+
* Base URI.
25+
*/
26+
private final URI baseUri;
27+
28+
/**
29+
* Docker engine.
30+
*/
31+
private Docker docker;
32+
33+
/**
34+
* Ctor.
35+
*
36+
* @param client Given HTTP Client.
37+
* @param baseUri Base URI, ending with /system.
38+
* @param dkr The Docker engine.
39+
*/
40+
RtDockerSystem(
41+
final HttpClient client, final URI baseUri, final Docker dkr
42+
) {
43+
this.client = client;
44+
this.baseUri = baseUri;
45+
this.docker = dkr;
46+
}
47+
48+
@Override
49+
public DiskSpaceInfo diskUsage()
50+
throws IOException, UnexpectedResponseException {
51+
final HttpGet init = new HttpGet(this.baseUri.toString() + "/df");
52+
try {
53+
return new SystemDiskSpaceInfo(
54+
this.client.execute(
55+
init,
56+
new ReadJsonObject(
57+
new MatchStatus(
58+
init.getURI(),
59+
HttpStatus.SC_OK
60+
)
61+
)
62+
)
63+
);
64+
} finally {
65+
init.releaseConnection();
66+
}
67+
}
68+
69+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.amihaiemil.docker;
2+
3+
import javax.json.JsonArray;
4+
import javax.json.JsonObject;
5+
6+
/**
7+
* Docker disk space usage information.
8+
* @author Boris Kuzmic ([email protected])
9+
* @since 0.0.6
10+
*/
11+
final class SystemDiskSpaceInfo extends JsonResource
12+
implements DiskSpaceInfo {
13+
14+
/**
15+
* Ctor.
16+
* @param jsonObject Response Json from system/df call
17+
*/
18+
SystemDiskSpaceInfo(final JsonObject jsonObject) {
19+
super(jsonObject);
20+
}
21+
22+
@Override
23+
public Long images() {
24+
return this.getJsonNumber("LayersSize").longValue();
25+
}
26+
27+
@Override
28+
public Long containers() {
29+
JsonArray containers = this.getJsonArray("Containers");
30+
return containers.stream().map(JsonObject.class::cast)
31+
.mapToLong(c -> c.getJsonNumber("SizeRootFs").longValue())
32+
.sum();
33+
}
34+
35+
@Override
36+
public Long volumes() {
37+
JsonArray volumes = this.getJsonArray("Volumes");
38+
return volumes.stream().map(JsonObject.class::cast)
39+
.mapToLong(v -> v.getJsonObject("UsageData")
40+
.getJsonNumber("Size").longValue())
41+
.sum();
42+
}
43+
44+
@Override
45+
public Long totalSpace() {
46+
return this.images() + this.containers() + this.volumes();
47+
}
48+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.amihaiemil.docker;
2+
3+
import org.hamcrest.MatcherAssert;
4+
import org.hamcrest.Matchers;
5+
import org.junit.Test;
6+
7+
import java.io.File;
8+
9+
/**
10+
* Integration tests for RtDockerSystem.
11+
*
12+
* @author Boris Kuzmic ([email protected])
13+
* @since 0.0.6
14+
*/
15+
public final class RtDockerSystemITCase {
16+
17+
/**
18+
* Show Docker disk space info.
19+
* @throws Exception If something goes wrong.
20+
*/
21+
@Test
22+
public void showDiskSpaceInfo() throws Exception {
23+
final Docker docker = new LocalDocker(
24+
new File("/var/run/docker.sock")
25+
);
26+
DiskSpaceInfo info = docker.system().diskUsage();
27+
MatcherAssert.assertThat(info.totalSpace(),
28+
Matchers.greaterThanOrEqualTo(0L));
29+
}
30+
31+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.amihaiemil.docker;
2+
3+
import com.amihaiemil.docker.mock.AssertRequest;
4+
import com.amihaiemil.docker.mock.Response;
5+
import org.apache.http.HttpStatus;
6+
import org.hamcrest.MatcherAssert;
7+
import org.hamcrest.Matchers;
8+
import org.junit.Test;
9+
import org.mockito.Mockito;
10+
11+
import javax.json.Json;
12+
import java.net.URI;
13+
14+
/**
15+
* Unit tests for RtDockerSystem.
16+
*
17+
* @author Boris Kuzmic ([email protected])
18+
* @since 0.0.6
19+
* @checkstyle MethodName (500 lines)
20+
*/
21+
public final class RtDockerSystemTestCase {
22+
23+
/**
24+
* Must return the same disk space usage for images, containers and
25+
* volumes as in json array returned by the service.
26+
*
27+
* @throws Exception If an error occurs.
28+
*/
29+
@Test
30+
public void returnsDiskSpaceUsage() throws Exception {
31+
long totalSpace = new RtDockerSystem(
32+
new AssertRequest(
33+
new Response(
34+
HttpStatus.SC_OK,
35+
Json.createObjectBuilder()
36+
.add("LayersSize", 250)
37+
.add(
38+
"Containers",
39+
Json.createArrayBuilder()
40+
.add(
41+
Json.createObjectBuilder()
42+
.add("SizeRootFs", 50)
43+
).add(
44+
Json.createObjectBuilder()
45+
.add("SizeRootFs", 60)
46+
)
47+
)
48+
.add(
49+
"Volumes",
50+
Json.createArrayBuilder()
51+
.add(
52+
Json.createObjectBuilder()
53+
.add(
54+
"UsageData",
55+
Json.createObjectBuilder()
56+
.add("Size", 200)
57+
)
58+
).add(
59+
Json.createObjectBuilder()
60+
.add(
61+
"UsageData",
62+
Json.createObjectBuilder()
63+
.add("Size", 100)
64+
)
65+
)
66+
).build().toString()
67+
)
68+
),
69+
URI.create("http://localhost/system"),
70+
Mockito.mock(Docker.class)
71+
).diskUsage().totalSpace();
72+
MatcherAssert.assertThat(
73+
totalSpace,
74+
Matchers.is(660L)
75+
);
76+
}
77+
78+
79+
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@
2828
import com.amihaiemil.docker.mock.AssertRequest;
2929
import com.amihaiemil.docker.mock.Condition;
3030
import com.amihaiemil.docker.mock.Response;
31-
import java.net.URI;
32-
import java.util.concurrent.atomic.AtomicInteger;
33-
import javax.json.Json;
3431
import org.apache.http.HttpStatus;
3532
import org.hamcrest.MatcherAssert;
3633
import org.hamcrest.Matchers;
3734
import org.junit.Test;
3835
import org.mockito.Mockito;
3936

37+
import javax.json.Json;
38+
import java.net.URI;
39+
import java.util.concurrent.atomic.AtomicInteger;
40+
4041
/**
4142
* Unit tests for {@link RtImages}.
4243
* @author George Aristy ([email protected])

0 commit comments

Comments
 (0)