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
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
2019-10-09 01:04:29 +03:00
systemd-analyze log-level debug
2019-10-17 18:06:18 +03:00
# test one: Restart=on-failure should restart the service
2021-04-08 02:27:33 +03:00
systemd-run --unit= one -p Type = oneshot -p Restart = on-failure /bin/bash -c "exit 1" \
&& { echo 'unexpected success' ; 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
2020-04-01 19:32:30 +03:00
[ [ " $( systemctl show one.service -P NRestarts) " -le 0 ] ] || break
2019-10-17 18:06:18 +03:00
sleep 1
done
2020-04-01 19:32:30 +03:00
if [ [ " $( systemctl show one.service -P NRestarts) " -le 0 ] ] ; then
2019-10-09 01:04:29 +03:00
exit 1
fi
2020-03-20 23:59:54 +03:00
TMP_FILE = "/tmp/test-41-oneshot-restart-test"
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=
2021-04-08 02:27:33 +03:00
systemd-run --unit= two \
-p StartLimitIntervalSec = 120 \
-p StartLimitBurst = 3 \
-p Type = oneshot \
-p Restart = on-failure \
-p ExecStart = " /bin/bash -c \"printf a >> $TMP_FILE \" " /bin/bash -c "exit 1" \
&& { echo 'unexpected success' ; 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
2019-10-17 18:06:18 +03:00
[ [ $( cat $TMP_FILE ) != "aaa" ] ] || break
sleep 1
done
if [ [ $( cat $TMP_FILE ) != "aaa" ] ] ; then
exit 1
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
exit 1
fi
systemd-analyze log-level info
2021-04-08 01:09:55 +03:00
echo OK >/testok
2019-10-09 01:04:29 +03:00
exit 0