1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3set -eux
4set -o pipefail
5
6fail () {
7    systemd-analyze log-level info
8    exit 1
9}
10
11# Wait for a service to enter a state within a timeout period, if it doesn't
12# enter the desired state within the timeout period then this function will
13# exit the test case with a non zero exit code.
14wait_on_state_or_fail () {
15    service=$1
16    expected_state=$2
17    timeout=$3
18
19    state=$(systemctl show "$service" --property=ActiveState --value)
20    while [ "$state" != "$expected_state" ]; do
21        if [ "$timeout" = "0" ]; then
22            fail
23        fi
24        timeout=$((timeout - 1))
25        sleep 1
26        state=$(systemctl show "$service" --property=ActiveState --value)
27    done
28}
29
30systemd-analyze log-level debug
31systemd-analyze log-target console
32
33
34cat >/run/systemd/system/testservice-fail-59.service <<EOF
35[Unit]
36Description=TEST-59-RELOADING-RESTART Normal exit
37
38[Service]
39Type=notify
40ExecStart=/bin/bash -c "systemd-notify --ready; systemd-notify RELOADING=1; sleep 1; exit 1"
41EOF
42
43cat >/run/systemd/system/testservice-fail-restart-59.service <<EOF
44[Unit]
45Description=TEST-59-RELOADING-RESTART Restart=on-failure
46
47[Service]
48Type=notify
49ExecStart=/bin/bash -c "systemd-notify --ready; systemd-notify RELOADING=1; sleep 1; exit 1"
50Restart=on-failure
51StartLimitBurst=1
52EOF
53
54
55cat >/run/systemd/system/testservice-abort-restart-59.service <<EOF
56[Unit]
57Description=TEST-59-RELOADING-RESTART Restart=on-abort
58
59[Service]
60Type=notify
61ExecStart=/bin/bash -c "systemd-notify --ready; systemd-notify RELOADING=1; sleep 5; exit 1"
62Restart=on-abort
63EOF
64
65systemctl daemon-reload
66
67# This service sends a RELOADING=1 message then exits before it sends a
68# READY=1. Ensure it enters failed state and does not linger in reloading
69# state.
70systemctl start testservice-fail-59.service
71wait_on_state_or_fail "testservice-fail-59.service" "failed" "30"
72
73# This service sends a RELOADING=1 message then exits before it sends a
74# READY=1. It should automatically restart on failure. Ensure it enters failed
75# state and does not linger in reloading state.
76systemctl start testservice-fail-restart-59.service
77wait_on_state_or_fail "testservice-fail-restart-59.service" "failed" "30"
78
79# This service sends a RELOADING=1 message then exits before it sends a
80# READY=1. It should automatically restart on abort. It will sleep for 5s
81# to allow us to send it a SIGABRT. Ensure the service enters the failed state
82# and does not linger in reloading state.
83systemctl start testservice-abort-restart-59.service
84systemctl --signal=SIGABRT kill testservice-abort-restart-59.service
85wait_on_state_or_fail "testservice-abort-restart-59.service" "failed" "30"
86
87systemd-analyze log-level info
88
89echo OK >/testok
90
91exit 0
92