1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3#
4# Run installed kselftest tests.
5#
6BASE_DIR=$(realpath $(dirname $0))
7cd $BASE_DIR
8TESTS="$BASE_DIR"/kselftest-list.txt
9if [ ! -r "$TESTS" ] ; then
10	echo "$0: Could not find list of tests to run ($TESTS)" >&2
11	available=""
12else
13	available="$(cat "$TESTS")"
14fi
15
16. ./kselftest/runner.sh
17ROOT=$PWD
18
19usage()
20{
21	cat <<EOF
22Usage: $0 [OPTIONS]
23  -s | --summary		Print summary with detailed log in output.log
24  -t | --test COLLECTION:TEST	Run TEST from COLLECTION
25  -c | --collection COLLECTION	Run all tests from COLLECTION
26  -l | --list			List the available collection:test entries
27  -d | --dry-run		Don't actually run any tests
28  -h | --help			Show this usage info
29  -o | --override-timeout	Number of seconds after which we timeout
30EOF
31	exit $1
32}
33
34COLLECTIONS=""
35TESTS=""
36dryrun=""
37kselftest_override_timeout=""
38while true; do
39	case "$1" in
40		-s | --summary)
41			logfile="$BASE_DIR"/output.log
42			cat /dev/null > $logfile
43			shift ;;
44		-t | --test)
45			TESTS="$TESTS $2"
46			shift 2 ;;
47		-c | --collection)
48			COLLECTIONS="$COLLECTIONS $2"
49			shift 2 ;;
50		-l | --list)
51			echo "$available"
52			exit 0 ;;
53		-d | --dry-run)
54			dryrun="echo"
55			shift ;;
56		-o | --override-timeout)
57			kselftest_override_timeout="$2"
58			shift 2 ;;
59		-h | --help)
60			usage 0 ;;
61		"")
62			break ;;
63		*)
64			usage 1 ;;
65	esac
66done
67
68# Add all selected collections to the explicit test list.
69if [ -n "$COLLECTIONS" ]; then
70	for collection in $COLLECTIONS ; do
71		found="$(echo "$available" | grep "^$collection:")"
72		if [ -z "$found" ] ; then
73			echo "No such collection '$collection'" >&2
74			exit 1
75		fi
76		TESTS="$TESTS $found"
77	done
78fi
79# Replace available test list with explicitly selected tests.
80if [ -n "$TESTS" ]; then
81	valid=""
82	for test in $TESTS ; do
83		found="$(echo "$available" | grep "^${test}$")"
84		if [ -z "$found" ] ; then
85			echo "No such test '$test'" >&2
86			exit 1
87		fi
88		valid="$valid $found"
89	done
90	available="$(echo "$valid" | sed -e 's/ /\n/g')"
91fi
92
93collections=$(echo "$available" | cut -d: -f1 | sort | uniq)
94for collection in $collections ; do
95	[ -w /dev/kmsg ] && echo "kselftest: Running tests in $collection" >> /dev/kmsg
96	tests=$(echo "$available" | grep "^$collection:" | cut -d: -f2)
97	($dryrun cd "$collection" && $dryrun run_many $tests)
98done
99