systemd/SOURCES/1044-test-restarting-elapsed-timer-shouldn-t-trigger-the-.patch

99 lines
3.8 KiB
Diff

From 9cb832f3c26c292d31109cb54d142277f8751e52 Mon Sep 17 00:00:00 2001
From: Frantisek Sumsal <frantisek@sumsal.cz>
Date: Tue, 23 Sep 2025 17:42:01 +0200
Subject: [PATCH] test: restarting elapsed timer shouldn't trigger the
corresponding service
Provides coverage for:
- https://github.com/systemd/systemd/issues/31231
- https://github.com/systemd/systemd/issues/35805
(cherry picked from commit 5730a400fd5ee82566fe03eb832121a0d4bc26b6)
Related: RHEL-108744
---
.../testsuite.restart-trigger.sh | 74 +++++++++++++++++++
1 file changed, 74 insertions(+)
create mode 100755 test/TEST-53-TIMER/testsuite.restart-trigger.sh
diff --git a/test/TEST-53-TIMER/testsuite.restart-trigger.sh b/test/TEST-53-TIMER/testsuite.restart-trigger.sh
new file mode 100755
index 0000000000..313b5a7fd8
--- /dev/null
+++ b/test/TEST-53-TIMER/testsuite.restart-trigger.sh
@@ -0,0 +1,74 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: LGPL-2.1-or-later
+#
+# Restarting an already elapsed timer shouldn't immediately trigger the corresponding service unit.
+#
+# Provides coverage for:
+# - https://github.com/systemd/systemd/issues/31231
+# - https://github.com/systemd/systemd/issues/35805
+set -eux
+set -o pipefail
+
+UNIT_NAME="timer-restart-$RANDOM"
+TEST_MESSAGE="Hello from timer $RANDOM"
+
+# Setup
+cat >"/run/systemd/system/$UNIT_NAME.timer" <<EOF
+[Timer]
+OnCalendar=$(date --date="+1 hour" "+%Y-%m-%d %H:%M:%S")
+AccuracySec=1s
+EOF
+
+cat >"/run/systemd/system/$UNIT_NAME.service" <<EOF
+[Service]
+ExecStart=echo "$TEST_MESSAGE"
+EOF
+
+systemctl daemon-reload
+
+JOURNAL_TS="$(date "+%s")"
+# Paranoia check that the test message is not already in the logs
+[[ "$(journalctl -q -p info --since="@$JOURNAL_TS" --unit="$UNIT_NAME" --grep="$TEST_MESSAGE" | wc -l)" -eq 0 ]]
+
+# Restart time timer and move time forward by 2 hours to trigger the timer
+systemctl restart "$UNIT_NAME.timer"
+systemctl status --no-pager "$UNIT_NAME.timer"
+
+date -s '+2 hours'
+trap 'date -s "-2 hours"' EXIT
+sleep 1
+systemctl status --no-pager "$UNIT_NAME.timer"
+[[ "$(journalctl -q -p info --since="@$JOURNAL_TS" --unit="$UNIT_NAME" --grep="$TEST_MESSAGE" | wc -l)" -eq 1 ]]
+
+# Restarting the timer unit shouldn't trigger neither the timer nor the service, so these
+# fields should remain constant through the following tests
+SERVICE_INV_ID="$(systemctl show --property=InvocationID "$UNIT_NAME.service")"
+TIMER_LAST_TRIGGER="$(systemctl show --property=LastTriggerUSec "$UNIT_NAME.timer")"
+
+# Now restart the timer and check if the timer and the service weren't triggered again
+systemctl restart "$UNIT_NAME.timer"
+sleep 5
+[[ "$(journalctl -q -p info --since="@$JOURNAL_TS" --unit="$UNIT_NAME" --grep="$TEST_MESSAGE" | wc -l)" -eq 1 ]]
+[[ "$SERVICE_INV_ID" == "$(systemctl show --property=InvocationID "$UNIT_NAME.service")" ]]
+[[ "$TIMER_LAST_TRIGGER" == "$(systemctl show --property=LastTriggerUSec "$UNIT_NAME.timer")" ]]
+
+# Set the timer into the past, restart it, and again check if it wasn't triggered
+TIMER_TS="$(date --date="-1 day" "+%Y-%m-%d %H:%M:%S")"
+mkdir "/run/systemd/system/$UNIT_NAME.timer.d/"
+cat >"/run/systemd/system/$UNIT_NAME.timer.d/99-override.conf" <<EOF
+[Timer]
+OnCalendar=$TIMER_TS
+EOF
+systemctl daemon-reload
+systemctl status --no-pager "$UNIT_NAME.timer"
+[[ "$(systemctl show -p TimersCalendar "$UNIT_NAME".timer)" == *"OnCalendar=$TIMER_TS"* ]]
+systemctl restart "$UNIT_NAME.timer"
+sleep 5
+[[ "$(journalctl -q -p info --since="@$JOURNAL_TS" --unit="$UNIT_NAME" --grep="$TEST_MESSAGE" | wc -l)" -eq 1 ]]
+[[ "$SERVICE_INV_ID" == "$(systemctl show --property=InvocationID "$UNIT_NAME.service")" ]]
+[[ "$TIMER_LAST_TRIGGER" == "$(systemctl show --property=LastTriggerUSec "$UNIT_NAME.timer")" ]]
+
+# Cleanup
+systemctl stop "$UNIT_NAME".{timer,service}
+rm -f "/run/systemd/system/$UNIT_NAME".{timer,service}
+systemctl daemon-reload