Skip to content

Commit fb629db

Browse files
committed
tests/int/helpers: fix shellcheck warnings
... and add the file to be checked by shellcheck. The warnings fixed are: In tests/integration/helpers.bash line 10: INTEGRATION_ROOT=$(dirname "$(readlink -f "$BASH_SOURCE")") ^----------^ SC2128: Expanding an array without an index only gives the first element. In tests/integration/helpers.bash line 22: TESTDATA="${INTEGRATION_ROOT}/testdata" ^------^ SC2034: TESTDATA appears unused. Verify use (or export if used externally). In tests/integration/helpers.bash line 42: echo "runc $@ (status=$status):" >&2 ^-- SC2145: Argument mixes string and array. Use * or separate argument. ^-----^ SC2154: status is referenced but not assigned. In tests/integration/helpers.bash line 43: echo "$output" >&2 ^-----^ SC2154: output is referenced but not assigned. In tests/integration/helpers.bash line 77: | .linux.gidMappings += [{"hostID": '"$(($ROOTLESS_GIDMAP_START + 10))"', "containerID": 1, "size": 20}] ^--------------------^ SC2004: $/${} is unnecessary on arithmetic variables. In tests/integration/helpers.bash line 78: | .linux.gidMappings += [{"hostID": '"$(($ROOTLESS_GIDMAP_START + 100))"', "containerID": 1000, "size": '"$(($ROOTLESS_GIDMAP_LENGTH - 1000))"'}]' ^--------------------^ SC2004: $/${} is unnecessary on arithmetic variables. ^---------------------^ SC2004: $/${} is unnecessary on arithmetic variables. In tests/integration/helpers.bash line 125: base_path=$(gawk '$(NF-2) == "cgroup" && $NF ~ /\<'${g}'\>/ { print $5; exit }' /proc/self/mountinfo) ^--^ SC2086: Double quote to prevent globbing and word splitting. Did you mean: base_path=$(gawk '$(NF-2) == "cgroup" && $NF ~ /\<'"${g}"'\>/ { print $5; exit }' /proc/self/mountinfo) In tests/integration/helpers.bash line 127: eval CGROUP_${g^^}_BASE_PATH="${base_path}" ^----^ SC2086: Double quote to prevent globbing and word splitting. Did you mean: eval CGROUP_"${g^^}"_BASE_PATH="${base_path}" In tests/integration/helpers.bash line 229: if [ "x$CGROUP_UNIFIED" = "xyes" ]; then ^----------------^ SC2268: Avoid x-prefix in comparisons as it no longer serves a purpose. Did you mean: if [ "$CGROUP_UNIFIED" = "yes" ]; then In tests/integration/helpers.bash line 234: eval cgroup=\$${var}${REL_CGROUPS_PATH} ^----^ SC2086: Double quote to prevent globbing and word splitting. ^-----------------^ SC2086: Double quote to prevent globbing and word splitting. Did you mean: eval cgroup=\$"${var}""${REL_CGROUPS_PATH}" In tests/integration/helpers.bash line 236: cat $cgroup/$source ^-----^ SC2086: Double quote to prevent globbing and word splitting. ^-----^ SC2086: Double quote to prevent globbing and word splitting. Did you mean: cat "$cgroup"/"$source" In tests/integration/helpers.bash line 242: current="$(get_cgroup_value $1)" ^-- SC2086: Double quote to prevent globbing and word splitting. Did you mean: current="$(get_cgroup_value "$1")" In tests/integration/helpers.bash line 245: echo "current" $current "!?" "$expected" ^------^ SC2086: Double quote to prevent globbing and word splitting. Did you mean: echo "current" "$current" "!?" "$expected" In tests/integration/helpers.bash line 257: [ $(id -u) != "0" ] && user="--user" ^------^ SC2046: Quote this to prevent word splitting. In tests/integration/helpers.bash line 259: current=$(systemctl show $user --property $source $SD_UNIT_NAME | awk -F= '{print $2}') ^-----^ SC2086: Double quote to prevent globbing and word splitting. Did you mean: current=$(systemctl show $user --property "$source" $SD_UNIT_NAME | awk -F= '{print $2}') In tests/integration/helpers.bash line 261: [ "$current" = "$expected" ] || [ -n "$expected2" -a "$current" = "$expected2" ] ^-- SC2166: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined. In tests/integration/helpers.bash line 309: check_cgroup_value "cpu.weight" $weight ^-----^ SC2086: Double quote to prevent globbing and word splitting. Did you mean: check_cgroup_value "cpu.weight" "$weight" In tests/integration/helpers.bash line 310: check_systemd_value "CPUWeight" $weight ^-----^ SC2086: Double quote to prevent globbing and word splitting. Did you mean: check_systemd_value "CPUWeight" "$weight" In tests/integration/helpers.bash line 383: if [ $CGROUP_UNIFIED = "no" -a ! -e "${CGROUP_MEMORY_BASE_PATH}/memory.memsw.limit_in_bytes" ]; then ^-- SC2166: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined. In tests/integration/helpers.bash line 412: local cpu_count=$(grep -c '^processor' /proc/cpuinfo) ^-------^ SC2155: Declare and assign separately to avoid masking return values. In tests/integration/helpers.bash line 450: sleep $delay ^----^ SC2086: Double quote to prevent globbing and word splitting. Did you mean: sleep "$delay" In tests/integration/helpers.bash line 453: echo "Command \"$@\" failed $attempts times. Output: $output" ^-- SC2145: Argument mixes string and array. Use * or separate argument. In tests/integration/helpers.bash line 471: runc state $1 ^-- SC2086: Double quote to prevent globbing and word splitting. Did you mean: runc state "$1" In tests/integration/helpers.bash line 472: if [ $2 == "checkpointed" ]; then ^-- SC2086: Double quote to prevent globbing and word splitting. Did you mean: if [ "$2" == "checkpointed" ]; then In tests/integration/helpers.bash line 484: mkdir $dir ^--^ SC2086: Double quote to prevent globbing and word splitting. Did you mean: mkdir "$dir" In tests/integration/helpers.bash line 497: kill -9 $(cat "$dir/pid") ^---------------^ SC2046: Quote this to prevent word splitting. In tests/integration/helpers.bash line 508: export ROOT=$(mktemp -d "$BATS_RUN_TMPDIR/runc.XXXXXX") ^--^ SC2155: Declare and assign separately to avoid masking return values. In tests/integration/helpers.bash line 512: cd "$ROOT/bundle" ^---------------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails. Did you mean: cd "$ROOT/bundle" || exit In tests/integration/helpers.bash line 535: cd "$INTEGRATION_ROOT" ^--------------------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails. Did you mean: cd "$INTEGRATION_ROOT" || exit For more information: https://www.shellcheck.net/wiki/SC2145 -- Argument mixes string and array. ... https://www.shellcheck.net/wiki/SC2034 -- TESTDATA appears unused. Verify u... https://www.shellcheck.net/wiki/SC2046 -- Quote this to prevent word splitt... Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent f65276d commit fb629db

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
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: 33 additions & 29 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

