naccyde / rpms / systemd

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