xref: /linux/tools/testing/selftests/run_kselftest.sh (revision 2488655b2f6b9d7d4afc19ecc1e7b1dccd67b13c)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3#
4# Run installed kselftest tests.
5#
6
7# Fallback to readlink if realpath is not available
8if which realpath > /dev/null; then
9        BASE_DIR=$(realpath $(dirname $0))
10else
11        BASE_DIR=$(readlink -f $(dirname $0))
12fi
13
14cd $BASE_DIR
15TESTS="$BASE_DIR"/kselftest-list.txt
16if [ ! -r "$TESTS" ] ; then
17	echo "$0: Could not find list of tests to run ($TESTS)" >&2
18	available=""
19else
20	available="$(cat "$TESTS")"
21fi
22
23. ./kselftest/runner.sh
24ROOT=$PWD
25
26usage()
27{
28	cat <<EOF
29Usage: $0 [OPTIONS]
30  -s | --summary		Print summary with detailed log in output.log (conflict with -p)
31  -p | --per-test-log		Print test log in /tmp with each test name (conflict with -s)
32  -t | --test COLLECTION:TEST	Run TEST from COLLECTION
33  -c | --collection COLLECTION	Run all tests from COLLECTION
34  -l | --list			List the available collection:test entries
35  -d | --dry-run		Don't actually run any tests
36  -f | --no-error-on-fail	Don't exit with an error just because tests failed
37  -n | --netns			Run each test in namespace
38  -h | --help			Show this usage info
39  -o | --override-timeout	Number of seconds after which we timeout
40EOF
41	exit $1
42}
43
44COLLECTIONS=""
45TESTS=""
46dryrun=""
47kselftest_override_timeout=""
48ERROR_ON_FAIL=true
49while true; do
50	case "$1" in
51		-s | --summary)
52			logfile="$BASE_DIR"/output.log
53			cat /dev/null > $logfile
54			shift ;;
55		-p | --per-test-log)
56			per_test_logging=1
57			shift ;;
58		-t | --test)
59			TESTS="$TESTS $2"
60			shift 2 ;;
61		-c | --collection)
62			COLLECTIONS="$COLLECTIONS $2"
63			shift 2 ;;
64		-l | --list)
65			echo "$available"
66			exit 0 ;;
67		-d | --dry-run)
68			dryrun="echo"
69			shift ;;
70		-f | --no-error-on-fail)
71			ERROR_ON_FAIL=false
72			shift ;;
73		-n | --netns)
74			RUN_IN_NETNS=1
75			shift ;;
76		-o | --override-timeout)
77			kselftest_override_timeout="$2"
78			shift 2 ;;
79		-h | --help)
80			usage 0 ;;
81		"")
82			break ;;
83		*)
84			usage 1 ;;
85	esac
86done
87
88# Add all selected collections to the explicit test list.
89if [ -n "$COLLECTIONS" ]; then
90	for collection in $COLLECTIONS ; do
91		found="$(echo "$available" | grep "^$collection:")"
92		if [ -z "$found" ] ; then
93			echo "No such collection '$collection'" >&2
94			exit 1
95		fi
96		TESTS="$TESTS $found"
97	done
98fi
99# Replace available test list with explicitly selected tests.
100if [ -n "$TESTS" ]; then
101	valid=""
102	for test in $TESTS ; do
103		found="$(echo "$available" | grep "^${test}$")"
104		if [ -z "$found" ] ; then
105			echo "No such test '$test'" >&2
106			exit 1
107		fi
108		valid="$valid $found"
109	done
110	available="$(echo "$valid" | sed -e 's/ /\n/g')"
111fi
112
113kselftest_failures_file="$(mktemp --tmpdir kselftest-failures-XXXXXX)"
114export kselftest_failures_file
115
116collections=$(echo "$available" | cut -d: -f1 | sort | uniq)
117for collection in $collections ; do
118	[ -w /dev/kmsg ] && echo "kselftest: Running tests in $collection" >> /dev/kmsg
119	tests=$(echo "$available" | grep "^$collection:" | cut -d: -f2)
120	($dryrun cd "$collection" && $dryrun run_many $tests)
121done
122
123failures="$(cat "$kselftest_failures_file")"
124rm "$kselftest_failures_file"
125if "$ERROR_ON_FAIL" && [ "$failures" ]; then
126	exit 1
127fi
128