summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2022-12-15 22:03:33 +0900
committerGitHub <noreply@github.com>2022-12-15 22:03:33 +0900
commitb3f1afc08910044f2f55188c266264738a538d10 (patch)
tree6ecceab84c81fb20f9544bb85c1409947d609670 /test
parent0969bb4246acdac8ef91ad99515ae75fe3da734b (diff)
parent1c9c6fc7dfdaa8fa29d1adeeacafc2450cc86e30 (diff)
downloadsystemd-b3f1afc08910044f2f55188c266264738a538d10.tar.gz
Merge pull request #24058 from qdeslandes/journald_regex_filtering
Allow for journald logs filtering on a per-unit basis
Diffstat (limited to 'test')
-rw-r--r--test/fuzz/fuzz-unit-file/directives-all.service1
-rw-r--r--test/testsuite-04.units/logs-filtering.service5
-rwxr-xr-xtest/units/testsuite-04.sh80
3 files changed, 86 insertions, 0 deletions
diff --git a/test/fuzz/fuzz-unit-file/directives-all.service b/test/fuzz/fuzz-unit-file/directives-all.service
index b4cfca2814..f8237d74eb 100644
--- a/test/fuzz/fuzz-unit-file/directives-all.service
+++ b/test/fuzz/fuzz-unit-file/directives-all.service
@@ -846,6 +846,7 @@ LogExtraFields=
LogLevelMax=
LogRateLimitIntervalSec=
LogRateLimitBurst=
+LogFilterPatterns=
LogsDirectory=
LogsDirectoryMode=
MACVLAN=
diff --git a/test/testsuite-04.units/logs-filtering.service b/test/testsuite-04.units/logs-filtering.service
new file mode 100644
index 0000000000..fc89021ca9
--- /dev/null
+++ b/test/testsuite-04.units/logs-filtering.service
@@ -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'
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