diff options
author | Michal Sekletar <msekleta@redhat.com> | 2022-06-21 18:41:46 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-07-07 03:11:43 +0200 |
commit | bf40417c7cbe0afb185eda97ea46395b1bb74bdf (patch) | |
tree | bee5421891da1be78cec6b69aac4e3a0644770b1 | |
parent | logind: don't start user@UID.service instance for background sessions (diff) | |
download | systemd-bf40417c7cbe0afb185eda97ea46395b1bb74bdf.tar.xz systemd-bf40417c7cbe0afb185eda97ea46395b1bb74bdf.zip |
tests: add test for handling of background sessions
-rw-r--r-- | test/test-functions | 1 | ||||
-rwxr-xr-x | test/units/testsuite-35.sh | 64 |
2 files changed, 65 insertions, 0 deletions
diff --git a/test/test-functions b/test/test-functions index 19559fc071..974fa4384b 100644 --- a/test/test-functions +++ b/test/test-functions @@ -1736,6 +1736,7 @@ install_basic_tools() { # 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 crond crontab image_install -o evemu-device evemu-event if command -v expect >/dev/null && command -v tclsh >/dev/null ; then # shellcheck disable=SC2016 diff --git a/test/units/testsuite-35.sh b/test/units/testsuite-35.sh index aa1a6af2fb..98283cad25 100755 --- a/test/units/testsuite-35.sh +++ b/test/units/testsuite-35.sh @@ -443,6 +443,69 @@ EOF rm -f dbus.log logind.log } +setup_cron() { + # Setup test user and cron + useradd test || : + crond -s -n & + # Install crontab for the test user that runs sleep every minute. But let's sleep for + # 65 seconds to make sure there is overlap between two consecutive runs, i.e. we have + # always a cron session running. + crontab -u test - <<EOF +RANDOM_DELAY=0 +* * * * * /bin/sleep 65 +EOF + # Let's wait (at most one interval plus 10s to accomodate for slow machines) for at least one session of test user + timeout 70s bash -c "while true; do loginctl --no-legend list-sessions | awk '{ print \$3 }' | grep -q test && break || sleep 1 ; done" +} + +teardown_cron() ( + set +ex + pkill -9 -u "$(id -u test)" + pkill crond + crontab -r -u test + userdel -r test +) + +test_no_user_instance_for_cron() { + if ! command -v crond || ! command -v crontab ; then + echo >&2 "Skipping test for background cron sessions because cron is missing." + return + fi + + trap teardown_cron EXIT + setup_cron + + if [[ $(loginctl --no-legend list-sessions | grep -c test) -lt 1 ]]; then + echo >&2 '"test" user should have at least one session' + loginctl list-sessions + return 1 + fi + + # Check that all sessions of test user have class=background and no user instance was started + # for the test user. + while read -r s _; do + local class + + class=$(loginctl --property Class --value show-session "$s") + if [[ "$class" != "background" ]]; then + echo >&2 "Session has incorrect class, expected \"background\", got \"$class\"." + return 1 + fi + done < <(loginctl --no-legend list-sessions | grep test) + + state=$(systemctl --property ActiveState --value show user@"$(id -u test)".service) + if [[ "$state" != "inactive" ]]; then + echo >&2 "User instance state is unexpected, expected \"inactive\", got \"$state\"" + return 1 + fi + + state=$(systemctl --property SubState --value show user@"$(id -u test)".service) + if [[ "$state" != "dead" ]]; then + echo >&2 "User instance state is unexpected, expected \"dead\", got \"$state\"" + return 1 + fi +} + : >/failed test_enable_debug @@ -452,6 +515,7 @@ test_suspend_on_lid test_shutdown test_session test_lock_idle_action +test_no_user_instance_for_cron touch /testok rm /failed |