]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journal: add integration tests for log filtering 24058/head
authorQuentin Deslandes <qde@naccy.de>
Tue, 13 Sep 2022 15:12:36 +0000 (16:12 +0100)
committerQuentin Deslandes <qde@naccy.de>
Thu, 15 Dec 2022 09:57:39 +0000 (09:57 +0000)
Add integration tests for journald's log filtering feature.

test/testsuite-04.units/logs-filtering.service [new file with mode: 0644]
test/units/testsuite-04.sh

diff --git a/test/testsuite-04.units/logs-filtering.service b/test/testsuite-04.units/logs-filtering.service
new file mode 100644 (file)
index 0000000..fc89021
--- /dev/null
@@ -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'
index fdc3273fea76a90303e72712c4488b15333d3d66..2874fc778f9de924410687b5b004008f29f27952 100755 (executable)
@@ -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