33import com .github .dockerjava .api .DockerClient ;
44import com .github .dockerjava .api .async .ResultCallback ;
55import com .github .dockerjava .api .command .CreateContainerResponse ;
6- import com .github .dockerjava .api .command .InspectContainerResponse ;
76import com .github .dockerjava .api .model .Frame ;
87import com .github .dockerjava .api .model .StreamType ;
98import org .junit .Assume ;
2625import static org .hamcrest .CoreMatchers .is ;
2726import static org .hamcrest .MatcherAssert .assertThat ;
2827import static org .hamcrest .Matchers .containsString ;
29- import static org .hamcrest .Matchers .equalTo ;
3028import static org .hamcrest .Matchers .emptyString ;
29+ import static org .hamcrest .Matchers .equalTo ;
3130import static org .hamcrest .Matchers .not ;
3231import static org .junit .Assert .assertEquals ;
3332import static org .junit .Assert .assertTrue ;
@@ -51,20 +50,17 @@ public void attachContainerWithStdin() throws Exception {
5150 String snippet = "hello world" ;
5251
5352 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 ();
5860
5961 LOG .info ("Created container: {}" , container .toString ());
6062 assertThat (container .getId (), not (is (emptyString ())));
6163
62- dockerClient .startContainerCmd (container .getId ()).exec ();
63-
64- InspectContainerResponse inspectContainerResponse = dockerClient .inspectContainerCmd (container .getId ()).exec ();
65-
66- assertThat (inspectContainerResponse .getState ().getRunning (), is (true ));
67-
6864 AttachContainerTestCallback callback = new AttachContainerTestCallback () {
6965 @ Override
7066 public void onNext (Frame frame ) {
@@ -75,7 +71,7 @@ public void onNext(Frame frame) {
7571
7672 try (
7773 PipedOutputStream out = new PipedOutputStream ();
78- PipedInputStream in = new PipedInputStream (out );
74+ PipedInputStream in = new PipedInputStream (out )
7975 ) {
8076 dockerClient .attachContainerCmd (container .getId ())
8177 .withStdErr (true )
@@ -84,6 +80,8 @@ public void onNext(Frame frame) {
8480 .withStdIn (in )
8581 .exec (callback );
8682
83+ dockerClient .startContainerCmd (container .getId ()).exec ();
84+
8785 out .write ((snippet + "\n " ).getBytes ());
8886 out .flush ();
8987
@@ -101,30 +99,33 @@ public void attachContainerWithoutTTY() throws Exception {
10199 String snippet = "hello world" ;
102100
103101 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 ();
107107
108108 LOG .info ("Created container: {}" , container .toString ());
109109 assertThat (container .getId (), not (is (emptyString ())));
110110
111- dockerClient .startContainerCmd (container .getId ()).exec ();
112-
113111 AttachContainerTestCallback callback = new AttachContainerTestCallback () {
114112 @ Override
115113 public void onNext (Frame frame ) {
116114 assertThat (frame .getStreamType (), equalTo (StreamType .STDOUT ));
117115 super .onNext (frame );
118- };
116+ }
119117 };
120118
121119 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 );
128129 callback .close ();
129130
130131 assertThat (callback .toString (), containsString (snippet ));
@@ -135,31 +136,37 @@ public void attachContainerWithTTY() throws Exception {
135136 DockerClient dockerClient = dockerRule .getClient ();
136137
137138 File baseDir = new File (Thread .currentThread ().getContextClassLoader ()
138- .getResource ("attachContainerTestDockerfile" ).getFile ());
139+ .getResource ("attachContainerTestDockerfile" ).getFile ());
139140
140141 String imageId = dockerRule .buildImage (baseDir );
141142
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 ();
143148
144149 LOG .info ("Created container: {}" , container .toString ());
145150 assertThat (container .getId (), not (is (emptyString ())));
146151
147- dockerClient .startContainerCmd (container .getId ()).exec ();
148152
149153 AttachContainerTestCallback callback = new AttachContainerTestCallback () {
150154 @ Override
151155 public void onNext (Frame frame ) {
152156 assertThat (frame .getStreamType (), equalTo (StreamType .RAW ));
153157 super .onNext (frame );
154- };
158+ }
155159 };
156160
157161 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 );
163170 callback .close ();
164171
165172 LOG .debug ("log: {}" , callback .toString ());
@@ -178,33 +185,37 @@ public void attachContainerStdinUnsupported() throws Exception {
178185 String snippet = "hello world" ;
179186
180187 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 ();
184194
185195 LOG .info ("Created container: {}" , container .toString ());
186196 assertThat (container .getId (), not (is (emptyString ())));
187197
188- dockerClient .startContainerCmd (container .getId ()).exec ();
189-
190198 AttachContainerTestCallback callback = new AttachContainerTestCallback () {
191199 @ Override
192200 public void onNext (Frame frame ) {
193201 assertThat (frame .getStreamType (), equalTo (StreamType .STDOUT ));
194202 super .onNext (frame );
195- };
203+ }
196204 };
197205
198206 InputStream stdin = new ByteArrayInputStream ("" .getBytes ());
199207
200208 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 );
208219 callback .close ();
209220 }
210221
@@ -217,33 +228,35 @@ public void attachContainerClosesStdoutWhenContainerExits() throws Exception {
217228 DockerClient dockerClient = dockerRule .getClient ();
218229
219230 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 ();
223236 LOG .info ("Created container: {}" , container .toString ());
224237
225238 CountDownLatch gotLine = new CountDownLatch (1 );
226239 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+ })
247260 ) {
248261 resultCallback .awaitStarted (5 , SECONDS );
249262 LOG .info ("Attach started" );
@@ -258,7 +271,7 @@ public void onComplete() {
258271 }
259272
260273 public static class AttachContainerTestCallback extends ResultCallback .Adapter <Frame > {
261- private StringBuffer log = new StringBuffer ();
274+ private final StringBuffer log = new StringBuffer ();
262275
263276 @ Override
264277 public void onNext (Frame item ) {
0 commit comments