|
| 1 | +#!/usr/bin/env bash |
| 2 | +# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | +# shellcheck disable=SC2002 |
| 4 | +set -eux |
| 5 | +set -o pipefail |
| 6 | + |
| 7 | +# shellcheck source=test/units/util.sh |
| 8 | +. "$(dirname "$0")"/util.sh |
| 9 | + |
| 10 | +SERVICE_NAME=invocation-id-test-"$RANDOM".service |
| 11 | + |
| 12 | +TMP_DIR=$(mktemp -d) |
| 13 | + |
| 14 | +# FIXME: if the maximum log level of PID1 is debug, then journal entries of |
| 15 | +# service stdout will not contain _SYSTEMD_INVOCATION_ID field. |
| 16 | +SAVED_LOG_LEVEL=$(systemctl log-level) |
| 17 | +systemctl log-level info |
| 18 | + |
| 19 | +# Note, if the service exits extremely fast, journald cannot find the source of the |
| 20 | +# stream. Hence, we need to call 'journalctl --sync' before service exits. |
| 21 | +for i in {1..10}; do |
| 22 | + systemd-run --wait -u "$SERVICE_NAME" bash -c "echo invocation ${i} \$INVOCATION_ID; journalctl --sync" |
| 23 | +done |
| 24 | + |
| 25 | +journalctl --list-invocation -u "$SERVICE_NAME" | tee "$TMP_DIR"/10 |
| 26 | +journalctl --list-invocation -u "$SERVICE_NAME" --reverse | tee "$TMP_DIR"/10-r |
| 27 | +journalctl --list-invocation -u "$SERVICE_NAME" -n +10| tee "$TMP_DIR"/p10 |
| 28 | +journalctl --list-invocation -u "$SERVICE_NAME" -n +10 --reverse | tee "$TMP_DIR"/p10-r |
| 29 | +journalctl --list-invocation -u "$SERVICE_NAME" -n 5 | tee "$TMP_DIR"/5 |
| 30 | +journalctl --list-invocation -u "$SERVICE_NAME" -n 5 --reverse | tee "$TMP_DIR"/5-r |
| 31 | +journalctl --list-invocation -u "$SERVICE_NAME" -n +5 | tee "$TMP_DIR"/p5 |
| 32 | +journalctl --list-invocation -u "$SERVICE_NAME" -n +5 --reverse | tee "$TMP_DIR"/p5-r |
| 33 | + |
| 34 | +[[ $(cat "$TMP_DIR"/10 | wc -l) == 11 ]] |
| 35 | +[[ $(cat "$TMP_DIR"/10-r | wc -l) == 11 ]] |
| 36 | +[[ $(cat "$TMP_DIR"/p10 | wc -l) == 11 ]] |
| 37 | +[[ $(cat "$TMP_DIR"/p10-r | wc -l) == 11 ]] |
| 38 | +[[ $(cat "$TMP_DIR"/5 | wc -l) == 6 ]] |
| 39 | +[[ $(cat "$TMP_DIR"/5-r | wc -l) == 6 ]] |
| 40 | +[[ $(cat "$TMP_DIR"/p5 | wc -l) == 6 ]] |
| 41 | +[[ $(cat "$TMP_DIR"/p5-r | wc -l) == 6 ]] |
| 42 | + |
| 43 | +diff <(tail -n 10 "$TMP_DIR"/10 | tac) <(tail -n 10 "$TMP_DIR"/10-r) |
| 44 | +diff <(tail -n 5 "$TMP_DIR"/10) <(tail -n 5 "$TMP_DIR"/5) |
| 45 | +diff <(tail -n 5 "$TMP_DIR"/10 | tac) <(tail -n 5 "$TMP_DIR"/5-r) |
| 46 | +diff <(tail -n 10 "$TMP_DIR"/p10 | tac) <(tail -n 10 "$TMP_DIR"/p10-r) |
| 47 | +diff <(tail -n 10 "$TMP_DIR"/p10 | head -n 5) <(tail -n 5 "$TMP_DIR"/p5) |
| 48 | +diff <(tail -n 10 "$TMP_DIR"/p10 | head -n 5 | tac) <(tail -n 5 "$TMP_DIR"/p5-r) |
| 49 | + |
| 50 | +tail -n 10 "$TMP_DIR"/10 | |
| 51 | + while read -r idx invocation _; do |
| 52 | + i="$(( idx + 10 ))" |
| 53 | + assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${i}" -u "$SERVICE_NAME")" |
| 54 | + assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${idx}" -u "$SERVICE_NAME")" |
| 55 | + assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${invocation}")" |
| 56 | + done |
| 57 | + |
| 58 | +tail -n 10 "$TMP_DIR"/p10 | |
| 59 | + while read -r i invocation _; do |
| 60 | + idx="$(( i - 10 ))" |
| 61 | + assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${i}" -u "$SERVICE_NAME")" |
| 62 | + assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${idx}" -u "$SERVICE_NAME")" |
| 63 | + assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${invocation}")" |
| 64 | + done |
| 65 | + |
| 66 | +# Restore the log level. |
| 67 | +systemctl log-level "$SAVED_LOG_LEVEL" |
0 commit comments