summaryrefslogtreecommitdiffstats
path: root/test/units/TEST-04-JOURNAL.invocation.sh
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2024-05-12 09:15:22 +0200
committerYu Watanabe <watanabe.yu+github@gmail.com>2024-08-01 00:31:44 +0200
commitb1b30d02263d886cf69dbb180fe85f1f86db5ccc (patch)
tree8848d86e2415d8bacc2e143232e01ebb525e1456 /test/units/TEST-04-JOURNAL.invocation.sh
parentjournalctl: add --list-invocations command and -I/--invocation options (diff)
downloadsystemd-b1b30d02263d886cf69dbb180fe85f1f86db5ccc.tar.xz
systemd-b1b30d02263d886cf69dbb180fe85f1f86db5ccc.zip
test: add test for journalctl --list-invocations and --invocation=
Diffstat (limited to 'test/units/TEST-04-JOURNAL.invocation.sh')
-rwxr-xr-xtest/units/TEST-04-JOURNAL.invocation.sh67
1 files changed, 67 insertions, 0 deletions
diff --git a/test/units/TEST-04-JOURNAL.invocation.sh b/test/units/TEST-04-JOURNAL.invocation.sh
new file mode 100755
index 0000000000..129d16d570
--- /dev/null
+++ b/test/units/TEST-04-JOURNAL.invocation.sh
@@ -0,0 +1,67 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: LGPL-2.1-or-later
+# shellcheck disable=SC2002
+set -eux
+set -o pipefail
+
+# shellcheck source=test/units/util.sh
+. "$(dirname "$0")"/util.sh
+
+SERVICE_NAME=invocation-id-test-"$RANDOM".service
+
+TMP_DIR=$(mktemp -d)
+
+# FIXME: if the maximum log level of PID1 is debug, then journal entries of
+# service stdout will not contain _SYSTEMD_INVOCATION_ID field.
+SAVED_LOG_LEVEL=$(systemctl log-level)
+systemctl log-level info
+
+# Note, if the service exits extremely fast, journald cannot find the source of the
+# stream. Hence, we need to call 'journalctl --sync' before service exits.
+for i in {1..10}; do
+ systemd-run --wait -u "$SERVICE_NAME" bash -c "echo invocation ${i} \$INVOCATION_ID; journalctl --sync"
+done
+
+journalctl --list-invocation -u "$SERVICE_NAME" | tee "$TMP_DIR"/10
+journalctl --list-invocation -u "$SERVICE_NAME" --reverse | tee "$TMP_DIR"/10-r
+journalctl --list-invocation -u "$SERVICE_NAME" -n +10 | tee "$TMP_DIR"/p10
+journalctl --list-invocation -u "$SERVICE_NAME" -n +10 --reverse | tee "$TMP_DIR"/p10-r
+journalctl --list-invocation -u "$SERVICE_NAME" -n 5 | tee "$TMP_DIR"/5
+journalctl --list-invocation -u "$SERVICE_NAME" -n 5 --reverse | tee "$TMP_DIR"/5-r
+journalctl --list-invocation -u "$SERVICE_NAME" -n +5 | tee "$TMP_DIR"/p5
+journalctl --list-invocation -u "$SERVICE_NAME" -n +5 --reverse | tee "$TMP_DIR"/p5-r
+
+[[ $(cat "$TMP_DIR"/10 | wc -l) == 11 ]]
+[[ $(cat "$TMP_DIR"/10-r | wc -l) == 11 ]]
+[[ $(cat "$TMP_DIR"/p10 | wc -l) == 11 ]]
+[[ $(cat "$TMP_DIR"/p10-r | wc -l) == 11 ]]
+[[ $(cat "$TMP_DIR"/5 | wc -l) == 6 ]]
+[[ $(cat "$TMP_DIR"/5-r | wc -l) == 6 ]]
+[[ $(cat "$TMP_DIR"/p5 | wc -l) == 6 ]]
+[[ $(cat "$TMP_DIR"/p5-r | wc -l) == 6 ]]
+
+diff <(tail -n 10 "$TMP_DIR"/10 | tac) <(tail -n 10 "$TMP_DIR"/10-r)
+diff <(tail -n 5 "$TMP_DIR"/10) <(tail -n 5 "$TMP_DIR"/5)
+diff <(tail -n 5 "$TMP_DIR"/10 | tac) <(tail -n 5 "$TMP_DIR"/5-r)
+diff <(tail -n 10 "$TMP_DIR"/p10 | tac) <(tail -n 10 "$TMP_DIR"/p10-r)
+diff <(tail -n 10 "$TMP_DIR"/p10 | head -n 5) <(tail -n 5 "$TMP_DIR"/p5)
+diff <(tail -n 10 "$TMP_DIR"/p10 | head -n 5 | tac) <(tail -n 5 "$TMP_DIR"/p5-r)
+
+tail -n 10 "$TMP_DIR"/10 |
+ while read -r idx invocation _; do
+ i="$(( idx + 10 ))"
+ assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${i}" -u "$SERVICE_NAME")"
+ assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${idx}" -u "$SERVICE_NAME")"
+ assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${invocation}")"
+ done
+
+tail -n 10 "$TMP_DIR"/p10 |
+ while read -r i invocation _; do
+ idx="$(( i - 10 ))"
+ assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${i}" -u "$SERVICE_NAME")"
+ assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${idx}" -u "$SERVICE_NAME")"
+ assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${invocation}")"
+ done
+
+# Restore the log level.
+systemctl log-level "$SAVED_LOG_LEVEL"