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