]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/units/testsuite-03.sh
service: add new RestartMode option
[thirdparty/systemd.git] / test / units / testsuite-03.sh
CommitLineData
ff12a795 1#!/usr/bin/env bash
7b3cec95 2# SPDX-License-Identifier: LGPL-2.1-or-later
084575ff
FS
3set -eux
4set -o pipefail
b5da077d 5
5db456d0
MY
6# shellcheck source=test/units/util.sh
7. "$(dirname "$0")"/util.sh
8
d1b1bbfb
FS
9# Simple test for that daemon-reexec works in container.
10# See: https://github.com/systemd/systemd/pull/23883
11systemctl daemon-reexec
12
903e7c37 13# Test merging of a --job-mode=ignore-dependencies job into a previously
b5da077d
MS
14# installed job.
15
16systemctl start --no-block hello-after-sleep.target
b46f4ef1 17
0ee99483 18systemctl list-jobs >/root/list-jobs.txt
b46f4ef1 19while ! grep 'sleep\.service.*running' /root/list-jobs.txt; do
0ee99483 20 systemctl list-jobs >/root/list-jobs.txt
b46f4ef1
EV
21done
22
818567fc 23grep 'hello\.service.*waiting' /root/list-jobs.txt
b5da077d
MS
24
25# This is supposed to finish quickly, not wait for sleep to finish.
26START_SEC=$(date -u '+%s')
903e7c37 27systemctl start --job-mode=ignore-dependencies hello
b5da077d 28END_SEC=$(date -u '+%s')
f7940983 29ELAPSED=$((END_SEC-START_SEC))
b5da077d 30
4e20fe27 31test "$ELAPSED" -lt 3
b5da077d
MS
32
33# sleep should still be running, hello not.
0ee99483 34systemctl list-jobs >/root/list-jobs.txt
818567fc 35grep 'sleep\.service.*running' /root/list-jobs.txt
b5da077d 36grep 'hello\.service' /root/list-jobs.txt && exit 1
818567fc 37systemctl stop sleep.service hello-after-sleep.target
06c1c4f9 38
f087c7e0 39# Some basic testing that --show-transaction does something useful
128db0aa 40(! systemctl is-active systemd-importd)
f087c7e0
LP
41systemctl -T start systemd-importd
42systemctl is-active systemd-importd
43systemctl --show-transaction stop systemd-importd
128db0aa 44(! systemctl is-active systemd-importd)
f087c7e0 45
59ef392e 46# Test for a crash when enqueuing a JOB_NOP when other job already exists
818567fc 47systemctl start --no-block hello-after-sleep.target
06c1c4f9
MS
48# hello.service should still be waiting, so these try-restarts will collapse
49# into NOPs.
818567fc
MP
50systemctl try-restart --job-mode=fail hello.service
51systemctl try-restart hello.service
52systemctl stop hello.service sleep.service hello-after-sleep.target
b5da077d
MS
53
54# TODO: add more job queueing/merging tests here.
55
dc06321f
AS
56# Test that restart propagates to activating units
57systemctl -T --no-block start always-activating.service
58systemctl list-jobs | grep 'always-activating.service'
59ACTIVATING_ID_PRE=$(systemctl show -P InvocationID always-activating.service)
60systemctl -T start always-activating.socket # Wait for the socket to come up
61systemctl -T restart always-activating.socket
62ACTIVATING_ID_POST=$(systemctl show -P InvocationID always-activating.service)
63[ "$ACTIVATING_ID_PRE" != "$ACTIVATING_ID_POST" ] || exit 1
64
d710d363 65# Test for irreversible jobs
818567fc 66systemctl start unstoppable.service
d710d363
MS
67
68# This is expected to fail with 'job cancelled'
69systemctl stop unstoppable.service && exit 1
70# But this should succeed
818567fc 71systemctl stop --job-mode=replace-irreversibly unstoppable.service
d710d363
MS
72
73# We're going to shutdown soon. Let's see if it succeeds when
74# there's an active service that tries to be unstoppable.
75# Shutdown of the container/VM will hang if not.
818567fc 76systemctl start unstoppable.service
d710d363 77
0923b425 78# Test waiting for a started units to terminate again
0ee99483 79cat <<EOF >/run/systemd/system/wait2.service
93a08841
MP
80[Unit]
81Description=Wait for 2 seconds
82[Service]
83ExecStart=/bin/sh -ec 'sleep 2'
84EOF
0ee99483 85cat <<EOF >/run/systemd/system/wait5fail.service
93a08841
MP
86[Unit]
87Description=Wait for 5 seconds and fail
88[Service]
89ExecStart=/bin/sh -ec 'sleep 5; false'
90EOF
91
92# wait2 succeeds
93START_SEC=$(date -u '+%s')
818567fc 94systemctl start --wait wait2.service
93a08841 95END_SEC=$(date -u '+%s')
f7940983 96ELAPSED=$((END_SEC-START_SEC))
5209e9af 97[[ "$ELAPSED" -ge 2 ]] && [[ "$ELAPSED" -le 4 ]] || exit 1
93a08841
MP
98
99# wait5fail fails, so systemctl should fail
100START_SEC=$(date -u '+%s')
128db0aa 101(! systemctl start --wait wait2.service wait5fail.service)
93a08841 102END_SEC=$(date -u '+%s')
f7940983 103ELAPSED=$((END_SEC-START_SEC))
93a08841
MP
104[[ "$ELAPSED" -ge 5 ]] && [[ "$ELAPSED" -le 7 ]] || exit 1
105
9ed7de60
PW
106# Test time-limited scopes
107START_SEC=$(date -u '+%s')
108set +e
109systemd-run --scope --property=RuntimeMaxSec=3s sleep 10
110RESULT=$?
111END_SEC=$(date -u '+%s')
f7940983 112ELAPSED=$((END_SEC-START_SEC))
9ed7de60
PW
113[[ "$ELAPSED" -ge 3 ]] && [[ "$ELAPSED" -le 5 ]] || exit 1
114[[ "$RESULT" -ne 0 ]] || exit 1
115
0651e717
FS
116# Test transactions with cycles
117# Provides coverage for issues like https://github.com/systemd/systemd/issues/26872
118for i in {0..19}; do
119 cat >"/run/systemd/system/transaction-cycle$i.service" <<EOF
120[Unit]
121After=transaction-cycle$(((i + 1) % 20)).service
122Requires=transaction-cycle$(((i + 1) % 20)).service
123
124[Service]
125ExecStart=true
126EOF
127done
128systemctl daemon-reload
129for i in {0..19}; do
130 systemctl start "transaction-cycle$i.service"
131done
132
5db456d0
MY
133# Test PropagatesStopTo= when restart (issue #26839)
134systemctl start propagatestopto-and-pullin.target
135systemctl --quiet is-active propagatestopto-and-pullin.target
136
137systemctl restart propagatestopto-and-pullin.target
138systemctl --quiet is-active propagatestopto-and-pullin.target
139systemctl --quiet is-active sleep-infinity-simple.service
140
141systemctl start propagatestopto-only.target
142systemctl --quiet is-active propagatestopto-only.target
143systemctl --quiet is-active sleep-infinity-simple.service
144
145systemctl restart propagatestopto-only.target
146assert_rc 3 systemctl --quiet is-active sleep-infinity-simple.service
147
bf3dfa62
MY
148systemctl start propagatesstopto-indirect.target propagatestopto-and-pullin.target
149systemctl --quiet is-active propagatestopto-indirect.target
150systemctl --quiet is-active propagatestopto-and-pullin.target
151
152systemctl restart propagatestopto-indirect.target
153assert_rc 3 systemctl --quiet is-active propagatestopto-and-pullin.target
154assert_rc 3 systemctl --quiet is-active sleep-infinity-simple.service
155
e568fea9
RP
156# Test restart mode direct
157systemctl start succeeds-on-restart-restartdirect.target
158assert_rc 0 systemctl --quiet is-active succeeds-on-restart-restartdirect.target
159
160systemctl start fails-on-restart-restartdirect.target || :
161assert_rc 3 systemctl --quiet is-active fails-on-restart-restartdirect.target
162
163systemctl start succeeds-on-restart.target || :
164assert_rc 3 systemctl --quiet is-active succeeds-on-restart.target
165
166systemctl start fails-on-restart.target || :
167assert_rc 3 systemctl --quiet is-active fails-on-restart.target
168
b5da077d 169touch /testok