Skip to content

Commit 654f331

Browse files
author
Mrunal Patel
authored
Merge pull request opencontainers#3175 from kolyshkin/tests-int-cleanups
tests/int/helpers: cleanup, enable shellcheck
2 parents 8b59b76 + fb629db commit 654f331

File tree

2 files changed

+40
-50
lines changed

2 files changed

+40
-50
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ cfmt:
118118
indent -linux -l120 -il0 -ppi2 -cp1 -T size_t -T jmp_buf $(C_SRC)
119119

120120
shellcheck:
121-
shellcheck tests/integration/*.bats tests/integration/*.sh tests/*.sh script/release.sh
121+
shellcheck tests/integration/*.bats tests/integration/*.sh tests/integration/*.bash tests/*.sh script/release.sh
122122
# TODO: add shellcheck for more sh files
123123

124124
shfmt:

tests/integration/helpers.bash

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ if [ -z "$BATS_RUN_TMPDIR" ]; then
77
fi
88

99
# Root directory of integration tests.
10-
INTEGRATION_ROOT=$(dirname "$(readlink -f "$BASH_SOURCE")")
10+
INTEGRATION_ROOT=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
1111

1212
# Download images, get *_IMAGE variables.
1313
IMAGES=$("${INTEGRATION_ROOT}"/get-images.sh)
@@ -18,6 +18,7 @@ RUNC="${INTEGRATION_ROOT}/../../runc"
1818
RECVTTY="${INTEGRATION_ROOT}/../../contrib/cmd/recvtty/recvtty"
1919

2020
# Test data path.
21+
# shellcheck disable=SC2034
2122
TESTDATA="${INTEGRATION_ROOT}/testdata"
2223

2324
# CRIU PATH
@@ -38,7 +39,9 @@ function runc() {
3839

3940
# Some debug information to make life easier. bats will only print it if the
4041
# test failed, in which case the output is useful.
41-
echo "runc $@ (status=$status):" >&2
42+
# shellcheck disable=SC2154
43+
echo "runc $* (status=$status):" >&2
44+
# shellcheck disable=SC2154
4245
echo "$output" >&2
4346
}
4447

@@ -47,44 +50,34 @@ function __runc() {
4750
"$RUNC" ${RUNC_USE_SYSTEMD+--systemd-cgroup} --root "$ROOT/state" "$@"
4851
}
4952

50-
# Wrapper for runc spec, which takes only one argument (the bundle path).
53+
# Wrapper for runc spec.
5154
function runc_spec() {
52-
! [[ "$#" > 1 ]]
53-
5455
local args=()
55-
local bundle=""
56-
5756
if [ "$ROOTLESS" -ne 0 ]; then
5857
args+=("--rootless")
5958
fi
60-
if [ "$#" -ne 0 ]; then
61-
bundle="$1"
62-
args+=("--bundle" "$bundle")
63-
fi
6459

6560
runc spec "${args[@]}"
6661

6762
# Always add additional mappings if we have idmaps.
6863
if [[ "$ROOTLESS" -ne 0 ]] && [[ "$ROOTLESS_FEATURES" == *"idmap"* ]]; then
69-
runc_rootless_idmap "$bundle"
64+
runc_rootless_idmap
7065
fi
7166
}
7267

7368
# Helper function to reformat config.json file. Input uses jq syntax.
7469
function update_config() {
75-
bundle="${2:-.}"
76-
jq "$1" "$bundle/config.json" | awk 'BEGIN{RS="";getline<"-";print>ARGV[1]}' "$bundle/config.json"
70+
jq "$1" "./config.json" | awk 'BEGIN{RS="";getline<"-";print>ARGV[1]}' "./config.json"
7771
}
7872

7973
# Shortcut to add additional uids and gids, based on the values set as part of
8074
# a rootless configuration.
8175
function runc_rootless_idmap() {
82-
bundle="${1:-.}"
8376
update_config ' .mounts |= map((select(.type == "devpts") | .options += ["gid=5"]) // .)
8477
| .linux.uidMappings += [{"hostID": '"$ROOTLESS_UIDMAP_START"', "containerID": 1000, "size": '"$ROOTLESS_UIDMAP_LENGTH"'}]
8578
| .linux.gidMappings += [{"hostID": '"$ROOTLESS_GIDMAP_START"', "containerID": 100, "size": 1}]
86-
| .linux.gidMappings += [{"hostID": '"$(($ROOTLESS_GIDMAP_START + 10))"', "containerID": 1, "size": 20}]
87-
| .linux.gidMappings += [{"hostID": '"$(($ROOTLESS_GIDMAP_START + 100))"', "containerID": 1000, "size": '"$(($ROOTLESS_GIDMAP_LENGTH - 1000))"'}]' $bundle
79+
| .linux.gidMappings += [{"hostID": '"$((ROOTLESS_GIDMAP_START + 10))"', "containerID": 1, "size": 20}]
80+
| .linux.gidMappings += [{"hostID": '"$((ROOTLESS_GIDMAP_START + 100))"', "containerID": 1000, "size": '"$((ROOTLESS_GIDMAP_LENGTH - 1000))"'}]'
8881
}
8982

9083
# Returns systemd version as a number (-1 if systemd is not enabled/supported).
@@ -131,17 +124,16 @@ function init_cgroup_paths() {
131124
CGROUP_SUBSYSTEMS=$(awk '!/^#/ {print $1}' /proc/cgroups)
132125
local g base_path
133126
for g in ${CGROUP_SUBSYSTEMS}; do
134-
base_path=$(gawk '$(NF-2) == "cgroup" && $NF ~ /\<'${g}'\>/ { print $5; exit }' /proc/self/mountinfo)
127+
base_path=$(gawk '$(NF-2) == "cgroup" && $NF ~ /\<'"${g}"'\>/ { print $5; exit }' /proc/self/mountinfo)
135128
test -z "$base_path" && continue
136-
eval CGROUP_${g^^}_BASE_PATH="${base_path}"
129+
eval CGROUP_"${g^^}"_BASE_PATH="${base_path}"
137130
done
138131
fi
139132
}
140133

141134
# Randomize cgroup path(s), and update cgroupsPath in config.json.
142135
# This function sets a few cgroup-related variables.
143136
function set_cgroups_path() {
144-
bundle="${1:-.}"
145137
init_cgroup_paths
146138

147139
local rnd="$RANDOM"
@@ -165,47 +157,47 @@ function set_cgroups_path() {
165157
CGROUP_PATH=${CGROUP_BASE_PATH}${REL_CGROUPS_PATH}
166158
fi
167159

168-
update_config '.linux.cgroupsPath |= "'"${OCI_CGROUPS_PATH}"'"' "$bundle"
160+
update_config '.linux.cgroupsPath |= "'"${OCI_CGROUPS_PATH}"'"'
169161
}
170162

171163
# Get a value from a cgroup file.
172164
function get_cgroup_value() {
173165
local source=$1
174166
local cgroup var current
175167

176-
if [ "x$CGROUP_UNIFIED" = "xyes" ]; then
168+
if [ "$CGROUP_UNIFIED" = "yes" ]; then
177169
cgroup=$CGROUP_PATH
178170
else
179171
var=${source%%.*} # controller name (e.g. memory)
180172
var=CGROUP_${var^^}_BASE_PATH # variable name (e.g. CGROUP_MEMORY_BASE_PATH)
181-
eval cgroup=\$${var}${REL_CGROUPS_PATH}
173+
eval cgroup=\$"${var}${REL_CGROUPS_PATH}"
182174
fi
183-
cat $cgroup/$source
175+
cat "$cgroup/$source"
184176
}
185177

186178
# Helper to check a if value in a cgroup file matches the expected one.
187179
function check_cgroup_value() {
188180
local current
189-
current="$(get_cgroup_value $1)"
181+
current="$(get_cgroup_value "$1")"
190182
local expected=$2
191183

192-
echo "current" $current "!?" "$expected"
184+
echo "current $current !? $expected"
193185
[ "$current" = "$expected" ]
194186
}
195187

196188
# Helper to check a value in systemd.
197189
function check_systemd_value() {
198190
[ -z "${RUNC_USE_SYSTEMD}" ] && return
199-
local source=$1
191+
local source="$1"
200192
[ "$source" = "unsupported" ] && return
201193
local expected="$2"
202194
local expected2="$3"
203195
local user=""
204-
[ $(id -u) != "0" ] && user="--user"
196+
[ "$(id -u)" != "0" ] && user="--user"
205197

206-
current=$(systemctl show $user --property $source $SD_UNIT_NAME | awk -F= '{print $2}')
198+
current=$(systemctl show $user --property "$source" "$SD_UNIT_NAME" | awk -F= '{print $2}')
207199
echo "systemd $source: current $current !? $expected $expected2"
208-
[ "$current" = "$expected" ] || [ -n "$expected2" -a "$current" = "$expected2" ]
200+
[ "$current" = "$expected" ] || [[ -n "$expected2" && "$current" = "$expected2" ]]
209201
}
210202

211203
function check_cpu_quota() {
@@ -253,21 +245,18 @@ function check_cpu_shares() {
253245
function check_cpu_weight() {
254246
local weight=$1
255247

256-
check_cgroup_value "cpu.weight" $weight
257-
check_systemd_value "CPUWeight" $weight
248+
check_cgroup_value "cpu.weight" "$weight"
249+
check_systemd_value "CPUWeight" "$weight"
258250
}
259251

260252
# Helper function to set a resources limit
261253
function set_resources_limit() {
262-
bundle="${1:-.}"
263-
update_config '.linux.resources.pids.limit |= 100' $bundle
254+
update_config '.linux.resources.pids.limit |= 100'
264255
}
265256

266257
# Helper function to make /sys/fs/cgroup writable
267258
function set_cgroup_mount_writable() {
268-
bundle="${1:-.}"
269-
update_config '.mounts |= map((select(.type == "cgroup") | .options -= ["ro"]) // .)' \
270-
$bundle
259+
update_config '.mounts |= map((select(.type == "cgroup") | .options -= ["ro"]) // .)'
271260
}
272261

273262
# Fails the current test, providing the error given.
@@ -330,7 +319,7 @@ function requires() {
330319
;;
331320
cgroups_swap)
332321
init_cgroup_paths
333-
if [ $CGROUP_UNIFIED = "no" -a ! -e "${CGROUP_MEMORY_BASE_PATH}/memory.memsw.limit_in_bytes" ]; then
322+
if [ $CGROUP_UNIFIED = "no" ] && [ ! -e "${CGROUP_MEMORY_BASE_PATH}/memory.memsw.limit_in_bytes" ]; then
334323
skip_me=1
335324
fi
336325
;;
@@ -359,8 +348,9 @@ function requires() {
359348
fi
360349
;;
361350
smp)
362-
local cpu_count=$(grep -c '^processor' /proc/cpuinfo)
363-
if [ "$cpu_count" -lt 2 ]; then
351+
local cpus
352+
cpus=$(grep -c '^processor' /proc/cpuinfo)
353+
if [ "$cpus" -lt 2 ]; then
364354
skip_me=1
365355
fi
366356
;;
@@ -397,10 +387,10 @@ function retry() {
397387
if [[ "$status" -eq 0 ]]; then
398388
return 0
399389
fi
400-
sleep $delay
390+
sleep "$delay"
401391
done
402392

403-
echo "Command \"$@\" failed $attempts times. Output: $output"
393+
echo "Command \"$*\" failed $attempts times. Output: $output"
404394
false
405395
}
406396

@@ -418,8 +408,8 @@ function wait_for_container() {
418408

419409
function testcontainer() {
420410
# test state of container
421-
runc state $1
422-
if [ $2 == "checkpointed" ]; then
411+
runc state "$1"
412+
if [ "$2" == "checkpointed" ]; then
423413
[ "$status" -eq 1 ]
424414
return
425415
fi
@@ -431,7 +421,7 @@ function setup_recvtty() {
431421
[ -z "$ROOT" ] && return 1 # must not be called without ROOT set
432422
local dir="$ROOT/tty"
433423

434-
mkdir $dir
424+
mkdir "$dir"
435425
export CONSOLE_SOCKET="$dir/sock"
436426

437427
# We need to start recvtty in the background, so we double fork in the shell.
@@ -444,7 +434,7 @@ function teardown_recvtty() {
444434

445435
# When we kill recvtty, the container will also be killed.
446436
if [ -f "$dir/pid" ]; then
447-
kill -9 $(cat "$dir/pid")
437+
kill -9 "$(cat "$dir/pid")"
448438
fi
449439

450440
# Clean up the files that might be left over.
@@ -455,11 +445,11 @@ function setup_bundle() {
455445
local image="$1"
456446

457447
# Root for various container directories (state, tty, bundle).
458-
export ROOT=$(mktemp -d "$BATS_RUN_TMPDIR/runc.XXXXXX")
448+
ROOT=$(mktemp -d "$BATS_RUN_TMPDIR/runc.XXXXXX")
459449
mkdir -p "$ROOT/state" "$ROOT/bundle/rootfs"
460450

461451
setup_recvtty
462-
cd "$ROOT/bundle"
452+
cd "$ROOT/bundle" || return
463453

464454
tar --exclude './dev/*' -C rootfs -xf "$image"
465455

@@ -482,7 +472,7 @@ function setup_debian() {
482472
function teardown_bundle() {
483473
[ -z "$ROOT" ] && return 0 # nothing to teardown
484474

485-
cd "$INTEGRATION_ROOT"
475+
cd "$INTEGRATION_ROOT" || return
486476
teardown_recvtty
487477
local ct
488478
for ct in $(__runc list -q); do

0 commit comments

Comments
 (0)