11package com .github .dockerjava .transport ;
22
3+ import com .github .dockerjava .api .DockerClient ;
4+ import com .github .dockerjava .api .async .ResultCallback ;
5+ import com .github .dockerjava .api .model .Frame ;
6+ import com .github .dockerjava .core .DefaultDockerClientConfig ;
7+ import com .github .dockerjava .core .DockerClientImpl ;
38import com .github .dockerjava .transport .DockerHttpClient .Request ;
49import com .github .dockerjava .transport .DockerHttpClient .Request .Method ;
510import com .github .dockerjava .transport .DockerHttpClient .Response ;
611import okhttp3 .mockwebserver .MockResponse ;
712import okhttp3 .mockwebserver .MockWebServer ;
813import org .junit .Test ;
14+ import org .testcontainers .DockerClientFactory ;
15+ import org .testcontainers .containers .GenericContainer ;
16+ import org .testcontainers .dockerclient .TransportConfig ;
917
18+ import java .io .PipedInputStream ;
19+ import java .io .PipedOutputStream ;
1020import java .net .URI ;
1121
22+ import static java .util .concurrent .TimeUnit .SECONDS ;
1223import static org .assertj .core .api .Assertions .assertThat ;
1324
1425public abstract class DockerHttpClientTCK {
1526
1627 protected abstract DockerHttpClient createDockerHttpClient (URI dockerHost , SSLConfig sslConfig );
1728
29+ @ Test
30+ public void testHijacking () throws Exception {
31+ try (
32+ DockerClient client = createDockerClient ();
33+
34+ PipedOutputStream out = new PipedOutputStream ();
35+ PipedInputStream in = new PipedInputStream (out );
36+
37+ AttachContainerTestCallback callback = new AttachContainerTestCallback ();
38+
39+ AttacheableContainer container = new AttacheableContainer () {
40+ @ Override
41+ protected void containerIsCreated (String containerId ) {
42+ client .attachContainerCmd (containerId )
43+ .withStdOut (true )
44+ .withFollowStream (true )
45+ .withStdIn (in )
46+ .exec (callback );
47+ }
48+ };
49+ ) {
50+ container .start ();
51+ assertThat (callback .awaitStarted (5 , SECONDS )).as ("attached" ).isTrue ();
52+
53+ String snippet = "hello world" ;
54+ out .write ((snippet + "\n " ).getBytes ());
55+ out .flush ();
56+
57+ assertThat (callback .awaitCompletion (15 , SECONDS )).as ("completed" ).isTrue ();
58+ assertThat (callback .toString ()).contains ("STDOUT: " + snippet );
59+ }
60+ }
61+
1862 /**
1963 * Test that docker-java supports path in DOCKER_HOST
2064 *
@@ -36,10 +80,27 @@ public final void testPath() throws Exception {
3680 }
3781 }
3882
83+ private DockerHttpClient createDockerHttpClient () {
84+ // Use Testcontainers to detect Docker environment
85+ TransportConfig transportConfig = DockerClientFactory .instance ().getTransportConfig ();
86+ return createDockerHttpClient (transportConfig .getDockerHost (), transportConfig .getSslConfig ());
87+ }
88+
3989 private DockerHttpClient createDockerHttpClient (String dockerHost ) {
4090 return createDockerHttpClient (URI .create (dockerHost ), null );
4191 }
4292
93+ private DockerClient createDockerClient () {
94+ return createDockerClient (createDockerHttpClient ());
95+ }
96+
97+ private DockerClient createDockerClient (DockerHttpClient dockerHttpClient ) {
98+ return DockerClientImpl .getInstance (
99+ DefaultDockerClientConfig .createDefaultConfigBuilder ().build (),
100+ dockerHttpClient
101+ );
102+ }
103+
43104 private void ping (DockerHttpClient client ) {
44105 Request pingRequest = Request .builder ()
45106 .method (Method .GET )
@@ -52,4 +113,36 @@ private void ping(DockerHttpClient client) {
52113 .isEqualTo (200 );
53114 }
54115 }
116+
117+ private static class AttachContainerTestCallback extends ResultCallback .Adapter <Frame > {
118+
119+ private final StringBuffer log = new StringBuffer ();
120+
121+ @ Override
122+ public void onNext (Frame item ) {
123+ log .append (item .toString ());
124+ super .onNext (item );
125+ }
126+
127+ @ Override
128+ public String toString () {
129+ return log .toString ();
130+ }
131+ }
132+
133+ private static class AttacheableContainer extends GenericContainer <AttacheableContainer > {
134+
135+ private AttacheableContainer () {
136+ super ("busybox:1.35.0" );
137+
138+ withCommand ("/bin/sh" , "-c" , "read line && echo $line" );
139+ withCreateContainerCmdModifier (it -> {
140+ it .withTty (false );
141+ it .withAttachStdin (true );
142+ it .withAttachStdout (true );
143+ it .withAttachStderr (true );
144+ it .withStdinOpen (true );
145+ });
146+ }
147+ }
55148}
0 commit comments