2424
2525teardown_idle_action_lock () {(
2626 set +ex
27+ [[ -e dbus.log ]] && cat dbus.log
28+ [[ -e logind.log ]] && cat logind.log
2729 rm -f /run/systemd/logind.conf.d/idle-action-lock.conf
2830 rm -f /run/systemd/systemd-logind.service.d/debug.conf
2931 pkill -9 -u " $( id -u testuser) "
@@ -37,56 +39,61 @@ test_lock_idle_action() {
3739 return 0
3840 fi
3941
42+ local busctl_pid expect_pid ts
43+
4044 setup_idle_action_lock
4145 trap teardown_idle_action_lock RETURN
4246
4347 if loginctl --no-legend | awk ' { print $3; }' | sort -u | grep -q testuser ; then
44- echo >&2 " Session of the \ 'testuser\ ' is already present."
48+ echo >&2 " Session of the 'testuser' is already present."
4549 return 1
4650 fi
4751
48- # IdleActionSec is set 1s but the accuracy of associated timer is 30s so we
52+ rm -f /tmp/expect-sync
53+
54+ # IdleActionSec= is set 1s but the accuracy of associated timer is 30s so we
4955 # need to sleep in worst case for 31s to make sure timer elapsed. We sleep
50- # here for 35s to accomodate for any possible scheudling delays.
51- cat > /tmp/test.exp << EOF
56+ # here for 35s to accommodate for any possible scheduling delays.
57+ cat > /tmp/test.exp << EOF
5258spawn systemd-run -G -t -p PAMName=login -p User=testuser bash
53- send "sleep 35\r"
59+ expect "three times"
60+ send "touch /tmp/expect-sync\r"
61+ sleep 35
5462send "echo foobar\r"
55- send " sleep 35\r"
63+ sleep 35
5664send "exit\r"
5765interact
5866wait
5967EOF
6068
6169 ts=" $( date ' +%H:%M:%S' ) "
62- busctl --match " type='signal',sender='org.freedesktop.login1',interface='org.freedesktop.login1.Session',member='Lock'" monitor > dbus.log &
70+ busctl --match " type='signal',sender='org.freedesktop.login1',interface='org.freedesktop.login1.Session',member='Lock'" monitor & > dbus.log &
71+ busctl_pid=$!
72+ timeout 15 bash -c ' while ! grep -q "Monitoring bus" dbus.log; do sleep .5; done'
6373
64- expect /tmp/test.exp &
74+ expect -d /tmp/test.exp &
75+ expect_pid=$!
6576
66- # Sleep a bit to give expect time to spawn systemd-run before we check for
77+ # Wait a bit to give expect time to spawn systemd-run before we check for
6778 # the presence of resulting session.
68- sleep 2
69- if [ " $( loginctl --no-legend | awk ' { print $3; }' | sort -u | grep -c testuser) " != 1 ] ; then
70- echo >&2 " \ 'testuser\ ' is expected to have exactly one session running."
79+ timeout 15 bash -c " while [[ ! -e /tmp/expect-sync ]]; do sleep .5; done "
80+ if [[ " $( loginctl --no-legend | awk ' { print $3; }' | grep -c testuser) " -ne 1 ] ] ; then
81+ echo >&2 " 'testuser' is expected to have exactly one session running."
7182 return 1
7283 fi
7384
74- wait %2
75- sleep 20
76- kill %1
85+ wait " $expect_pid "
7786
7887 # We slept for 35s , in that interval all sessions should have become idle
7988 # and "Lock" signal should have been sent out. Then we wrote to tty to make
8089 # session active again and next we slept for another 35s so sessions have
8190 # become idle again. 'Lock' signal is sent out for each session, we have at
8291 # least one session, so minimum of 2 "Lock" signals must have been sent.
83- if [ " $( grep -c Member=Lock dbus.log) " -lt 2 ]; then
84- echo >&2 " Too few 'Lock' D-Bus signal sent, expected at least 2."
85- return 1
86- fi
92+ timeout 30 bash -c ' while [[ $(grep -c Member=Lock dbus.log) -lt 2 ]]; do sleep .5; done'
93+ kill " $busctl_pid "
8794
88- journalctl -b -u systemd-logind.service --since=" $ts " > logind.log
89- if [ " $( grep -c ' System idle. Doing lock operation.' logind.log) " -lt 2 ]; then
95+ journalctl -b -u systemd-logind.service --since=" $ts " > logind.log
96+ if [[ " $( grep -c ' System idle. Doing lock operation.' logind.log) " -lt 2 ] ]; then
9097 echo >&2 " System haven't entered idle state at least 2 times."
9198 return 1
9299 fi
0 commit comments