From d74da7b5efb5c69f8ae99775213481ccf37bdca6 Mon Sep 17 00:00:00 2001 From: Alexander Koshevoy Date: Mon, 26 Oct 2020 23:08:27 +0300 Subject: [PATCH 1/6] Remove unnecessary semicolons --- .../com/github/dockerjava/cmd/AttachContainerCmdIT.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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..b661c28c7 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 @@ -75,7 +75,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) @@ -115,7 +115,7 @@ public void attachContainerWithoutTTY() throws Exception { public void onNext(Frame frame) { assertThat(frame.getStreamType(), equalTo(StreamType.STDOUT)); super.onNext(frame); - }; + } }; dockerClient.attachContainerCmd(container.getId()) @@ -151,7 +151,7 @@ public void attachContainerWithTTY() throws Exception { public void onNext(Frame frame) { assertThat(frame.getStreamType(), equalTo(StreamType.RAW)); super.onNext(frame); - }; + } }; dockerClient.attachContainerCmd(container.getId()) @@ -192,7 +192,7 @@ public void attachContainerStdinUnsupported() throws Exception { public void onNext(Frame frame) { assertThat(frame.getStreamType(), equalTo(StreamType.STDOUT)); super.onNext(frame); - }; + } }; InputStream stdin = new ByteArrayInputStream("".getBytes()); From 881ccf7fcce3861a9a194e901d365a13ddd47402 Mon Sep 17 00:00:00 2001 From: Alexander Koshevoy Date: Mon, 26 Oct 2020 23:09:28 +0300 Subject: [PATCH 2/6] Make log field of AttachContainerTestCallback final --- .../java/com/github/dockerjava/cmd/AttachContainerCmdIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 b661c28c7..ab40a641a 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 @@ -258,7 +258,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) { From c89af75cc855da41338f6dd5f59c1fb03045c434 Mon Sep 17 00:00:00 2001 From: Alexander Koshevoy Date: Mon, 26 Oct 2020 23:11:23 +0300 Subject: [PATCH 3/6] Reformat AttachContainerCmdIT.java --- .../dockerjava/cmd/AttachContainerCmdIT.java | 106 +++++++++--------- 1 file changed, 53 insertions(+), 53 deletions(-) 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 ab40a641a..f742b8240 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 @@ -26,8 +26,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,10 +51,10 @@ 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", "sleep 1 && read line && echo $line") + .withTty(false) + .withStdinOpen(true) + .exec(); LOG.info("Created container: {}", container.toString()); assertThat(container.getId(), not(is(emptyString()))); @@ -101,9 +101,9 @@ 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) + .exec(); LOG.info("Created container: {}", container.toString()); assertThat(container.getId(), not(is(emptyString()))); @@ -119,12 +119,12 @@ public void onNext(Frame 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) + .awaitCompletion(30, TimeUnit.SECONDS); callback.close(); assertThat(callback.toString(), containsString(snippet)); @@ -135,7 +135,7 @@ 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); @@ -155,11 +155,11 @@ public void onNext(Frame 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) + .awaitCompletion(15, TimeUnit.SECONDS); callback.close(); LOG.debug("log: {}", callback.toString()); @@ -178,9 +178,9 @@ 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) + .exec(); LOG.info("Created container: {}", container.toString()); assertThat(container.getId(), not(is(emptyString()))); @@ -198,13 +198,13 @@ public void onNext(Frame 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) + .awaitCompletion(30, TimeUnit.SECONDS); callback.close(); } @@ -217,33 +217,33 @@ 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) + .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"); From 33d425958caabf9a2b6d09a7e5318132af9a27e1 Mon Sep 17 00:00:00 2001 From: Alexander Koshevoy Date: Mon, 26 Oct 2020 23:21:48 +0300 Subject: [PATCH 4/6] Fix sporadic failures of AttachContainerCmdIT tests --- .../dockerjava/cmd/AttachContainerCmdIT.java | 51 ++++++++++++------- 1 file changed, 32 insertions(+), 19 deletions(-) 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 f742b8240..fb3d7397f 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; @@ -53,18 +52,15 @@ public void attachContainerWithStdin() throws Exception { CreateContainerResponse container = dockerClient.createContainerCmd("busybox") .withCmd("/bin/sh", "-c", "sleep 1 && 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) { @@ -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(); @@ -103,13 +101,13 @@ public void attachContainerWithoutTTY() throws Exception { CreateContainerResponse container = dockerClient.createContainerCmd(DEFAULT_IMAGE) .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) { @@ -123,8 +121,11 @@ public void onNext(Frame frame) { .withStdOut(true) .withFollowStream(true) .withLogs(true) - .exec(callback) - .awaitCompletion(30, TimeUnit.SECONDS); + .exec(callback); + + dockerClient.startContainerCmd(container.getId()).exec(); + + callback.awaitCompletion(30, TimeUnit.SECONDS); callback.close(); assertThat(callback.toString(), containsString(snippet)); @@ -139,12 +140,15 @@ public void attachContainerWithTTY() throws Exception { 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 @@ -158,8 +162,11 @@ public void onNext(Frame frame) { .withStdErr(true) .withStdOut(true) .withFollowStream(true) - .exec(callback) - .awaitCompletion(15, TimeUnit.SECONDS); + .exec(callback); + + dockerClient.startContainerCmd(container.getId()).exec(); + + callback.awaitCompletion(15, TimeUnit.SECONDS); callback.close(); LOG.debug("log: {}", callback.toString()); @@ -180,13 +187,14 @@ public void attachContainerStdinUnsupported() throws Exception { CreateContainerResponse container = dockerClient.createContainerCmd(DEFAULT_IMAGE) .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) { @@ -203,8 +211,11 @@ public void onNext(Frame frame) { .withFollowStream(true) .withLogs(true) .withStdIn(stdin) - .exec(callback) - .awaitCompletion(30, TimeUnit.SECONDS); + .exec(callback); + + dockerClient.startContainerCmd(container.getId()).exec(); + + callback.awaitCompletion(30, TimeUnit.SECONDS); callback.close(); } @@ -219,6 +230,8 @@ public void attachContainerClosesStdoutWhenContainerExits() throws Exception { CreateContainerResponse container = dockerClient.createContainerCmd(DEFAULT_IMAGE) .withCmd("echo", "hello") .withTty(false) + .withAttachStdout(true) + .withAttachStderr(true) .exec(); LOG.info("Created container: {}", container.toString()); From b1acebce7d5acf03639a9826b9139456e0be00b7 Mon Sep 17 00:00:00 2001 From: Alexander Koshevoy Date: Tue, 3 Nov 2020 20:55:53 +0300 Subject: [PATCH 5/6] Remove no longer required "sleep 1" before reading line and echoing it in "attachContainerWithStdin" test --- .../java/com/github/dockerjava/cmd/AttachContainerCmdIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 fb3d7397f..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 @@ -50,7 +50,7 @@ public void attachContainerWithStdin() throws Exception { String snippet = "hello world"; CreateContainerResponse container = dockerClient.createContainerCmd("busybox") - .withCmd("/bin/sh", "-c", "sleep 1 && read line && echo $line") + .withCmd("/bin/sh", "-c", "read line && echo $line") .withTty(false) .withAttachStdin(true) .withAttachStdout(true) From 4e81f6e13deabcab1b88f9c6ae9ea292464309a3 Mon Sep 17 00:00:00 2001 From: Alexander Koshevoy Date: Tue, 3 Nov 2020 20:59:05 +0300 Subject: [PATCH 6/6] Remove no longer required cyclic output to stdout and stderr in "attachContainerWithTTY" test --- .../src/test/resources/attachContainerTestDockerfile/echo.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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