]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/TEST-03-JOBS/test-jobs.sh
Merge pull request #13423 from pwithnall/12035-session-time-limits
[thirdparty/systemd.git] / test / TEST-03-JOBS / test-jobs.sh
CommitLineData
efd0b6cc 1#!/bin/bash -ex
b5da077d 2
903e7c37 3# Test merging of a --job-mode=ignore-dependencies job into a previously
b5da077d
MS
4# installed job.
5
6systemctl start --no-block hello-after-sleep.target
b46f4ef1 7
b5da077d 8systemctl list-jobs > /root/list-jobs.txt
b46f4ef1
EV
9while ! grep 'sleep\.service.*running' /root/list-jobs.txt; do
10 systemctl list-jobs > /root/list-jobs.txt
11done
12
818567fc 13grep 'hello\.service.*waiting' /root/list-jobs.txt
b5da077d
MS
14
15# This is supposed to finish quickly, not wait for sleep to finish.
16START_SEC=$(date -u '+%s')
903e7c37 17systemctl start --job-mode=ignore-dependencies hello
b5da077d
MS
18END_SEC=$(date -u '+%s')
19ELAPSED=$(($END_SEC-$START_SEC))
20
818567fc 21[ "$ELAPSED" -lt 3 ]
b5da077d
MS
22
23# sleep should still be running, hello not.
24systemctl list-jobs > /root/list-jobs.txt
818567fc 25grep 'sleep\.service.*running' /root/list-jobs.txt
b5da077d 26grep 'hello\.service' /root/list-jobs.txt && exit 1
818567fc 27systemctl stop sleep.service hello-after-sleep.target
06c1c4f9 28
f087c7e0
LP
29# Some basic testing that --show-transaction does something useful
30! systemctl is-active systemd-importd
31systemctl -T start systemd-importd
32systemctl is-active systemd-importd
33systemctl --show-transaction stop systemd-importd
34! systemctl is-active systemd-importd
35
59ef392e 36# Test for a crash when enqueuing a JOB_NOP when other job already exists
818567fc 37systemctl start --no-block hello-after-sleep.target
06c1c4f9
MS
38# hello.service should still be waiting, so these try-restarts will collapse
39# into NOPs.
818567fc
MP
40systemctl try-restart --job-mode=fail hello.service
41systemctl try-restart hello.service
42systemctl stop hello.service sleep.service hello-after-sleep.target
b5da077d
MS
43
44# TODO: add more job queueing/merging tests here.
45
d710d363 46# Test for irreversible jobs
818567fc 47systemctl start unstoppable.service
d710d363
MS
48
49# This is expected to fail with 'job cancelled'
50systemctl stop unstoppable.service && exit 1
51# But this should succeed
818567fc 52systemctl stop --job-mode=replace-irreversibly unstoppable.service
d710d363
MS
53
54# We're going to shutdown soon. Let's see if it succeeds when
55# there's an active service that tries to be unstoppable.
56# Shutdown of the container/VM will hang if not.
818567fc 57systemctl start unstoppable.service
d710d363 58
93a08841
MP
59# Test waiting for a started unit(s) to terminate again
60cat <<EOF > /run/systemd/system/wait2.service
61[Unit]
62Description=Wait for 2 seconds
63[Service]
64ExecStart=/bin/sh -ec 'sleep 2'
65EOF
66cat <<EOF > /run/systemd/system/wait5fail.service
67[Unit]
68Description=Wait for 5 seconds and fail
69[Service]
70ExecStart=/bin/sh -ec 'sleep 5; false'
71EOF
72
73# wait2 succeeds
74START_SEC=$(date -u '+%s')
818567fc 75systemctl start --wait wait2.service
93a08841
MP
76END_SEC=$(date -u '+%s')
77ELAPSED=$(($END_SEC-$START_SEC))
5209e9af 78[[ "$ELAPSED" -ge 2 ]] && [[ "$ELAPSED" -le 4 ]] || exit 1
93a08841
MP
79
80# wait5fail fails, so systemctl should fail
81START_SEC=$(date -u '+%s')
82! systemctl start --wait wait2.service wait5fail.service || exit 1
83END_SEC=$(date -u '+%s')
84ELAPSED=$(($END_SEC-$START_SEC))
85[[ "$ELAPSED" -ge 5 ]] && [[ "$ELAPSED" -le 7 ]] || exit 1
86
9ed7de60
PW
87# Test time-limited scopes
88START_SEC=$(date -u '+%s')
89set +e
90systemd-run --scope --property=RuntimeMaxSec=3s sleep 10
91RESULT=$?
92END_SEC=$(date -u '+%s')
93ELAPSED=$(($END_SEC-$START_SEC))
94[[ "$ELAPSED" -ge 3 ]] && [[ "$ELAPSED" -le 5 ]] || exit 1
95[[ "$RESULT" -ne 0 ]] || exit 1
96
b5da077d 97touch /testok