From acb7ee41d8d67610b882b1ac59790b1784aebfb0 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Sun, 5 Feb 2017 14:10:23 -0500 Subject: [PATCH] Add ability to add labels when running commit --- .../dockerjava/api/command/CommitCmd.java | 10 ++++++++ .../core/command/CommitCmdImpl.java | 19 +++++++++++++++ .../core/command/CommitCmdImplTest.java | 24 ++++++++++++++++++- .../netty/exec/CommitCmdExecTest.java | 24 ++++++++++++++++++- 4 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/dockerjava/api/command/CommitCmd.java b/src/main/java/com/github/dockerjava/api/command/CommitCmd.java index c5da5bab6..a182751b8 100644 --- a/src/main/java/com/github/dockerjava/api/command/CommitCmd.java +++ b/src/main/java/com/github/dockerjava/api/command/CommitCmd.java @@ -3,6 +3,8 @@ import javax.annotation.CheckForNull; import javax.annotation.Nonnull; +import java.util.Map; + import com.github.dockerjava.api.exception.NotFoundException; import com.github.dockerjava.api.model.ExposedPorts; import com.github.dockerjava.api.model.Volumes; @@ -29,6 +31,9 @@ public interface CommitCmd extends SyncDockerCmd { @CheckForNull String getHostname(); + @CheckForNull + Map getLabels(); + @CheckForNull Integer getMemory(); @@ -88,6 +93,11 @@ public interface CommitCmd extends SyncDockerCmd { CommitCmd withHostname(String hostname); + /** + * @since 1.19 + */ + CommitCmd withLabels(Map labels); + CommitCmd withMemory(Integer memory); CommitCmd withMemorySwap(Integer memorySwap); diff --git a/src/main/java/com/github/dockerjava/core/command/CommitCmdImpl.java b/src/main/java/com/github/dockerjava/core/command/CommitCmdImpl.java index 534cce52c..1c8ad26a4 100644 --- a/src/main/java/com/github/dockerjava/core/command/CommitCmdImpl.java +++ b/src/main/java/com/github/dockerjava/core/command/CommitCmdImpl.java @@ -2,6 +2,8 @@ import static com.google.common.base.Preconditions.checkNotNull; +import java.util.Map; + import com.fasterxml.jackson.annotation.JsonProperty; import com.github.dockerjava.api.command.CommitCmd; import com.github.dockerjava.api.exception.NotFoundException; @@ -43,6 +45,12 @@ public class CommitCmdImpl extends AbstrDockerCmd implements @JsonProperty("Hostname") private String hostname; + /** + * @since 1.19 + */ + @JsonProperty("Labels") + private Map labels; + @JsonProperty("Memory") private Integer memory; @@ -189,6 +197,17 @@ public CommitCmdImpl withEnv(String... env) { return this; } + @Override + public Map getLabels() { + return labels; + } + + @Override + public CommitCmdImpl withLabels(Map labels) { + this.labels = labels; + return this; + } + @Override public ExposedPorts getExposedPorts() { return exposedPorts; diff --git a/src/test/java/com/github/dockerjava/core/command/CommitCmdImplTest.java b/src/test/java/com/github/dockerjava/core/command/CommitCmdImplTest.java index 64e1d7ddf..a6195ed4d 100644 --- a/src/test/java/com/github/dockerjava/core/command/CommitCmdImplTest.java +++ b/src/test/java/com/github/dockerjava/core/command/CommitCmdImplTest.java @@ -8,7 +8,9 @@ import static org.testinfected.hamcrest.jpa.HasFieldWithValue.hasField; import java.lang.reflect.Method; +import java.util.Map; +import com.google.common.collect.ImmutableMap; import org.testng.ITestResult; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterTest; @@ -54,7 +56,7 @@ public void commit() throws DockerException { assertThat(container.getId(), not(isEmptyString())); dockerClient.startContainerCmd(container.getId()).exec(); - LOG.info("Commiting container: {}", container.toString()); + LOG.info("Committing container: {}", container.toString()); String imageId = dockerClient.commitCmd(container.getId()).exec(); InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec(); @@ -68,6 +70,26 @@ public void commit() throws DockerException { assertThat(inspectImageResponse.getParent(), equalTo(busyboxImg.getId())); } + @Test + public void commitWithLabels() throws DockerException { + + CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("touch", "/test").exec(); + + LOG.info("Created container: {}", container.toString()); + assertThat(container.getId(), not(isEmptyString())); + dockerClient.startContainerCmd(container.getId()).exec(); + + LOG.info("Committing container: {}", container.toString()); + Map labels = ImmutableMap.of("label1", "abc", "label2", "123"); + String imageId = dockerClient.commitCmd(container.getId()).withLabels(labels).exec(); + + InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec(); + LOG.info("Image Inspect: {}", inspectImageResponse.toString()); + Map responseLabels = inspectImageResponse.getContainerConfig().getLabels(); + assertThat(responseLabels.get("label1"), equalTo("abc")); + assertThat(responseLabels.get("label2"), equalTo("123")); + } + @Test(expectedExceptions = NotFoundException.class) public void commitNonExistingContainer() throws DockerException { diff --git a/src/test/java/com/github/dockerjava/netty/exec/CommitCmdExecTest.java b/src/test/java/com/github/dockerjava/netty/exec/CommitCmdExecTest.java index 3d1a66333..8d8f6e6bb 100644 --- a/src/test/java/com/github/dockerjava/netty/exec/CommitCmdExecTest.java +++ b/src/test/java/com/github/dockerjava/netty/exec/CommitCmdExecTest.java @@ -8,7 +8,9 @@ import static org.testinfected.hamcrest.jpa.HasFieldWithValue.hasField; import java.lang.reflect.Method; +import java.util.Map; +import com.google.common.collect.ImmutableMap; import org.testng.ITestResult; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterTest; @@ -54,7 +56,7 @@ public void commit() throws DockerException { assertThat(container.getId(), not(isEmptyString())); dockerClient.startContainerCmd(container.getId()).exec(); - LOG.info("Commiting container: {}", container.toString()); + LOG.info("Committing container: {}", container.toString()); String imageId = dockerClient.commitCmd(container.getId()).exec(); InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec(); @@ -68,6 +70,26 @@ public void commit() throws DockerException { assertThat(inspectImageResponse.getParent(), equalTo(busyboxImg.getId())); } + @Test + public void commitWithLabels() throws DockerException { + + CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("touch", "/test").exec(); + + LOG.info("Created container: {}", container.toString()); + assertThat(container.getId(), not(isEmptyString())); + dockerClient.startContainerCmd(container.getId()).exec(); + + LOG.info("Committing container: {}", container.toString()); + Map labels = ImmutableMap.of("label1", "abc", "label2", "123"); + String imageId = dockerClient.commitCmd(container.getId()).withLabels(labels).exec(); + + InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec(); + LOG.info("Image Inspect: {}", inspectImageResponse.toString()); + Map responseLabels = inspectImageResponse.getContainerConfig().getLabels(); + assertThat(responseLabels.get("label1"), equalTo("abc")); + assertThat(responseLabels.get("label2"), equalTo("123")); + } + @Test(expectedExceptions = NotFoundException.class) public void commitNonExistingContainer() throws DockerException {