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