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