Skip to content

Commit d90729c

Browse files
committed
Add test tooling for signals
Signed-off-by: apostasie <spam_blackhole@farcloser.world>
1 parent 6f608da commit d90729c

File tree

4 files changed

+75
-19
lines changed

4 files changed

+75
-19
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package nerdtest
18+
19+
import (
20+
"os"
21+
"strconv"
22+
"strings"
23+
"syscall"
24+
"time"
25+
26+
"github.com/containerd/nerdctl/v2/pkg/testutil"
27+
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
28+
)
29+
30+
const SignalCaught = "received"
31+
32+
var SigQuit os.Signal = syscall.SIGQUIT
33+
var SigUsr1 os.Signal = syscall.SIGUSR1
34+
35+
func RunSigProxyContainer(signal os.Signal, exitOnSignal bool, args []string, data test.Data, helpers test.Helpers) test.TestableCommand {
36+
sig := strconv.Itoa(int(signal.(syscall.Signal)))
37+
ready := "trap ready"
38+
script := `#!/bin/sh
39+
set -eu
40+
41+
sig_msg () {
42+
printf "` + SignalCaught + `\n"
43+
[ "` + strconv.FormatBool(exitOnSignal) + `" != true ] || exit 0
44+
}
45+
46+
trap sig_msg ` + sig + `
47+
printf "` + ready + `\n"
48+
while true; do
49+
sleep 0.1
50+
done
51+
`
52+
53+
args = append(args, "--name", data.Identifier(), testutil.CommonImage, "sh", "-c", script)
54+
args = append([]string{"run"}, args...)
55+
56+
cmd := helpers.Command(args...)
57+
cmd.Background(10 * time.Second)
58+
EnsureContainerStarted(helpers, data.Identifier())
59+
60+
for {
61+
out := helpers.Capture("logs", data.Identifier())
62+
if strings.Contains(out, ready) {
63+
break
64+
}
65+
time.Sleep(100 * time.Millisecond)
66+
}
67+
68+
return cmd
69+
}

pkg/testutil/test/command.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ func (gc *GenericCommand) Background(timeout time.Duration) {
185185
gc.result = icmd.StartCmd(i)
186186
}
187187

188+
func (gc *GenericCommand) Signal(sig os.Signal) error {
189+
return gc.result.Cmd.Process.Signal(sig)
190+
}
191+
188192
func (gc *GenericCommand) withEnv(env map[string]string) {
189193
if gc.Env == nil {
190194
gc.Env = map[string]string{}

pkg/testutil/test/test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ type TestableCommand interface {
114114
Run(expect *Expected)
115115
// Background allows starting a command in the background
116116
Background(timeout time.Duration)
117+
// Signal sends a signal to a backgrounded command
118+
Signal(sig os.Signal) error
117119
// Stderr allows retrieving the raw stderr output of the command
118120
Stderr() string
119121
}

pkg/testutil/testutil_linux.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,6 @@ var (
5454
// It should be "connection refused" as per the TCP RFC.
5555
// https://www.rfc-editor.org/rfc/rfc793
5656
ExpectedConnectionRefusedError = "connection refused"
57-
58-
SigProxyTrueOut = "received SIGINT"
59-
SigProxyTimeoutMsg = "Timed Out; No signal received"
60-
SigProxyTestScript = `#!/bin/sh
61-
set -eu
62-
63-
sig_msg () {
64-
printf "` + SigProxyTrueOut + `"
65-
end
66-
}
67-
68-
trap sig_msg INT
69-
timeout=0
70-
while [ $timeout -ne 10 ]; do
71-
timeout=$((timeout+1))
72-
sleep 1
73-
done
74-
printf "` + SigProxyTimeoutMsg + `"
75-
end`
7657
)
7758

7859
const (

0 commit comments

Comments
 (0)