1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-05 09:17:44 +03:00

test: shellcheck-ify test scripts

This commit is contained in:
Frantisek Sumsal 2021-09-29 20:30:08 +02:00
parent 91c64ad620
commit 1c3f490f23
19 changed files with 103 additions and 84 deletions

View File

@ -14,7 +14,7 @@ test_append_files() {
# On openSUSE the static linked version of busybox is named "busybox-static". # On openSUSE the static linked version of busybox is named "busybox-static".
busybox="$(type -P busybox-static || type -P busybox)" busybox="$(type -P busybox-static || type -P busybox)"
inst_simple "$busybox" "$(dirname $busybox)/busybox" inst_simple "$busybox" "$(dirname "$busybox")/busybox"
if selinuxenabled >/dev/null; then if selinuxenabled >/dev/null; then
image_install selinuxenabled image_install selinuxenabled

View File

@ -2,6 +2,7 @@
set -e set -e
TEST_DESCRIPTION="test OnSuccess= + Uphold= + PropagatesStopTo= + BindsTo=" TEST_DESCRIPTION="test OnSuccess= + Uphold= + PropagatesStopTo= + BindsTo="
. $TEST_BASE_DIR/test-functions # shellcheck source=test/test-functions
. "$TEST_BASE_DIR/test-functions"
do_test "$@" 57 do_test "$@" 57

View File

@ -1,7 +1,10 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -e
TEST_DESCRIPTION="test systemd-repart" TEST_DESCRIPTION="test systemd-repart"
TEST_NO_NSPAWN=1 TEST_NO_NSPAWN=1
. $TEST_BASE_DIR/test-functions
# shellcheck source=test/test-functions
. "$TEST_BASE_DIR/test-functions"
do_test "$@" do_test "$@"

View File

@ -1,9 +1,10 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -e
TEST_DESCRIPTION="Test auto restart of exited services which are stuck in reloading state"
TEST_DESCRIPTION="Test auto restart of exited services which are stuck in reloading state"
TEST_NO_QEMU=1 TEST_NO_QEMU=1
. $TEST_BASE_DIR/test-functions # shellcheck source=test/test-functions
. "$TEST_BASE_DIR/test-functions"
do_test "$@" do_test "$@"

View File

@ -1,7 +1,9 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -e
TEST_DESCRIPTION="Test that mount/unmount storms can enter/exit rate limit state and will not leak units" TEST_DESCRIPTION="Test that mount/unmount storms can enter/exit rate limit state and will not leak units"
. $TEST_BASE_DIR/test-functions # shellcheck source=test/test-functions
. "$TEST_BASE_DIR/test-functions"
do_test "$@" do_test "$@"

View File

@ -1,9 +1,10 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e
TEST_DESCRIPTION="test RestrictNetworkInterfaces="
TEST_NO_NSPAWN=1 TEST_NO_NSPAWN=1
set -e # shellcheck source=test/test-functions
TEST_DESCRIPTION="test RestrictNetworkInterfaces=" . "$TEST_BASE_DIR/test-functions"
. $TEST_BASE_DIR/test-functions
do_test "$@" 62 do_test "$@"

View File

@ -10,7 +10,7 @@
set -e set -e
export SYSTEMD_LOG_LEVEL=info export SYSTEMD_LOG_LEVEL=info
ROOTDIR=$(dirname $(dirname $(readlink -f $0))) ROOTDIR="$(dirname "$(dirname "$(readlink -f "$0")")")"
SYSTEMD_HWDB="${1:?missing argument}" SYSTEMD_HWDB="${1:?missing argument}"
if [ ! -x "$SYSTEMD_HWDB" ]; then if [ ! -x "$SYSTEMD_HWDB" ]; then
@ -18,7 +18,8 @@ if [ ! -x "$SYSTEMD_HWDB" ]; then
exit 1 exit 1
fi fi
D=$(mktemp --tmpdir --directory "hwdb-test.XXXXXXXXXX") D="$(mktemp --tmpdir --directory "hwdb-test.XXXXXXXXXX")"
# shellcheck disable=SC2064
trap "rm -rf '$D'" EXIT INT QUIT PIPE trap "rm -rf '$D'" EXIT INT QUIT PIPE
mkdir -p "$D/etc/udev" mkdir -p "$D/etc/udev"
ln -s "$ROOTDIR/hwdb.d" "$D/etc/udev/hwdb.d" ln -s "$ROOTDIR/hwdb.d" "$D/etc/udev/hwdb.d"

View File

@ -3,7 +3,7 @@ set -e
if [ "$NO_BUILD" ]; then if [ "$NO_BUILD" ]; then
BUILD_DIR="" BUILD_DIR=""
elif BUILD_DIR="$($(dirname "$0")/../tools/find-build-dir.sh)"; then elif BUILD_DIR="$("$(dirname "$0")/../tools/find-build-dir.sh")"; then
ninja -C "$BUILD_DIR" ninja -C "$BUILD_DIR"
else else
echo "No build found, please set BUILD_DIR or NO_BUILD" >&2 echo "No build found, please set BUILD_DIR or NO_BUILD" >&2
@ -73,35 +73,36 @@ fi
# Run actual tests (if requested) # Run actual tests (if requested)
if [[ $args =~ [a-z] ]]; then if [[ $args =~ [a-z] ]]; then
for TEST in $SELECTED_TESTS; do for TEST in $SELECTED_TESTS; do
COUNT=$(($COUNT+1)) COUNT=$((COUNT+1))
pass_deny_list $TEST || continue pass_deny_list "$TEST" || continue
start=$(date +%s) start=$(date +%s)
echo -e "\n--x-- Running $TEST --x--" echo -e "\n--x-- Running $TEST --x--"
set +e set +e
# shellcheck disable=SC2086
( set -x ; make -C "$TEST" $args ) ( set -x ; make -C "$TEST" $args )
RESULT=$? RESULT=$?
set -e set -e
echo "--x-- Result of $TEST: $RESULT --x--" echo "--x-- Result of $TEST: $RESULT --x--"
results["$TEST"]="$RESULT" results["$TEST"]="$RESULT"
times["$TEST"]=$(( $(date +%s) - $start )) times["$TEST"]=$(( $(date +%s) - start ))
[ "$RESULT" -ne "0" ] && FAILURES=$(($FAILURES+1)) [ "$RESULT" -ne "0" ] && FAILURES=$((FAILURES+1))
done done
fi fi
# Run clean-again, if requested, and if no tests failed # Run clean-again, if requested, and if no tests failed
if [ $FAILURES -eq 0 -a $CLEANAGAIN = 1 ]; then if [[ $FAILURES -eq 0 && $CLEANAGAIN -eq 1 ]]; then
for TEST in ${!results[@]}; do for TEST in "${!results[@]}"; do
( set -x ; make -C "$TEST" clean-again ) ( set -x ; make -C "$TEST" clean-again )
done done
fi fi
echo "" echo ""
for TEST in ${!results[@]}; do for TEST in "${!results[@]}"; do
RESULT="${results[$TEST]}" RESULT="${results[$TEST]}"
time="${times[$TEST]}" time="${times[$TEST]}"
string=$([ "$RESULT" = "0" ] && echo "SUCCESS" || echo "FAIL") string=$([ "$RESULT" = "0" ] && echo "SUCCESS" || echo "FAIL")

View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# shellcheck disable=SC2031 # shellcheck disable=SC2030,SC2031
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*- # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh tw=180 # ex: ts=8 sw=4 sts=4 et filetype=sh tw=180
# Note: the shellcheck line above disables warning for variables which were # Note: the shellcheck line above disables warning for variables which were
@ -992,9 +992,11 @@ install_iscsi() {
# dumps a list of files (perl modules) required by `tgt-admin` at # dumps a list of files (perl modules) required by `tgt-admin` at
# the runtime plus any DSOs loaded via DynaLoader. This list is then # the runtime plus any DSOs loaded via DynaLoader. This list is then
# passed to `inst_simple` which installs the necessary files into the image # passed to `inst_simple` which installs the necessary files into the image
#
# shellcheck disable=SC2016
while read -r file; do while read -r file; do
inst_simple "$file" inst_simple "$file"
done < <(perl -- <(cat $(command -v tgt-admin) <(echo -e 'use DynaLoader; print map { "$_\n" } values %INC; print join("\n", @DynaLoader::dl_shared_objects)')) -p | awk '/^\// { print $1 }') done < <(perl -- <(cat "$(command -v tgt-admin)" <(echo -e 'use DynaLoader; print map { "$_\n" } values %INC; print join("\n", @DynaLoader::dl_shared_objects)')) -p | awk '/^\// { print $1 }')
fi fi
} }
@ -1529,7 +1531,7 @@ install_haveged() {
dinfo "Install haveged files" dinfo "Install haveged files"
inst /usr/sbin/haveged inst /usr/sbin/haveged
for u in /usr/lib/systemd/system/haveged*; do for u in /usr/lib/systemd/system/haveged*; do
inst $u inst "$u"
done done
fi fi
} }
@ -1718,6 +1720,7 @@ install_pam() {
done done
} }
# shellcheck disable=SC2120
install_keymaps() { install_keymaps() {
dinfo "Install keymaps" dinfo "Install keymaps"
# The first three paths may be deprecated. # The first three paths may be deprecated.

View File

@ -18,11 +18,13 @@ for f in "$src"/test-*.input; do
( (
out=$(mktemp --tmpdir --directory "test-network-generator-conversion.XXXXXXXXXX") out=$(mktemp --tmpdir --directory "test-network-generator-conversion.XXXXXXXXXX")
# shellcheck disable=SC2064
trap "rm -rf '$out'" EXIT INT QUIT PIPE trap "rm -rf '$out'" EXIT INT QUIT PIPE
$generator --root "$out" -- $(cat $f) # shellcheck disable=SC2046
$generator --root "$out" -- $(cat "$f")
if ! diff -u "$out"/run/systemd/network ${f%.input}.expected; then if ! diff -u "$out/run/systemd/network" "${f%.input}.expected"; then
echo "**** Unexpected output for $f" echo "**** Unexpected output for $f"
exit 1 exit 1
fi fi

View File

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
echo "$0 $@" echo "$0 $*"
test "$(basename $0)" = "script.sh" || exit 1 test "$(basename "$0")" = "script.sh" || exit 1
test "$1" = "--version" || exit 2 test "$1" = "--version" || exit 2
echo "Life is good" echo "Life is good"

View File

@ -1,62 +1,62 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -x set -eux
set -e
set -o pipefail set -o pipefail
# sleep interval (seconds) # sleep interval (seconds)
: ${sleep_interval:=1} sleep_interval="${sleep_interval:-1}"
# extend_timeout_interval second(s) # extend_timeout_interval second(s)
: ${extend_timeout_interval:=1} extend_timeout_interval="${extend_timeout_interval:-1}"
# number of sleep_intervals before READY=1 # number of sleep_intervals before READY=1
: ${start_intervals:=10} start_intervals="${start_intervals:-10}"
# number of sleep_intervals before exiting # number of sleep_intervals before exiting
: ${stop_intervals:=10} stop_intervals="${stop_intervals:-10}"
# run intervals, number of sleep_intervals to run # run intervals, number of sleep_intervals to run
: ${run_intervals:=7} run_intervals="${run_intervals:-7}"
# We convert to usec # We convert to usec
extend_timeout_interval=$(( $extend_timeout_interval * 1000000 )) extend_timeout_interval=$((extend_timeout_interval * 1000000))
# shellcheck disable=SC2064
trap "{ touch /${SERVICE}.terminated; exit 1; }" SIGTERM SIGABRT trap "{ touch /${SERVICE}.terminated; exit 1; }" SIGTERM SIGABRT
rm -f /${SERVICE}.* rm -f "/${SERVICE}".*
touch /${SERVICE}.startfail touch "/${SERVICE}.startfail"
systemd-notify EXTEND_TIMEOUT_USEC=$extend_timeout_interval systemd-notify EXTEND_TIMEOUT_USEC="$extend_timeout_interval"
while [ $start_intervals -gt 0 ] while [[ $start_intervals -gt 0 ]]
do do
sleep $sleep_interval sleep "$sleep_interval"
start_intervals=$(( $start_intervals - 1 )) start_intervals=$((start_intervals - 1))
systemd-notify EXTEND_TIMEOUT_USEC=$extend_timeout_interval systemd-notify EXTEND_TIMEOUT_USEC="$extend_timeout_interval"
done done
systemd-notify --ready --status="Waiting for your request" systemd-notify --ready --status="Waiting for your request"
touch /${SERVICE}.runtimefail touch "/${SERVICE}.runtimefail"
rm /${SERVICE}.startfail rm "/${SERVICE}.startfail"
systemd-notify EXTEND_TIMEOUT_USEC=$extend_timeout_interval systemd-notify EXTEND_TIMEOUT_USEC="$extend_timeout_interval"
while [ $run_intervals -gt 0 ] while [[ $run_intervals -gt 0 ]]
do do
sleep $sleep_interval sleep "$sleep_interval"
run_intervals=$(( $run_intervals - 1 )) run_intervals=$((run_intervals - 1))
systemd-notify EXTEND_TIMEOUT_USEC=$extend_timeout_interval systemd-notify EXTEND_TIMEOUT_USEC="$extend_timeout_interval"
done done
systemd-notify STOPPING=1 systemd-notify STOPPING=1
touch /${SERVICE}.stopfail touch "/${SERVICE}.stopfail"
rm /${SERVICE}.runtimefail rm "/${SERVICE}.runtimefail"
systemd-notify EXTEND_TIMEOUT_USEC=$extend_timeout_interval systemd-notify EXTEND_TIMEOUT_USEC="$extend_timeout_interval"
while [ $stop_intervals -gt 0 ] while [[ $stop_intervals -gt 0 ]]
do do
sleep $sleep_interval sleep "$sleep_interval"
stop_intervals=$(( $stop_intervals - 1 )) stop_intervals=$((stop_intervals - 1))
systemd-notify EXTEND_TIMEOUT_USEC=$extend_timeout_interval systemd-notify EXTEND_TIMEOUT_USEC="$extend_timeout_interval"
done done
touch /${SERVICE}.success touch "/${SERVICE}.success"
rm /${SERVICE}.stopfail rm "/${SERVICE}.stopfail"
exit 0 exit 0

View File

@ -8,10 +8,11 @@ input="$2"
expected="$3" expected="$3"
output=$(mktemp --tmpdir "test-udev-dmi-memory-id.XXXXXXXXXX") output=$(mktemp --tmpdir "test-udev-dmi-memory-id.XXXXXXXXXX")
# shellcheck disable=SC2064
trap "rm '$output'" EXIT INT QUIT PIPE trap "rm '$output'" EXIT INT QUIT PIPE
( (
set -x set -x
"$dmi_memory_id" -F "$input" >$output "$dmi_memory_id" -F "$input" >"$output"
diff -u "$output" "$expected" diff -u "$output" "$expected"
) )

View File

@ -7,7 +7,7 @@ trap "journalctl --rotate --vacuum-size=16M" EXIT
# Rotation/flush test, see https://github.com/systemd/systemd/issues/19895 # Rotation/flush test, see https://github.com/systemd/systemd/issues/19895
journalctl --relinquish-var journalctl --relinquish-var
for i in {0..50}; do for _ in {0..50}; do
dd if=/dev/urandom bs=1M count=1 | base64 | systemd-cat dd if=/dev/urandom bs=1M count=1 | base64 | systemd-cat
done done
journalctl --rotate journalctl --rotate
@ -116,7 +116,7 @@ cmp /expected /output
# test that LogLevelMax can also suppress logging about services, not only by services # test that LogLevelMax can also suppress logging about services, not only by services
systemctl start silent-success systemctl start silent-success
journalctl --sync journalctl --sync
[[ -z `journalctl -b -q -u silent-success.service` ]] [[ -z "$(journalctl -b -q -u silent-success.service)" ]]
# Add new tests before here, the journald restarts below # Add new tests before here, the journald restarts below
# may make tests flappy. # may make tests flappy.

View File

@ -8,9 +8,9 @@ function check_validity() {
local f ID_OR_HANDLE local f ID_OR_HANDLE
for f in /run/udev/watch/*; do for f in /run/udev/watch/*; do
ID_OR_HANDLE=$(readlink $f) ID_OR_HANDLE="$(readlink "$f")"
test -L /run/udev/watch/${ID_OR_HANDLE} test -L "/run/udev/watch/${ID_OR_HANDLE}"
test $(readlink /run/udev/watch/${ID_OR_HANDLE}) = $(basename $f) test "$(readlink "/run/udev/watch/${ID_OR_HANDLE}")" = "$(basename "$f")"
done done
} }
@ -49,7 +49,7 @@ check
MAJOR=$(udevadm info /dev/sda | grep -e '^E: MAJOR=' | sed -e 's/^E: MAJOR=//') MAJOR=$(udevadm info /dev/sda | grep -e '^E: MAJOR=' | sed -e 's/^E: MAJOR=//')
MINOR=$(udevadm info /dev/sda | grep -e '^E: MINOR=' | sed -e 's/^E: MINOR=//') MINOR=$(udevadm info /dev/sda | grep -e '^E: MINOR=' | sed -e 's/^E: MINOR=//')
test -L /run/udev/watch/b${MAJOR}:${MINOR} test -L "/run/udev/watch/b${MAJOR}:${MINOR}"
cat >/run/udev/rules.d/50-testsuite.rules <<EOF cat >/run/udev/rules.d/50-testsuite.rules <<EOF
ACTION=="change", SUBSYSTEM=="block", KERNEL=="sda", OPTIONS:="nowatch" ACTION=="change", SUBSYSTEM=="block", KERNEL=="sda", OPTIONS:="nowatch"
@ -59,7 +59,7 @@ check
MAJOR=$(udevadm info /dev/sda | grep -e '^E: MAJOR=' | sed -e 's/^E: MAJOR=//') MAJOR=$(udevadm info /dev/sda | grep -e '^E: MAJOR=' | sed -e 's/^E: MAJOR=//')
MINOR=$(udevadm info /dev/sda | grep -e '^E: MINOR=' | sed -e 's/^E: MINOR=//') MINOR=$(udevadm info /dev/sda | grep -e '^E: MINOR=' | sed -e 's/^E: MINOR=//')
test ! -e /run/udev/watch/b${MAJOR}:${MINOR} test ! -e "/run/udev/watch/b${MAJOR}:${MINOR}"
rm /run/udev/rules.d/00-debug.rules rm /run/udev/rules.d/00-debug.rules
rm /run/udev/rules.d/50-testsuite.rules rm /run/udev/rules.d/50-testsuite.rules

View File

@ -43,7 +43,7 @@ if systemctl --version | grep -q -- +OPENSSL ; then
systemd-creds encrypt --name=test-54 /tmp/test-54-plaintext /tmp/test-54-ciphertext systemd-creds encrypt --name=test-54 /tmp/test-54-plaintext /tmp/test-54-ciphertext
systemd-creds decrypt --name=test-54 /tmp/test-54-ciphertext | cmp /tmp/test-54-plaintext systemd-creds decrypt --name=test-54 /tmp/test-54-ciphertext | cmp /tmp/test-54-plaintext
systemd-run -p SetCredentialEncrypted=test-54:"`cat /tmp/test-54-ciphertext`" \ systemd-run -p SetCredentialEncrypted=test-54:"$(cat /tmp/test-54-ciphertext)" \
--wait \ --wait \
--pipe \ --pipe \
cat '${CREDENTIALS_DIRECTORY}/test-54' | cmp /tmp/test-54-plaintext cat '${CREDENTIALS_DIRECTORY}/test-54' | cmp /tmp/test-54-plaintext

View File

@ -3,6 +3,7 @@ set -eux
set -o pipefail set -o pipefail
TESTS_GLOB="test-loop-block" TESTS_GLOB="test-loop-block"
. $(dirname $0)/testsuite-02.sh # shellcheck source=test/units/testsuite-02.sh
. "$(dirname "$0")/testsuite-02.sh"
exit 0 exit 0

View File

@ -6,28 +6,28 @@ setup() {
systemd-analyze log-level debug systemd-analyze log-level debug
systemd-analyze log-target console systemd-analyze log-target console
for i in `seq 0 3`; for i in {0..3};
do do
ip netns del ns${i} || true ip netns del "ns${i}" || true
ip link del veth${i} || true ip link del "veth${i}" || true
ip netns add ns${i} ip netns add "ns${i}"
ip link add veth${i} type veth peer name veth${i}_ ip link add "veth${i}" type veth peer name "veth${i}_"
ip link set veth${i}_ netns ns${i} ip link set "veth${i}_" netns "ns${i}"
ip -n ns${i} link set dev veth${i}_ up ip -n "ns${i}" link set dev "veth${i}_" up
ip -n ns${i} link set dev lo up ip -n "ns${i}" link set dev lo up
ip -n ns${i} addr add "192.168.113."$((4*i+1))/30 dev veth${i}_ ip -n "ns${i}" addr add "192.168.113."$((4*i+1))/30 dev "veth${i}_"
ip link set dev veth${i} up ip link set dev "veth${i}" up
ip addr add "192.168.113."$((4*i+2))/30 dev veth${i} ip addr add "192.168.113."$((4*i+2))/30 dev "veth${i}"
done done
} }
teardown() { teardown() {
set +e set +e
for i in `seq 0 3`; for i in {0..3};
do do
ip netns del ns${i} ip netns del "ns${i}"
ip link del veth${i} ip link del "veth${i}"
done done
systemd-analyze log-level info systemd-analyze log-level info

View File

@ -76,6 +76,8 @@ helper_wait_for_pvscan() {
# Get major and minor numbers from the udev database # Get major and minor numbers from the udev database
# (udevadm returns MAJOR= and MINOR= expressions, so let's pull them into # (udevadm returns MAJOR= and MINOR= expressions, so let's pull them into
# the current environment via `source` for easier parsing) # the current environment via `source` for easier parsing)
#
# shellcheck source=/dev/null
source <(udevadm info -q property "$real_dev" | grep -E "(MAJOR|MINOR)=") source <(udevadm info -q property "$real_dev" | grep -E "(MAJOR|MINOR)=")
# Sanity check if we got correct major and minor numbers # Sanity check if we got correct major and minor numbers
test -e "/sys/dev/block/$MAJOR:$MINOR/" test -e "/sys/dev/block/$MAJOR:$MINOR/"