journal: add integration tests for log filtering

Add integration tests for journald's log filtering feature.
This commit is contained in:
Quentin Deslandes 2022-09-13 16:12:36 +01:00
parent 87a13dabbd
commit 1c9c6fc7df
2 changed files with 85 additions and 0 deletions

View file

@ -0,0 +1,5 @@
[Unit]
Description=Log filtering unit
[Service]
ExecStart=sh -c 'while true; do echo "Logging from the service, and ~more~"; sleep .25; done'

View file

@ -179,4 +179,84 @@ sleep 3
# https://github.com/systemd/systemd/issues/15528
journalctl --follow --file=/var/log/journal/*/* | head -n1 || [[ $? -eq 1 ]]
function add_logs_filtering_override() {
UNIT=${1:?}
OVERRIDE_NAME=${2:?}
LOG_FILTER=${3:-""}
mkdir -p /etc/systemd/system/"$UNIT".d/
echo "[Service]" >/etc/systemd/system/logs-filtering.service.d/"${OVERRIDE_NAME}".conf
echo "LogFilterPatterns=$LOG_FILTER" >>/etc/systemd/system/logs-filtering.service.d/"${OVERRIDE_NAME}".conf
systemctl daemon-reload
}
function run_service_and_fetch_logs() {
UNIT=$1
START=$(date '+%Y-%m-%d %T.%6N')
systemctl restart "$UNIT"
sleep .5
journalctl --sync
END=$(date '+%Y-%m-%d %T.%6N')
journalctl -q -u "$UNIT" -S "$START" -U "$END" | grep -Pv "systemd\[[0-9]+\]"
systemctl stop "$UNIT"
}
function is_xattr_supported() {
START=$(date '+%Y-%m-%d %T.%6N')
systemd-run --unit text_xattr --property LogFilterPatterns=log sh -c "sleep .5"
sleep .5
journalctl --sync
END=$(date '+%Y-%m-%d %T.%6N')
systemctl stop text_xattr
if journalctl -q -u "text_xattr" -S "$START" -U "$END" --grep "Failed to set 'user.journald_log_filter_patterns' xattr.*not supported$"; then
return 1
fi
return 0
}
if is_xattr_supported; then
# Accept all log messages
add_logs_filtering_override "logs-filtering.service" "00-reset" ""
[[ -n $(run_service_and_fetch_logs "logs-filtering.service") ]]
add_logs_filtering_override "logs-filtering.service" "01-allow-all" ".*"
[[ -n $(run_service_and_fetch_logs "logs-filtering.service") ]]
# Discard all log messages
add_logs_filtering_override "logs-filtering.service" "02-discard-all" "~.*"
[[ -z $(run_service_and_fetch_logs "logs-filtering.service") ]]
# Accept all test messages
add_logs_filtering_override "logs-filtering.service" "03-reset" ""
[[ -n $(run_service_and_fetch_logs "logs-filtering.service") ]]
# Discard all test messages
add_logs_filtering_override "logs-filtering.service" "04-discard-gg" "~.*gg.*"
[[ -z $(run_service_and_fetch_logs "logs-filtering.service") ]]
# Deny filter takes precedence
add_logs_filtering_override "logs-filtering.service" "05-allow-all-but-too-late" ".*"
[[ -z $(run_service_and_fetch_logs "logs-filtering.service") ]]
# Use tilde in a deny pattern
add_logs_filtering_override "logs-filtering.service" "06-reset" ""
add_logs_filtering_override "logs-filtering.service" "07-prevent-tilde" "~~more~"
[[ -z $(run_service_and_fetch_logs "logs-filtering.service") ]]
# Only allow a pattern that won't be matched
add_logs_filtering_override "logs-filtering.service" "08-reset" ""
add_logs_filtering_override "logs-filtering.service" "09-allow-only-non-existing" "non-existing string"
[[ -z $(run_service_and_fetch_logs "logs-filtering.service") ]]
# Allow a pattern starting with a tilde
add_logs_filtering_override "logs-filtering.service" "10-allow-with-escape-char" "\x7emore~"
[[ -n $(run_service_and_fetch_logs "logs-filtering.service") ]]
rm -rf /etc/systemd/system/logs-filtering.service.d
fi
touch /testok