1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
summary: tests for tests.session
details: |
Check the tests.session tool can run commands in a user session
# Session-tool depends on busctl and doesn't work on 14.04
systems: [-ubuntu-14.04-*]
environment:
USER/root: root
USER/test: test
prepare: |
truncate --size=0 defer.sh
chmod +x defer.sh
# Prevent anacron/cron from interfering with their background sessions, grr!
for unit in cron.service crond.service anacron.timer anacron.service; do
if [ "$(systemctl is-active "$unit")" = active ]; then
systemctl stop "$unit"
echo "systemctl start \"$unit\"" >> defer.sh
fi
done
# For whatever reason (what spawns it!?!) cron may be running in session 2.
# To remove any background activity, get rid of it.
if loginctl show-session 2 >/dev/null 2>&1 && loginctl show-session 2 | grep -q Service=crond; then
loginctl kill-session 2
fi
# Kill sessions that systemd has leaked earlier.
tests.session kill-leaked
# Remember details of sessions before we start.
tests.session dump > before.debug.txt
# Brief version of existing sessions to measure during "restore".
loginctl --no-legend list-sessions | sort > before.txt
# Prepare for using sessions as the given user
tests.session prepare -u "$USER"
restore: |
# Restore after using sessions as the given user
tests.session restore -u "$USER"
# Kill cron if it is running (check prepare for details).
if loginctl show-session 2 >/dev/null 2>&1 && loginctl show-session 2 | grep -q Service=crond; then
loginctl kill-session 2
fi
# Kill sessions that systemd has leaked over time.
tests.session kill-leaked
# NOTE: This part of the test is very flaky if you restart systemd-logind
# It *seems* that all new systems (Debian Sid, Arch, Tumbleweed and Ubuntu 20.04)
# Forget about the root session that was there to begin with.
# When we are done the sessions are exactly what we started with but this
# can take a moment as the termination process is asnychronous.
retry -n 10 --wait 1 sh -c 'loginctl --no-legend list-sessions | sort > after.txt && diff -u before.txt after.txt'
# Remove files we've created
rm -f /tmp/{inner,outer}.pid
rm -f {before,after}.txt
# Restart background stuff we stopped.
sh -xe defer.sh && rm -f defer.sh
debug: |
tests.session dump > after.debug.txt
diff -u before.debug.txt after.debug.txt
echo "Active timers"
systemctl list-timers
execute: |
# Check that stdin is forwarded correctly.
echo "it-works" | tests.session -u "$USER" exec cat | MATCH "it-works"
for n in $(seq 300); do
echo "ITERATION $(date) $n"
tests.session -u "$USER" exec id -u 2>/tmp/tests.session.log | MATCH "$(id -u "$USER")"
tests.session -u "$USER" exec env 2>/tmp/tests.session.log | MATCH "XDG_RUNTIME_DIR=/run/user/$(id -u "$USER")"
# We get a logind session
tests.session -u "$USER" exec loginctl list-sessions | grep "$USER"
# Exit code is forwarded
tests.session -u "$USER" exec true
not tests.session -u "$USER" exec false
# The -p option can be used to know the PID of the started program.
# This is different from the pid of tests.session as there's a session
# manager overlooking the termination of PAM stack (internally we use runuser).
tests.session -u "$USER" -p /tmp/outer.pid exec python3 -c 'import os; print(os.getpid())' >/tmp/inner.pid
test "$(cat /tmp/outer.pid)" = "$(cat /tmp/inner.pid)"
done
|