Skip to content

Commit 0325e33

Browse files
committed
Rewrite signal tests
Signed-off-by: apostasie <spam_blackhole@farcloser.world>
1 parent 7d74483 commit 0325e33

3 files changed

Lines changed: 97 additions & 111 deletions

File tree

cmd/nerdctl/container/container_restart_linux_test.go

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package container
1818

1919
import (
2020
"fmt"
21+
"strconv"
2122
"strings"
2223
"testing"
2324
"time"
@@ -26,6 +27,7 @@ import (
2627

2728
"github.com/containerd/nerdctl/v2/pkg/testutil"
2829
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
30+
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
2931
)
3032

3133
func TestRestart(t *testing.T) {
@@ -123,30 +125,38 @@ func TestRestartWithTime(t *testing.T) {
123125
}
124126

125127
func TestRestartWithSignal(t *testing.T) {
126-
t.Parallel()
127-
base := testutil.NewBase(t)
128-
tID := testutil.Identifier(t)
129-
130-
base.Cmd("run", "-d", "--name", tID, testutil.AlpineImage, "sh", "-c", `
131-
trap 'echo "Received SIGUSR1"; exit 0' SIGUSR1
132-
echo "Starting"
133-
while true; do
134-
sleep 1
135-
done
136-
`).AssertOK()
137-
defer base.Cmd("rm", "-f", tID).Run()
138-
139-
base.EnsureContainerStarted(tID)
140-
141-
inspect := base.InspectContainer(tID)
142-
initialPid := inspect.State.Pid
143-
144-
base.Cmd("restart", "--signal", "SIGUSR1", tID).AssertOK()
145-
base.EnsureContainerStarted(tID)
146-
147-
newInspect := base.InspectContainer(tID)
148-
newPid := newInspect.State.Pid
149-
150-
assert.Assert(t, initialPid != newPid, "Container PID should have changed after restart")
151-
128+
testCase := nerdtest.Setup()
129+
130+
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
131+
helpers.Anyhow("rm", "-f", data.Identifier())
132+
}
133+
134+
testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
135+
cmd := nerdtest.RunSigProxyContainer(nerdtest.SigUsr1, false, nil, data, helpers)
136+
// Capture the current pid
137+
data.Set("oldpid", strconv.Itoa(nerdtest.InspectContainer(helpers, data.Identifier()).State.Pid))
138+
// Send the signal
139+
helpers.Ensure("restart", "--signal", "SIGUSR1", data.Identifier())
140+
return cmd
141+
}
142+
143+
testCase.Expected = func(data test.Data, helpers test.Helpers) *test.Expected {
144+
return &test.Expected{
145+
// Check the container did indeed exit
146+
ExitCode: 137,
147+
Output: test.All(
148+
// Check that we saw SIGUSR1 inside the container
149+
test.Contains(nerdtest.SignalCaught),
150+
func(stdout string, info string, t *testing.T) {
151+
// Ensure the container was restarted
152+
nerdtest.EnsureContainerStarted(helpers, data.Identifier())
153+
// Check the new pid is different
154+
newpid := strconv.Itoa(nerdtest.InspectContainer(helpers, data.Identifier()).State.Pid)
155+
assert.Assert(helpers.T(), newpid != data.Get("oldpid"), info)
156+
},
157+
),
158+
}
159+
}
160+
161+
testCase.Run(t)
152162
}

cmd/nerdctl/container/container_run_linux_test.go

Lines changed: 43 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"path/filepath"
2929
"strconv"
3030
"strings"
31-
"syscall"
3231
"testing"
3332
"time"
3433

@@ -328,78 +327,61 @@ func TestRunTTY(t *testing.T) {
328327
assert.Equal(t, 0, res.ExitCode, res)
329328
}
330329

