richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
594167
From 8eda88f8c46d053137cf6cf52280b9988c3c996b Mon Sep 17 00:00:00 2001
594167
From: Yu Watanabe <watanabe.yu+github@gmail.com>
594167
Date: Thu, 7 Jul 2022 10:10:05 +0900
594167
Subject: [PATCH] test: several cleanups for TEST-35-LOGIN
594167
594167
- use test_append_files() to install additional commands
594167
- drop use of expect
594167
- include assert.sh and use assertions at several places
594167
- use timeout command at several places
594167
- always use logind-test-user
594167
- etc
594167
594167
(cherry picked from commit 63663a0f0f0e1dcd6036cecf013de3f1de87cd99)
594167
594167
Related: #2100464
594167
---
594167
 test/TEST-35-LOGIN/test.sh |   6 +-
594167
 test/test-functions        |   2 -
594167
 test/units/testsuite-35.sh | 228 ++++++++++++++++++-------------------
594167
 3 files changed, 116 insertions(+), 120 deletions(-)
594167
594167
diff --git a/test/TEST-35-LOGIN/test.sh b/test/TEST-35-LOGIN/test.sh
594167
index 9762410fa3..add862c962 100755
594167
--- a/test/TEST-35-LOGIN/test.sh
594167
+++ b/test/TEST-35-LOGIN/test.sh
594167
@@ -2,9 +2,13 @@
594167
 # SPDX-License-Identifier: LGPL-2.1-or-later
594167
 set -e
594167
 
594167
-TEST_DESCRIPTION="LOGIN"
594167
+TEST_DESCRIPTION="Tests for systemd-logind"
594167
 
594167
 # shellcheck source=test/test-functions
594167
 . "${TEST_BASE_DIR:?}/test-functions"
594167
 
594167
+test_append_files() {
594167
+    image_install -o evemu-device evemu-event crond crontab
594167
+}
594167
+
594167
 do_test "$@"
594167
diff --git a/test/test-functions b/test/test-functions
594167
index b0f3b28def..8ea2f97b71 100644
594167
--- a/test/test-functions
594167
+++ b/test/test-functions
594167
@@ -1670,8 +1670,6 @@ install_basic_tools() {
594167
     image_install -o sushell
594167
     # in Debian ldconfig is just a shell script wrapper around ldconfig.real
594167
     image_install -o ldconfig.real
594167
-    # for TEST-35-LOGIN
594167
-    image_install -o evemu-device evemu-event
594167
 }
594167
 
594167
 install_debug_tools() {
594167
diff --git a/test/units/testsuite-35.sh b/test/units/testsuite-35.sh
594167
index 21cb01c16f..86787e7a02 100755
594167
--- a/test/units/testsuite-35.sh
594167
+++ b/test/units/testsuite-35.sh
594167
@@ -3,6 +3,24 @@
594167
 set -eux
594167
 set -o pipefail
594167
 
594167
+# shellcheck source=test/units/assert.sh
594167
+. "$(dirname "$0")"/assert.sh
594167
+
594167
+cleanup_test_user() (
594167
+    set +ex
594167
+
594167
+    pkill -u "$(id -u logind-test-user)"
594167
+    sleep 1
594167
+    pkill -KILL -u "$(id -u logind-test-user)"
594167
+    userdel -r logind-test-user
594167
+)
594167
+
594167
+setup_test_user() {
594167
+    mkdir -p /var/spool/cron /var/spool/mail
594167
+    useradd -m -s /bin/bash logind-test-user
594167
+    trap cleanup_test_user EXIT
594167
+}
594167
+
594167
 test_enable_debug() {
594167
     mkdir -p /run/systemd/system/systemd-logind.service.d
594167
     cat >/run/systemd/system/systemd-logind.service.d/debug.conf <
594167
@@ -10,6 +28,7 @@ test_enable_debug() {
594167
 Environment=SYSTEMD_LOG_LEVEL=debug
594167
 EOF
594167
     systemctl daemon-reload
594167
+    systemctl stop systemd-logind.service
594167
 }
594167
 
594167
 test_properties() {
594167
@@ -21,11 +40,7 @@ KillUserProcesses=no
594167
 EOF
594167
 
594167
     systemctl restart systemd-logind.service
594167
-    r=$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager KillUserProcesses)
594167
-    if [[ "$r" != "b false" ]]; then
594167
-        echo "Unexpected KillUserProcesses property '$r', expected='b false'" >&2
594167
-        exit 1
594167
-    fi
594167
+    assert_eq "$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager KillUserProcesses)" "b false"
594167
 
594167
     cat >/run/systemd/logind.conf.d/kill-user-processes.conf <
594167
 [Login]
594167
@@ -33,46 +48,46 @@ KillUserProcesses=yes
594167
 EOF
594167
 
594167
     systemctl restart systemd-logind.service
594167
-    r=$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager KillUserProcesses)
594167
-    if [[ "$r" != "b true" ]]; then
594167
-        echo "Unexpected KillUserProcesses property '$r', expected='b true'" >&2
594167
-        exit 1
594167
-    fi
594167
+    assert_eq "$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager KillUserProcesses)" "b true"
594167
 
594167
     rm -rf /run/systemd/logind.conf.d
594167
 }
