88
99import java .io .IOException ;
1010import java .lang .reflect .Method ;
11+ import java .util .concurrent .TimeUnit ;
1112
1213import org .testng .ITestResult ;
1314import org .testng .annotations .AfterMethod ;
1819
1920import com .github .dockerjava .api .command .CreateContainerResponse ;
2021import com .github .dockerjava .api .exception .NotFoundException ;
22+ import com .github .dockerjava .api .model .StreamType ;
2123import com .github .dockerjava .core .command .WaitContainerResultCallback ;
2224import com .github .dockerjava .netty .AbstractNettyDockerClientTest ;
2325
@@ -45,31 +47,65 @@ public void afterMethod(ITestResult result) {
4547 }
4648
4749 @ Test
48- public void asyncLogContainer () throws Exception {
50+ public void asyncLogContainerWithTtyEnabled () throws Exception {
4951
50- String snippet = "hello world" ;
51-
52- CreateContainerResponse container = dockerClient . createContainerCmd ( "busybox" ). withCmd ( "/bin/echo" , snippet )
52+ CreateContainerResponse container = dockerClient . createContainerCmd ( "busybox" )
53+ . withCmd ( "/bin/sh" , "-c" , "while true; do echo hello; sleep 1; done" )
54+ . withTty ( true )
5355 .exec ();
5456
5557 LOG .info ("Created container: {}" , container .toString ());
5658 assertThat (container .getId (), not (isEmptyString ()));
5759
58- dockerClient .startContainerCmd (container .getId ()).exec ();
60+ dockerClient .startContainerCmd (container .getId ())
61+ .exec ();
5962
60- int exitCode = dockerClient .waitContainerCmd (container .getId ()).exec (new WaitContainerResultCallback ())
61- .awaitStatusCode ();
63+ LogContainerTestCallback loggingCallback = new LogContainerTestCallback (true );
6264
63- assertThat (exitCode , equalTo (0 ));
65+ // this essentially test the since=0 case
66+ dockerClient .logContainerCmd (container .getId ())
67+ .withStdErr (true )
68+ .withStdOut (true )
69+ .withFollowStream (true )
70+ .withTailAll ()
71+ .exec (loggingCallback );
6472
65- LogContainerTestCallback loggingCallback = new LogContainerTestCallback ();
73+ loggingCallback .awaitCompletion (3 , TimeUnit .SECONDS );
74+
75+ assertTrue (loggingCallback .toString ().contains ("hello" ));
76+
77+ assertEquals (loggingCallback .getCollectedFrames ().get (0 ).getStreamType (), StreamType .RAW );
78+ }
79+
80+ @ Test
81+ public void asyncLogContainerWithTtyDisabled () throws Exception {
82+
83+ CreateContainerResponse container = dockerClient .createContainerCmd ("busybox" )
84+ .withCmd ("/bin/sh" , "-c" , "while true; do echo hello; sleep 1; done" )
85+ .withTty (false )
86+ .exec ();
87+
88+ LOG .info ("Created container: {}" , container .toString ());
89+ assertThat (container .getId (), not (isEmptyString ()));
90+
91+ dockerClient .startContainerCmd (container .getId ())
92+ .exec ();
93+
94+ LogContainerTestCallback loggingCallback = new LogContainerTestCallback (true );
6695
6796 // this essentially test the since=0 case
68- dockerClient .logContainerCmd (container .getId ()).withStdErr (true ).withStdOut (true ).exec (loggingCallback );
97+ dockerClient .logContainerCmd (container .getId ())
98+ .withStdErr (true )
99+ .withStdOut (true )
100+ .withFollowStream (true )
101+ .withTailAll ()
102+ .exec (loggingCallback );
69103
70- loggingCallback .awaitCompletion ();
104+ loggingCallback .awaitCompletion (3 , TimeUnit . SECONDS );
71105
72- assertTrue (loggingCallback .toString ().contains (snippet ));
106+ assertTrue (loggingCallback .toString ().contains ("hello" ));
107+
108+ assertEquals (loggingCallback .getCollectedFrames ().get (0 ).getStreamType (), StreamType .STDOUT );
73109 }
74110
75111 @ Test
@@ -106,34 +142,45 @@ public void asyncMultipleLogContainer() throws Exception {
106142
107143 String snippet = "hello world" ;
108144
109- CreateContainerResponse container = dockerClient .createContainerCmd ("busybox" ).withCmd ("/bin/echo" , snippet )
145+ CreateContainerResponse container = dockerClient .createContainerCmd ("busybox" )
146+ .withCmd ("/bin/echo" , snippet )
110147 .exec ();
111148
112149 LOG .info ("Created container: {}" , container .toString ());
113150 assertThat (container .getId (), not (isEmptyString ()));
114151
115152 dockerClient .startContainerCmd (container .getId ()).exec ();
116153
117- int exitCode = dockerClient .waitContainerCmd (container .getId ()).exec (new WaitContainerResultCallback ())
154+ int exitCode = dockerClient .waitContainerCmd (container .getId ())
155+ .exec (new WaitContainerResultCallback ())
118156 .awaitStatusCode ();
119157
120158 assertThat (exitCode , equalTo (0 ));
121159
122160 LogContainerTestCallback loggingCallback = new LogContainerTestCallback ();
123161
124- dockerClient .logContainerCmd (container .getId ()).withStdErr (true ).withStdOut (true ).exec (loggingCallback );
162+ dockerClient .logContainerCmd (container .getId ())
163+ .withStdErr (true )
164+ .withStdOut (true )
165+ .exec (loggingCallback );
125166
126167 loggingCallback .close ();
127168
128169 loggingCallback = new LogContainerTestCallback ();
129170
130- dockerClient .logContainerCmd (container .getId ()).withStdErr (true ).withStdOut (true ).exec (loggingCallback );
171+ dockerClient .logContainerCmd (container .getId ())
172+ .withStdErr (true )
173+ .withStdOut (true )
174+ .exec (loggingCallback );
131175
132176 loggingCallback .close ();
133177
134178 loggingCallback = new LogContainerTestCallback ();
135179
136- dockerClient .logContainerCmd (container .getId ()).withStdErr (true ).withStdOut (true ).exec (loggingCallback );
180+ dockerClient .logContainerCmd (container .getId ())
181+ .withStdErr (true )
182+ .withStdOut (true )
183+ .exec (loggingCallback );
137184
138185 loggingCallback .awaitCompletion ();
139186
@@ -144,7 +191,8 @@ public void asyncMultipleLogContainer() throws Exception {
144191 public void asyncLogContainerWithSince () throws Exception {
145192 String snippet = "hello world" ;
146193
147- CreateContainerResponse container = dockerClient .createContainerCmd ("busybox" ).withCmd ("/bin/echo" , snippet )
194+ CreateContainerResponse container = dockerClient .createContainerCmd ("busybox" )
195+ .withCmd ("/bin/echo" , snippet )
148196 .exec ();
149197
150198 LOG .info ("Created container: {}" , container .toString ());
@@ -154,19 +202,22 @@ public void asyncLogContainerWithSince() throws Exception {
154202
155203 dockerClient .startContainerCmd (container .getId ()).exec ();
156204
157- int exitCode = dockerClient .waitContainerCmd (container .getId ()).exec (new WaitContainerResultCallback ())
205+ int exitCode = dockerClient .waitContainerCmd (container .getId ())
206+ .exec (new WaitContainerResultCallback ())
158207 .awaitStatusCode ();
159208
160209 assertThat (exitCode , equalTo (0 ));
161210
162211 LogContainerTestCallback loggingCallback = new LogContainerTestCallback ();
163212
164- dockerClient .logContainerCmd (container .getId ()).withStdErr (true ).withStdOut (true ).withSince (timestamp )
213+ dockerClient .logContainerCmd (container .getId ())
214+ .withStdErr (true )
215+ .withStdOut (true )
216+ .withSince (timestamp )
165217 .exec (loggingCallback );
166218
167219 loggingCallback .awaitCompletion ();
168220
169221 assertThat (loggingCallback .toString (), containsString (snippet ));
170222 }
171-
172223}
0 commit comments