|
16 | 16 | import java.io.IOException; |
17 | 17 | import java.io.OutputStream; |
18 | 18 | import java.net.InetSocketAddress; |
| 19 | +import java.util.List; |
19 | 20 | import java.util.UUID; |
20 | 21 |
|
21 | 22 | import static org.assertj.core.api.Assertions.assertThat; |
@@ -94,59 +95,49 @@ public void testExposedHostPortOnFixedInternalPorts() { |
94 | 95 | public void testExposedHostWithReusableContainerAndFixedNetworkName() throws IOException, InterruptedException { |
95 | 96 | Testcontainers.exposeHostPorts(server.getAddress().getPort()); |
96 | 97 |
|
97 | | - try ( |
98 | | - GenericContainer<?> container = new GenericContainer<>(tinyContainerDef()) |
99 | | - .withReuse(true) |
100 | | - .withNetwork(network) |
101 | | - ) { |
102 | | - container.start(); |
| 98 | + GenericContainer<?> container = new GenericContainer<>(tinyContainerDef()).withReuse(true).withNetwork(network); |
| 99 | + container.start(); |
103 | 100 |
|
104 | | - assertHttpResponseFromHost(container, server.getAddress().getPort()); |
| 101 | + assertHttpResponseFromHost(container, server.getAddress().getPort()); |
105 | 102 |
|
106 | | - PortForwardingContainer.INSTANCE.reset(); |
107 | | - Testcontainers.exposeHostPorts(server.getAddress().getPort()); |
| 103 | + PortForwardingContainer.INSTANCE.reset(); |
| 104 | + Testcontainers.exposeHostPorts(server.getAddress().getPort()); |
108 | 105 |
|
109 | | - try ( |
110 | | - GenericContainer<?> reusedContainer = new GenericContainer<>(tinyContainerDef()) |
111 | | - .withReuse(true) |
112 | | - .withNetwork(network) |
113 | | - ) { |
114 | | - reusedContainer.start(); |
| 106 | + GenericContainer<?> reusedContainer = new GenericContainer<>(tinyContainerDef()) |
| 107 | + .withReuse(true) |
| 108 | + .withNetwork(network); |
| 109 | + reusedContainer.start(); |
115 | 110 |
|
116 | | - assertThat(reusedContainer.getContainerId()).isEqualTo(container.getContainerId()); |
117 | | - assertHttpResponseFromHost(reusedContainer, server.getAddress().getPort()); |
118 | | - } |
119 | | - } |
| 111 | + assertThat(reusedContainer.getContainerId()).isEqualTo(container.getContainerId()); |
| 112 | + assertHttpResponseFromHost(reusedContainer, server.getAddress().getPort()); |
| 113 | + |
| 114 | + container.stop(); |
| 115 | + reusedContainer.stop(); |
120 | 116 | } |
121 | 117 |
|
122 | 118 | @Test |
123 | 119 | public void testExposedHostOnFixedInternalPortsWithReusableContainerAndFixedNetworkName() |
124 | 120 | throws IOException, InterruptedException { |
125 | 121 | Testcontainers.exposeHostPorts(ImmutableMap.of(server.getAddress().getPort(), 1234)); |
126 | 122 |
|
127 | | - try ( |
128 | | - GenericContainer<?> container = new GenericContainer<>(tinyContainerDef()) |
129 | | - .withReuse(true) |
130 | | - .withNetwork(network) |
131 | | - ) { |
132 | | - container.start(); |
| 123 | + GenericContainer<?> container = new GenericContainer<>(tinyContainerDef()).withReuse(true).withNetwork(network); |
| 124 | + container.start(); |
133 | 125 |
|
134 | | - assertHttpResponseFromHost(container, 1234); |
| 126 | + assertHttpResponseFromHost(container, 1234); |
135 | 127 |
|
136 | | - PortForwardingContainer.INSTANCE.reset(); |
137 | | - Testcontainers.exposeHostPorts(ImmutableMap.of(server.getAddress().getPort(), 1234)); |
| 128 | + PortForwardingContainer.INSTANCE.reset(); |
| 129 | + Testcontainers.exposeHostPorts(ImmutableMap.of(server.getAddress().getPort(), 1234)); |
138 | 130 |
|
139 | | - try ( |
140 | | - GenericContainer<?> reusedContainer = new GenericContainer<>(tinyContainerDef()) |
141 | | - .withReuse(true) |
142 | | - .withNetwork(network) |
143 | | - ) { |
144 | | - reusedContainer.start(); |
| 131 | + GenericContainer<?> reusedContainer = new GenericContainer<>(tinyContainerDef()) |
| 132 | + .withReuse(true) |
| 133 | + .withNetwork(network); |
| 134 | + reusedContainer.start(); |
145 | 135 |
|
146 | | - assertThat(reusedContainer.getContainerId()).isEqualTo(container.getContainerId()); |
147 | | - assertHttpResponseFromHost(reusedContainer, 1234); |
148 | | - } |
149 | | - } |
| 136 | + assertThat(reusedContainer.getContainerId()).isEqualTo(container.getContainerId()); |
| 137 | + assertHttpResponseFromHost(reusedContainer, 1234); |
| 138 | + |
| 139 | + container.stop(); |
| 140 | + reusedContainer.stop(); |
150 | 141 | } |
151 | 142 |
|
152 | 143 | @SneakyThrows |
@@ -185,47 +176,30 @@ private void assertHttpResponseFromHost(GenericContainer<?> container, int port) |
185 | 176 | } |
186 | 177 |
|
187 | 178 | private static Network createReusableNetwork(UUID name) { |
188 | | - String id = DockerClientFactory |
189 | | - .instance() |
190 | | - .client() |
191 | | - .listNetworksCmd() |
192 | | - .exec() |
193 | | - .stream() |
194 | | - .filter(network -> { |
195 | | - return ( |
196 | | - network.getName().equals(name.toString()) && |
197 | | - network.getLabels().equals(DockerClientFactory.DEFAULT_LABELS) |
198 | | - ); |
199 | | - }) |
200 | | - .map(com.github.dockerjava.api.model.Network::getId) |
201 | | - .findFirst() |
202 | | - .orElseGet(() -> { |
203 | | - return DockerClientFactory |
204 | | - .instance() |
205 | | - .client() |
206 | | - .createNetworkCmd() |
207 | | - .withName(name.toString()) |
208 | | - .withCheckDuplicate(true) |
209 | | - .withLabels(DockerClientFactory.DEFAULT_LABELS) |
210 | | - .exec() |
211 | | - .getId(); |
212 | | - }); |
213 | | - |
214 | | - return new Network() { |
| 179 | + String networkName = name.toString(); |
| 180 | + Network network = new Network() { |
215 | 181 | @Override |
216 | | - public Statement apply(Statement base, Description description) { |
217 | | - return base; |
| 182 | + public String getId() { |
| 183 | + return networkName; |
218 | 184 | } |
219 | 185 |
|
220 | 186 | @Override |
221 | | - public String getId() { |
222 | | - return id; |
223 | | - } |
| 187 | + public void close() {} |
224 | 188 |
|
225 | 189 | @Override |
226 | | - public void close() { |
227 | | - // never close |
| 190 | + public Statement apply(Statement base, Description description) { |
| 191 | + return null; |
228 | 192 | } |
229 | 193 | }; |
| 194 | + |
| 195 | + List<com.github.dockerjava.api.model.Network> networks = DockerClientFactory |
| 196 | + .lazyClient() |
| 197 | + .listNetworksCmd() |
| 198 | + .withNameFilter(networkName) |
| 199 | + .exec(); |
| 200 | + if (networks.isEmpty()) { |
| 201 | + Network.builder().createNetworkCmdModifier(cmd -> cmd.withName(networkName)).build().getId(); |
| 202 | + } |
| 203 | + return network; |
230 | 204 | } |
231 | 205 | } |
0 commit comments