984f77
From a7375d12da80d633dfa802f2b66d2361c70c62f5 Mon Sep 17 00:00:00 2001
984f77
From: Dan Streetman <ddstreet@canonical.com>
984f77
Date: Thu, 17 Oct 2019 11:06:18 -0400
984f77
Subject: [PATCH] test: correct TEST-41 StartLimitBurst test
984f77
984f77
TEST-41 verifies that the StartLimitBurst property will correctly
984f77
limit the number of unit restarts, but the test currently doesn't
984f77
adjust the StartLimitIntervalSec which defaults to 10 seconds.
984f77
984f77
On Ubuntu CI, running under un-accelerated qemu, it can take more than
984f77
10 seconds to perform all 3 restarts, which avoids the burst limit,
984f77
and fails the test.
984f77
984f77
Instead, specify a long StartLimitIntervalSec in the test, so we can
984f77
be sure to correctly test StartLimitBurst even on slow testbeds.
984f77
984f77
Fixes #13794.
984f77
984f77
(cherry picked from commit dfec314d41159117c28dffc2b980d3bdd67c3dcb)
984f77
984f77
Related: #2042896
984f77
---
984f77
 test/TEST-41-ONESHOT-RESTART/testsuite.sh | 26 ++++++++++++++++++-----
984f77
 1 file changed, 21 insertions(+), 5 deletions(-)
984f77
984f77
diff --git a/test/TEST-41-ONESHOT-RESTART/testsuite.sh b/test/TEST-41-ONESHOT-RESTART/testsuite.sh
984f77
index f7423dbf9a..4465614ff3 100755
984f77
--- a/test/TEST-41-ONESHOT-RESTART/testsuite.sh
984f77
+++ b/test/TEST-41-ONESHOT-RESTART/testsuite.sh
984f77
@@ -2,14 +2,19 @@
984f77
 set -ex
984f77
 set -o pipefail
984f77
 
984f77
+# wait this many secs for each test service to succeed in what is being tested
984f77
+MAX_SECS=60
984f77
+
984f77
 systemd-analyze log-level debug
984f77
 systemd-analyze log-target console
984f77
 
984f77
-# These three commands should succeed.
984f77
+# test one: Restart=on-failure should restart the service
984f77
 ! systemd-run --unit=one -p Type=oneshot -p Restart=on-failure /bin/bash -c "exit 1"
984f77
 
984f77
-sleep 5
984f77
-
984f77
+for ((secs=0; secs<$MAX_SECS; secs++)); do
984f77
+  [[ "$(systemctl show one.service -p NRestarts --value)" -le 0 ]] || break
984f77
+  sleep 1
984f77
+done
984f77
 if [[ "$(systemctl show one.service -p NRestarts --value)" -le 0 ]]; then
984f77
   exit 1
984f77
 fi
984f77
@@ -18,10 +23,21 @@ TMP_FILE="/test-41-oneshot-restart-test"
984f77
 
984f77
 touch $TMP_FILE
984f77
 
984f77
-! systemd-run --unit=two -p StartLimitBurst=3 -p Type=oneshot -p Restart=on-failure -p ExecStart="/bin/bash -c \"printf a >>  $TMP_FILE\"" /bin/bash -c "exit 1"
984f77
+# test two: make sure StartLimitBurst correctly limits the number of restarts
984f77
+# and restarts execution of the unit from the first ExecStart=
984f77
+! 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"
984f77
 
984f77
-sleep 5
984f77
+# wait for at least 3 restarts
984f77
+for ((secs=0; secs<$MAX_SECS; secs++)); do
984f77
+  [[ $(cat $TMP_FILE) != "aaa" ]] || break
984f77
+  sleep 1
984f77
+done
984f77
+if [[ $(cat $TMP_FILE) != "aaa" ]]; then
984f77
+  exit 1
984f77
+fi
984f77
 
984f77
+# wait for 5 more seconds to make sure there aren't excess restarts
984f77
+sleep 5
984f77
 if [[ $(cat $TMP_FILE) != "aaa" ]]; then
984f77
   exit 1
984f77
 fi