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