2020-03-04 12:35:06 +03:00
#!/usr/bin/env bash
2021-10-17 19:13:06 +03:00
# SPDX-License-Identifier: LGPL-2.1-or-later
2021-04-09 20:39:41 +03:00
set -eux
2019-10-09 01:04:29 +03:00
set -o pipefail
2024-01-31 20:25:49 +03:00
# shellcheck source=test/units/util.sh
. " $( dirname " $0 " ) " /util.sh
2023-05-08 23:38:34 +03:00
# Test oneshot unit restart on failure
2019-10-17 18:06:18 +03:00
# wait this many secs for each test service to succeed in what is being tested
MAX_SECS = 60
2024-01-31 20:25:49 +03:00
systemctl log-level debug
2019-10-09 01:04:29 +03:00
2019-10-17 18:06:18 +03:00
# test one: Restart=on-failure should restart the service
2023-05-08 23:38:34 +03:00
( ! systemd-run --unit= oneshot-restart-one -p Type = oneshot -p Restart = on-failure /bin/bash -c "exit 1" )
2019-10-09 01:04:29 +03:00
2021-04-09 20:50:52 +03:00
for ( ( secs = 0; secs < MAX_SECS; secs++) ) ; do
2023-05-08 23:38:34 +03:00
[ [ " $( systemctl show oneshot-restart-one.service -P NRestarts) " -le 0 ] ] || break
2023-04-05 16:50:42 +03:00
sleep 1
2019-10-17 18:06:18 +03:00
done
2023-05-08 23:38:34 +03:00
if [ [ " $( systemctl show oneshot-restart-one.service -P NRestarts) " -le 0 ] ] ; then
2023-04-05 16:50:42 +03:00
exit 1
2019-10-09 01:04:29 +03:00
fi
2024-01-31 20:25:49 +03:00
TMP_FILE = " /tmp/test-23-oneshot-restart-test $RANDOM "
2019-10-09 01:04:29 +03:00
2020-03-20 23:59:54 +03:00
: >$TMP_FILE
2019-10-09 01:04:29 +03:00
2019-10-17 18:06:18 +03:00
# test two: make sure StartLimitBurst correctly limits the number of restarts
# and restarts execution of the unit from the first ExecStart=
2023-05-08 23:38:34 +03:00
( ! systemd-run --unit= oneshot-restart-two \
2023-04-05 16:50:42 +03:00
-p StartLimitIntervalSec = 120 \
-p StartLimitBurst = 3 \
-p Type = oneshot \
-p Restart = on-failure \
2024-01-31 20:25:49 +03:00
-p ExecStart = " /bin/bash -c 'printf a >> $TMP_FILE ' " /bin/bash -c "exit 1" )
2019-10-09 01:04:29 +03:00
2019-10-17 18:06:18 +03:00
# wait for at least 3 restarts
2021-04-09 20:50:52 +03:00
for ( ( secs = 0; secs < MAX_SECS; secs++) ) ; do
2023-04-05 16:50:42 +03:00
[ [ $( cat $TMP_FILE ) != "aaa" ] ] || break
sleep 1
2019-10-17 18:06:18 +03:00
done
if [ [ $( cat $TMP_FILE ) != "aaa" ] ] ; then
2023-04-05 16:50:42 +03:00
exit 1
2019-10-17 18:06:18 +03:00
fi
2019-10-09 01:04:29 +03:00
2019-10-17 18:06:18 +03:00
# wait for 5 more seconds to make sure there aren't excess restarts
sleep 5
2019-10-09 01:04:29 +03:00
if [ [ $( cat $TMP_FILE ) != "aaa" ] ] ; then
2023-04-05 16:50:42 +03:00
exit 1
2019-10-09 01:04:29 +03:00
fi
2024-01-31 20:25:49 +03:00
rm " $TMP_FILE "
# Test RestartForceExitStatus=. Note that success exit statuses are meant to be skipped
TMP_FILE = " /tmp/test-23-oneshot-restart-test $RANDOM "
2024-05-11 20:17:13 +03:00
UNIT_NAME = "TEST-23-UNIT-FILE-oneshot-restartforce.service"
ONSUCCESS_UNIT_NAME = "TEST-23-UNIT-FILE-oneshot-restartforce-onsuccess.service"
2024-01-31 20:25:49 +03:00
FIFO_FILE = "/tmp/test-23-oneshot-restart-test-fifo"
cat >" /run/systemd/system/ $UNIT_NAME " <<EOF
[ Unit]
OnSuccess = $ONSUCCESS_UNIT_NAME
[ Service]
Type = oneshot
RestartForceExitStatus = 0 2
2024-05-11 20:17:13 +03:00
ExecStart = /usr/lib/systemd/tests/testdata/TEST-23-UNIT-FILE.units/TEST-23-UNIT-FILE-oneshot-restartforce.sh " $TMP_FILE "
2024-01-31 20:25:49 +03:00
[ Install]
WantedBy = multi-user.target
EOF
cat >" /run/systemd/system/ $ONSUCCESS_UNIT_NAME " <<EOF
[ Service]
Type = oneshot
ExecStart = bash -c 'echo finished >$FIFO_FILE'
EOF
mkfifo " $FIFO_FILE "
# Pin the unit in memory
systemctl enable " $UNIT_NAME "
# Initial run should fail
( ! systemctl start " $UNIT_NAME " )
# Wait for OnSuccess=
read -r x <" $FIFO_FILE "
assert_eq " $x " "finished"
cmp -b <( systemctl show " $UNIT_NAME " -p Result -p NRestarts -p SubState) <<EOF
Result = success
NRestarts = 1
SubState = dead
EOF
systemctl disable " $UNIT_NAME "
rm " $TMP_FILE " /run/systemd/system/{ " $UNIT_NAME " ," $ONSUCCESS_UNIT_NAME " } " $FIFO_FILE "
2019-10-09 01:04:29 +03:00
2024-01-31 20:25:49 +03:00
systemctl log-level info