Skip to content

Commit d3f5d10

Browse files
committed
add tests
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 99210b6 commit d3f5d10

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

cmd/nerdctl/container/container_stop_linux_test.go

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
8384
set -eu
84-
trap 'quit=1' QUIT
85+
echo "Script started"
8586
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"
8895
sleep 1
8996
done
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+
}

pkg/api/types/container_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ type ContainerStopOptions struct {
276276
// If it's nil, the default is 10 seconds.
277277
Timeout *time.Duration
278278

279-
// singal to send to the container, before sending sigkill
279+
// Signal to send to the container, before sending SIGKILL
280280
Signal string
281281
}
282282

0 commit comments

Comments
 (0)