]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: bunch of assorted tweaks to make shellcheck happy 19266/head
authorFrantisek Sumsal <frantisek@sumsal.cz>
Fri, 9 Apr 2021 17:56:12 +0000 (19:56 +0200)
committerFrantisek Sumsal <frantisek@sumsal.cz>
Tue, 13 Apr 2021 17:14:35 +0000 (19:14 +0200)
12 files changed:
test/units/testsuite-02.sh
test/units/testsuite-05.sh
test/units/testsuite-07.sh
test/units/testsuite-11.sh
test/units/testsuite-12.sh
test/units/testsuite-17.03.sh
test/units/testsuite-25.sh
test/units/testsuite-32.sh
test/units/testsuite-36.sh
test/units/testsuite-38.sh
test/units/testsuite-43.sh
test/units/testsuite-55-slowgrowth.sh

index ea4efda70bdbcbc45350576b20a20d80cedc4829..da02304a32a2577e711c83c7bdc69e304b0ab860 100755 (executable)
@@ -4,10 +4,10 @@ set -o pipefail
 
 NPROC=$(nproc)
 MAX_QUEUE_SIZE=${NPROC:-2}
-IFS=$'\n' TEST_LIST=($(ls /usr/lib/systemd/tests/test-*))
+mapfile -t TEST_LIST < <(find /usr/lib/systemd/tests/ -maxdepth 1 -type f -name "test-*")
 
 # reset state
-rm /failed-tests /skipped-tests /skipped
+rm -fv /failed-tests /skipped-tests /skipped
 
 # Check & report test results
 # Arguments:
@@ -56,8 +56,7 @@ for task in "${TEST_LIST[@]}"; do
         for key in "${!running[@]}"; do
             if ! kill -0 "${running[$key]}" &>/dev/null; then
                 # Task has finished, report its result and drop it from the queue
-                wait "${running[$key]}"
-                ec=$?
+                wait "${running[$key]}" && ec=0 || ec=$?
                 report_result "$key" $ec
                 unset running["$key"]
                 # Break from inner for loop and outer while loop to skip
index f899fcaa4f628bfc72c9b0848d58009331983646..c98d849b08aa0004be48ae3a37a7495f6a209c97 100755 (executable)
@@ -18,7 +18,9 @@ systemctl daemon-reload
 [[ "$(systemctl show -P LimitNOFILESoft testsuite-05.service)" = "10000" ]]
 [[ "$(systemctl show -P LimitNOFILE testsuite-05.service)" = "16384" ]]
 
+# shellcheck disable=SC2016
 systemd-run --wait -t bash -c '[[ "$(ulimit -n -S)" = "10000" ]]'
+# shellcheck disable=SC2016
 systemd-run --wait -t bash -c '[[ "$(ulimit -n -H)" = "16384" ]]'
 
 touch /testok
index c63394e68cdb45341b49448391a6395684b92d2b..bd1da341acb67b8bf4cef915182d4e06982b0590 100755 (executable)
@@ -2,30 +2,30 @@
 set -eux
 set -o pipefail
 
->/failed
+>/failed
 
-cat <<'EOL' >/lib/systemd/system/my.service
+cat >/lib/systemd/system/my.service <<EOF
 [Service]
 Type=oneshot
 ExecStart=/bin/echo Timer runs me
-EOL
+EOF
 
-cat <<'EOL' >/lib/systemd/system/my.timer
+cat >/lib/systemd/system/my.timer <<EOF
 [Timer]
 OnBootSec=10s
 OnUnitInactiveSec=1h
-EOL
+EOF
 
 systemctl unmask my.timer
 
 systemctl start my.timer
 
 mkdir -p /etc/systemd/system/my.timer.d/
-cat <<'EOL' >/etc/systemd/system/my.timer.d/override.conf
+cat >/etc/systemd/system/my.timer.d/override.conf <<EOF
 [Timer]
 OnBootSec=10s
 OnUnitInactiveSec=1h
