|
5 | 5 | import com.github.dockerjava.api.DockerClient;
|
6 | 6 | import com.github.dockerjava.api.command.InspectContainerResponse;
|
7 | 7 | import com.github.dockerjava.api.command.InspectContainerResponse.ContainerState;
|
| 8 | +import com.github.dockerjava.api.model.Container; |
8 | 9 | import com.github.dockerjava.api.model.ExposedPort;
|
9 | 10 | import com.github.dockerjava.api.model.Info;
|
10 | 11 | import com.github.dockerjava.api.model.Ports;
|
| 12 | +import com.google.common.base.MoreObjects; |
| 13 | +import com.google.common.collect.ImmutableList; |
11 | 14 | import lombok.RequiredArgsConstructor;
|
12 | 15 | import lombok.SneakyThrows;
|
13 | 16 | import lombok.experimental.FieldDefaults;
|
|
28 | 31 | import org.testcontainers.utility.DockerImageName;
|
29 | 32 | import org.testcontainers.utility.MountableFile;
|
30 | 33 |
|
| 34 | +import java.time.Duration; |
31 | 35 | import java.util.Arrays;
|
| 36 | +import java.util.Collections; |
32 | 37 | import java.util.List;
|
33 | 38 | import java.util.Map;
|
| 39 | +import java.util.Optional; |
34 | 40 | import java.util.concurrent.TimeUnit;
|
35 | 41 | import java.util.function.Predicate;
|
36 | 42 | import java.util.stream.Collectors;
|
|
42 | 48 |
|
43 | 49 | public class GenericContainerTest {
|
44 | 50 |
|
| 51 | + @Test |
| 52 | + public void testStartupTimeoutWithAttemptsNotLeakingContainers() { |
| 53 | + try ( |
| 54 | + GenericContainer<?> container = new GenericContainer<>(TestImages.TINY_IMAGE) |
| 55 | + .withStartupAttempts(3) |
| 56 | + .waitingFor( |
| 57 | + Wait.forLogMessage("this text does not exist in logs", 1).withStartupTimeout(Duration.ofMillis(1)) |
| 58 | + ) |
| 59 | + .withCommand("tail", "-f", "/dev/null"); |
| 60 | + ) { |
| 61 | + assertThatThrownBy(container::start).hasStackTraceContaining("Retry limit hit with exception"); |
| 62 | + } |
| 63 | + assertThat(reportLeakedContainers()).isEmpty(); |
| 64 | + } |
| 65 | + |
45 | 66 | @Test
|
46 | 67 | public void shouldReportOOMAfterWait() {
|
47 | 68 | Info info = DockerClientFactory.instance().client().infoCmd().exec();
|
@@ -273,6 +294,46 @@ public void shouldRespectWaitStrategy() {
|
273 | 294 | }
|
274 | 295 | }
|
275 | 296 |
|
| 297 | + private static Optional<String> reportLeakedContainers() { |
| 298 | + @SuppressWarnings("resource") // Throws when close is attempted, as this is a global instance. |
| 299 | + DockerClient dockerClient = DockerClientFactory.lazyClient(); |
| 300 | + |
| 301 | + List<Container> containers = dockerClient |
| 302 | + .listContainersCmd() |
| 303 | + .withLabelFilter( |
| 304 | + Collections.singletonMap( |
| 305 | + DockerClientFactory.TESTCONTAINERS_SESSION_ID_LABEL, |
| 306 | + DockerClientFactory.SESSION_ID |
| 307 | + ) |
| 308 | + ) |
| 309 | + // ignore status "exited" - for example, failed containers after using `withStartupAttempts()` |
| 310 | + .withStatusFilter(Arrays.asList("created", "restarting", "running", "paused")) |
| 311 | + .exec() |
| 312 | + .stream() |
| 313 | + .collect(ImmutableList.toImmutableList()); |
| 314 | + |
| 315 | + if (containers.isEmpty()) { |
| 316 | + return Optional.empty(); |
| 317 | + } |
| 318 | + |
| 319 | + return Optional.of( |
| 320 | + String.format( |
| 321 | + "Leaked containers: %s", |
| 322 | + containers |
| 323 | + .stream() |
| 324 | + .map(container -> { |
| 325 | + return MoreObjects |
| 326 | + .toStringHelper("container") |
| 327 | + .add("id", container.getId()) |
| 328 | + .add("image", container.getImage()) |
| 329 | + .add("imageId", container.getImageId()) |
| 330 | + .toString(); |
| 331 | + }) |
| 332 | + .collect(Collectors.joining(", ", "[", "]")) |
| 333 | + ) |
| 334 | + ); |
| 335 | + } |
| 336 | + |
276 | 337 | static class NoopStartupCheckStrategy extends StartupCheckStrategy {
|
277 | 338 |
|
278 | 339 | @Override
|
|
0 commit comments