ryantimwilson / rpms / systemd

Forked from rpms/systemd 6 days ago
Clone
6f381c
From 638c2418e705410344e07e77f944530df0f4608f Mon Sep 17 00:00:00 2001
6f381c
From: Michal Sekletar <msekleta@redhat.com>
6f381c
Date: Fri, 1 Jul 2022 12:59:57 +0200
6f381c
Subject: [PATCH] tests: verify that Lock D-Bus signal is sent when
6f381c
 IdleAction=lock
6f381c
6f381c
(cherry picked from commit 181656fc0faa885d69bc34822b8e9b5de3fdf6bf)
6f381c
6f381c
Related: #1866955
6f381c
---
6f381c
 test/TEST-35-LOGIN/test.sh      | 11 +++-
6f381c
 test/TEST-35-LOGIN/testsuite.sh | 93 +++++++++++++++++++++++++++++++++
6f381c
 2 files changed, 103 insertions(+), 1 deletion(-)
6f381c
6f381c
diff --git a/test/TEST-35-LOGIN/test.sh b/test/TEST-35-LOGIN/test.sh
6f381c
index 32410c8149..f83afcff49 100755
6f381c
--- a/test/TEST-35-LOGIN/test.sh
6f381c
+++ b/test/TEST-35-LOGIN/test.sh
6f381c
@@ -14,14 +14,23 @@ test_setup() {
6f381c
 
6f381c
     (
6f381c
         LOG_LEVEL=5
6f381c
-        eval $(udevadm info --export --query=env --name="${LOOPDEV}p2")
6f381c
+        eval "$(udevadm info --export --query=env --name="${LOOPDEV}p2")"
6f381c
 
6f381c
         setup_basic_environment
6f381c
 
6f381c
+        inst_binary awk
6f381c
         inst_binary pkill
6f381c
         inst_binary useradd
6f381c
         inst_binary userdel
6f381c
 
6f381c
+        if command -v expect >/dev/null && command -v tclsh >/dev/null ; then
6f381c
+                # shellcheck disable=SC2016
6f381c
+                version="$(tclsh <<< 'puts $tcl_version')"
6f381c
+
6f381c
+                dracut_install expect
6f381c
+                inst_recursive /usr/lib64/tcl"$version" /usr/share/tcl"$version"
6f381c
+        fi
6f381c
+
6f381c
         # mask some services that we do not want to run in these tests
6f381c
         ln -fs /dev/null "$initdir"/etc/systemd/system/systemd-hwdb-update.service
6f381c
         ln -fs /dev/null "$initdir"/etc/systemd/system/systemd-journal-catalog-update.service
6f381c
diff --git a/test/TEST-35-LOGIN/testsuite.sh b/test/TEST-35-LOGIN/testsuite.sh
6f381c
index 9855b4bc80..e4d72beb74 100755
6f381c
--- a/test/TEST-35-LOGIN/testsuite.sh
6f381c
+++ b/test/TEST-35-LOGIN/testsuite.sh
6f381c
@@ -3,7 +3,100 @@
6f381c
 set -eux
6f381c
 set -o pipefail
6f381c
 
6f381c
+setup_idle_action_lock() {
6f381c
+    useradd testuser ||:
6f381c
+
6f381c
+    mkdir -p /run/systemd/logind.conf.d/
6f381c
+    cat >/run/systemd/logind.conf.d/idle-action-lock.conf <
6f381c
+[Login]
6f381c
+IdleAction=lock
6f381c
+IdleActionSec=1s
6f381c
+EOF
6f381c
+
6f381c
+    mkdir -p /run/systemd/systemd-logind.service.d/
6f381c
+    cat >/run/systemd/systemd-logind.service.d/debug.conf <
6f381c
+[Service]
6f381c
+Environment=SYSTEMD_LOG_LEVEL=debug
6f381c
+EOF
6f381c
+
6f381c
+    systemctl restart systemd-logind.service
6f381c
+}
6f381c
+
6f381c
+teardown_idle_action_lock() {(
6f381c
+    set +ex
6f381c
+    rm -f /run/systemd/logind.conf.d/idle-action-lock.conf
6f381c
+    rm -f /run/systemd/systemd-logind.service.d/debug.conf
6f381c
+    pkill -9 -u "$(id -u testuser)"
6f381c
+    userdel -r testuser
6f381c
+    systemctl restart systemd-logind.service
6f381c
+)}
6f381c
+
6f381c
+test_lock_idle_action() {
6f381c
+    if ! command -v expect >/dev/null ; then
6f381c
+        echo >&2 "expect not installed, skiping test ${FUNCNAME[0]}"
6f381c
+        return 0
6f381c
+    fi
6f381c
+
6f381c
+    setup_idle_action_lock
6f381c
+    trap teardown_idle_action_lock RETURN
6f381c
+
6f381c
+    if loginctl --no-legend | awk '{ print $3; }' | sort -u | grep -q testuser ; then
6f381c
+        echo >&2 "Session of the \'testuser\' is already present."
6f381c
+        return 1
6f381c
+    fi
6f381c
+
6f381c
+    # IdleActionSec is set 1s but the accuracy of associated timer is 30s so we
6f381c
+    # need to sleep in worst case for 31s to make sure timer elapsed. We sleep
6f381c
+    # here for 35s to accomodate for any possible scheudling delays.
6f381c
+    cat > /tmp/test.exp <
6f381c
+spawn systemd-run -G -t -p PAMName=login -p User=testuser bash
6f381c
+send "sleep 35\r"
6f381c
+send "echo foobar\r"
6f381c
+send "sleep 35\r"
6f381c
+send "exit\r"
6f381c
+interact
6f381c
+wait
6f381c
+EOF
6f381c
+
6f381c
+    ts="$(date '+%H:%M:%S')"
6f381c
+    busctl --match "type='signal',sender='org.freedesktop.login1',interface='org.freedesktop.login1.Session',member='Lock'" monitor  > dbus.log &
6f381c
+
6f381c
+    expect /tmp/test.exp &
6f381c
+
6f381c
+    # Sleep a bit to give expect time to spawn systemd-run before we check for
6f381c
+    # the presence of resulting session.
6f381c
+    sleep 2
6f381c
+    if [ "$(loginctl --no-legend | awk '{ print $3; }' | sort -u | grep -c testuser)" != 1 ] ; then
6f381c
+        echo >&2 "\'testuser\' is expected to have exactly one session running."
6f381c
+        return 1
6f381c
+    fi
6f381c
+
6f381c
+    wait %2
6f381c
+    sleep 20
6f381c
+    kill %1
6f381c
+
6f381c
+    # We slept for 35s , in that interval all sessions should have become idle
6f381c
+    # and "Lock" signal should have been sent out. Then we wrote to tty to make
6f381c
+    # session active again and next we slept for another 35s so sessions have
6f381c
+    # become idle again. 'Lock' signal is sent out for each session, we have at
6f381c
+    # least one session, so minimum of 2 "Lock" signals must have been sent.
6f381c
+    if [ "$(grep -c Member=Lock dbus.log)" -lt 2 ]; then
6f381c
+        echo >&2 "Too few 'Lock' D-Bus signal sent, expected at least 2."
6f381c
+        return 1
6f381c
+    fi
6f381c
+
6f381c
+    journalctl -b -u systemd-logind.service --since="$ts" > logind.log
6f381c
+    if [ "$(grep -c 'System idle. Doing lock operation.' logind.log)" -lt 2 ]; then
6f381c
+        echo >&2 "System haven't entered idle state at least 2 times."
6f381c
+        return 1
6f381c
+    fi
6f381c
+
6f381c
+    rm -f dbus.log logind.log
6f381c
+}
6f381c
+
6f381c
 : >/failed
6f381c
 
6f381c
+test_lock_idle_action
6f381c
+
6f381c
 touch /testok
6f381c
 rm /failed