@@ -21,6 +21,7 @@ import (
2121 "io"
2222 "strings"
2323 "testing"
24+ "time"
2425
2526 "github.com/coreos/go-iptables/iptables"
2627 "gotest.tools/v3/assert"
@@ -81,13 +82,20 @@ func TestStopWithStopSignal(t *testing.T) {
8182
8283 base .Cmd ("run" , "-d" , "--stop-signal" , "SIGQUIT" , "--name" , testContainerName , testutil .CommonImage , "sh" , "-euxc" , `#!/bin/sh
8384set -eu
84- trap 'quit=1' QUIT
85+ echo "Script started"
8586quit=0
86- while [ $quit -ne 1 ]; do
87- printf 'wait quit'
87+ trap 'echo "SIGQUIT received"; quit=1' QUIT
88+ echo "Trap set"
89+ while true; do
90+ if [ $quit -eq 1 ]; then
91+ echo "Quitting loop"
92+ break
93+ fi
94+ echo "In loop"
8895 sleep 1
8996done
90- echo "signal quit"` ).AssertOK ()
97+ echo "signal quit"
98+ sync` ).AssertOK ()
9199 base .Cmd ("stop" , testContainerName ).AssertOK ()
92100 base .Cmd ("logs" , "-f" , testContainerName ).AssertOutContains ("signal quit" )
93101}
@@ -159,3 +167,39 @@ func TestStopCreated(t *testing.T) {
159167
160168 base .Cmd ("stop" , tID ).AssertOK ()
161169}
170+
171+ func TestStopWithLongTimeoutAndSIGKILL (t * testing.T ) {
172+ t .Parallel ()
173+ base := testutil .NewBase (t )
174+ testContainerName := testutil .Identifier (t )
175+ defer base .Cmd ("rm" , "-f" , testContainerName ).Run ()
176+
177+ // Start a container that sleeps forever
178+ base .Cmd ("run" , "-d" , "--name" , testContainerName , testutil .CommonImage , "sleep" , "Inf" ).AssertOK ()
179+
180+ // Stop the container with a 5-second timeout and SIGKILL
181+ start := time .Now ()
182+ base .Cmd ("stop" , "--time=5" , "--signal" , "SIGKILL" , testContainerName ).AssertOK ()
183+ elapsed := time .Since (start )
184+
185+ // The container should be stopped almost immediately, well before the 5-second timeout
186+ assert .Assert (t , elapsed < 5 * time .Second , "Container wasn't stopped immediately with SIGKILL" )
187+ }
188+
189+ func TestStopWithTimeout (t * testing.T ) {
190+ t .Parallel ()
191+ base := testutil .NewBase (t )
192+ testContainerName := testutil .Identifier (t )
193+ defer base .Cmd ("rm" , "-f" , testContainerName ).Run ()
194+
195+ // Start a container that sleeps forever
196+ base .Cmd ("run" , "-d" , "--name" , testContainerName , testutil .CommonImage , "sleep" , "Inf" ).AssertOK ()
197+
198+ // Stop the container with a 3-second timeout
199+ start := time .Now ()
200+ base .Cmd ("stop" , "--time=3" , testContainerName ).AssertOK ()
201+ elapsed := time .Since (start )
202+
203+ // The container should get the SIGKILL before the 10s default timeout
204+ assert .Assert (t , elapsed < 10 * time .Second , "Container did not respect --timeout flag" )
205+ }
0 commit comments