]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: wait until the unit leaves the 'inactive' state as well
authorFrantisek Sumsal <frantisek@sumsal.cz>
Thu, 17 Jun 2021 18:17:25 +0000 (20:17 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 18 Jun 2021 04:46:29 +0000 (13:46 +0900)
In many CI runs I noticed a race where we check the "active" state a bit
too early where the unit is still in the "inactive" state, causing the
`is-failed` check to fail. Mitigate this by waiting even if the unit is
in the inactive state and introduce a "safe net" which checks whether
the unit is not restarting indefinitely or more than it should (as
described in the original issue #3166).

Example:
```
[    5.757784] testsuite-11.sh[216]: + systemctl --no-block start fail-on-restart.service
[    5.853657] testsuite-11.sh[222]: ++ systemctl show --value --property ActiveState fail-on-restart.service
[    5.946044] testsuite-11.sh[216]: + active_state=inactive
[    5.946044] testsuite-11.sh[216]: + [[ inactive == \a\c\t\i\v\a\t\i\n\g ]]
[    5.946044] testsuite-11.sh[216]: + [[ inactive == \a\c\t\i\v\e ]]
[    5.946044] testsuite-11.sh[216]: + systemctl is-failed fail-on-restart.service
[    5.946816] systemd[1]: fail-on-restart.service: Passing 0 fds to service
[    5.946913] systemd[1]: fail-on-restart.service: About to execute false
[    5.947011] systemd[1]: fail-on-restart.service: Forked false as 228
[    5.947093] systemd[1]: fail-on-restart.service: Changed dead -> start
[    5.947172] systemd[1]: Starting Fail on restart...
[    5.947272] systemd[228]: fail-on-restart.service: Executing: false
[    5.960553] testsuite-11.sh[227]: activating
[    5.965188] testsuite-11.sh[216]: + exit 1
[    6.011838] systemd[1]: Received SIGCHLD from PID 228 (4).
[    6.012510] systemd[1]: fail-on-restart.service: Main process exited, code=exited, status=1/FAILURE
[    6.012638] systemd[1]: fail-on-restart.service: Failed with result 'exit-code'.
[    6.012834] systemd[1]: fail-on-restart.service: Service will restart (restart setting)
[    6.012963] systemd[1]: fail-on-restart.service: Changed running -> failed
[    6.013081] systemd[1]: fail-on-restart.service: Unit entered failed state.
```

test/units/testsuite-11.sh

index 97ab8be7574e4a342747c38dce71d3f9b5b6b885..d05f6098777dae20bc3c95420614dd64570a480b 100755 (executable)
@@ -4,9 +4,10 @@ set -o pipefail
 
 systemctl --no-block start fail-on-restart.service
 active_state=$(systemctl show --value --property ActiveState fail-on-restart.service)
-while [[ "$active_state" == "activating" || "$active_state" == "active" ]]; do
-    sleep 1
+while [[ "$active_state" == "activating" || "$active_state" =~ ^(in)?active$ ]]; do
+    sleep .5
     active_state=$(systemctl show --value --property ActiveState fail-on-restart.service)
 done
 systemctl is-failed fail-on-restart.service || exit 1
+[[ "$(systemctl show --value --property NRestarts fail-on-restart.service)" -le 3 ]] || exit 1
 touch /testok