diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/AttachContainerCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/AttachContainerCmdIT.java index 055eb640b..68d1867ad 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/AttachContainerCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/AttachContainerCmdIT.java @@ -3,7 +3,6 @@ import com.github.dockerjava.api.DockerClient; import com.github.dockerjava.api.async.ResultCallback; import com.github.dockerjava.api.command.CreateContainerResponse; -import com.github.dockerjava.api.command.InspectContainerResponse; import com.github.dockerjava.api.model.Frame; import com.github.dockerjava.api.model.StreamType; import org.junit.Assume; @@ -26,8 +25,8 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.emptyString; +import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -51,20 +50,17 @@ public void attachContainerWithStdin() throws Exception { String snippet = "hello world"; CreateContainerResponse container = dockerClient.createContainerCmd("busybox") - .withCmd("/bin/sh", "-c", "sleep 1 && read line && echo $line") - .withTty(false) - .withStdinOpen(true) - .exec(); + .withCmd("/bin/sh", "-c", "read line && echo $line") + .withTty(false) + .withAttachStdin(true) + .withAttachStdout(true) + .withAttachStderr(true) + .withStdinOpen(true) + .exec(); LOG.info("Created container: {}", container.toString()); assertThat(container.getId(), not(is(emptyString()))); - dockerClient.startContainerCmd(container.getId()).exec(); - - InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd(container.getId()).exec(); - - assertThat(inspectContainerResponse.getState().getRunning(), is(true)); - AttachContainerTestCallback callback = new AttachContainerTestCallback() { @Override public void onNext(Frame frame) { @@ -75,7 +71,7 @@ public void onNext(Frame frame) { try ( PipedOutputStream out = new PipedOutputStream(); - PipedInputStream in = new PipedInputStream(out); + PipedInputStream in = new PipedInputStream(out) ) { dockerClient.attachContainerCmd(container.getId()) .withStdErr(true) @@ -84,6 +80,8 @@ public void onNext(Frame frame) { .withStdIn(in) .exec(callback); + dockerClient.startContainerCmd(container.getId()).exec(); + out.write((snippet + "\n").getBytes()); out.flush(); @@ -101,30 +99,33 @@ public void attachContainerWithoutTTY() throws Exception { String snippet = "hello world"; CreateContainerResponse container = dockerClient.createContainerCmd(DEFAULT_IMAGE) - .withCmd("echo", snippet) - .withTty(false) - .exec(); + .withCmd("echo", snippet) + .withTty(false) + .withAttachStdout(true) + .withAttachStderr(true) + .exec(); LOG.info("Created container: {}", container.toString()); assertThat(container.getId(), not(is(emptyString()))); - dockerClient.startContainerCmd(container.getId()).exec(); - AttachContainerTestCallback callback = new AttachContainerTestCallback() { @Override public void onNext(Frame frame) { assertThat(frame.getStreamType(), equalTo(StreamType.STDOUT)); super.onNext(frame); - }; + } }; dockerClient.attachContainerCmd(container.getId()) - .withStdErr(true) - .withStdOut(true) - .withFollowStream(true) - .withLogs(true) - .exec(callback) - .awaitCompletion(30, TimeUnit.SECONDS); + .withStdErr(true) + .withStdOut(true) + .withFollowStream(true) + .withLogs(true) + .exec(callback); + + dockerClient.startContainerCmd(container.getId()).exec(); + + callback.awaitCompletion(30, TimeUnit.SECONDS); callback.close(); assertThat(callback.toString(), containsString(snippet)); @@ -135,31 +136,37 @@ public void attachContainerWithTTY() throws Exception { DockerClient dockerClient = dockerRule.getClient(); File baseDir = new File(Thread.currentThread().getContextClassLoader() - .getResource("attachContainerTestDockerfile").getFile()); + .getResource("attachContainerTestDockerfile").getFile()); String imageId = dockerRule.buildImage(baseDir); - CreateContainerResponse container = dockerClient.createContainerCmd(imageId).withTty(true).exec(); + CreateContainerResponse container = dockerClient.createContainerCmd(imageId) + .withTty(true) + .withAttachStdout(true) + .withAttachStderr(true) + .exec(); LOG.info("Created container: {}", container.toString()); assertThat(container.getId(), not(is(emptyString()))); - dockerClient.startContainerCmd(container.getId()).exec(); AttachContainerTestCallback callback = new AttachContainerTestCallback() { @Override public void onNext(Frame frame) { assertThat(frame.getStreamType(), equalTo(StreamType.RAW)); super.onNext(frame); - }; + } }; dockerClient.attachContainerCmd(container.getId()) - .withStdErr(true) - .withStdOut(true) - .withFollowStream(true) - .exec(callback) - .awaitCompletion(15, TimeUnit.SECONDS); + .withStdErr(true) + .withStdOut(true) + .withFollowStream(true) + .exec(callback); + + dockerClient.startContainerCmd(container.getId()).exec(); + + callback.awaitCompletion(15, TimeUnit.SECONDS); callback.close(); LOG.debug("log: {}", callback.toString()); @@ -178,33 +185,37 @@ public void attachContainerStdinUnsupported() throws Exception { String snippet = "hello world"; CreateContainerResponse container = dockerClient.createContainerCmd(DEFAULT_IMAGE) - .withCmd("echo", snippet) - .withTty(false) - .exec(); + .withCmd("echo", snippet) + .withTty(false) + .withAttachStdin(true) + .withAttachStdout(true) + .withAttachStderr(true) + .exec(); LOG.info("Created container: {}", container.toString()); assertThat(container.getId(), not(is(emptyString()))); - dockerClient.startContainerCmd(container.getId()).exec(); - AttachContainerTestCallback callback = new AttachContainerTestCallback() { @Override public void onNext(Frame frame) { assertThat(frame.getStreamType(), equalTo(StreamType.STDOUT)); super.onNext(frame); - }; + } }; InputStream stdin = new ByteArrayInputStream("".getBytes()); dockerClient.attachContainerCmd(container.getId()) - .withStdErr(true) - .withStdOut(true) - .withFollowStream(true) - .withLogs(true) - .withStdIn(stdin) - .exec(callback) - .awaitCompletion(30, TimeUnit.SECONDS); + .withStdErr(true) + .withStdOut(true) + .withFollowStream(true) + .withLogs(true) + .withStdIn(stdin) + .exec(callback); + + dockerClient.startContainerCmd(container.getId()).exec(); + + callback.awaitCompletion(30, TimeUnit.SECONDS); callback.close(); } @@ -217,33 +228,35 @@ public void attachContainerClosesStdoutWhenContainerExits() throws Exception { DockerClient dockerClient = dockerRule.getClient(); CreateContainerResponse container = dockerClient.createContainerCmd(DEFAULT_IMAGE) - .withCmd("echo", "hello") - .withTty(false) - .exec(); + .withCmd("echo", "hello") + .withTty(false) + .withAttachStdout(true) + .withAttachStderr(true) + .exec(); LOG.info("Created container: {}", container.toString()); CountDownLatch gotLine = new CountDownLatch(1); try ( - ResultCallback.Adapter resultCallback = dockerClient.attachContainerCmd(container.getId()) - .withStdOut(true) - .withStdErr(true) - .withFollowStream(true) - .exec(new ResultCallback.Adapter() { - @Override - public void onNext(Frame item) { - LOG.info("Got frame: {}", item); - if (item.getStreamType() == StreamType.STDOUT) { - gotLine.countDown(); - } - super.onNext(item); - } - - @Override - public void onComplete() { - LOG.info("On complete"); - super.onComplete(); - } - }) + ResultCallback.Adapter resultCallback = dockerClient.attachContainerCmd(container.getId()) + .withStdOut(true) + .withStdErr(true) + .withFollowStream(true) + .exec(new ResultCallback.Adapter() { + @Override + public void onNext(Frame item) { + LOG.info("Got frame: {}", item); + if (item.getStreamType() == StreamType.STDOUT) { + gotLine.countDown(); + } + super.onNext(item); + } + + @Override + public void onComplete() { + LOG.info("On complete"); + super.onComplete(); + } + }) ) { resultCallback.awaitStarted(5, SECONDS); LOG.info("Attach started"); @@ -258,7 +271,7 @@ public void onComplete() { } public static class AttachContainerTestCallback extends ResultCallback.Adapter { - private StringBuffer log = new StringBuffer(); + private final StringBuffer log = new StringBuffer(); @Override public void onNext(Frame item) { diff --git a/docker-java/src/test/resources/attachContainerTestDockerfile/echo.sh b/docker-java/src/test/resources/attachContainerTestDockerfile/echo.sh index 88b444bf0..370cda203 100644 --- a/docker-java/src/test/resources/attachContainerTestDockerfile/echo.sh +++ b/docker-java/src/test/resources/attachContainerTestDockerfile/echo.sh @@ -1,2 +1,2 @@ #!/bin/sh -while sleep 2; do echo stdout && echo stderr >&2; done \ No newline at end of file +echo stdout && echo stderr >&2