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