Skip to content

Commit b77e655

Browse files
committed
add tests
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 938c43a commit b77e655

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

cmd/nerdctl/container/container_stop_linux_test.go

Lines changed: 37 additions & 0 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"
@@ -159,3 +160,39 @@ func TestStopCreated(t *testing.T) {
159160

160161
base.Cmd("stop", tID).AssertOK()
161162
}
163+
164+
func TestStopWithLongTimeoutAndSIGKILL(t *testing.T) {
165+
t.Parallel()
166+
base := testutil.NewBase(t)
167+
testContainerName := testutil.Identifier(t)
168+
defer base.Cmd("rm", "-f", testContainerName).Run()
169+
170+
// Start a container that sleeps forever
171+
base.Cmd("run", "-d", "--name", testContainerName, testutil.CommonImage, "sleep", "Inf").AssertOK()
172+
173+
// Stop the container with a 5-second timeout and SIGKILL
174+
start := time.Now()
175+
base.Cmd("stop", "--time=5", "--signal", "SIGKILL", testContainerName).AssertOK()
176+
elapsed := time.Since(start)
177+
178+
// The container should be stopped almost immediately, well before the 5-second timeout
179+
assert.Assert(t, elapsed < 5*time.Second, "Container wasn't stopped immediately with SIGKILL")
180+
}
181+
182+
func TestStopWithTimeout(t *testing.T) {
183+
t.Parallel()
184+
base := testutil.NewBase(t)
185+
testContainerName := testutil.Identifier(t)
186+
defer base.Cmd("rm", "-f", testContainerName).Run()
187+
188+
// Start a container that sleeps forever
189+
base.Cmd("run", "-d", "--name", testContainerName, testutil.CommonImage, "sleep", "Inf").AssertOK()
190+
191+
// Stop the container with a 3-second timeout
192+
start := time.Now()
193+
base.Cmd("stop", "--time=3", testContainerName).AssertOK()
194+
elapsed := time.Since(start)
195+
196+
// The container should get the SIGKILL before the 10s default timeout
197+
assert.Assert(t, elapsed < 10*time.Second, "Container did not respect --timeout flag")
198+
}

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)