331-
func runSigProxy(t *testing.T, args ...string) (string, bool, bool) {
332-
t.Parallel()
333-
base := testutil.NewBase(t)
334-
testContainerName := testutil.Identifier(t)
335-
defer base.Cmd("rm", "-f", testContainerName).Run()
336-
337-
fullArgs := []string{"run"}
338-
fullArgs = append(fullArgs, args...)
339-
fullArgs = append(fullArgs,
340-
"--name",
341-
testContainerName,
342-
testutil.CommonImage,
343-
"sh",
344-
"-c",
345-
testutil.SigProxyTestScript,
346-
)
330+
func TestRunSigProxy(t *testing.T) {
331+
testCase := nerdtest.Setup()
347332

348-
result := base.Cmd(fullArgs...).Start()
349-
process := result.Cmd.Process
333+
testCase.SubTests = []*test.Case{
334+
{
335+
Description: "SigProxyDefault",
350336

351-
// Waits until we reach the trap command in the shell script, then sends SIGINT.
352-
time.Sleep(3 * time.Second)
353-
syscall.Kill(process.Pid, syscall.SIGINT)
337+
Cleanup: func(data test.Data, helpers test.Helpers) {
338+
helpers.Anyhow("rm", "-f", data.Identifier())
339+
},
354340

355-
// Waits until SIGINT is sent and responded to, then kills process to avoid timeout
356-
time.Sleep(3 * time.Second)
357-
process.Kill()
341+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
342+
cmd := nerdtest.RunSigProxyContainer(os.Interrupt, true, nil, data, helpers)
343+
err := cmd.Signal(os.Interrupt)
344+
assert.NilError(helpers.T(), err)
345+
return cmd
346+
},
358347

359-
sigIntRecieved := strings.Contains(result.Stdout(), testutil.SigProxyTrueOut)
360-
timedOut := strings.Contains(result.Stdout(), testutil.SigProxyTimeoutMsg)
348+
Expected: test.Expects(0, nil, test.Contains(nerdtest.SignalCaught)),
349+
},
350+
{
351+
Description: "SigProxyTrue",
361352

362-
return result.Stdout(), sigIntRecieved, timedOut
363-
}
353+
Cleanup: func(data test.Data, helpers test.Helpers) {
354+
helpers.Anyhow("rm", "-f", data.Identifier())
355+
},
364356

365-
func TestRunSigProxy(t *testing.T) {
357+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
358+
cmd := nerdtest.RunSigProxyContainer(os.Interrupt, true, []string{"--sig-proxy=true"}, data, helpers)
359+
err := cmd.Signal(os.Interrupt)
360+
assert.NilError(helpers.T(), err)
361+
return cmd
362+
},
366363

367-
type testCase struct {
368-
name string
369-
args []string
370-
want bool
371-
expectedOut string
372-
}
373-
testCases := []testCase{
374-
{
375-
name: "SigProxyDefault",
376-
args: []string{},
377-
want: true,
378-
expectedOut: testutil.SigProxyTrueOut,
364+
Expected: test.Expects(0, nil, test.Contains(nerdtest.SignalCaught)),
379365
},
380366
{
381-
name: "SigProxyTrue",
382-
args: []string{"--sig-proxy=true"},
383-
want: true,
384-
expectedOut: testutil.SigProxyTrueOut,
385-
},
386-
{
387-
name: "SigProxyFalse",
388-
args: []string{"--sig-proxy=false"},
389-
want: false,
390-
expectedOut: "",
367+
Description: "SigProxyFalse",
368+
369+
Cleanup: func(data test.Data, helpers test.Helpers) {
370+
helpers.Anyhow("rm", "-f", data.Identifier())
371+
},
372+
373+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
374+
cmd := nerdtest.RunSigProxyContainer(os.Interrupt, true, []string{"--sig-proxy=false"}, data, helpers)
375+
err := cmd.Signal(os.Interrupt)
376+
assert.NilError(helpers.T(), err)
377+
return cmd
378+
},
379+
380+
Expected: test.Expects(127, nil, test.DoesNotContain(nerdtest.SignalCaught)),
391381
},
392382
}
393383

394-
for _, tc := range testCases {
395-
tc := tc
396-
t.Run(tc.name, func(t *testing.T) {
397-
stdout, sigIntRecieved, timedOut := runSigProxy(t, tc.args...)
398-
errorMsg := fmt.Sprintf("%s failed;\nExpected: '%s'\nActual: '%s'", tc.name, tc.expectedOut, stdout)
399-
assert.Equal(t, false, timedOut, errorMsg)
400-
assert.Equal(t, tc.want, sigIntRecieved, errorMsg)
401-
})
402-
}
384+
testCase.Run(t)
403385
}
404386

405387
func TestRunWithFluentdLogDriver(t *testing.T) {

cmd/nerdctl/container/container_stop_linux_test.go

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ import (
2929
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
3030
"github.com/containerd/nerdctl/v2/pkg/testutil"
3131
iptablesutil "github.com/containerd/nerdctl/v2/pkg/testutil/iptables"
32+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
3233
"github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil"
34+
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
3335
)
3436

3537
func TestStopStart(t *testing.T) {
@@ -73,31 +75,23 @@ func TestStopStart(t *testing.T) {
7375
}
7476

7577
func TestStopWithStopSignal(t *testing.T) {
76-
t.Parallel()
77-
// There may be issues with logs in Docker.
78-
// This test is flaky with Docker. Might be related to https://github.com/containerd/nerdctl/pull/3557
79-
base := testutil.NewBase(t)
80-
testContainerName := testutil.Identifier(t)
81-
defer base.Cmd("rm", "-f", testContainerName).Run()
78+
testCase := nerdtest.Setup()
8279

83-
base.Cmd("run", "-d", "--stop-signal", "SIGQUIT", "--name", testContainerName, testutil.CommonImage, "sh", "-euxc", `#!/bin/sh
84-
set -eu
85-
echo "Script started"
86-
quit=0
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"
95-
sleep 1
96-
done
97-
echo "signal quit"
98-
sync`).AssertOK()
99-
base.Cmd("stop", testContainerName).AssertOK()
100-
base.Cmd("logs", "-f", testContainerName).AssertOutContains("signal quit")
80+
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
81+
helpers.Anyhow("rm", "-f", data.Identifier())
82+
}
83+
84+
testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
85+
cmd := nerdtest.RunSigProxyContainer(nerdtest.SigQuit, false,
86+
[]string{"--stop-signal", nerdtest.SigQuit.String()}, data, helpers)
87+
helpers.Ensure("stop", data.Identifier())
88+
return cmd
89+
}
90+
91+
// Verify that SIGQUIT was sent to the container AND that the container did forcefully exit
92+
testCase.Expected = test.Expects(137, nil, test.Contains(nerdtest.SignalCaught))
93+
94+
testCase.Run(t)
10195
}
10296

10397
func TestStopCleanupForwards(t *testing.T) {

0 commit comments

Comments
 (0)