7
7
import org .junit .AfterClass ;
8
8
import org .junit .BeforeClass ;
9
9
import org .junit .Test ;
10
+ import org .junit .runner .Description ;
11
+ import org .junit .runners .model .Statement ;
12
+ import org .testcontainers .DockerClientFactory ;
10
13
import org .testcontainers .TestImages ;
11
14
import org .testcontainers .Testcontainers ;
15
+ import org .testcontainers .utility .TestcontainersConfiguration ;
12
16
17
+ import java .io .IOException ;
13
18
import java .io .OutputStream ;
14
19
import java .net .InetSocketAddress ;
20
+ import java .util .List ;
21
+ import java .util .UUID ;
15
22
16
23
import static org .assertj .core .api .Assertions .assertThat ;
24
+ import static org .assertj .core .api .Assumptions .assumeThat ;
17
25
18
26
public class ExposedHostTest {
19
27
@@ -33,7 +41,6 @@ public static void setUpClass() throws Exception {
33
41
}
34
42
}
35
43
);
36
-
37
44
server .start ();
38
45
}
39
46
@@ -82,6 +89,61 @@ public void testExposedHostPortOnFixedInternalPorts() {
82
89
assertResponse (new GenericContainer <>(tinyContainerDef ()), 81 );
83
90
}
84
91
92
+ @ Test
93
+ public void testExposedHostWithReusableContainerAndFixedNetworkName () throws IOException , InterruptedException {
94
+ assumeThat (TestcontainersConfiguration .getInstance ().environmentSupportsReuse ()).isTrue ();
95
+ Network network = createReusableNetwork (UUID .randomUUID ());
96
+ Testcontainers .exposeHostPorts (server .getAddress ().getPort ());
97
+
98
+ GenericContainer <?> container = new GenericContainer <>(tinyContainerDef ()).withReuse (true ).withNetwork (network );
99
+ container .start ();
100
+
101
+ assertHttpResponseFromHost (container , server .getAddress ().getPort ());
102
+
103
+ PortForwardingContainer .INSTANCE .reset ();
104
+ Testcontainers .exposeHostPorts (server .getAddress ().getPort ());
105
+
106
+ GenericContainer <?> reusedContainer = new GenericContainer <>(tinyContainerDef ())
107
+ .withReuse (true )
108
+ .withNetwork (network );
109
+ reusedContainer .start ();
110
+
111
+ assertThat (reusedContainer .getContainerId ()).isEqualTo (container .getContainerId ());
112
+ assertHttpResponseFromHost (reusedContainer , server .getAddress ().getPort ());
113
+
114
+ container .stop ();
115
+ reusedContainer .stop ();
116
+ DockerClientFactory .lazyClient ().removeNetworkCmd (network .getId ()).exec ();
117
+ }
118
+
119
+ @ Test
120
+ public void testExposedHostOnFixedInternalPortsWithReusableContainerAndFixedNetworkName ()
121
+ throws IOException , InterruptedException {
122
+ assumeThat (TestcontainersConfiguration .getInstance ().environmentSupportsReuse ()).isTrue ();
123
+ Network network = createReusableNetwork (UUID .randomUUID ());
124
+ Testcontainers .exposeHostPorts (ImmutableMap .of (server .getAddress ().getPort (), 1234 ));
125
+
126
+ GenericContainer <?> container = new GenericContainer <>(tinyContainerDef ()).withReuse (true ).withNetwork (network );
127
+ container .start ();
128
+
129
+ assertHttpResponseFromHost (container , 1234 );
130
+
131
+ PortForwardingContainer .INSTANCE .reset ();
132
+ Testcontainers .exposeHostPorts (ImmutableMap .of (server .getAddress ().getPort (), 1234 ));
133
+
134
+ GenericContainer <?> reusedContainer = new GenericContainer <>(tinyContainerDef ())
135
+ .withReuse (true )
136
+ .withNetwork (network );
137
+ reusedContainer .start ();
138
+
139
+ assertThat (reusedContainer .getContainerId ()).isEqualTo (container .getContainerId ());
140
+ assertHttpResponseFromHost (reusedContainer , 1234 );
141
+
142
+ container .stop ();
143
+ reusedContainer .stop ();
144
+ DockerClientFactory .lazyClient ().removeNetworkCmd (network .getId ()).exec ();
145
+ }
146
+
85
147
@ SneakyThrows
86
148
protected void assertResponse (GenericContainer <?> container , int port ) {
87
149
try {
@@ -108,4 +170,40 @@ private static class TinyContainerDef extends ContainerDef {
108
170
setCommand ("top" );
109
171
}
110
172
}
173
+
174
+ private void assertHttpResponseFromHost (GenericContainer <?> container , int port )
175
+ throws IOException , InterruptedException {
176
+ String httpResponseFromHost = container
177
+ .execInContainer ("wget" , "-O" , "-" , "http://host.testcontainers.internal:" + port )
178
+ .getStdout ();
179
+ assertThat (httpResponseFromHost ).isEqualTo ("Hello World!" );
180
+ }
181
+
182
+ private static Network createReusableNetwork (UUID name ) {
183
+ String networkName = name .toString ();
184
+ Network network = new Network () {
185
+ @ Override
186
+ public String getId () {
187
+ return networkName ;
188
+ }
189
+
190
+ @ Override
191
+ public void close () {}
192
+
193
+ @ Override
194
+ public Statement apply (Statement base , Description description ) {
195
+ return null ;
196
+ }
197
+ };
198
+
199
+ List <com .github .dockerjava .api .model .Network > networks = DockerClientFactory
200
+ .lazyClient ()
201
+ .listNetworksCmd ()
202
+ .withNameFilter (networkName )
203
+ .exec ();
204
+ if (networks .isEmpty ()) {
205
+ Network .builder ().createNetworkCmdModifier (cmd -> cmd .withName (networkName )).build ().getId ();
206
+ }
207
+ return network ;
208
+ }
111
209
}
0 commit comments