594167
 
594167
 test_started() {
594167
+    local pid
594167
+
594167
     systemctl restart systemd-logind.service
594167
 
594167
     # should start at boot, not with D-BUS activation
594167
-    LOGINDPID=$(systemctl show systemd-logind.service -p ExecMainPID --value)
594167
+    pid=$(systemctl show systemd-logind.service -p ExecMainPID --value)
594167
 
594167
     # loginctl should succeed
594167
-    loginctl --no-pager
594167
+    loginctl
594167
+
594167
+    # logind should still be running
594167
+    assert_eq "$(systemctl show systemd-logind.service -p ExecMainPID --value)" "$pid"
594167
 }
594167
 
594167
-# args: <timeout>
594167
 wait_suspend() {
594167
-    timeout="$1"
594167
-    while [[ $timeout -gt 0 && ! -e /run/suspend.flag ]]; do
594167
-        sleep 1
594167
-        timeout=$((timeout - 1))
594167
-    done
594167
-    if [[ ! -e /run/suspend.flag ]]; then
594167
-        echo "closing lid did not cause suspend" >&2
594167
-        exit 1
594167
-    fi
594167
+    timeout "${1?}" bash -c "while [[ ! -e /run/suspend.flag ]]; do sleep 1; done"
594167
     rm /run/suspend.flag
594167
 }
594167
 
594167
-test_suspend_tear_down() {
594167
-    set +e
594167
+teardown_suspend() (
594167
+    set +eux
594167
 
594167
-    kill "$KILL_PID"
594167
-}
594167
+    pkill evemu-device
594167
+
594167
+    rm -rf /run/systemd/system/systemd-suspend.service.d
594167
+    systemctl daemon-reload
594167
+
594167
+    rm -f /run/udev/rules.d/70-logindtest-lid.rules
594167
+    udevadm control --reload
594167
+)
594167
 