-EOL
+EOF
 
 systemctl daemon-reload
 
index 3ec391f6e651581fcb61de3f7f24c8de33a7b967..97ab8be7574e4a342747c38dce71d3f9b5b6b885 100755 (executable)
@@ -2,7 +2,7 @@
 set -eux
 set -o pipefail
 
-systemctl start fail-on-restart.service
+systemctl --no-block start fail-on-restart.service
 active_state=$(systemctl show --value --property ActiveState fail-on-restart.service)
 while [[ "$active_state" == "activating" || "$active_state" == "active" ]]; do
     sleep 1
index a02f38c488d090161e0f4f9f1dfd15b1b7913ec2..c4ee600abe1c96f692e16c92ea66e0a7bbd405d5 100755 (executable)
@@ -3,7 +3,7 @@ set -eux
 set -o pipefail
 
 U=/run/systemd/system/test12.socket
-cat <<'EOF' >$U
+cat >$U <<EOF
 [Unit]
 Description=Test 12 socket
 [Socket]
@@ -13,7 +13,7 @@ SocketGroup=adm
 SocketMode=0660
 EOF
 
-cat <<'EOF' >/run/systemd/system/test12@.service
+cat >/run/systemd/system/test12@.service <<EOF
 [Unit]
 Description=Test service
 [Service]
