Skip to content

Commit 59b0465

Browse files
committed
tests: helpers: provide a "sane" run helper
The runc wrapper we have had for a long time is useful for debugging errors (because bats's built-in "run" doesn't provide the output of the program in case of an error). However, it might be necessary to run other programs in a similar wrapper. In addition, it might be needed to add timeouts or use the bats_pipe feature (bats v1.10.0) with similar wrapping. Adding support for this in the sane_run wrapper makes it a little easier to use these things when writing tests. This is somewhat adapted from umoci's sane_run helper. Signed-off-by: Aleksa Sarai <[email protected]>
1 parent 1f3707b commit 59b0465

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ localunittest: all
164164
integration: runcimage
165165
$(CONTAINER_ENGINE) run $(CONTAINER_ENGINE_RUN_FLAGS) \
166166
-t --privileged --rm \
167+
-v /boot:/boot:ro \
167168
-v /lib/modules:/lib/modules:ro \
168169
-v $(CURDIR):/go/src/$(PROJECT) \
169170
$(RUNC_IMAGE) make localintegration TESTPATH="$(TESTPATH)"

tests/integration/helpers.bash

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,54 @@ ARCH=$(uname -m)
3939
# Seccomp agent socket.
4040
SECCCOMP_AGENT_SOCKET="$BATS_TMPDIR/seccomp-agent.sock"
4141

42-
# Wrapper for runc.
43-
function runc() {
44-
run __runc "$@"
42+
function sane_run() { # --pipe --timeout=<timeout>
43+
local getopt
44+
getopt="$(getopt -o + --long pipe,timeout: -- "$@")"
45+
eval set -- "$getopt"
46+
47+
pipe=
48+
timeout=
49+
while true; do
50+
case "$1" in
51+
--pipe)
52+
pipe=1
53+
shift
54+
;;
55+
--timeout)
56+
timeout="$2"
57+
shift 2
58+
;;
59+
--)
60+
shift
61+
break
62+
;;
63+
*)
64+
fail "unknown argument $1 ${2:-} to sane_run"
65+
;;
66+
esac
67+
done
68+
cmd_prefix=()
69+
[ -n "$pipe" ] && cmd_prefix+=("bats_pipe")
70+
[ -n "$timeout" ] && cmd_prefix+=("timeout" "--foreground" "--signal=KILL" "$timeout")
71+
72+
local cmd="$1"
73+
shift
74+
75+
run "${cmd_prefix[@]}" "$cmd" "$@"
4576

4677
# Some debug information to make life easier. bats will only print it if the
4778
# test failed, in which case the output is useful.
4879
# shellcheck disable=SC2154
49-
echo "$(basename "$RUNC") $* (status=$status):" >&2
80+
echo "$cmd $* (status=$status):" >&2
5081
# shellcheck disable=SC2154
5182
echo "$output" >&2
5283
}
5384

85+
# Wrapper for runc.
86+
function runc() {
87+
sane_run __runc "$@"
88+
}
89+
5490
# Raw wrapper for runc.
5591
function __runc() {
5692
"$RUNC" ${RUNC_USE_SYSTEMD+--systemd-cgroup} \

0 commit comments

Comments
 (0)