|
1 | 1 | package com.github.dockerjava.core.command; |
2 | 2 |
|
| 3 | +import static java.util.concurrent.TimeUnit.SECONDS; |
3 | 4 | import static org.apache.commons.lang.StringUtils.isEmpty; |
4 | 5 | import static org.hamcrest.MatcherAssert.assertThat; |
5 | 6 | import static org.hamcrest.Matchers.containsString; |
|
12 | 13 | import java.lang.reflect.Method; |
13 | 14 | import java.util.concurrent.TimeUnit; |
14 | 15 |
|
| 16 | +import com.github.dockerjava.api.command.InspectContainerResponse; |
15 | 17 | import org.apache.commons.codec.binary.StringUtils; |
16 | 18 | import org.testng.ITestResult; |
17 | 19 | import org.testng.SkipException; |
@@ -71,7 +73,51 @@ public void onNext(Frame frame) { |
71 | 73 | }; |
72 | 74 |
|
73 | 75 | dockerClient.attachContainerCmd(container.getId()).withStdErr(true).withStdOut(true).withFollowStream(true) |
74 | | - .withLogs(true).exec(callback).awaitCompletion(30, TimeUnit.SECONDS); |
| 76 | + .withLogs(true).exec(callback).awaitCompletion(30, SECONDS); |
| 77 | + callback.close(); |
| 78 | + |
| 79 | + assertThat(callback.toString(), containsString(snippet)); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void attachContainerWithStdin() throws Exception { |
| 84 | + |
| 85 | + String snippet = "hello world"; |
| 86 | + |
| 87 | + CreateContainerResponse container = dockerClient.createContainerCmd("busybox") |
| 88 | + .withCmd("/bin/sh", "-c", "sleep 1 && read line && echo $line") |
| 89 | + .withTty(false) |
| 90 | + .withStdinOpen(true) |
| 91 | + .exec(); |
| 92 | + |
| 93 | + LOG.info("Created container: {}", container.toString()); |
| 94 | + assertThat(container.getId(), not(isEmptyString())); |
| 95 | + |
| 96 | + dockerClient.startContainerCmd(container.getId()).exec(); |
| 97 | + |
| 98 | + Thread.sleep(SECONDS.toMillis(3)); //wait bash initialisation |
| 99 | + |
| 100 | + InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd(container.getId()).exec(); |
| 101 | + |
| 102 | + assertTrue(inspectContainerResponse.getState().getRunning()); |
| 103 | + |
| 104 | + AttachContainerTestCallback callback = new AttachContainerTestCallback() { |
| 105 | + @Override |
| 106 | + public void onNext(Frame frame) { |
| 107 | + assertEquals(frame.getStreamType(), StreamType.STDOUT); |
| 108 | + super.onNext(frame); |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + InputStream stdin = new ByteArrayInputStream((snippet + "\n").getBytes()); |
| 113 | + |
| 114 | + dockerClient.attachContainerCmd(container.getId()) |
| 115 | + .withStdErr(true) |
| 116 | + .withStdOut(true) |
| 117 | + .withFollowStream(true) |
| 118 | + .withStdIn(stdin) |
| 119 | + .exec(callback) |
| 120 | + .awaitCompletion(15, SECONDS); |
75 | 121 | callback.close(); |
76 | 122 |
|
77 | 123 | assertThat(callback.toString(), containsString(snippet)); |
@@ -105,7 +151,7 @@ public void onNext(Frame frame) { |
105 | 151 | .withStdOut(true) |
106 | 152 | .withFollowStream(true) |
107 | 153 | .exec(callback) |
108 | | - .awaitCompletion(15, TimeUnit.SECONDS); |
| 154 | + .awaitCompletion(15, SECONDS); |
109 | 155 | callback.close(); |
110 | 156 |
|
111 | 157 | System.out.println("log: " + callback.toString()); |
@@ -138,7 +184,7 @@ public void onNext(Frame frame) { |
138 | 184 | InputStream stdin = new ByteArrayInputStream("".getBytes()); |
139 | 185 |
|
140 | 186 | dockerClient.attachContainerCmd(container.getId()).withStdErr(true).withStdOut(true).withFollowStream(true) |
141 | | - .withLogs(true).withStdIn(stdin).exec(callback).awaitCompletion(30, TimeUnit.SECONDS); |
| 187 | + .withLogs(true).withStdIn(stdin).exec(callback).awaitCompletion(30, SECONDS); |
142 | 188 | callback.close(); |
143 | 189 | } |
144 | 190 |
|
|
0 commit comments