Merge tag 'staging-6.5-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[platform/kernel/linux-starfive.git] / tools / testing / selftests / run_kselftest.sh
1 #!/bin/sh
2 # SPDX-License-Identifier: GPL-2.0
3 #
4 # Run installed kselftest tests.
5 #
6 BASE_DIR=$(realpath $(dirname $0))
7 cd $BASE_DIR
8 TESTS="$BASE_DIR"/kselftest-list.txt
9 if [ ! -r "$TESTS" ] ; then
10         echo "$0: Could not find list of tests to run ($TESTS)" >&2
11         available=""
12 else
13         available="$(cat "$TESTS")"
14 fi
15
16 . ./kselftest/runner.sh
17 ROOT=$PWD
18
19 usage()
20 {
21         cat <<EOF
22 Usage: $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
30 EOF
31         exit $1
32 }
33
34 COLLECTIONS=""
35 TESTS=""
36 dryrun=""
37 kselftest_override_timeout=""
38 while 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
66 done
67
68 # Add all selected collections to the explicit test list.
69 if [ -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
78 fi
79 # Replace available test list with explicitly selected tests.
80 if [ -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')"
91 fi
92
93 collections=$(echo "$available" | cut -d: -f1 | sort | uniq)
94 for 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)
98 done