xref: /linux/tools/testing/selftests/run_kselftest.sh (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
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  -S | --skip COLLECTION:TEST	Skip TEST from COLLECTION
34  -c | --collection COLLECTION	Run all tests from COLLECTION
35  -l | --list			List the available collection:test entries
36  -d | --dry-run		Don't actually run any tests
37  -f | --no-error-on-fail	Don't exit with an error just because tests failed
38  -n | --netns			Run each test in namespace
39  -h | --help			Show this usage info
40  -o | --override-timeout	Number of seconds after which we timeout
41EOF
42	exit $1
43}
44
45COLLECTIONS=""
46TESTS=""
47SKIP=""
48dryrun=""
49kselftest_override_timeout=""
50ERROR_ON_FAIL=true
51while true; do
52	case "$1" in
53		-s | --summary)
54			logfile="$BASE_DIR"/output.log
55			cat /dev/null > $logfile
56			shift ;;
57		-p | --per-test-log)
58			per_test_logging=1
59			shift ;;
60		-t | --test)
61			TESTS="$TESTS $2"
62			shift 2 ;;
63		-S | --skip)
64			SKIP="$SKIP $2"
65			shift 2 ;;
66		-c | --collection)
67			COLLECTIONS="$COLLECTIONS $2"
68			shift 2 ;;
69		-l | --list)
70			echo "$available"
71			exit 0 ;;
72		-d | --dry-run)
73			dryrun="echo"
74			shift ;;
75		-f | --no-error-on-fail)
76			ERROR_ON_FAIL=false
77			shift ;;
78		-n | --netns)
79			RUN_IN_NETNS=1
80			shift ;;
81		-o | --override-timeout)
82			kselftest_override_timeout="$2"
83			shift 2 ;;
84		-h | --help)
85			usage 0 ;;
86		"")
87			break ;;
88		*)
89			usage 1 ;;
90	esac
91done
92
93# Add all selected collections to the explicit test list.
94if [ -n "$COLLECTIONS" ]; then
95	for collection in $COLLECTIONS ; do
96		found="$(echo "$available" | grep "^$collection:")"
97		if [ -z "$found" ] ; then
98			echo "No such collection '$collection'" >&2
99			exit 1
100		fi
101		TESTS="$TESTS $found"
102	done
103fi
104# Replace available test list with explicitly selected tests.
105if [ -n "$TESTS" ]; then
106	valid=""
107	for test in $TESTS ; do
108		found="$(echo "$available" | grep "^${test}$")"
109		if [ -z "$found" ] ; then
110			echo "No such test '$test'" >&2
111			exit 1
112		fi
113		valid="$valid $found"
114	done
115	available="$(echo "$valid" | sed -e 's/ /\n/g')"
116fi
117# Remove tests to be skipped from available list
118if [ -n "$SKIP" ]; then
119	for skipped in $SKIP ; do
120		available="$(echo "$available" | grep -v "^${skipped}$")"
121	done
122fi
123
124kselftest_failures_file="$(mktemp --tmpdir kselftest-failures-XXXXXX)"
125export kselftest_failures_file
126
127collections=$(echo "$available" | cut -d: -f1 | sort | uniq)
128for collection in $collections ; do
129	[ -w /dev/kmsg ] && echo "kselftest: Running tests in $collection" >> /dev/kmsg
130	tests=$(echo "$available" | grep "^$collection:" | cut -d: -f2)
131	($dryrun cd "$collection" && $dryrun run_many $tests)
132done
133
134failures="$(cat "$kselftest_failures_file")"
135rm "$kselftest_failures_file"
136if "$ERROR_ON_FAIL" && [ "$failures" ]; then
137	exit 1
138fi
139