1+ package org .testcontainers .containers ;
2+
3+ import org .junit .jupiter .api .Test ;
4+ import org .testcontainers .containers .output .ToStringConsumer ;
5+ import org .testcontainers .images .builder .ImageFromDockerfile ;
6+
7+ import java .time .Duration ;
8+
9+ import static org .assertj .core .api .Assertions .assertThat ;
10+
11+ public class Issue10365Test {
12+
13+ @ Test
14+ public void testPostHookExecutionOnStop () {
15+ String scriptContent =
16+ "#!/bin/bash\n " +
17+ "function on_shutdown() {\n " +
18+ " echo 'HOOK_TRIGGERED'\n " +
19+ "}\n " +
20+ "trap on_shutdown SIGTERM SIGINT EXIT\n " +
21+ "echo 'CONTAINER_STARTED'\n " +
22+ "while true; do sleep 1; done\n " ;
23+
24+ try (
25+ GenericContainer <?> container = new GenericContainer <>(
26+ new ImageFromDockerfile ()
27+ .withFileFromString ("entrypoint.sh" , scriptContent )
28+ .withDockerfileFromBuilder (builder ->
29+ builder
30+ .from ("alpine:3.18" )
31+ .run ("apk add --no-cache tini bash" )
32+ .copy ("entrypoint.sh" , "/entrypoint.sh" )
33+ .run ("chmod +x /entrypoint.sh" )
34+ .entryPoint ("/sbin/tini" , "--" , "/entrypoint.sh" )
35+ .build ()
36+ )
37+ )
38+ ) {
39+ ToStringConsumer logConsumer = new ToStringConsumer ();
40+ container .withLogConsumer (logConsumer );
41+
42+ container .withStartupTimeout (Duration .ofSeconds (30 ));
43+
44+ container .start ();
45+
46+ container .stop ();
47+
48+ String logs = logConsumer .toUtf8String ();
49+ System .out .println ("=== CONTAINER LOGS ===\n " + logs + "\n ======================" );
50+
51+ assertThat (logs )
52+ .contains ("HOOK_TRIGGERED" );
53+ }
54+ }
55+ }
0 commit comments