]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tests: add test for handling of background sessions
authorMichal Sekletar <msekleta@redhat.com>
Tue, 21 Jun 2022 16:41:46 +0000 (18:41 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 7 Jul 2022 01:11:43 +0000 (10:11 +0900)
test/test-functions
test/units/testsuite-35.sh

index 19559fc071f6846b2fe77f7fa856912973624ec0..974fa4384beb1363b8cf4b65ca164c944550ddc9 100644 (file)
@@ -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
index aa1a6af2fb7a94f1764a71fe01bb3f5e3247d6af..98283cad25b59ef867903fcc16df284591c0b52d 100755 (executable)
@@ -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