|
21 | 21 | import org.rnorth.ducttape.unreliables.Unreliables; |
22 | 22 | import org.testcontainers.DockerClientFactory; |
23 | 23 | import org.testcontainers.TestImages; |
| 24 | +import org.testcontainers.containers.output.ToStringConsumer; |
24 | 25 | import org.testcontainers.containers.startupcheck.StartupCheckStrategy; |
25 | 26 | import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy; |
26 | 27 | import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy; |
@@ -336,6 +337,46 @@ private static Optional<String> reportLeakedContainers() { |
336 | 337 | ); |
337 | 338 | } |
338 | 339 |
|
| 340 | + @Test |
| 341 | + public void testPostHookExecutionOnStop() { |
| 342 | + String scriptContent = |
| 343 | + "#!/bin/bash\n" + |
| 344 | + "function on_shutdown() {\n" + |
| 345 | + " echo 'HOOK_TRIGGERED'\n" + |
| 346 | + "}\n" + |
| 347 | + "trap on_shutdown SIGTERM SIGINT EXIT\n" + |
| 348 | + "echo 'CONTAINER_STARTED'\n" + |
| 349 | + "while true; do sleep 1; done\n"; |
| 350 | + |
| 351 | + ToStringConsumer logConsumer = new ToStringConsumer(); |
| 352 | + |
| 353 | + try ( |
| 354 | + GenericContainer<?> container = new GenericContainer<>( |
| 355 | + new ImageFromDockerfile() |
| 356 | + .withFileFromString("entrypoint.sh", scriptContent) |
| 357 | + .withDockerfileFromBuilder(builder -> { |
| 358 | + builder |
| 359 | + .from("alpine:3.18") |
| 360 | + .run("apk add --no-cache tini bash") |
| 361 | + .copy("entrypoint.sh", "/entrypoint.sh") |
| 362 | + .run("chmod +x /entrypoint.sh") |
| 363 | + .entryPoint("/sbin/tini", "--", "/entrypoint.sh") |
| 364 | + .build(); |
| 365 | + }) |
| 366 | + ) |
| 367 | + .withLogConsumer(logConsumer) |
| 368 | + .waitingFor(Wait.forLogMessage(".*CONTAINER_STARTED.*", 1)) |
| 369 | + ) { |
| 370 | + container.start(); |
| 371 | + |
| 372 | + container.stop(); |
| 373 | + |
| 374 | + assertThat(logConsumer.toUtf8String()) |
| 375 | + .as("The shutdown hook defined in 'trap' should be executed upon container stop") |
| 376 | + .contains("HOOK_TRIGGERED"); |
| 377 | + } |
| 378 | + } |
| 379 | + |
339 | 380 | static class NoopStartupCheckStrategy extends StartupCheckStrategy { |
340 | 381 |
|
341 | 382 | @Override |
|
0 commit comments