594167
 test_suspend_on_lid() {
594167
+    local pid input_name lid_dev
594167
+
594167
     if systemd-detect-virt --quiet --container; then
594167
         echo "Skipping suspend test in container"
594167
         return
594167
@@ -90,8 +105,10 @@ test_suspend_on_lid() {
594167
         return
594167
     fi
594167
 
594167
-    KILL_PID=
594167
-    trap test_suspend_tear_down RETURN
594167
+    trap teardown_suspend RETURN
594167
+
594167
+    # save pid
594167
+    pid=$(systemctl show systemd-logind.service -p ExecMainPID --value)
594167
 
594167
     # create fake suspend
594167
     mkdir -p /run/systemd/system/systemd-suspend.service.d
594167
@@ -147,129 +164,127 @@ B: 15 00 00 00 00 00 00 00 00
594167
 EOF
594167
 
594167
     evemu-device /run/lidswitch.evemu &
594167
-    KILL_PID="$!"
594167
 
594167
-    for ((i = 0; i < 20; i++)); do
594167
-        if (( i != 0 )); then sleep .5; fi
594167
-
594167
-        INPUT_NAME=$(grep -l '^Fake Lid Switch' /sys/class/input/*/device/name || :)
594167
-        if [[ -n "$INPUT_NAME" ]]; then break; fi
594167
-    done
594167
-    if [[ -z "$INPUT_NAME" ]]; then
594167
+    timeout 20 bash -c 'while ! grep "^Fake Lid Switch" /sys/class/input/*/device/name; do sleep .5; done'
594167
+    input_name=$(grep -l '^Fake Lid Switch' /sys/class/input/*/device/name || :)
594167
+    if [[ -z "$input_name" ]]; then
594167
         echo "cannot find fake lid switch." >&2
594167
         exit 1
594167
     fi
594167
-    INPUT_NAME=${INPUT_NAME%/device/name}
594167
-    LID_DEV=/dev/${INPUT_NAME#/sys/class/}
594167
-    udevadm info --wait-for-initialization=10s "$LID_DEV"
594167
+    input_name=${input_name%/device/name}
594167
+    lid_dev=/dev/${input_name#/sys/class/}
594167
+    udevadm info --wait-for-initialization=10s "$lid_dev"
594167
     udevadm settle
594167
 
594167
     # close lid
594167
-    evemu-event "$LID_DEV" --sync --type 5 --code 0 --value 1
594167
+    evemu-event "$lid_dev" --sync --type 5 --code 0 --value 1
594167
     # need to wait for 30s suspend inhibition after boot
594167
     wait_suspend 31
594167
     # open lid again
594167
-    evemu-event "$LID_DEV" --sync --type 5 --code 0 --value 0
594167
+    evemu-event "$lid_dev" --sync --type 5 --code 0 --value 0
594167
 
594167
     # waiting for 30s inhibition time between suspends
594167
     sleep 30
594167
 
594167
     # now closing lid should cause instant suspend
594167
-    evemu-event "$LID_DEV" --sync --type 5 --code 0 --value 1
594167
+    evemu-event "$lid_dev" --sync --type 5 --code 0 --value 1
594167
     wait_suspend 2
594167
-    evemu-event "$LID_DEV" --sync --type 5 --code 0 --value 0
594167
+    evemu-event "$lid_dev" --sync --type 5 --code 0 --value 0
594167
 
594167
-    P=$(systemctl show systemd-logind.service -p ExecMainPID --value)
594167
-    if [[ "$P" != "$LOGINDPID" ]]; then
594167
-        echo "logind crashed" >&2
594167
-        exit 1
594167
-    fi
594167
+    assert_eq "$(systemctl show systemd-logind.service -p ExecMainPID --value)" "$pid"
594167
 }
594167
 
594167
 test_shutdown() {
594167
+    local pid
594167
+
594167
+    # save pid
594167
+    pid=$(systemctl show systemd-logind.service -p ExecMainPID --value)
594167
+
594167
     # scheduled shutdown with wall message
594167
     shutdown 2>&1
594167
     sleep 5
594167
     shutdown -c || :
594167
     # logind should still be running
594167
-    P=$(systemctl show systemd-logind.service -p ExecMainPID --value)
594167
-    if [[ "$P" != "$LOGINDPID" ]]; then
594167
-        echo "logind crashed" >&2
594167
-        exit 1
594167
-    fi
594167
+    assert_eq "$(systemctl show systemd-logind.service -p ExecMainPID --value)" "$pid"
594167
 
594167
     # scheduled shutdown without wall message
594167
     shutdown --no-wall 2>&1
594167
     sleep 5
594167
     shutdown -c --no-wall || true
594167
-    P=$(systemctl show systemd-logind.service -p ExecMainPID --value)
594167
-    if [[ "$P" != "$LOGINDPID" ]]; then
594167
-        echo "logind crashed" >&2
594167
-        exit 1
594167
-    fi
594167
+    assert_eq "$(systemctl show systemd-logind.service -p ExecMainPID --value)" "$pid"
594167
 }
594167
 
594167
-test_session_tear_down() {
594167
-    set +e
594167
-
594167
-    rm -f /run/udev/rules.d/70-logindtest-scsi_debug-user.rules
594167
-    udevadm control --reload
594167
+teardown_session() (
594167
+    set +ex
594167
 
594167
     systemctl stop getty@tty2.service
594167
     rm -rf /run/systemd/system/getty@tty2.service.d
594167
     systemctl daemon-reload
594167
 
594167
-    pkill -u logind-test-user
594167
-    userdel logind-test-user
594167
+    pkill -u "$(id -u logind-test-user)"
594167
+    sleep 1
594167
+    pkill -KILL -u "$(id -u logind-test-user)"
594167
 
594167
+    rm -f /run/udev/rules.d/70-logindtest-scsi_debug-user.rules
594167
+    udevadm control --reload
594167
     rmmod scsi_debug
594167
-}
594167
+)
594167
+
594167
+check_session() (
594167
+    set +ex
594167
+
594167
+    local seat session leader_pid
594167
 
594167
-check_session() {
594167
-    loginctl
594167
     if [[ $(loginctl --no-legend | grep -c "logind-test-user") != 1 ]]; then
594167
         echo "no session or multiple sessions for logind-test-user." >&2
594167
         return 1
594167
     fi
594167
 
594167
-    SEAT=$(loginctl --no-legend | grep 'logind-test-user *seat' | awk '{ print $4 }')
594167
-    if [[ -z "$SEAT" ]]; then
594167
+    seat=$(loginctl --no-legend | grep 'logind-test-user *seat' | awk '{ print $4 }')
594167
+    if [[ -z "$seat" ]]; then
594167
         echo "no seat found for user logind-test-user" >&2
594167
         return 1
594167
     fi
594167
 
594167
-    SESSION=$(loginctl --no-legend | grep "logind-test-user" | awk '{ print $1 }')
594167
-    if [[ -z "$SESSION" ]]; then
594167
+    session=$(loginctl --no-legend | grep "logind-test-user" | awk '{ print $1 }')
594167
+    if [[ -z "$session" ]]; then
594167
         echo "no session found for user logind-test-user" >&2
594167
         return 1
594167
     fi
594167
 
594167
-    loginctl session-status "$SESSION"
594167
-    loginctl session-status "$SESSION" | grep -q "Unit: session-${SESSION}\.scope"
594167
-    LEADER_PID=$(loginctl session-status "$SESSION" | grep "Leader:" | awk '{ print $2 }')
594167
-    if [[ -z "$LEADER_PID" ]]; then
594167
-        echo "cannot found leader process for session $SESSION" >&2
594167
+    if ! loginctl session-status "$session" | grep -q "Unit: session-${session}\.scope"; then
594167
+        echo "cannot find scope unit for session $session" >&2
594167
+        return 1
594167
+    fi
594167
+
594167
+    leader_pid=$(loginctl session-status "$session" | grep "Leader:" | awk '{ print $2 }')
594167
+    if [[ -z "$leader_pid" ]]; then
594167
+        echo "cannot found leader process for session $session" >&2
594167
         return 1
594167
     fi
594167
 
594167
     # cgroup v1: "1:name=systemd:/user.slice/..."; unified hierarchy: "0::/user.slice"
594167
-    if ! grep -q -E '(name=systemd|^0:):.*session.*scope' /proc/"$LEADER_PID"/cgroup; then
594167
-        echo "FAIL: process $LEADER_PID is not in the session cgroup" >&2
594167
+    if ! grep -q -E '(name=systemd|^0:):.*session.*scope' /proc/"$leader_pid"/cgroup; then
594167
+        echo "FAIL: process $leader_pid is not in the session cgroup" >&2
594167
         cat /proc/self/cgroup
594167
         return 1
594167
     fi
594167
-}
594167
+)
594167
 
594167
 test_session() {
594167
+    local dev
594167
+
594167
     if systemd-detect-virt --quiet --container; then
594167
-        echo " * Skipping ACL tests in container"
594167
+        echo "Skipping ACL tests in container"
594167
         return
594167
     fi
594167
 
594167
-    trap test_session_tear_down RETURN
594167
+    if [[ ! -c /dev/tty2 ]]; then
594167
+        echo "/dev/tty2 does not exist, skipping test ${FUNCNAME[0]}."
594167
+        return
594167
+    fi
594167
 
594167
-    # add user
594167
-    useradd -s /bin/bash logind-test-user
594167
+    trap teardown_session RETURN
594167
 
594167
     # login with the test user to start a session
594167
     mkdir -p /run/systemd/system/getty@tty2.service.d
594167
@@ -280,20 +295,14 @@ ExecStart=
594167
 ExecStart=-/sbin/agetty --autologin logind-test-user --noclear %I $TERM
594167
 EOF
594167
     systemctl daemon-reload
594167
-    systemctl start getty@tty2.service
594167
+    systemctl restart getty@tty2.service
594167
 
594167
     # check session
594167
-    ret=1
594167
     for ((i = 0; i < 30; i++)); do
594167
-        if (( i != 0)); then sleep 1; fi
594167
-        if check_session; then
594167
-            ret=0
594167
-            break
594167
-        fi
594167
+        (( i != 0 )) && sleep 1
594167
+        check_session && break
594167
     done
594167
-    if [[ "$ret" == "1" ]]; then
594167
-        exit 1
594167
-    fi
594167
+    check_session
594167
 
594167
     # scsi_debug should not be loaded yet
594167
     if [[ -d /sys/bus/pseudo/drivers/scsi_debug ]]; then
594167
@@ -312,12 +321,8 @@ EOF
594167
     # coldplug: logind started with existing device
594167
     systemctl stop systemd-logind.service
594167
     modprobe scsi_debug
594167
-    for ((i = 0; i < 30; i++)); do
594167
-        if (( i != 0)); then sleep 1; fi
594167
-        if dev=/dev/$(ls /sys/bus/pseudo/drivers/scsi_debug/adapter*/host*/target*/*:*/block 2>/dev/null); then
594167
-            break
594167
-        fi
594167
-    done
594167
+    timeout 30 bash -c 'while ! ls /sys/bus/pseudo/drivers/scsi_debug/adapter*/host*/target*/*:*/block 2>/dev/null; do sleep 1; done'
594167
+    dev=/dev/$(ls /sys/bus/pseudo/drivers/scsi_debug/adapter*/host*/target*/*:*/block 2>/dev/null)
594167
     if [[ ! -b "$dev" ]]; then
594167
         echo "cannot find suitable scsi block device" >&2
594167
         exit 1
594167
@@ -326,25 +331,17 @@ EOF
594167
     udevadm info "$dev"
594167
 
594167
     # trigger logind and activate session
594167
-    loginctl activate "$SESSION"
594167
+    loginctl activate "$(loginctl --no-legend | grep "logind-test-user" | awk '{ print $1 }')"
594167
 
594167
     # check ACL
594167
     sleep 1
594167
-    if ! getfacl -p "$dev" | grep -q "user:logind-test-user:rw-"; then
594167
-        echo "$dev has no ACL for user logind-test-user" >&2
594167
-        getfacl -p "$dev" >&2
594167
-        exit 1
594167
-    fi
594167
+    assert_in "user:logind-test-user:rw-" "$(getfacl -p "$dev")"
594167
 
594167
     # hotplug: new device appears while logind is running
594167
     rmmod scsi_debug
594167
     modprobe scsi_debug
594167
-    for ((i = 0; i < 30; i++)); do
594167
-        if (( i != 0)); then sleep 1; fi
594167
-        if dev=/dev/$(ls /sys/bus/pseudo/drivers/scsi_debug/adapter*/host*/target*/*:*/block 2>/dev/null); then
594167
-            break
594167
-        fi
594167
-    done
594167
+    timeout 30 bash -c 'while ! ls /sys/bus/pseudo/drivers/scsi_debug/adapter*/host*/target*/*:*/block 2>/dev/null; do sleep 1; done'
594167
+    dev=/dev/$(ls /sys/bus/pseudo/drivers/scsi_debug/adapter*/host*/target*/*:*/block 2>/dev/null)
594167
     if [[ ! -b "$dev" ]]; then
594167
         echo "cannot find suitable scsi block device" >&2
594167
         exit 1
594167
@@ -353,15 +350,12 @@ EOF
594167
 
594167
     # check ACL
594167
     sleep 1
594167
-    if ! getfacl -p "$dev" | grep -q "user:logind-test-user:rw-"; then
594167
-        echo "$dev has no ACL for user logind-test-user" >&2
594167
-        getfacl -p "$dev" >&2
594167
-        exit 1
594167
-    fi
594167
+    assert_in "user:logind-test-user:rw-" "$(getfacl -p "$dev")"
594167
 }
594167
 
594167
 : >/failed
594167
 
594167
+setup_test_user
594167
 test_enable_debug
594167
 test_properties
594167
 test_started