|
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;
|
@@ -273,6 +279,63 @@ public void shouldRespectWaitStrategy() {
|
273 | 279 | }
|
274 | 280 | }
|
275 | 281 |
|
| 282 | + @Test |
| 283 | + public void testStartupAttemptsDoesNotLeaveContainersRunningWhenWrongWaitStrategyIsUsed() { |
| 284 | + try ( |
| 285 | + GenericContainer<?> container = new GenericContainer<>(TestImages.TINY_IMAGE) |
| 286 | + .withLabel("waitstrategy", "wrong") |
| 287 | + .withStartupAttempts(3) |
| 288 | + .waitingFor( |
| 289 | + Wait.forLogMessage("this text does not exist in logs", 1).withStartupTimeout(Duration.ofMillis(1)) |
| 290 | + ) |
| 291 | + .withCommand("tail", "-f", "/dev/null"); |
| 292 | + ) { |
| 293 | + assertThatThrownBy(container::start).hasStackTraceContaining("Retry limit hit with exception"); |
| 294 | + } |
| 295 | + assertThat(reportLeakedContainers()).isEmpty(); |
| 296 | + } |
| 297 | + |
| 298 | + private static Optional<String> reportLeakedContainers() { |
| 299 | + @SuppressWarnings("resource") // Throws when close is attempted, as this is a global instance. |
| 300 | + DockerClient dockerClient = DockerClientFactory.lazyClient(); |
| 301 | + |
| 302 | + List<Container> containers = dockerClient |
| 303 | + .listContainersCmd() |
| 304 | + .withAncestorFilter(Collections.singletonList("alpine:3.17")) |
| 305 | + .withLabelFilter( |
| 306 | + Arrays.asList( |
| 307 | + DockerClientFactory.TESTCONTAINERS_SESSION_ID_LABEL + "=" + DockerClientFactory.SESSION_ID, |
| 308 | + "waitstrategy=wrong" |
| 309 | + ) |
| 310 | + ) |
| 311 | + // ignore status "exited" - for example, failed containers after using `withStartupAttempts()` |
| 312 | + .withStatusFilter(Arrays.asList("created", "restarting", "running", "paused")) |
| 313 | + .exec() |
| 314 | + .stream() |
| 315 | + .collect(ImmutableList.toImmutableList()); |
| 316 | + |
| 317 | + if (containers.isEmpty()) { |
| 318 | + return Optional.empty(); |
| 319 | + } |
| 320 | + |
| 321 | + return Optional.of( |
| 322 | + String.format( |
| 323 | + "Leaked containers: %s", |
| 324 | + containers |
| 325 | + .stream() |
| 326 | + .map(container -> { |
| 327 | + return MoreObjects |
| 328 | + .toStringHelper("container") |
| 329 | + .add("id", container.getId()) |
| 330 | + .add("image", container.getImage()) |
| 331 | + .add("imageId", container.getImageId()) |
| 332 | + .toString(); |
| 333 | + }) |
| 334 | + .collect(Collectors.joining(", ", "[", "]")) |
| 335 | + ) |
| 336 | + ); |
| 337 | + } |
| 338 | + |
276 | 339 | static class NoopStartupCheckStrategy extends StartupCheckStrategy {
|
277 | 340 |
|
278 | 341 | @Override
|
|
0 commit comments