test: sync journal in short-living services

This also adjusts LogLevelMax=.

Hopefully, this makes the test much stabler.
This commit is contained in:
Yu Watanabe 2024-06-01 15:54:44 +09:00
parent 56d23c2850
commit 7faf8987ed
6 changed files with 19 additions and 10 deletions

View file

@ -6,3 +6,4 @@ Type=oneshot
ExecStart=/usr/lib/systemd/tests/testdata/units/delegated_cgroup_filtering_payload.sh ExecStart=/usr/lib/systemd/tests/testdata/units/delegated_cgroup_filtering_payload.sh
Delegate=yes Delegate=yes
SyslogLevel=notice SyslogLevel=notice
LogLevelMax=info

View file

@ -3,5 +3,9 @@ Description=Log filtering unit
[Service] [Service]
Type=oneshot Type=oneshot
ExecStart=sh -c 'echo "Logging from the service, and ~more~ foo bar" && sleep 2' ExecStart=sh -c 'echo "Logging from the service, and ~more~ foo bar"'
# If the service finishes extremely fast, journald cannot find the source of the
# stream. Hence, we need to call 'journalctl --sync' before service finishes.
ExecStart=journalctl --sync
SyslogLevel=notice SyslogLevel=notice
LogLevelMax=info

View file

@ -3,5 +3,9 @@
Description=Silent successful service Description=Silent successful service
[Service] [Service]
Type=oneshot
LogLevelMax=notice LogLevelMax=notice
ExecStart=/bin/true ExecStart=/bin/true
# If the service finishes extremely fast, journald cannot find the source of the
# stream. Hence, we need to call 'journalctl --sync' before service finishes.
ExecStart=journalctl --sync

View file

@ -4,8 +4,10 @@ Description=Verbose successful service
[Service] [Service]
Type=oneshot Type=oneshot
# Sleep so that the cgroup is still there when journald processes the log message which is required for
# journald to add the expected fields to the log message.
ExecStart=sleep 2
ExecStart=echo success ExecStart=echo success
ExecStart=sleep 2 # If the service finishes extremely fast, journald cannot find the source of the
# stream. Hence, we need to call 'journalctl --sync' before service finishes.
ExecStart=journalctl --sync
# Suppress debugging logs from PID1 or sd-executor. Otherwise, the client context
# may be outdated when the stream from 'echo' command in the above comes.
LogLevelMax=info

View file

@ -20,9 +20,9 @@ run_service_and_fetch_logs() {
local unit="${1:?}" local unit="${1:?}"
local start local start
journalctl --sync
start="$(date '+%Y-%m-%d %T.%6N')" start="$(date '+%Y-%m-%d %T.%6N')"
systemctl start "$unit" systemctl start "$unit"
journalctl --sync
journalctl -q -u "$unit" -S "$start" -p notice journalctl -q -u "$unit" -S "$start" -p notice
} }

View file

@ -104,12 +104,10 @@ diff /tmp/expected /tmp/output
# test that LogLevelMax can also suppress logging about services, not only by services # test that LogLevelMax can also suppress logging about services, not only by services
systemctl start silent-success systemctl start silent-success
journalctl --sync
[[ -z "$(journalctl -b -q -u silent-success.service)" ]] [[ -z "$(journalctl -b -q -u silent-success.service)" ]]
# Test syslog identifiers exclusion # Test syslog identifiers exclusion
systemctl start verbose-success.service systemctl start verbose-success.service
journalctl --sync
[[ -n "$(journalctl -b -q -u verbose-success.service -t systemd)" ]] [[ -n "$(journalctl -b -q -u verbose-success.service -t systemd)" ]]
[[ -n "$(journalctl -b -q -u verbose-success.service -t echo)" ]] [[ -n "$(journalctl -b -q -u verbose-success.service -t echo)" ]]
[[ -n "$(journalctl -b -q -u verbose-success.service -T systemd)" ]] [[ -n "$(journalctl -b -q -u verbose-success.service -T systemd)" ]]
@ -262,8 +260,8 @@ UNIT_NAME="test-cursor-$RANDOM.service"
CURSOR_FILE="$(mktemp)" CURSOR_FILE="$(mktemp)"
# Generate some messages we can match against # Generate some messages we can match against
journalctl --cursor-file="$CURSOR_FILE" -n1 journalctl --cursor-file="$CURSOR_FILE" -n1
systemd-run --unit="$UNIT_NAME" --wait --service-type=exec bash -ec "sleep 2; set -x; echo hello; echo world; set +x; sleep 2" systemd-run --unit="$UNIT_NAME" --wait --service-type=exec -p LogLevelMax=info \
journalctl --sync bash -ec "set -x; echo hello; echo world; set +x; journalctl --sync"
# --after-cursor= + --unit= # --after-cursor= + --unit=
# The format of the "Starting ..." message depends on StatusUnitFormat=, so match only the beginning # The format of the "Starting ..." message depends on StatusUnitFormat=, so match only the beginning
# which should be enough in this case # which should be enough in this case