summaryrefslogtreecommitdiffstats
path: root/test/units/testsuite-04.sh
diff options
context:
space:
mode:
authorQuentin Deslandes <qde@naccy.de>2022-09-13 17:12:36 +0200
committerQuentin Deslandes <qde@naccy.de>2022-12-15 10:57:39 +0100
commit1c9c6fc7dfdaa8fa29d1adeeacafc2450cc86e30 (patch)
tree12e52ee82aed752db86777ea880b72a69850974d /test/units/testsuite-04.sh
parentjournal: filter log based on LogFilterPatterns (diff)
downloadsystemd-1c9c6fc7dfdaa8fa29d1adeeacafc2450cc86e30.tar.xz
systemd-1c9c6fc7dfdaa8fa29d1adeeacafc2450cc86e30.zip
journal: add integration tests for log filtering
Add integration tests for journald's log filtering feature.
Diffstat (limited to '')
-rwxr-xr-xtest/units/testsuite-04.sh80
1 files changed, 80 insertions, 0 deletions
diff --git a/test/units/testsuite-04.sh b/test/units/testsuite-04.sh
index fdc3273fea..2874fc778f 100755
--- a/test/units/testsuite-04.sh
+++ b/test/units/testsuite-04.sh
@@ -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