xref: /linux/tools/testing/selftests/run_kselftest.sh (revision 5ea5880764cbb164afb17a62e76ca75dc371409d)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3#
4# Run installed kselftest tests.
5#
6
7BASE_DIR=$(cd "$(dirname "$0")" && pwd -P)
8
9cd $BASE_DIR
10TESTS="$BASE_DIR"/kselftest-list.txt
11if [ ! -r "$TESTS" ] ; then
12	echo "$0: Could not find list of tests to run ($TESTS)" >&2
13	available=""
14else
15	available="$(cat "$TESTS")"
16fi
17
18. ./kselftest/runner.sh
19
20usage()
21{
22	cat <<EOF
23Usage: $0 [OPTIONS]
24  -s | --summary		Print summary with detailed log in output.log (conflict with -p)
25  -p | --per-test-log [DIR]	Print test log in /tmp or DIR with each test name (conflict with -s)
26  -t | --test COLLECTION:TEST	Run TEST from COLLECTION
27  -S | --skip COLLECTION:TEST	Skip TEST from COLLECTION
28  -c | --collection COLLECTION	Run all tests from COLLECTION
29  -l | --list			List the available collection:test entries
30  -d | --dry-run		Don't actually run any tests
31  -f | --no-error-on-fail	Don't exit with an error just because tests failed
32  -n | --netns			Run each test in namespace
33  -h | --help			Show this usage info
34  -o | --override-timeout	Number of seconds after which we timeout
35EOF
36	exit $1
37}
38
39COLLECTIONS=""
40TESTS=""
41SKIP=""
42dryrun=""
43kselftest_override_timeout=""
44ERROR_ON_FAIL=true
45while true; do
46	case "$1" in
47		-s | --summary)
48			logfile="$BASE_DIR"/output.log
49			cat /dev/null > $logfile
50			shift ;;
51		-p | --per-test-log)
52			per_test_logging=1
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 ;;
80		-t | --test)
81			TESTS="$TESTS $2"
82			shift 2 ;;
83		-S | --skip)
84			SKIP="$SKIP $2"
85			shift 2 ;;
86		-c | --collection)
87			COLLECTIONS="$COLLECTIONS $2"
88			shift 2 ;;
89		-l | --list)
90			echo "$available"
91			exit 0 ;;
92		-d | --dry-run)
93			dryrun="echo"
94			shift ;;
95		-f | --no-error-on-fail)
96			ERROR_ON_FAIL=false
97			shift ;;
98		-n | --netns)
99			RUN_IN_NETNS=1
100			shift ;;
101		-o | --override-timeout)
102			kselftest_override_timeout="$2"
103			shift 2 ;;
104		-h | --help)
105			usage 0 ;;
106		"")
107			break ;;
108		*)
109			usage 1 ;;
110	esac
111done
112
113# Add all selected collections to the explicit test list.
114if [ -n "$COLLECTIONS" ]; then
115	for collection in $COLLECTIONS ; do
116		found="$(echo "$available" | grep "^$collection:")"
117		if [ -z "$found" ] ; then
118			echo "No such collection '$collection'" >&2
119			exit 1
120		fi
121		TESTS="$TESTS $found"
122	done
123fi
124# Replace available test list with explicitly selected tests.
125if [ -n "$TESTS" ]; then
126	valid=""
127	for test in $TESTS ; do
128		found="$(echo "$available" | grep "^${test}$")"
129		if [ -z "$found" ] ; then
130			echo "No such test '$test'" >&2
131			exit 1
132		fi
133		valid="$valid $found"
134	done
135	available="$(echo "$valid" | sed -e 's/ /\n/g')"
136fi
137# Remove tests to be skipped from available list
138if [ -n "$SKIP" ]; then
139	for skipped in $SKIP ; do
140		available="$(echo "$available" | grep -v "^${skipped}$")"
141	done
142fi
143
144curdir=$(pwd)
145total=$(echo "$available" | wc -w)
146collections=$(echo "$available" | cut -d: -f1 | sort | uniq)
147
148ktap_print_header
149ktap_set_plan "$total"
150
151for collection in $collections ; do
152	[ -w /dev/kmsg ] && echo "kselftest: Running tests in $collection" >> /dev/kmsg
153	tests=$(echo "$available" | grep "^$collection:" | cut -d: -f2)
154	$dryrun cd "$collection" && $dryrun run_many $tests
155	$dryrun cd "$curdir"
156done
157
158ktap_print_totals
159if "$ERROR_ON_FAIL" && [ "$KTAP_CNT_FAIL" -ne 0 ]; then
160	exit "$KSFT_FAIL"
161else
162	exit "$KSFT_PASS"
163fi
164