]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/run_kselftest.sh: Allow choosing per-test log directory
authorRicardo B. Marlière <rbm@suse.com>
Fri, 20 Mar 2026 18:29:19 +0000 (15:29 -0300)
committerShuah Khan <skhan@linuxfoundation.org>
Mon, 13 Apr 2026 17:05:39 +0000 (11:05 -0600)
The --per-test-log option currently hard-codes /tmp. However, the system
under test will most likely have tmpfs mounted there. Since it's not clear
which filenames the log files will have, the user should be able to specify
a persistent directory to store the logs. Keeping those logs are important
because the run_kselftest.sh runner will only yield KTAP output, trimming
information that is otherwise available through running individual tests
directly.

Allow --per-test-log to take an optional directory argument. Keep the
existing behaviour when the option is passed without an argument, but if
a directory is provided, create it if needed, reject non-directory paths
and non-writable directories, canonicalize it, and have runner.sh write
per-test logs there instead of /tmp.

This also makes relative paths safe by resolving them before the runner
changes into a collection directory.

Signed-off-by: Ricardo B. Marlière <rbm@suse.com>
Link: https://lore.kernel.org/r/20260320-selftests-fixes-v1-4-79144f76be01@suse.com
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
tools/testing/selftests/kselftest/runner.sh
tools/testing/selftests/run_kselftest.sh

index 3eeec93c9da4253ae6a091ca408a10187f547b19..6da3390825fe6338dc7210de1d00ec88f98f04ca 100644 (file)
@@ -6,6 +6,7 @@
 export timeout_rc=124
 export logfile=/dev/stdout
 export per_test_logging=
+export per_test_log_dir=/tmp
 export RUN_IN_NETNS=
 
 # Defaults for "settings" file fields:
@@ -196,7 +197,7 @@ run_many()
                BASENAME_TEST=$(basename $TEST)
                test_num=$(( test_num + 1 ))
                if [ -n "$per_test_logging" ]; then
-                       logfile="/tmp/$BASENAME_TEST"
+                       logfile="$per_test_log_dir/$BASENAME_TEST"
                        cat /dev/null > "$logfile"
                fi
                if [ -n "$RUN_IN_NETNS" ]; then
index 8fa808e10455efa9d1abadd8b264c6b2d803209d..e11b0e93ba41f5ff6bc32fed2b1e9a8c0594fc1c 100755 (executable)
@@ -22,7 +22,7 @@ usage()
        cat <<EOF
 Usage: $0 [OPTIONS]
   -s | --summary               Print summary with detailed log in output.log (conflict with -p)
-  -p | --per-test-log          Print test log in /tmp with each test name (conflict with -s)
+  -p | --per-test-log [DIR]    Print test log in /tmp or DIR with each test name (conflict with -s)
   -t | --test COLLECTION:TEST  Run TEST from COLLECTION
   -S | --skip COLLECTION:TEST  Skip TEST from COLLECTION
   -c | --collection COLLECTION Run all tests from COLLECTION
@@ -50,7 +50,33 @@ while true; do
                        shift ;;
                -p | --per-test-log)
                        per_test_logging=1
-                       shift ;;
+                       if [ -n "$2" ] && [ "${2#-}" = "$2" ]; then
+                               per_test_log_dir="$2"
+                               if [ -e "$per_test_log_dir" ] && [ ! -d "$per_test_log_dir" ]; then
+                                       echo "Per-test log path is not a dir:" \
+                                            "$per_test_log_dir" >&2
+                                       exit 1
+                               fi
+                               if [ ! -d "$per_test_log_dir" ] && \
+                                  ! mkdir -p "$per_test_log_dir"; then
+                                       echo "Could not create log dir:" \
+                                            "$per_test_log_dir" >&2
+                                       exit 1
+                               fi
+                               per_test_log_dir=$(cd "$per_test_log_dir" && pwd -P)
+                               if [ -z "$per_test_log_dir" ]; then
+                                       echo "Could not resolve per-test log directory" >&2
+                                       exit 1
+                               fi
+                               if [ ! -w "$per_test_log_dir" ]; then
+                                       echo "Per-test log dir is not writable:" \
+                                            "$per_test_log_dir" >&2
+                                       exit 1
+                               fi
+                               shift 2
+                       else
+                               shift
+                       fi ;;
                -t | --test)
                        TESTS="$TESTS $2"
                        shift 2 ;;