@@ -73,8 +76,8 @@ function runc_rootless_idmap() {
7376
update_config ' .mounts |= map((select(.type == "devpts") | .options += ["gid=5"]) // .)
7477
| .linux.uidMappings += [{"hostID": '"$ROOTLESS_UIDMAP_START"', "containerID": 1000, "size": '"$ROOTLESS_UIDMAP_LENGTH"'}]
7578
| .linux.gidMappings += [{"hostID": '"$ROOTLESS_GIDMAP_START"', "containerID": 100, "size": 1}]
76-
| .linux.gidMappings += [{"hostID": '"$(($ROOTLESS_GIDMAP_START + 10))"', "containerID": 1, "size": 20}]
77-
| .linux.gidMappings += [{"hostID": '"$(($ROOTLESS_GIDMAP_START + 100))"', "containerID": 1000, "size": '"$(($ROOTLESS_GIDMAP_LENGTH - 1000))"'}]'
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))"'}]'
7881
}
7982

8083
# Returns systemd version as a number (-1 if systemd is not enabled/supported).
@@ -121,9 +124,9 @@ function init_cgroup_paths() {
121124
CGROUP_SUBSYSTEMS=$(awk '!/^#/ {print $1}' /proc/cgroups)
122125
local g base_path
123126
for g in ${CGROUP_SUBSYSTEMS}; do
124-
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)
125128
test -z "$base_path" && continue
126-
eval CGROUP_${g^^}_BASE_PATH="${base_path}"
129+
eval CGROUP_"${g^^}"_BASE_PATH="${base_path}"
127130
done
128131
fi
129132
}
@@ -162,39 +165,39 @@ function get_cgroup_value() {
162165
local source=$1
163166
local cgroup var current
164167

165-
if [ "x$CGROUP_UNIFIED" = "xyes" ]; then
168+
if [ "$CGROUP_UNIFIED" = "yes" ]; then
166169
cgroup=$CGROUP_PATH
167170
else
168171
var=${source%%.*} # controller name (e.g. memory)
169172
var=CGROUP_${var^^}_BASE_PATH # variable name (e.g. CGROUP_MEMORY_BASE_PATH)
170-
eval cgroup=\$${var}${REL_CGROUPS_PATH}
173+
eval cgroup=\$"${var}${REL_CGROUPS_PATH}"
171174
fi
172-
cat $cgroup/$source
175+
cat "$cgroup/$source"
173176
}
174177

