@@ -21,6 +21,7 @@ import (
21
21
"io"
22
22
"strings"
23
23
"testing"
24
+ "time"
24
25
25
26
"github.com/coreos/go-iptables/iptables"
26
27
"gotest.tools/v3/assert"
@@ -81,13 +82,20 @@ func TestStopWithStopSignal(t *testing.T) {
81
82
82
83
base .Cmd ("run" , "-d" , "--stop-signal" , "SIGQUIT" , "--name" , testContainerName , testutil .CommonImage , "sh" , "-euxc" , `#!/bin/sh
83
84
set -eu
84
- trap 'quit=1' QUIT
85
+ echo "Script started"
85
86
quit=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"
88
95
sleep 1
89
96
done
90
- echo "signal quit"` ).AssertOK ()
97
+ echo "signal quit"
98
+ sync` ).AssertOK ()
91
99
base .Cmd ("stop" , testContainerName ).AssertOK ()
92
100
base .Cmd ("logs" , "-f" , testContainerName ).AssertOutContains ("signal quit" )
93
101
}
@@ -159,3 +167,39 @@ func TestStopCreated(t *testing.T) {
159
167
160
168
base .Cmd ("stop" , tID ).AssertOK ()
161
169
}
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