From: Zorro Lang <zlang@kernel.org>
To: fstests@vger.kernel.org
Cc: Andrey Albershteyn <aalbersh@redhat.com>,
cem@kernel.org, Dave Chinner <david@fromorbit.com>,
linux-xfs@vger.kernel.org, linux-btrfs@vger.kernel.org,
linux-ext4@vger.kernel.org
Subject: [PATCH 1/6] common: add builtin test hook infrastructure
Date: Thu, 26 Nov 2026 05:28:18 +0800 [thread overview]
Message-ID: <20261125212823.3389430-2-zlang@kernel.org> (raw)
In-Reply-To: <20261125212823.3389430-1-zlang@kernel.org>
From: Dave Chinner <dchinner@redhat.com>
Add support for user-defined test hooks so users can customize a
test run without modifying fstests. Each hook is a bash script that
defines hook_execute(). 'check' sources it in a subshell at specific
points during a test, giving it full access to the test environment
(devices, mount points, options, and fstests helpers).
Hooks are registered as:
--hook-pre-test path=/path/to/script[,match=<glob>]...
--hook-post-test and --hook-post-result take the same format. These
options can be repeated; hooks run in the order given. Without a
match pattern, a hook runs for every test; otherwise, it runs only
when the test name (<type>/<seq>, e.g. generic/482) matches at least
one of the glob patterns.
hook_execute() is passed the test name, along with the exit code
(post-test) or final result (post-result). Hook output is saved to
$seqres.hooks, and hook failures are logged without affecting the
test result.
Based on Dave Chinner's hook infrastructure RFC:
https://lore.kernel.org/all/20210722064725.3077558-1-david@fromorbit.com/
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Signed-off-by: Zorro Lang <zlang@kernel.org>
---
common/hooks | 175 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 175 insertions(+)
create mode 100644 common/hooks
diff --git a/common/hooks b/common/hooks
new file mode 100644
index 00000000..b40028f2
--- /dev/null
+++ b/common/hooks
@@ -0,0 +1,175 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Test hooks let you customize a test run without modifying fstests.
+# Each hook is a bash script that 'check' sources in a subshell at
+# specific points during a test, with full access to the test
+# environment (devices, mount points, options, and fstests helpers).
+#
+# Register hooks on the command line:
+#
+# --hook-pre-test path=/path/to/script[,match=<glob>]...
+#
+# --hook-post-test and --hook-post-result take the same format. These
+# options can be repeated; hooks run in the order given. Without a
+# match pattern, a hook runs for every test; otherwise, it runs only
+# when the test name (<type>/<seq>, e.g. generic/482) matches at least
+# one of the glob patterns.
+#
+# Relative paths are resolved from $here (the fstests root directory).
+# Tilde (~) expansion is not supported; use $HOME or an absolute path.
+# Match patterns cannot contain whitespace or commas.
+#
+# Each hook script should define a hook_execute() function, which is
+# invoked with:
+#
+# $1 test name (e.g. generic/482)
+# $2 status: test exit code (post-test), final result
+# such as pass/fail/notrun/expunge (post-result),
+# or unset (pre-test)
+# HOOK_EVENT the event name (pre-test, post-test, post-result)
+# runtime test runtime in seconds (if applicable)
+# cwd $here
+# stdout/stderr appended to $seqres.hooks
+#
+# Scripts that don't define hook_execute() are silently skipped. Hook
+# failures are logged to $seqres.hooks and never affect the test result.
+
+_hook_pre_test_paths=()
+_hook_pre_test_matches=()
+_hook_post_test_paths=()
+_hook_post_test_matches=()
+_hook_post_result_paths=()
+_hook_post_result_matches=()
+
+# Parse a single --hook-<event> option and record it in the per-event
+# arrays. Repeated options are preserved in the order given.
+_hook_parse_args()
+{
+ local event="$1" args="$2"
+ local opts pair key value
+ local path="" matches=()
+
+ case "$event" in
+ pre-test|post-test|post-result)
+ ;;
+ *)
+ _fatal "hook $event: unknown event"
+ ;;
+ esac
+
+ IFS=, read -ra opts <<< "$args"
+ for pair in "${opts[@]}"; do
+ key="${pair%%=*}"
+ value="${pair#*=}"
+ if [ "$key" = "$pair" ]; then
+ _fatal "hook $event: expected key=value, got '$pair'"
+ fi
+ case "$key" in
+ path)
+ [ -z "$path" ] || _fatal "hook $event: duplicate 'path'"
+ path="$value"
+ ;;
+ match)
+ matches+=("$value")
+ ;;
+ *)
+ _fatal "hook $event: unknown option '$key'"
+ ;;
+ esac
+ done
+
+ [ -n "$path" ] || _fatal "hook $event: missing 'path'"
+ case "$path" in
+ /*) ;;
+ *) path="$here/$path" ;;
+ esac
+ [ -r "$path" ] || _fatal "hook $event: hook script not readable: $path"
+
+ case "$event" in
+ pre-test)
+ _hook_pre_test_paths+=("$path")
+ _hook_pre_test_matches+=("${matches[*]}")
+ ;;
+ post-test)
+ _hook_post_test_paths+=("$path")
+ _hook_post_test_matches+=("${matches[*]}")
+ ;;
+ post-result)
+ _hook_post_result_paths+=("$path")
+ _hook_post_result_matches+=("${matches[*]}")
+ ;;
+ esac
+}
+
+# Return 0 if $test_seqnum matches any of the space-separated patterns,
+# or if no patterns are specified.
+_hook_matches()
+{
+ local test_seqnum="$1" patterns="$2"
+ local p
+
+ [ -z "$patterns" ] && return 0
+ for p in $patterns; do
+ [[ "$test_seqnum" == $p ]] && return 0
+ done
+ return 1
+}
+
+# Run a single hook in a subshell if its patterns match $test_seqnum.
+# Output and errors go to $seqres.hooks; failures never fail the test.
+_hook_run_one()
+{
+ local event="$1" test_seqnum="$2" status="$3" runtime="$4"
+ local path="$5" patterns="$6"
+ local rc log="$seqres.hooks"
+
+ _hook_matches "$test_seqnum" "$patterns" || return 0
+
+ (
+ export HOOK_EVENT="$event"
+ export runtime="$runtime"
+ . "$path"
+ declare -F hook_execute > /dev/null || return 0
+ hook_execute "$test_seqnum" ${status:+"$status"}
+ ) >> "$log" 2>&1
+ rc=$?
+ if [ $rc -ne 0 ]; then
+ echo "hook $event: $path failed (exit code $rc)" >> "$log"
+ fi
+}
+
+# Run all hooks registered for $event that match $test_seqnum, in the
+# order they were given. $status and $runtime are optional and depend
+# on the event.
+_hook_run_event()
+{
+ local event="$1" test_seqnum="$2" status="$3" runtime="$4"
+ local i
+
+ case "$event" in
+ pre-test)
+ for ((i = 0; i < ${#_hook_pre_test_paths[@]}; i++)); do
+ _hook_run_one "$event" "$test_seqnum" \
+ "$status" "$runtime" \
+ "${_hook_pre_test_paths[$i]}" \
+ "${_hook_pre_test_matches[$i]}"
+ done
+ ;;
+ post-test)
+ for ((i = 0; i < ${#_hook_post_test_paths[@]}; i++)); do
+ _hook_run_one "$event" "$test_seqnum" \
+ "$status" "$runtime" \
+ "${_hook_post_test_paths[$i]}" \
+ "${_hook_post_test_matches[$i]}"
+ done
+ ;;
+ post-result)
+ for ((i = 0; i < ${#_hook_post_result_paths[@]}; i++)); do
+ _hook_run_one "$event" "$test_seqnum" \
+ "$status" "$runtime" \
+ "${_hook_post_result_paths[$i]}" \
+ "${_hook_post_result_matches[$i]}"
+ done
+ ;;
+ esac
+}
--
2.56.0
next prev parent reply other threads:[~2026-11-25 21:28 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 21:28 [PATCH 0/6] Add test hook infrastructure Zorro Lang
2026-11-25 21:28 ` Zorro Lang [this message]
2026-11-25 21:28 ` [PATCH 2/6] common: add out-of-process hook mode Zorro Lang
2026-11-25 21:28 ` [PATCH 3/6] check: introduce pre-test hook and CLI option Zorro Lang
2026-11-25 21:28 ` [PATCH 4/6] check: introduce post-test " Zorro Lang
2026-11-25 21:28 ` [PATCH 5/6] check: introduce post-result " Zorro Lang
2026-11-25 21:28 ` [PATCH 6/6] doc: document the test hooks Zorro Lang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125212823.3389430-2-zlang@kernel.org \
--to=zlang@kernel.org \
--cc=aalbersh@redhat.com \
--cc=cem@kernel.org \
--cc=david@fromorbit.com \
--cc=fstests@vger.kernel.org \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-xfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox