diff --git a/src/main/java/com/github/dockerjava/core/command/ExecStartCmdImpl.java b/src/main/java/com/github/dockerjava/core/command/ExecStartCmdImpl.java index f77460730..836c9158e 100644 --- a/src/main/java/com/github/dockerjava/core/command/ExecStartCmdImpl.java +++ b/src/main/java/com/github/dockerjava/core/command/ExecStartCmdImpl.java @@ -5,17 +5,27 @@ import java.io.InputStream; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; import com.github.dockerjava.api.async.ResultCallback; import com.github.dockerjava.api.command.ExecStartCmd; import com.github.dockerjava.api.exception.NotFoundException; import com.github.dockerjava.api.model.Frame; +@JsonInclude(Include.NON_NULL) public class ExecStartCmdImpl extends AbstrAsyncDockerCmd implements ExecStartCmd { + @JsonIgnore private String execId; - private Boolean detach, tty; + @JsonProperty("Detach") + private Boolean detach; + + @JsonProperty("Tty") + private Boolean tty; + @JsonIgnore private InputStream stdin; public ExecStartCmdImpl(ExecStartCmd.Exec exec, String execId) { diff --git a/src/main/java/com/github/dockerjava/core/command/ExecStartResultCallback.java b/src/main/java/com/github/dockerjava/core/command/ExecStartResultCallback.java index ebf1d08a1..23f549c49 100644 --- a/src/main/java/com/github/dockerjava/core/command/ExecStartResultCallback.java +++ b/src/main/java/com/github/dockerjava/core/command/ExecStartResultCallback.java @@ -35,6 +35,7 @@ public void onNext(Frame frame) { try { switch (frame.getStreamType()) { case STDOUT: + case RAW: if (stdout != null) { stdout.write(frame.getPayload()); stdout.flush(); diff --git a/src/main/java/com/github/dockerjava/jaxrs/ExecStartCmdExec.java b/src/main/java/com/github/dockerjava/jaxrs/ExecStartCmdExec.java index f737f6e28..dc7cf6598 100644 --- a/src/main/java/com/github/dockerjava/jaxrs/ExecStartCmdExec.java +++ b/src/main/java/com/github/dockerjava/jaxrs/ExecStartCmdExec.java @@ -1,5 +1,6 @@ package com.github.dockerjava.jaxrs; +import static javax.ws.rs.client.Entity.entity; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; @@ -30,6 +31,6 @@ protected AbstractCallbackNotifier callbackNotifier(ExecStartCmd command, LOGGER.trace("POST: {}", webTarget); return new POSTCallbackNotifier(new FrameStreamProcessor(), resultCallback, webTarget.request().accept( - MediaType.APPLICATION_JSON), null); + MediaType.APPLICATION_JSON), entity(command, MediaType.APPLICATION_JSON)); } } diff --git a/src/main/java/com/github/dockerjava/netty/handler/FramedResponseStreamHandler.java b/src/main/java/com/github/dockerjava/netty/handler/FramedResponseStreamHandler.java index 87878f0da..945833cdd 100644 --- a/src/main/java/com/github/dockerjava/netty/handler/FramedResponseStreamHandler.java +++ b/src/main/java/com/github/dockerjava/netty/handler/FramedResponseStreamHandler.java @@ -1,5 +1,7 @@ package com.github.dockerjava.netty.handler; +import java.util.Arrays; + import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; @@ -74,14 +76,14 @@ private Frame decode() { headerCnt += headerCount; - if (headerCnt < HEADER_SIZE) { - return null; - } - streamType = streamType(header[0]); if (streamType.equals(StreamType.RAW)) { - return new Frame(streamType, header); + return new Frame(streamType, Arrays.copyOf(header, headerCount)); + } + + if (headerCnt < HEADER_SIZE) { + return null; } } diff --git a/src/test/java/com/github/dockerjava/core/command/ExecStartCmdImplTest.java b/src/test/java/com/github/dockerjava/core/command/ExecStartCmdImplTest.java index 6d15cf03b..d7b689a9e 100644 --- a/src/test/java/com/github/dockerjava/core/command/ExecStartCmdImplTest.java +++ b/src/test/java/com/github/dockerjava/core/command/ExecStartCmdImplTest.java @@ -55,7 +55,7 @@ public void execStart() throws Exception { ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(container.getId()) .withAttachStdout(true).withCmd("touch", "/execStartTest.log").exec(); dockerClient.execStartCmd(execCreateCmdResponse.getId()).exec( - new ExecStartResultCallback(System.out, System.err)); + new ExecStartResultCallback(System.out, System.err)).awaitCompletion(); InputStream response = dockerClient.copyArchiveFromContainerCmd(container.getId(), "/execStartTest.log").exec(); Boolean bytesAvailable = response.available() > 0; diff --git a/src/test/java/com/github/dockerjava/core/command/InspectExecCmdImplTest.java b/src/test/java/com/github/dockerjava/core/command/InspectExecCmdImplTest.java index 4e87d5abf..51c2e3c29 100644 --- a/src/test/java/com/github/dockerjava/core/command/InspectExecCmdImplTest.java +++ b/src/test/java/com/github/dockerjava/core/command/InspectExecCmdImplTest.java @@ -46,7 +46,7 @@ public void afterMethod(ITestResult result) { } @Test(groups = "ignoreInCircleCi") - public void inspectExec() throws IOException { + public void inspectExec() throws Exception { String containerName = "generated_" + new SecureRandom().nextInt(); CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("sleep", "9999") @@ -56,34 +56,34 @@ public void inspectExec() throws IOException { dockerClient.startContainerCmd(container.getId()).exec(); - ExecCreateCmdResponse touchFileCmdCreateResponse = dockerClient.execCreateCmd(container.getId()) - .withAttachStdout(true).withAttachStderr(true).withCmd("touch", "/marker").exec(); - LOG.info("Created exec {}", touchFileCmdCreateResponse.toString()); - assertThat(touchFileCmdCreateResponse.getId(), not(isEmptyString())); - ExecCreateCmdResponse checkFileCmdCreateResponse = dockerClient.execCreateCmd(container.getId()) - .withAttachStdout(true).withAttachStderr(true).withCmd("test", "-e", "/marker").exec(); - LOG.info("Created exec {}", checkFileCmdCreateResponse.toString()); - assertThat(checkFileCmdCreateResponse.getId(), not(isEmptyString())); - // Check that file does not exist - dockerClient.execStartCmd(container.getId()) - .withExecId(checkFileCmdCreateResponse.getId()).exec(new ExecStartResultCallback(System.out, System.err)); - - InspectExecResponse first = dockerClient.inspectExecCmd(checkFileCmdCreateResponse.getId()).exec(); - assertThat(first.getExitCode(), is(0)); + ExecCreateCmdResponse checkFileExec1 = dockerClient.execCreateCmd(container.getId()) + .withAttachStdout(true).withAttachStderr(true).withCmd("test", "-e", "/marker").exec(); + LOG.info("Created exec {}", checkFileExec1.toString()); + assertThat(checkFileExec1.getId(), not(isEmptyString())); + dockerClient.execStartCmd(checkFileExec1.getId()).exec(new ExecStartResultCallback(System.out, System.err)).awaitCompletion(); + InspectExecResponse first = dockerClient.inspectExecCmd(checkFileExec1.getId()).exec(); + assertThat(first.getExitCode(), is(1)); // Create the file + ExecCreateCmdResponse touchFileExec = dockerClient.execCreateCmd(container.getId()) + .withAttachStdout(true).withAttachStderr(true).withCmd("touch", "/marker").exec(); + LOG.info("Created exec {}", touchFileExec.toString()); + assertThat(touchFileExec.getId(), not(isEmptyString())); dockerClient.execStartCmd(container.getId()) - .withExecId(touchFileCmdCreateResponse.getId()).exec(new ExecStartResultCallback(System.out, System.err)); - - InspectExecResponse second = dockerClient.inspectExecCmd(touchFileCmdCreateResponse.getId()).exec(); + .withExecId(touchFileExec.getId()).exec(new ExecStartResultCallback(System.out, System.err)); + InspectExecResponse second = dockerClient.inspectExecCmd(touchFileExec.getId()).exec(); assertThat(second.getExitCode(), is(0)); + // Check that file does exist now + ExecCreateCmdResponse checkFileExec2 = dockerClient.execCreateCmd(container.getId()) + .withAttachStdout(true).withAttachStderr(true).withCmd("test", "-e", "/marker").exec(); + LOG.info("Created exec {}", checkFileExec2.toString()); + assertThat(checkFileExec2.getId(), not(isEmptyString())); dockerClient.execStartCmd(container.getId()) - .withExecId(checkFileCmdCreateResponse.getId()).exec(new ExecStartResultCallback(System.out, System.err)); - - InspectExecResponse third = dockerClient.inspectExecCmd(checkFileCmdCreateResponse.getId()).exec(); + .withExecId(checkFileExec2.getId()).exec(new ExecStartResultCallback(System.out, System.err)); + InspectExecResponse third = dockerClient.inspectExecCmd(checkFileExec2.getId()).exec(); assertThat(third.getExitCode(), is(0)); // Get container info and check its roundtrip to ensure the consistency diff --git a/src/test/java/com/github/dockerjava/netty/exec/ExecStartCmdExecTest.java b/src/test/java/com/github/dockerjava/netty/exec/ExecStartCmdExecTest.java index b9d81db9d..1e5af11da 100644 --- a/src/test/java/com/github/dockerjava/netty/exec/ExecStartCmdExecTest.java +++ b/src/test/java/com/github/dockerjava/netty/exec/ExecStartCmdExecTest.java @@ -61,7 +61,10 @@ public void execStart() throws Exception { dockerClient.execStartCmd(execCreateCmdResponse.getId()).exec( new ExecStartResultCallback(System.out, System.err)); - InputStream response = dockerClient.copyFileFromContainerCmd(container.getId(), "/execStartTest.log").exec(); + LOG.info("Wait for 5 seconds"); + Thread.sleep(5000); + + InputStream response = dockerClient.copyArchiveFromContainerCmd(container.getId(), "/execStartTest.log").exec(); Boolean bytesAvailable = response.available() > 0; assertTrue(bytesAvailable, "The file was not copied from the container."); @@ -88,7 +91,7 @@ public void execStartAttached() throws Exception { dockerClient.execStartCmd(execCreateCmdResponse.getId()).withDetach(false).withTty(true) .exec(new ExecStartResultCallback(System.out, System.err)); - InputStream response = dockerClient.copyFileFromContainerCmd(container.getId(), "/execStartTest.log").exec(); + InputStream response = dockerClient.copyArchiveFromContainerCmd(container.getId(), "/execStartTest.log").exec(); Boolean bytesAvailable = response.available() > 0; assertTrue(bytesAvailable, "The file was not copied from the container."); diff --git a/src/test/java/com/github/dockerjava/netty/exec/InspectExecCmdExecTest.java b/src/test/java/com/github/dockerjava/netty/exec/InspectExecCmdExecTest.java index efeba6e99..1849b2fe3 100644 --- a/src/test/java/com/github/dockerjava/netty/exec/InspectExecCmdExecTest.java +++ b/src/test/java/com/github/dockerjava/netty/exec/InspectExecCmdExecTest.java @@ -48,48 +48,44 @@ public void afterMethod(ITestResult result) { } @Test(groups = "ignoreInCircleCi") - public void inspectExecTest() throws IOException { + public void inspectExec() throws Exception { String containerName = "generated_" + new SecureRandom().nextInt(); - CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("top") + CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("sleep", "9999") .withName(containerName).exec(); LOG.info("Created container {}", container.toString()); assertThat(container.getId(), not(isEmptyString())); dockerClient.startContainerCmd(container.getId()).exec(); - ExecCreateCmdResponse touchFileCmdCreateResponse = dockerClient.execCreateCmd(container.getId()) - .withAttachStdout(true).withAttachStderr(true).withCmd("touch", "/marker").withTty(true).exec(); - LOG.info("Created exec {}", touchFileCmdCreateResponse.toString()); - assertThat(touchFileCmdCreateResponse.getId(), not(isEmptyString())); - ExecCreateCmdResponse checkFileCmdCreateResponse = dockerClient.execCreateCmd(container.getId()) - .withAttachStdout(true).withAttachStderr(true).withCmd("test", "-e", "/marker").exec(); - LOG.info("Created exec {}", checkFileCmdCreateResponse.toString()); - assertThat(checkFileCmdCreateResponse.getId(), not(isEmptyString())); - // Check that file does not exist - dockerClient.execStartCmd(container.getId()).withDetach(false).withTty(true) - .withExecId(checkFileCmdCreateResponse.getId()) - .exec(new ExecStartResultCallback(System.out, System.err)); - - InspectExecResponse first = dockerClient.inspectExecCmd(checkFileCmdCreateResponse.getId()).exec(); - assertEquals(first.isRunning(), new Boolean(false)); + ExecCreateCmdResponse checkFileExec1 = dockerClient.execCreateCmd(container.getId()) + .withAttachStdout(false).withAttachStderr(false).withCmd("test", "-e", "/marker").exec(); + LOG.info("Created exec {}", checkFileExec1.toString()); + assertThat(checkFileExec1.getId(), not(isEmptyString())); + dockerClient.execStartCmd(checkFileExec1.getId()).exec(new ExecStartResultCallback(System.out, System.err)); + InspectExecResponse first = dockerClient.inspectExecCmd(checkFileExec1.getId()).exec(); assertThat(first.getExitCode(), is(1)); // Create the file - dockerClient.execStartCmd(container.getId()).withDetach(false).withTty(true) - .withExecId(touchFileCmdCreateResponse.getId()) - .exec(new ExecStartResultCallback(System.out, System.err)); - - InspectExecResponse second = dockerClient.inspectExecCmd(touchFileCmdCreateResponse.getId()).exec(); - assertEquals(first.isRunning(), new Boolean(false)); + ExecCreateCmdResponse touchFileExec = dockerClient.execCreateCmd(container.getId()) + .withAttachStdout(true).withAttachStderr(true).withCmd("touch", "/marker").exec(); + LOG.info("Created exec {}", touchFileExec.toString()); + assertThat(touchFileExec.getId(), not(isEmptyString())); + dockerClient.execStartCmd(container.getId()) + .withExecId(touchFileExec.getId()).exec(new ExecStartResultCallback(System.out, System.err)); + InspectExecResponse second = dockerClient.inspectExecCmd(touchFileExec.getId()).exec(); assertThat(second.getExitCode(), is(0)); - // Check that file does exist now - dockerClient.execStartCmd(container.getId()).withExecId(checkFileCmdCreateResponse.getId()) - .exec(new ExecStartResultCallback(System.out, System.err)); - InspectExecResponse third = dockerClient.inspectExecCmd(checkFileCmdCreateResponse.getId()).exec(); + // Check that file does exist now + ExecCreateCmdResponse checkFileExec2 = dockerClient.execCreateCmd(container.getId()) + .withAttachStdout(true).withAttachStderr(true).withCmd("test", "-e", "/marker").exec(); + LOG.info("Created exec {}", checkFileExec2.toString()); + assertThat(checkFileExec2.getId(), not(isEmptyString())); + dockerClient.execStartCmd(container.getId()) + .withExecId(checkFileExec2.getId()).exec(new ExecStartResultCallback(System.out, System.err)); + InspectExecResponse third = dockerClient.inspectExecCmd(checkFileExec2.getId()).exec(); assertThat(third.getExitCode(), is(0)); // Get container info and check its roundtrip to ensure the consistency