|
14 | 14 | import org.apache.commons.io.IOUtils; |
15 | 15 | import org.hamcrest.BaseMatcher; |
16 | 16 | import org.hamcrest.Description; |
| 17 | +import org.rnorth.ducttape.unreliables.Unreliables; |
17 | 18 | import org.rnorth.visibleassertions.VisibleAssertions; |
18 | 19 | import org.testcontainers.dockerclient.*; |
19 | 20 | import org.testcontainers.utility.ComparableVersion; |
|
29 | 30 | import java.util.List; |
30 | 31 | import java.util.Optional; |
31 | 32 | import java.util.ServiceLoader; |
| 33 | +import java.util.concurrent.TimeUnit; |
32 | 34 | import java.util.function.BiFunction; |
33 | 35 | import java.util.function.Consumer; |
34 | 36 |
|
@@ -177,16 +179,18 @@ private void checkMountableFile(DockerClient dockerClient, String id) { |
177 | 179 | } |
178 | 180 |
|
179 | 181 | private void checkExposedPort(String hostIpAddress, DockerClient dockerClient, String id) { |
180 | | - InspectContainerResponse inspectedContainer = dockerClient.inspectContainerCmd(id).exec(); |
| 182 | + String response = Unreliables.retryUntilSuccess(3, TimeUnit.SECONDS, () -> { |
| 183 | + InspectContainerResponse inspectedContainer = dockerClient.inspectContainerCmd(id).exec(); |
181 | 184 |
|
182 | | - String portSpec = inspectedContainer.getNetworkSettings().getPorts().getBindings().values().iterator().next()[0].getHostPortSpec(); |
| 185 | + String portSpec = inspectedContainer.getNetworkSettings().getPorts().getBindings().values().iterator().next()[0].getHostPortSpec(); |
| 186 | + |
| 187 | + try (Socket socket = new Socket(hostIpAddress, Integer.parseInt(portSpec))) { |
| 188 | + return IOUtils.toString(socket.getInputStream(), Charset.defaultCharset()); |
| 189 | + } catch (IOException e) { |
| 190 | + return e.getMessage(); |
| 191 | + } |
| 192 | + }); |
183 | 193 |
|
184 | | - String response; |
185 | | - try (Socket socket = new Socket(hostIpAddress, Integer.parseInt(portSpec))) { |
186 | | - response = IOUtils.toString(socket.getInputStream(), Charset.defaultCharset()); |
187 | | - } catch (IOException e) { |
188 | | - response = e.getMessage(); |
189 | | - } |
190 | 194 | VisibleAssertions.assertEquals("A port exposed by a docker container should be accessible", "hello", response); |
191 | 195 | } |
192 | 196 |
|
|
0 commit comments