index e5178ce554371c32b302ec24f6cb025367e2ea31..0452a061260677c5677d8b11dfe2e187555cb0ba 100755 (executable)
@@ -27,7 +27,7 @@ run_test() {
 
     echo add >/sys/class/net/lo/uevent
 
-    for n in {1..20}; do
+    for _ in {1..20}; do
         sleep 5
         if coredumpctl --since "$since" --no-legend --no-pager | grep /bin/udevadm ; then
             return 0
index 05448efb0ac77453ed4cf2c5a16c469ed42a82a8..fbe2d0b1d4d9965906ffed22b68862a3d78ff0f6 100755 (executable)
@@ -120,6 +120,7 @@ test ! -f /var/lib/machines/scratch4
 machinectl image-status scratch4 && { echo 'unexpected success'; exit 1; }
 
 # Test import-tar hyphen/stdin pipe behavior
+# shellcheck disable=SC2002
 cat /var/tmp/scratch.tar.gz | machinectl import-tar - scratch5
 test -d /var/lib/machines/scratch5
 machinectl image-status scratch5
index a025b73a0423ecc90c72ba106c5cfd42159cf1c5..2393601a007ff8932895d83351aa21b6a652483e 100755 (executable)
@@ -9,7 +9,6 @@ set -o pipefail
 # kernels where the concept was still new.
 
 if test -f /sys/fs/cgroup/system.slice/testsuite-32.service/memory.oom.group; then
-
     systemd-analyze log-level debug
     systemd-analyze log-target console
 
index 6d3713c96231edd5df98102a4d13cb92c575f0f9..94fd927a5d13c16446ea2cecda574f3f1df2e33e 100755 (executable)
@@ -3,11 +3,12 @@ set -eux
 set -o pipefail
 
 at_exit() {
-    if [ $? -ne 0 ]; then
+    # shellcheck disable=SC2181
+    if [[ $? -ne 0 ]]; then
         # We're exiting with a non-zero EC, let's dump test artifacts
         # for easier debugging
-        [ -f "$straceLog" ] && cat "$straceLog"
-        [ -f "$journalLog" ] && cat "$journalLog"
+        [[ -v straceLog && -f "$straceLog" ]] && cat "$straceLog"
+        [[ -v journalLog && -f "$journalLog" ]] && cat "$journalLog"
     fi
 }
 
@@ -26,25 +27,28 @@ testUnitFile="/run/systemd/system/$testUnit"
 testUnitNUMAConf="$testUnitFile.d/numa.conf"
 
 # Sleep constants (we should probably figure out something better but nothing comes to mind)
-journalSleep=5
 sleepAfterStart=1
 
 # Journal cursor for easier navigation
 journalCursorFile="jounalCursorFile"
 
 startStrace() {
-    coproc strace -qq -p 1 -o $straceLog -e set_mempolicy -s 1024 $1
+    coproc strace -qq -p 1 -o "$straceLog" -e set_mempolicy -s 1024 ${1:+"$1"}
     # Wait for strace to properly "initialize"
     sleep $sleepAfterStart
 }
 
 stopStrace() {
-    kill -s TERM $COPROC_PID
+    [[ -v COPROC_PID ]] || return
+
+    local PID=$COPROC_PID
+    kill -s TERM "$PID"
     # Make sure the strace process is indeed dead
-    while kill -0 $COPROC_PID 2>/dev/null; do sleep 0.1; done
+    while kill -0 "$PID" 2>/dev/null; do sleep 0.1; done
 }
 
 startJournalctl() {
+    : >"$journalCursorFile"
     # Save journal's cursor for later navigation
     journalctl --no-pager --cursor-file="$journalCursorFile" -n0 -ocat
 }
@@ -64,21 +68,24 @@ checkNUMA() {
 }
 
 writePID1NUMAPolicy() {
-    echo [Manager] >$confDir/numa.conf
-    echo NUMAPolicy=$1 >>$confDir/numa.conf
-    echo NUMAMask=$2 >>$confDir/numa.conf
+    cat >"$confDir/numa.conf" <<EOF
+[Manager]
+NUMAPolicy=${1:?missing argument: NUMAPolicy}
+NUMAMask=${2:-""}
+EOF
 }
 
 writeTestUnit() {
-    mkdir -p $testUnitFile.d/
-    echo [Service] >$testUnitFile
-    echo ExecStart=/bin/sleep 3600 >>$testUnitFile
+    mkdir -p "$testUnitFile.d/"
+    printf "[Service]\nExecStart=/bin/sleep 3600\n" >"$testUnitFile"
 }
 
 writeTestUnitNUMAPolicy() {
-    echo [Service] >$testUnitNUMAConf
-    echo NUMAPolicy=$1 >>$testUnitNUMAConf
-    echo NUMAMask=$2 >>$testUnitNUMAConf
+    cat >"$testUnitNUMAConf" <<EOF
+[Service]
+NUMAPolicy=${1:?missing argument: NUMAPolicy}
+NUMAMask=${2:-""}
+EOF
     systemctl daemon-reload
 }
 
index baf52fd4991af3ef626122c9c7bc8d2b60ce49c7..818f69bc63553bc3e84f9ca434f0de634918cfbf 100755 (executable)
@@ -14,11 +14,11 @@ start_test_service() {
 }
 
 dbus_freeze() {
-    local suffix=
-    suffix="${1##*.}"
+    local name object_path suffix
 
-    local name="$(echo ${1%.$suffix} | sed s/-/_2d/g)"
-    local object_path="/org/freedesktop/systemd1/unit/${name}_2e${suffix}"
+    suffix="${1##*.}"
+    name="${1%.$suffix}"
+    object_path="/org/freedesktop/systemd1/unit/${name//-/_2d}_2e${suffix}"
 
     busctl call \
            org.freedesktop.systemd1 \
@@ -28,11 +28,11 @@ dbus_freeze() {
 }
 
 dbus_thaw() {
-    local suffix=
-    suffix="${1##*.}"
+    local name object_path suffix
 
-    local name="$(echo ${1%.$suffix} | sed s/-/_2d/g)"
-    local object_path="/org/freedesktop/systemd1/unit/${name}_2e${suffix}"
+    suffix="${1##*.}"
+    name="${1%.$suffix}"
+    object_path="/org/freedesktop/systemd1/unit/${name//-/_2d}_2e${suffix}"
 
     busctl call \
            org.freedesktop.systemd1 \
@@ -62,11 +62,11 @@ dbus_thaw_unit() {
 }
 
 dbus_can_freeze() {
-    local suffix=
-    suffix="${1##*.}"
+    local name object_path suffix
 
-    local name="$(echo ${1%.$suffix} | sed s/-/_2d/g)"
-    local object_path="/org/freedesktop/systemd1/unit/${name}_2e${suffix}"
+    suffix="${1##*.}"
+    name="${1%.$suffix}"
+    object_path="/org/freedesktop/systemd1/unit/${name//-/_2d}_2e${suffix}"
 
     busctl get-property \
            org.freedesktop.systemd1 \
@@ -76,11 +76,11 @@ dbus_can_freeze() {
 }
 
 check_freezer_state() {
-    local suffix=
-    suffix="${1##*.}"
+    local name object_path suffix
 
-    local name="$(echo ${1%.$suffix} | sed s/-/_2d/g)"
-    local object_path="/org/freedesktop/systemd1/unit/${name}_2e${suffix}"
+    suffix="${1##*.}"
+    name="${1%.$suffix}"
+    object_path="/org/freedesktop/systemd1/unit/${name//-/_2d}_2e${suffix}"
 
     state=$(busctl get-property \
                    org.freedesktop.systemd1 \
index dcbe32def6194cb74d19141b2babf00acc344987..c6399f9920bb1882b20aa67992cbe1daf7325c75 100755 (executable)
@@ -7,6 +7,7 @@ systemd-analyze log-level debug
 runas() {
     declare userid=$1
     shift
+    # shellcheck disable=SC2016
     su "$userid" -s /bin/sh -c 'XDG_RUNTIME_DIR=/run/user/$UID exec "$@"' -- sh "$@"
 }
 
@@ -46,6 +47,7 @@ runas testuser systemd-run --wait --user --unit=test-protect-home-tmpfs \
     -P test ! -e /home/testuser
 
 # Confirm that home, /root, and /run/user are inaccessible under "yes"
+# shellcheck disable=SC2016
 runas testuser systemd-run --wait --user --unit=test-protect-home-yes \
     -p PrivateUsers=yes -p ProtectHome=yes \
     -P bash -c '
index 918ed6c34167e781090f34d00183a37f9bb47923..68bffad454b53611af052f307ba879b801a22ce5 100755 (executable)
@@ -5,24 +5,26 @@ set -eu
 set -o pipefail
 
 PAGE_SIZE=$(getconf PAGE_SIZE)
-BLOAT_ITERATION_TARGET=$(( 100 << 20 )) # 100 MB
+BLOAT_ITERATION_TARGET=$((100 << 20)) # 100 MB
 BLOAT_HOLDER=()
 PID="$$"
 
 function bloat {
         local set_size mem_usage target_mem_size
 
-        set_size=$(cut -d " " -f2 "/proc/$PID/statm")
-        mem_usage=$(( "$set_size" * "$PAGE_SIZE" ))
-        target_mem_size=$(( "$mem_usage" + "$1" ))
+        # Following `| cat` weirdness is intentional to generate some reclaim
+        # activity in case there's no swap available.
+        set_size=$(cut -d " " -f2 "/proc/$PID/statm" | cat)
+        mem_usage=$((set_size * PAGE_SIZE))
+        target_mem_size=$((mem_usage + $1))
 
         BLOAT_HOLDER=()
         while [[ "$mem_usage" -lt "$target_mem_size" ]]; do
                 echo "target $target_mem_size"
                 echo "mem usage $mem_usage"
                 BLOAT_HOLDER+=("$(printf "=%0.s" {1..1000000})")
-                set_size=$(cut -d " " -f2 "/proc/$PID/statm")
-                mem_usage=$(("$set_size" * "$PAGE_SIZE"))
+                set_size=$(cut -d " " -f2 "/proc/$PID/statm" | cat)
+                mem_usage=$((set_size * PAGE_SIZE))
         done
 }