Skip to content

Commit d6ea9f4

Browse files
rbmarliereshuahkh
authored andcommitted
selftests/run_kselftest.sh: Allow choosing per-test log directory
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>
1 parent a82e076 commit d6ea9f4

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

tools/testing/selftests/kselftest/runner.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
export timeout_rc=124
77
export logfile=/dev/stdout
88
export per_test_logging=
9+
export per_test_log_dir=/tmp
910
export RUN_IN_NETNS=
1011

1112
# Defaults for "settings" file fields:
@@ -196,7 +197,7 @@ run_many()
196197
BASENAME_TEST=$(basename $TEST)
197198
test_num=$(( test_num + 1 ))
198199
if [ -n "$per_test_logging" ]; then
199-
logfile="/tmp/$BASENAME_TEST"
200+
logfile="$per_test_log_dir/$BASENAME_TEST"
200201
cat /dev/null > "$logfile"
201202
fi
202203
if [ -n "$RUN_IN_NETNS" ]; then

tools/testing/selftests/run_kselftest.sh

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ usage()
2222
cat <<EOF
2323
Usage: $0 [OPTIONS]
2424
-s | --summary Print summary with detailed log in output.log (conflict with -p)
25-
-p | --per-test-log Print test log in /tmp with each test name (conflict with -s)
25+
-p | --per-test-log [DIR] Print test log in /tmp or DIR with each test name (conflict with -s)
2626
-t | --test COLLECTION:TEST Run TEST from COLLECTION
2727
-S | --skip COLLECTION:TEST Skip TEST from COLLECTION
2828
-c | --collection COLLECTION Run all tests from COLLECTION
@@ -50,7 +50,33 @@ while true; do
5050
shift ;;
5151
-p | --per-test-log)
5252
per_test_logging=1
53-
shift ;;
53+
if [ -n "$2" ] && [ "${2#-}" = "$2" ]; then
54+
per_test_log_dir="$2"
55+
if [ -e "$per_test_log_dir" ] && [ ! -d "$per_test_log_dir" ]; then
56+
echo "Per-test log path is not a dir:" \
57+
"$per_test_log_dir" >&2
58+
exit 1
59+
fi
60+
if [ ! -d "$per_test_log_dir" ] && \
61+
! mkdir -p "$per_test_log_dir"; then
62+
echo "Could not create log dir:" \
63+
"$per_test_log_dir" >&2
64+
exit 1
65+
fi
66+
per_test_log_dir=$(cd "$per_test_log_dir" && pwd -P)
67+
if [ -z "$per_test_log_dir" ]; then
68+
echo "Could not resolve per-test log directory" >&2
69+
exit 1
70+
fi
71+
if [ ! -w "$per_test_log_dir" ]; then
72+
echo "Per-test log dir is not writable:" \
73+
"$per_test_log_dir" >&2
74+
exit 1
75+
fi
76+
shift 2
77+
else
78+
shift
79+
fi ;;
5480
-t | --test)
5581
TESTS="$TESTS $2"
5682
shift 2 ;;

0 commit comments

Comments
 (0)