From d8ecac6ccd3b34d8de5c559610a000955d3b9dae Mon Sep 17 00:00:00 2001 From: Diego Fernando Molina Bocanegra Date: Wed, 12 Oct 2016 23:22:05 +0200 Subject: [PATCH] Add "autoRemove" field for HostConfig --- .../docker/client/messages/HostConfig.java | 27 +++++++++++++++-- .../client/DefaultDockerClientTest.java | 29 +++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/spotify/docker/client/messages/HostConfig.java b/src/main/java/com/spotify/docker/client/messages/HostConfig.java index 5cc77a856..17d2280a8 100755 --- a/src/main/java/com/spotify/docker/client/messages/HostConfig.java +++ b/src/main/java/com/spotify/docker/client/messages/HostConfig.java @@ -99,6 +99,8 @@ public class HostConfig { private Boolean oomKillDisable; @JsonProperty("OomScoreAdj") private Integer oomScoreAdj; + @JsonProperty("AutoRemove") + private Boolean autoRemove; private HostConfig() { } @@ -135,6 +137,7 @@ private HostConfig(final Builder builder) { this.shmSize = builder.shmSize; this.oomKillDisable = builder.oomKillDisable; this.oomScoreAdj = builder.oomScoreAdj; + this.autoRemove = builder.autoRemove; } public List binds() { @@ -257,6 +260,10 @@ public Integer oomScoreAdj() { return oomScoreAdj; } + public Boolean autoRemove() { + return autoRemove; + } + @Override public boolean equals(final Object o) { if (this == o) { @@ -296,7 +303,8 @@ public boolean equals(final Object o) { Objects.equals(this.ipcMode, that.ipcMode) && Objects.equals(this.ulimits, that.ulimits) && Objects.equals(this.oomKillDisable, that.oomKillDisable) && - Objects.equals(this.oomScoreAdj, that.oomScoreAdj); + Objects.equals(this.oomScoreAdj, that.oomScoreAdj) && + Objects.equals(this.autoRemove, that.autoRemove); } @Override @@ -306,7 +314,7 @@ public int hashCode() { capDrop, networkMode, securityOpt, devices, memory, memorySwap, memoryReservation, cpuShares, cpusetCpus, cpuQuota, cgroupParent, restartPolicy, logConfig, ipcMode, ulimits, pidMode, shmSize, - oomKillDisable, oomScoreAdj); + oomKillDisable, oomScoreAdj, autoRemove); } @Override @@ -343,6 +351,7 @@ public String toString() { .add("shmSize", shmSize) .add("oomKillDisable", oomKillDisable) .add("oomScoreAdj", oomScoreAdj) + .add("autoRemove", autoRemove) .toString(); } @@ -501,6 +510,7 @@ public static class Builder { private Long shmSize; private Boolean oomKillDisable; private Integer oomScoreAdj; + private Boolean autoRemove; private Builder() { } @@ -537,6 +547,7 @@ private Builder(final HostConfig hostConfig) { this.shmSize = hostConfig.shmSize; this.oomKillDisable = hostConfig.oomKillDisable; this.oomScoreAdj = hostConfig.oomScoreAdj; + this.autoRemove = hostConfig.autoRemove; } /** @@ -1042,6 +1053,18 @@ public Integer oomScoreAdj() { return oomScoreAdj; } + public Boolean autoRemove() { + return autoRemove; + } + + /** + * Only works for Docker API version >= 1.25. + */ + public Builder autoRemove(final Boolean autoRemove) { + this.autoRemove = autoRemove; + return this; + } + public HostConfig build() { return new HostConfig(this); } diff --git a/src/test/java/com/spotify/docker/client/DefaultDockerClientTest.java b/src/test/java/com/spotify/docker/client/DefaultDockerClientTest.java index 6ed0eac4f..edb4be7a8 100755 --- a/src/test/java/com/spotify/docker/client/DefaultDockerClientTest.java +++ b/src/test/java/com/spotify/docker/client/DefaultDockerClientTest.java @@ -3358,6 +3358,35 @@ public void testOomScoreAdj() throws Exception { assertThat(info.hostConfig().oomScoreAdj(), is(500)); } + @Test(expected = ContainerNotFoundException.class) + public void testAutoRemoveWhenSetToTrue() throws Exception { + requireDockerApiVersionAtLeast("1.25", "AutoRemove"); + + // Container should be removed after it is stopped (new since API v.1.25) + // Pull image + sut.pull(BUSYBOX_LATEST); + + final ContainerConfig config = ContainerConfig.builder() + .image(BUSYBOX_LATEST) + .hostConfig(HostConfig.builder() + .autoRemove(true) // Default is false + .build()) + .build(); + + final ContainerCreation container = sut.createContainer(config, randomName()); + + sut.startContainer(container.id()); + + final ContainerInfo info = sut.inspectContainer(container.id()); + assertThat(info.hostConfig().autoRemove(), is(true)); + assertThat(info.state().running(), equalTo(true)); + + sut.stopContainer(container.id(), 5); + + // A ContainerNotFoundException should be thrown since the container is removed when it stops + sut.inspectContainer(container.id()); + } + @Test public void testInspectSwarm() throws Exception { requireDockerApiVersionAtLeast("1.24", "swarm support");