|
3 | 3 | import com.github.dockerjava.api.DockerClient; |
4 | 4 | import com.github.dockerjava.api.async.ResultCallback; |
5 | 5 | import com.github.dockerjava.api.command.CreateContainerResponse; |
6 | | -import com.github.dockerjava.api.command.InspectContainerResponse; |
7 | 6 | import com.github.dockerjava.api.model.Frame; |
8 | 7 | import com.github.dockerjava.api.model.StreamType; |
9 | 8 | import org.junit.Assume; |
|
26 | 25 | import static org.hamcrest.CoreMatchers.is; |
27 | 26 | import static org.hamcrest.MatcherAssert.assertThat; |
28 | 27 | import static org.hamcrest.Matchers.containsString; |
29 | | -import static org.hamcrest.Matchers.equalTo; |
30 | 28 | import static org.hamcrest.Matchers.emptyString; |
| 29 | +import static org.hamcrest.Matchers.equalTo; |
31 | 30 | import static org.hamcrest.Matchers.not; |
32 | 31 | import static org.junit.Assert.assertEquals; |
33 | 32 | import static org.junit.Assert.assertTrue; |
@@ -51,20 +50,17 @@ public void attachContainerWithStdin() throws Exception { |
51 | 50 | String snippet = "hello world"; |
52 | 51 |
|
53 | 52 | CreateContainerResponse container = dockerClient.createContainerCmd("busybox") |
54 | | - .withCmd("/bin/sh", "-c", "sleep 1 && read line && echo $line") |
55 | | - .withTty(false) |
56 | | - .withStdinOpen(true) |
57 | | - .exec(); |
| 53 | + .withCmd("/bin/sh", "-c", "read line && echo $line") |
| 54 | + .withTty(false) |
| 55 | + .withAttachStdin(true) |
| 56 | + .withAttachStdout(true) |
| 57 | + .withAttachStderr(true) |
| 58 | + .withStdinOpen(true) |
| 59 | + .exec(); |
58 | 60 |
|
59 | 61 | LOG.info("Created container: {}", container.toString()); |
60 | 62 | assertThat(container.getId(), not(is(emptyString()))); |
61 | 63 |
|
62 | | - dockerClient.startContainerCmd(container.getId()).exec(); |
63 | | - |
64 | | - InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd(container.getId()).exec(); |
65 | | - |
66 | | - assertThat(inspectContainerResponse.getState().getRunning(), is(true)); |
67 | | - |
68 | 64 | AttachContainerTestCallback callback = new AttachContainerTestCallback() { |
69 | 65 | @Override |
70 | 66 | public void onNext(Frame frame) { |
@@ -75,7 +71,7 @@ public void onNext(Frame frame) { |
75 | 71 |
|
76 | 72 | try ( |
77 | 73 | PipedOutputStream out = new PipedOutputStream(); |
78 | | - PipedInputStream in = new PipedInputStream(out); |
| 74 | + PipedInputStream in = new PipedInputStream(out) |
79 | 75 | ) { |
80 | 76 | dockerClient.attachContainerCmd(container.getId()) |
81 | 77 | .withStdErr(true) |
@@ -84,6 +80,8 @@ public void onNext(Frame frame) { |
84 | 80 | .withStdIn(in) |
85 | 81 | .exec(callback); |
86 | 82 |
|
| 83 | + dockerClient.startContainerCmd(container.getId()).exec(); |
| 84 | + |
87 | 85 | out.write((snippet + "\n").getBytes()); |
88 | 86 | out.flush(); |
89 | 87 |
|
@@ -101,30 +99,33 @@ public void attachContainerWithoutTTY() throws Exception { |
101 | 99 | String snippet = "hello world"; |
102 | 100 |
|
103 | 101 | CreateContainerResponse container = dockerClient.createContainerCmd(DEFAULT_IMAGE) |
104 | | - .withCmd("echo", snippet) |
105 | | - .withTty(false) |
106 | | - .exec(); |
| 102 | + .withCmd("echo", snippet) |
| 103 | + .withTty(false) |
| 104 | + .withAttachStdout(true) |
| 105 | + .withAttachStderr(true) |
| 106 | + .exec(); |
107 | 107 |
|
108 | 108 | LOG.info("Created container: {}", container.toString()); |
109 | 109 | assertThat(container.getId(), not(is(emptyString()))); |
110 | 110 |
|
111 | | - dockerClient.startContainerCmd(container.getId()).exec(); |
112 | | - |
113 | 111 | AttachContainerTestCallback callback = new AttachContainerTestCallback() { |
114 | 112 | @Override |
115 | 113 | public void onNext(Frame frame) { |
116 | 114 | assertThat(frame.getStreamType(), equalTo(StreamType.STDOUT)); |
117 | 115 | super.onNext(frame); |
118 | | - }; |
| 116 | + } |
119 | 117 | }; |
120 | 118 |
|
121 | 119 | dockerClient.attachContainerCmd(container.getId()) |
122 | | - .withStdErr(true) |
123 | | - .withStdOut(true) |
124 | | - .withFollowStream(true) |
125 | | - .withLogs(true) |
126 | | - .exec(callback) |
127 | | - .awaitCompletion(30, TimeUnit.SECONDS); |
| 120 | + .withStdErr(true) |
| 121 | + .withStdOut(true) |
| 122 | + .withFollowStream(true) |
| 123 | + .withLogs(true) |
| 124 | + .exec(callback); |
| 125 | + |
| 126 | + dockerClient.startContainerCmd(container.getId()).exec(); |
| 127 | + |
| 128 | + callback.awaitCompletion(30, TimeUnit.SECONDS); |
128 | 129 | callback.close(); |
129 | 130 |
|
130 | 131 | assertThat(callback.toString(), containsString(snippet)); |
@@ -135,31 +136,37 @@ public void attachContainerWithTTY() throws Exception { |
135 | 136 | DockerClient dockerClient = dockerRule.getClient(); |
136 | 137 |
|
137 | 138 | File baseDir = new File(Thread.currentThread().getContextClassLoader() |
138 | | - .getResource("attachContainerTestDockerfile").getFile()); |
| 139 | + .getResource("attachContainerTestDockerfile").getFile()); |
139 | 140 |
|
140 | 141 | String imageId = dockerRule.buildImage(baseDir); |
141 | 142 |
|
142 | | - CreateContainerResponse container = dockerClient.createContainerCmd(imageId).withTty(true).exec(); |
| 143 | + CreateContainerResponse container = dockerClient.createContainerCmd(imageId) |
| 144 | + .withTty(true) |
| 145 | + .withAttachStdout(true) |
| 146 | + .withAttachStderr(true) |
| 147 | + .exec(); |
143 | 148 |
|
144 | 149 | LOG.info("Created container: {}", container.toString()); |
145 | 150 | assertThat(container.getId(), not(is(emptyString()))); |
146 | 151 |
|
147 | | - dockerClient.startContainerCmd(container.getId()).exec(); |
148 | 152 |
|
149 | 153 | AttachContainerTestCallback callback = new AttachContainerTestCallback() { |
150 | 154 | @Override |
151 | 155 | public void onNext(Frame frame) { |
152 | 156 | assertThat(frame.getStreamType(), equalTo(StreamType.RAW)); |
153 | 157 | super.onNext(frame); |
154 | | - }; |
| 158 | + } |
155 | 159 | }; |
156 | 160 |
|
157 | 161 | dockerClient.attachContainerCmd(container.getId()) |
158 | | - .withStdErr(true) |
159 | | - .withStdOut(true) |
160 | | - .withFollowStream(true) |
161 | | - .exec(callback) |
162 | | - .awaitCompletion(15, TimeUnit.SECONDS); |
| 162 | + .withStdErr(true) |
| 163 | + .withStdOut(true) |
| 164 | + .withFollowStream(true) |
| 165 | + .exec(callback); |
| 166 | + |
| 167 | + dockerClient.startContainerCmd(container.getId()).exec(); |
| 168 | + |
| 169 | + callback.awaitCompletion(15, TimeUnit.SECONDS); |
163 | 170 | callback.close(); |
164 | 171 |
|
165 | 172 | LOG.debug("log: {}", callback.toString()); |
@@ -178,33 +185,37 @@ public void attachContainerStdinUnsupported() throws Exception { |
178 | 185 | String snippet = "hello world"; |
179 | 186 |
|
180 | 187 | CreateContainerResponse container = dockerClient.createContainerCmd(DEFAULT_IMAGE) |
181 | | - .withCmd("echo", snippet) |
182 | | - .withTty(false) |
183 | | - .exec(); |
| 188 | + .withCmd("echo", snippet) |
| 189 | + .withTty(false) |
| 190 | + .withAttachStdin(true) |
| 191 | + .withAttachStdout(true) |
| 192 | + .withAttachStderr(true) |
| 193 | + .exec(); |
184 | 194 |
|
185 | 195 | LOG.info("Created container: {}", container.toString()); |
186 | 196 | assertThat(container.getId(), not(is(emptyString()))); |
187 | 197 |
|
188 | | - dockerClient.startContainerCmd(container.getId()).exec(); |
189 | | - |
190 | 198 | AttachContainerTestCallback callback = new AttachContainerTestCallback() { |
191 | 199 | @Override |
192 | 200 | public void onNext(Frame frame) { |
193 | 201 | assertThat(frame.getStreamType(), equalTo(StreamType.STDOUT)); |
194 | 202 | super.onNext(frame); |
195 | | - }; |
| 203 | + } |
196 | 204 | }; |
197 | 205 |
|
198 | 206 | InputStream stdin = new ByteArrayInputStream("".getBytes()); |
199 | 207 |
|
200 | 208 | dockerClient.attachContainerCmd(container.getId()) |
201 | | - .withStdErr(true) |
202 | | - .withStdOut(true) |
203 | | - .withFollowStream(true) |
204 | | - .withLogs(true) |
205 | | - .withStdIn(stdin) |
206 | | - .exec(callback) |
207 | | - .awaitCompletion(30, TimeUnit.SECONDS); |
| 209 | + .withStdErr(true) |
| 210 | + .withStdOut(true) |
| 211 | + .withFollowStream(true) |
| 212 | + .withLogs(true) |
| 213 | + .withStdIn(stdin) |
| 214 | + .exec(callback); |
| 215 | + |
| 216 | + dockerClient.startContainerCmd(container.getId()).exec(); |
| 217 | + |
| 218 | + callback.awaitCompletion(30, TimeUnit.SECONDS); |
208 | 219 | callback.close(); |
209 | 220 | } |
210 | 221 |
|
@@ -217,33 +228,35 @@ public void attachContainerClosesStdoutWhenContainerExits() throws Exception { |
217 | 228 | DockerClient dockerClient = dockerRule.getClient(); |
218 | 229 |
|
219 | 230 | CreateContainerResponse container = dockerClient.createContainerCmd(DEFAULT_IMAGE) |
220 | | - .withCmd("echo", "hello") |
221 | | - .withTty(false) |
222 | | - .exec(); |
| 231 | + .withCmd("echo", "hello") |
| 232 | + .withTty(false) |
| 233 | + .withAttachStdout(true) |
| 234 | + .withAttachStderr(true) |
| 235 | + .exec(); |
223 | 236 | LOG.info("Created container: {}", container.toString()); |
224 | 237 |
|
225 | 238 | CountDownLatch gotLine = new CountDownLatch(1); |
226 | 239 | try ( |
227 | | - ResultCallback.Adapter<Frame> resultCallback = dockerClient.attachContainerCmd(container.getId()) |
228 | | - .withStdOut(true) |
229 | | - .withStdErr(true) |
230 | | - .withFollowStream(true) |
231 | | - .exec(new ResultCallback.Adapter<Frame>() { |
232 | | - @Override |
233 | | - public void onNext(Frame item) { |
234 | | - LOG.info("Got frame: {}", item); |
235 | | - if (item.getStreamType() == StreamType.STDOUT) { |
236 | | - gotLine.countDown(); |
237 | | - } |
238 | | - super.onNext(item); |
239 | | - } |
240 | | - |
241 | | - @Override |
242 | | - public void onComplete() { |
243 | | - LOG.info("On complete"); |
244 | | - super.onComplete(); |
245 | | - } |
246 | | - }) |
| 240 | + ResultCallback.Adapter<Frame> resultCallback = dockerClient.attachContainerCmd(container.getId()) |
| 241 | + .withStdOut(true) |
| 242 | + .withStdErr(true) |
| 243 | + .withFollowStream(true) |
| 244 | + .exec(new ResultCallback.Adapter<Frame>() { |
| 245 | + @Override |
| 246 | + public void onNext(Frame item) { |
| 247 | + LOG.info("Got frame: {}", item); |
| 248 | + if (item.getStreamType() == StreamType.STDOUT) { |
| 249 | + gotLine.countDown(); |
| 250 | + } |
| 251 | + super.onNext(item); |
| 252 | + } |
| 253 | + |
| 254 | + @Override |
| 255 | + public void onComplete() { |
| 256 | + LOG.info("On complete"); |
| 257 | + super.onComplete(); |
| 258 | + } |
| 259 | + }) |
247 | 260 | ) { |
248 | 261 | resultCallback.awaitStarted(5, SECONDS); |
249 | 262 | LOG.info("Attach started"); |
@@ -258,7 +271,7 @@ public void onComplete() { |
258 | 271 | } |
259 | 272 |
|
260 | 273 | public static class AttachContainerTestCallback extends ResultCallback.Adapter<Frame> { |
261 | | - private StringBuffer log = new StringBuffer(); |
| 274 | + private final StringBuffer log = new StringBuffer(); |
262 | 275 |
|
263 | 276 | @Override |
264 | 277 | public void onNext(Frame item) { |
|