175178
# Helper to check a if value in a cgroup file matches the expected one.
176179
function check_cgroup_value() {
177180
local current
178-
current="$(get_cgroup_value $1)"
181+
current="$(get_cgroup_value "$1")"
179182
local expected=$2
180183

181-
echo "current" $current "!?" "$expected"
184+
echo "current $current !? $expected"
182185
[ "$current" = "$expected" ]
183186
}
184187

185188
# Helper to check a value in systemd.
186189
function check_systemd_value() {
187190
[ -z "${RUNC_USE_SYSTEMD}" ] && return
188-
local source=$1
191+
local source="$1"
189192
[ "$source" = "unsupported" ] && return
190193
local expected="$2"
191194
local expected2="$3"
192195
local user=""
193-
[ $(id -u) != "0" ] && user="--user"
196+
[ "$(id -u)" != "0" ] && user="--user"
194197

195-
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}')
196199
echo "systemd $source: current $current !? $expected $expected2"
197-
[ "$current" = "$expected" ] || [ -n "$expected2" -a "$current" = "$expected2" ]
200+
[ "$current" = "$expected" ] || [[ -n "$expected2" && "$current" = "$expected2" ]]
198201
}
199202

200203
function check_cpu_quota() {
@@ -242,8 +245,8 @@ function check_cpu_shares() {
242245
function check_cpu_weight() {
243246
local weight=$1
244247

245-
check_cgroup_value "cpu.weight" $weight
246-
check_systemd_value "CPUWeight" $weight
248+
check_cgroup_value "cpu.weight" "$weight"
249+
check_systemd_value "CPUWeight" "$weight"
247250
}
248251

249252
# Helper function to set a resources limit
@@ -316,7 +319,7 @@ function requires() {
316319
;;
317320
cgroups_swap)
318321
init_cgroup_paths
319-
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
320323
skip_me=1
321324
fi
322325
;;
@@ -345,8 +348,9 @@ function requires() {
345348
fi
346349
;;
347350
smp)
348-
local cpu_count=$(grep -c '^processor' /proc/cpuinfo)
349-
if [ "$cpu_count" -lt 2 ]; then
351+
local cpus
352+
cpus=$(grep -c '^processor' /proc/cpuinfo)
353+
if [ "$cpus" -lt 2 ]; then
350354
skip_me=1
351355
fi
352356
;;
@@ -383,10 +387,10 @@ function retry() {
383387
if [[ "$status" -eq 0 ]]; then
384388
return 0
385389
fi
386-
sleep $delay
390+
sleep "$delay"
387391
done
388392

389-
echo "Command \"$@\" failed $attempts times. Output: $output"
393+
echo "Command \"$*\" failed $attempts times. Output: $output"
390394
false
391395
}
392396

@@ -404,8 +408,8 @@ function wait_for_container() {
404408

405409
function testcontainer() {
406410
# test state of container
407-
runc state $1
408-
if [ $2 == "checkpointed" ]; then
411+
runc state "$1"
412+
if [ "$2" == "checkpointed" ]; then
409413
[ "$status" -eq 1 ]
410414
return
411415
fi
@@ -417,7 +421,7 @@ function setup_recvtty() {
417421
[ -z "$ROOT" ] && return 1 # must not be called without ROOT set
418422
local dir="$ROOT/tty"
419423

420-
mkdir $dir
424+
mkdir "$dir"
421425
export CONSOLE_SOCKET="$dir/sock"
422426

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

431435
# When we kill recvtty, the container will also be killed.
432436
if [ -f "$dir/pid" ]; then
433-
kill -9 $(cat "$dir/pid")
437+
kill -9 "$(cat "$dir/pid")"
434438
fi
435439

436440
# Clean up the files that might be left over.
@@ -441,11 +445,11 @@ function setup_bundle() {
441445
local image="$1"
442446

443447
# Root for various container directories (state, tty, bundle).
444-
export ROOT=$(mktemp -d "$BATS_RUN_TMPDIR/runc.XXXXXX")
448+
ROOT=$(mktemp -d "$BATS_RUN_TMPDIR/runc.XXXXXX")
445449
mkdir -p "$ROOT/state" "$ROOT/bundle/rootfs"
446450

447451
setup_recvtty
448-
cd "$ROOT/bundle"
452+
cd "$ROOT/bundle" || return
449453

450454
tar --exclude './dev/*' -C rootfs -xf "$image"
451455

@@ -468,7 +472,7 @@ function setup_debian() {
468472
function teardown_bundle() {
469473
[ -z "$ROOT" ] && return 0 # nothing to teardown
470474

471-
cd "$INTEGRATION_ROOT"
475+
cd "$INTEGRATION_ROOT" || return
472476
teardown_recvtty
473477
local ct
474478
for ct in $(__runc list -q); do

0 commit comments

Comments
 (0)