naccyde / rpms / systemd

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