Skip to content

Commit 2f2f910

Browse files
committed
tests/int: improve runc wrapper
1. Add status check support (same as in bats' run helper). 2. Add PRE_CMD support (so we can use commands like taskset or timeout). 3. Drop sane_helper since the output of the command is shown in case of an error, and we show the command itself in runc wrapper (unless -N or ! is provided -- in this case the command is shown by bats, together with the error). This does not show the output of successful commands which IMO is a net positive since we are almost always interested in failed command output only. 4. Use the new functionality in cpu_affinity.bats and start.bats as a showcase (the test of refactoring is in a separate commit). Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 644bb1e commit 2f2f910

File tree

3 files changed

+42
-60
lines changed

3 files changed

+42
-60
lines changed

tests/integration/cpu_affinity.bats

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,12 @@ function cpus_to_mask() {
106106
}
107107

108108
@test "runc run [CPU affinity should reset]" {
109-
# We need to use RUNC_CMDLINE since taskset requires a proper binary, not a
110-
# bash function (which is what runc and __runc are).
111-
setup_runc_cmdline
112-
113109
first="$(first_cpu)"
114110

115111
# Running without cpuset should result in an affinity for all CPUs.
116112
update_config '.process.args = [ "/bin/grep", "-F", "Cpus_allowed_list:", "/proc/self/status" ]'
117113
update_config 'del(.linux.resources.cpu)'
118-
sane_run taskset -c "$first" "${RUNC_CMDLINE[@]}" run ctr
119-
[ "$status" -eq 0 ]
114+
PRE_CMD="taskset -c $first" runc -0 run ctr
120115
[[ "$output" != $'Cpus_allowed_list:\t'"$first" ]]
121116
[[ "$output" == $'Cpus_allowed_list:\t'"$INITIAL_CPU_MASK" ]]
122117
}
@@ -125,47 +120,35 @@ function cpus_to_mask() {
125120
[ $EUID -ne 0 ] && requires rootless_cgroup
126121
set_cgroups_path
127122

128-
# We need to use RUNC_CMDLINE since taskset requires a proper binary, not a
129-
# bash function (which is what runc and __runc are).
130-
setup_runc_cmdline
131-
132123
first="$(first_cpu)"
133124
second="$((first + 1))" # Hacky; might not work in all environments.
134125

135126
# Running with a cpuset should result in an affinity that matches.
136127
update_config '.process.args = [ "/bin/grep", "-F", "Cpus_allowed_list:", "/proc/self/status" ]'
137128
update_config '.linux.resources.cpu = {"mems": "0", "cpus": "'"$first-$second"'"}'
138-
sane_run taskset -c "$first" "${RUNC_CMDLINE[@]}" run ctr
139-
[ "$status" -eq 0 ]
129+
PRE_CMD="taskset -c $first" runc -0 run ctr
140130
[[ "$output" != $'Cpus_allowed_list:\t'"$first" ]]
141131
# XXX: For some reason, systemd-cgroup leads to us using the all-set
142132
# cpumask rather than the cpuset we configured?
143133
[ -v RUNC_USE_SYSTEMD ] || [[ "$output" == $'Cpus_allowed_list:\t'"$first-$second" ]]
144134

145135
# Ditto for a cpuset that has no overlap with the original cpumask.
146136
update_config '.linux.resources.cpu = {"mems": "0", "cpus": "'"$second"'"}'
147-
sane_run taskset -c "$first" "${RUNC_CMDLINE[@]}" run ctr
148-
[ "$status" -eq 0 ]
137+
PRE_CMD="taskset -c $first" runc -0 run ctr
149138
[[ "$output" != $'Cpus_allowed_list:\t'"$first" ]]
150139
# XXX: For some reason, systemd-cgroup leads to us using the all-set
151140
# cpumask rather than the cpuset we configured?
152141
[ -v RUNC_USE_SYSTEMD ] || [[ "$output" == $'Cpus_allowed_list:\t'"$second" ]]
153142
}
154143

155144
@test "runc exec [default CPU affinity should reset]" {
156-
# We need to use RUNC_CMDLINE since taskset requires a proper binary, not a
157-
# bash function (which is what runc and __runc are).
158-
setup_runc_cmdline
159-
160145
first="$(first_cpu)"
161146

162147
# Running without cpuset should result in an affinity for all CPUs.
163148
update_config '.process.args = [ "/bin/sleep", "infinity" ]'
164149
update_config 'del(.linux.resources.cpu)'
165-
sane_run taskset -c "$first" "${RUNC_CMDLINE[@]}" run -d --console-socket "$CONSOLE_SOCKET" ctr3
166-
[ "$status" -eq 0 ]
167-
sane_run taskset -c "$first" "${RUNC_CMDLINE[@]}" exec ctr3 grep -F Cpus_allowed_list: /proc/self/status
168-
[ "$status" -eq 0 ]
150+
PRE_CMD="taskset -c $first" runc -0 run -d --console-socket "$CONSOLE_SOCKET" ctr3
151+
PRE_CMD="taskset -c $first" runc -0 exec ctr3 grep -F Cpus_allowed_list: /proc/self/status
169152
[[ "$output" != $'Cpus_allowed_list:\t'"$first" ]]
170153
[[ "$output" == $'Cpus_allowed_list:\t'"$INITIAL_CPU_MASK" ]]
171154
}
@@ -174,20 +157,14 @@ function cpus_to_mask() {
174157
[ $EUID -ne 0 ] && requires rootless_cgroup
175158
set_cgroups_path
176159

177-
# We need to use RUNC_CMDLINE since taskset requires a proper binary, not a
178-
# bash function (which is what runc and __runc are).
179-
setup_runc_cmdline
180-
181160
first="$(first_cpu)"
182161
second="$((first + 1))" # Hacky; might not work in all environments.
183162

184163
# Running with a cpuset should result in an affinity that matches.
185164
update_config '.process.args = [ "/bin/sleep", "infinity" ]'
186165
update_config '.linux.resources.cpu = {"mems": "0", "cpus": "'"$first-$second"'"}'
187-
sane_run taskset -c "$first" "${RUNC_CMDLINE[@]}" run -d --console-socket "$CONSOLE_SOCKET" ctr
188-
[ "$status" -eq 0 ]
189-
sane_run taskset -c "$first" "${RUNC_CMDLINE[@]}" exec ctr grep -F Cpus_allowed_list: /proc/self/status
190-
[ "$status" -eq 0 ]
166+
PRE_CMD="taskset -c $first" runc -0 run -d --console-socket "$CONSOLE_SOCKET" ctr
167+
PRE_CMD="taskset -c $first" runc -0 exec ctr grep -F Cpus_allowed_list: /proc/self/status
191168
[[ "$output" != $'Cpus_allowed_list:\t'"$first" ]]
192169
# XXX: For some reason, systemd-cgroup leads to us using the all-set
193170
# cpumask rather than the cpuset we configured?
@@ -199,10 +176,8 @@ function cpus_to_mask() {
199176

200177
# Ditto for a cpuset that has no overlap with the original cpumask.
201178
update_config '.linux.resources.cpu = {"mems": "0", "cpus": "'"$second"'"}'
202-
sane_run taskset -c "$first" "${RUNC_CMDLINE[@]}" run -d --console-socket "$CONSOLE_SOCKET" ctr
203-
[ "$status" -eq 0 ]
204-
sane_run taskset -c "$first" "${RUNC_CMDLINE[@]}" exec ctr grep -F Cpus_allowed_list: /proc/self/status
205-
[ "$status" -eq 0 ]
179+
PRE_CMD="taskset -c $first" runc -0 run -d --console-socket "$CONSOLE_SOCKET" ctr
180+
PRE_CMD="taskset -c $first" runc -0 exec ctr grep -F Cpus_allowed_list: /proc/self/status
206181
[[ "$output" != $'Cpus_allowed_list:\t'"$first" ]]
207182
# XXX: For some reason, systemd-cgroup leads to us using the all-set
208183
# cpumask rather than the cpuset we configured?

tests/integration/helpers.bash

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,43 @@ ARCH=$(uname -m)
3636
# Seccomp agent socket.
3737
SECCCOMP_AGENT_SOCKET="$BATS_TMPDIR/seccomp-agent.sock"
3838

39-
# Wrapper around "run" that logs output to make tests easier to debug.
40-
function sane_run() {
41-
local cmd="$1"
42-
local cmdname="${CMDNAME:-$(basename "$cmd")}"
43-
shift
44-
45-
run "$cmd" "$@"
46-
47-
# Some debug information to make life easier. bats will only print it if the
48-
# test failed, in which case the output is useful.
49-
# shellcheck disable=SC2154
50-
echo "$cmdname $* (status=$status)" >&2
51-
# shellcheck disable=SC2154
52-
echo "$output" >&2
53-
}
54-
55-
# Wrapper for runc.
39+
# Wrapper for runc to run it via bats run helper.
40+
#
41+
# Optional $1 parameter (as in bats run):
42+
# -N expect exit status N (0-255), fail otherwise;
43+
# ! expect nonzero exit status (1-255), fail if command succeeds.
44+
#
45+
# Optional environment:
46+
# PRE_CMD a command to insert before runc (taskset, timeout etc.)
5647
function runc() {
57-
CMDNAME="$(basename "$RUNC")" sane_run __runc "$@"
48+
local run=(run)
49+
local show=yes
50+
case $1 in
51+
"!" | -[0-9]*)
52+
run+=("$1")
53+
unset show
54+
shift
55+
;;
56+
esac
57+
setup_runc_cmdline
58+
[ -v show ] && echo "# ${RUNC_CMDLINE[*]} $*" | sed "s| $RUNC | runc |" >&2
59+
"${run[@]}" "${RUNC_CMDLINE[@]}" "$@"
5860
}
5961

6062
function setup_runc_cmdline() {
61-
RUNC_CMDLINE=("$RUNC")
63+
RUNC_CMDLINE=()
64+
# If PRE_CMD is set, prepend it.
65+
for pre in ${PRE_CMD:+$PRE_CMD}; do
66+
RUNC_CMDLINE+=("$pre")
67+
done
68+
RUNC_CMDLINE+=("$RUNC")
6269
[[ -v RUNC_USE_SYSTEMD ]] && RUNC_CMDLINE+=("--systemd-cgroup")
6370
[[ -n "${ROOT:-}" ]] && RUNC_CMDLINE+=("--root" "$ROOT/state")
6471
export RUNC_CMDLINE
6572
}
6673

67-
# Raw wrapper for runc.
74+
# Raw wrapper for runc (no bats' run helper, use in special cases,
75+
# e.g. if I/O redirection is needed).
6876
function __runc() {
6977
setup_runc_cmdline
7078
"${RUNC_CMDLINE[@]}" "$@"
@@ -708,12 +716,14 @@ function retry() {
708716

709717
for ((i = 0; i < attempts; i++)); do
710718
run "$@"
719+
# shellcheck disable=SC2154
711720
if [[ "$status" -eq 0 ]]; then
712721
return 0
713722
fi
714723
sleep "$delay"
715724
done
716725

726+
# shellcheck disable=SC2154
717727
echo "Command \"$*\" failed $attempts times. Output: $output"
718728
false
719729
}

tests/integration/start.bats

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@ function teardown() {
1111
}
1212

1313
@test "runc start" {
14-
runc create --console-socket "$CONSOLE_SOCKET" test_busybox
15-
[ "$status" -eq 0 ]
14+
runc -0 create --console-socket "$CONSOLE_SOCKET" test_busybox
1615

1716
testcontainer test_busybox created
1817

19-
runc start test_busybox
20-
[ "$status" -eq 0 ]
18+
runc -0 start test_busybox
2119

2220
testcontainer test_busybox running
2321

2422
runc delete --force test_busybox
2523

26-
runc state test_busybox
27-
[ "$status" -ne 0 ]
24+
runc ! state test_busybox
2825
}

0 commit comments

Comments
 (0)