]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
TEST-03-JOBS: modernize test code and extend timeout
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 29 Jul 2025 02:07:35 +0000 (11:07 +0900)
committerLuca Boccassi <luca.boccassi@gmail.com>
Mon, 4 Aug 2025 15:48:07 +0000 (16:48 +0100)
- use timeout command more,
- use `(! cmd)` rather than `cmd && exit 1`,
- drop unnecessary `|| exit 1`,
- extend timeout to support slow test environment.

Fixes #38395.

(cherry picked from commit 065941a92c81dde6706215cb3390b90fc06ff2ce)

test/units/TEST-03-JOBS.sh

index 183439e913bfecfc6fbe7628bb8e3e5017a43570..719c16b986ad36f37950079d1f8df2e60d6f4b03 100755 (executable)
@@ -17,25 +17,16 @@ systemctl daemon-reexec
 
 systemctl start --no-block hello-after-sleep.target
 
-systemctl list-jobs >/root/list-jobs.txt
-until grep 'sleep\.service.*running' /root/list-jobs.txt; do
-    systemctl list-jobs >/root/list-jobs.txt
-done
-
+timeout 10 bash -c "until systemctl list-jobs | tee /root/list-jobs.txt | grep 'sleep\.service.*running'; do sleep .1; done"
 grep 'hello\.service.*waiting' /root/list-jobs.txt
 
 # This is supposed to finish quickly, not wait for sleep to finish.
-START_SEC=$(date -u '+%s')
-systemctl start --job-mode=ignore-dependencies hello
-END_SEC=$(date -u '+%s')
-ELAPSED=$((END_SEC-START_SEC))
-
-test "$ELAPSED" -lt 3
+timeout 10 systemctl start --job-mode=ignore-dependencies hello
 
 # sleep should still be running, hello not.
 systemctl list-jobs >/root/list-jobs.txt
 grep 'sleep\.service.*running' /root/list-jobs.txt
-grep 'hello\.service' /root/list-jobs.txt && exit 1
+(! grep 'hello\.service' /root/list-jobs.txt)
 systemctl stop sleep.service hello-after-sleep.target
 
 # Some basic testing that --show-transaction does something useful
@@ -62,13 +53,13 @@ ACTIVATING_ID_PRE=$(systemctl show -P InvocationID always-activating.service)
 systemctl -T start always-activating.socket # Wait for the socket to come up
 systemctl -T restart always-activating.socket
 ACTIVATING_ID_POST=$(systemctl show -P InvocationID always-activating.service)
-[ "$ACTIVATING_ID_PRE" != "$ACTIVATING_ID_POST" ] || exit 1
+[[ "$ACTIVATING_ID_PRE" != "$ACTIVATING_ID_POST" ]]
 
 # Test for irreversible jobs
 systemctl start unstoppable.service
 
 # This is expected to fail with 'job cancelled'
-systemctl stop unstoppable.service && exit 1
+(! systemctl stop unstoppable.service)
 # But this should succeed
 systemctl stop --job-mode=replace-irreversibly unstoppable.service
 
@@ -93,27 +84,28 @@ EOF
 
 # wait2 succeeds
 START_SEC=$(date -u '+%s')
-systemctl start --wait wait2.service
+timeout 10 systemctl start --wait wait2.service
 END_SEC=$(date -u '+%s')
 ELAPSED=$((END_SEC-START_SEC))
-[[ "$ELAPSED" -ge 2 ]] && [[ "$ELAPSED" -le 4 ]] || exit 1
+[[ "$ELAPSED" -ge 2 ]]
 
 # wait5fail fails, so systemctl should fail
 START_SEC=$(date -u '+%s')
 (! systemctl start --wait wait2.service wait5fail.service)
 END_SEC=$(date -u '+%s')
 ELAPSED=$((END_SEC-START_SEC))
-[[ "$ELAPSED" -ge 5 ]] && [[ "$ELAPSED" -le 7 ]] || exit 1
+[[ "$ELAPSED" -ge 5 ]]
 
 # Test time-limited scopes
 START_SEC=$(date -u '+%s')
 set +e
-systemd-run --scope --property=RuntimeMaxSec=3s sleep 10
+systemd-run --scope --property=RuntimeMaxSec=3s sleep 30
 RESULT=$?
 END_SEC=$(date -u '+%s')
 ELAPSED=$((END_SEC-START_SEC))
-[[ "$ELAPSED" -ge 3 ]] && [[ "$ELAPSED" -le 5 ]] || exit 1
-[[ "$RESULT" -ne 0 ]] || exit 1
+[[ "$ELAPSED" -ge 3 ]]
+[[ "$ELAPSED" -le 10 ]]
+[[ "$RESULT" -ne 0 ]]
 
 # Test transactions with cycles
 # Provides coverage for issues like https://github.com/systemd/systemd/issues/26872