blob: 50fd4d3d957b06fe88d64536858cc2aa0b6f84d6 [file] [log] [blame]
Naresh Kambojuc6462732017-01-11 16:26:30 +05301#!/bin/sh
2# Linux kernel self test
3
4# shellcheck disable=SC1091
5. ../../lib/sh-test-lib
6OUTPUT="$(pwd)/output"
7RESULT_FILE="${OUTPUT}/result.txt"
8LOGFILE="${OUTPUT}/kselftest.txt"
Milosz Wasilewski597e24c2017-09-04 14:06:36 +01009KSELFTEST_PATH="/opt/kselftests/mainline/"
Naresh Kamboju687ede42017-05-30 15:02:23 +053010
11SCRIPT="$(readlink -f "${0}")"
12SCRIPTPATH="$(dirname "${SCRIPT}")"
13# List of known unsupported test cases to be skipped
14SKIPFILE=""
Dan Ruea12a8382018-03-05 12:03:15 -060015# List of test cases to be skipped in yaml/skipgen format
16SKIPFILE_YAML=""
17BOARD=""
18BRANCH=""
19ENVIRONMENT=""
Naresh Kamboju687ede42017-05-30 15:02:23 +053020SKIPLIST=""
Naresh Kamboju92375fe2017-06-27 17:16:21 +053021TESTPROG_URL=""
Naresh Kamboju687ede42017-05-30 15:02:23 +053022
Kees Cook888eda42020-09-14 22:52:00 -070023# Architecture-specific tarball name defaults.
24if [ "$(uname -m)" = "aarch64" ]; then
Riku Voipio85cdee42017-05-15 16:01:06 +030025 TESTPROG="kselftest_aarch64.tar.gz"
Kees Cook888eda42020-09-14 22:52:00 -070026else
27 TESTPROG="kselftest_armhf.tar.gz"
Riku Voipio85cdee42017-05-15 16:01:06 +030028fi
Naresh Kambojuc6462732017-01-11 16:26:30 +053029
30usage() {
Naresh Kamboju687ede42017-05-30 15:02:23 +053031 echo "Usage: $0 [-t kselftest_aarch64.tar.gz | kselftest_armhf.tar.gz]
32 [-s True|False]
Naresh Kamboju92375fe2017-06-27 17:16:21 +053033 [-u url]
Milosz Wasilewski597e24c2017-09-04 14:06:36 +010034 [-p path]
Naresh Kamboju687ede42017-05-30 15:02:23 +053035 [-L List of skip test cases]
Dan Ruea12a8382018-03-05 12:03:15 -060036 [-S kselftest-skipfile]
37 [-b board]
38 [-g branch]
39 [-e environment]" 1>&2
Naresh Kambojuc6462732017-01-11 16:26:30 +053040 exit 1
41}
42
Naresh Kambojue87b37e2018-11-23 10:15:00 +053043while getopts "t:s:u:p:L:S:b:g:e:h" opt; do
Naresh Kambojuc6462732017-01-11 16:26:30 +053044 case "${opt}" in
45 t) TESTPROG="${OPTARG}" ;;
46 s) SKIP_INSTALL="${OPTARG}" ;;
Naresh Kamboju92375fe2017-06-27 17:16:21 +053047 # Download kselftest tarball from given URL
48 u) TESTPROG_URL="${OPTARG}" ;;
Naresh Kamboju687ede42017-05-30 15:02:23 +053049 # List of known unsupported test cases to be skipped
50 L) SKIPLIST="${OPTARG}" ;;
Milosz Wasilewski597e24c2017-09-04 14:06:36 +010051 p) KSELFTEST_PATH="${OPTARG}" ;;
Naresh Kamboju687ede42017-05-30 15:02:23 +053052 S)
Dan Ruea12a8382018-03-05 12:03:15 -060053
54 #OPT=$(echo "${OPTARG}" | grep "http")
55 #if [ -z "${OPT}" ] ; then
56 ## kselftest skipfile
57 # SKIPFILE="${SCRIPTPATH}/${OPTARG}"
58 #else
59 ## Download kselftest skipfile from speficied URL
60 # wget "${OPTARG}" -O "skipfile"
61 # SKIPFILE="skipfile"
62 # SKIPFILE="${SCRIPTPATH}/${SKIPFILE}"
63 #fi
64
65 if [ -z "${OPTARG##*http*}" ]; then
66 if [ -z "${OPTARG##*yaml*}" ]; then
67 # Skipfile is of type yaml
68 SKIPFILE_TMP="http-skipfile.yaml"
69 SKIPFILE_YAML="${SCRIPTPATH}/${SKIPFILE_TMP}"
70 else
71 # Skipfile is normal skipfile
72 SKIPFILE_TMP="http-skipfile"
73 SKIPFILE="${SCRIPTPATH}/${SKIPFILE_TMP}"
74 fi
75 # Download LTP skipfile from specified URL
76 if ! wget "${OPTARG}" -O "${SKIPFILE_TMP}"; then
77 error_msg "Failed to fetch ${OPTARG}"
78 exit 1
79 fi
80 elif [ "${OPTARG##*.}" = "yaml" ]; then
81 # yaml skipfile; use skipgen to generate a skipfile
82 SKIPFILE_YAML="${SCRIPTPATH}/${OPTARG}"
Naresh Kamboju687ede42017-05-30 15:02:23 +053083 else
Dan Ruea12a8382018-03-05 12:03:15 -060084 # Regular LTP skipfile
85 SKIPFILE="${SCRIPTPATH}/${OPTARG}"
Naresh Kamboju687ede42017-05-30 15:02:23 +053086 fi
87 ;;
Dan Ruea12a8382018-03-05 12:03:15 -060088
89 b)
90 export BOARD="${OPTARG}"
91 ;;
92 g)
93 export BRANCH="${OPTARG}"
94 ;;
95 e)
96 export ENVIRONMENT="${OPTARG}"
97 ;;
Naresh Kambojuc6462732017-01-11 16:26:30 +053098 h|*) usage ;;
99 esac
100done
101
Kees Cook888eda42020-09-14 22:52:00 -0700102# If no explicit URL given, use the default URL for the kselftest tarball.
103if [ -z "${TESTPROG_URL}" ]; then
104 TESTPROG_URL=http://testdata.validation.linaro.org/tests/kselftest/"${TESTPROG}"
105fi
106
Dan Ruea12a8382018-03-05 12:03:15 -0600107if [ -n "${SKIPFILE_YAML}" ]; then
108 export SKIPFILE_PATH="${SCRIPTPATH}/generated_skipfile"
109 generate_skipfile
110 if [ ! -f "${SKIPFILE_PATH}" ]; then
111 error_msg "Skipfile ${SKIPFILE} does not exist";
112 exit 1
113 fi
114 SKIPFILE="${SKIPFILE_PATH}"
115fi
116
117
Naresh Kambojuc6462732017-01-11 16:26:30 +0530118parse_output() {
Kees Cook8bd338b2020-09-15 00:49:06 -0700119 perl -ne '
120 if (m|^# selftests: (.*)$|) {
121 $testdir = $1;
122 $testdir =~ s|[:/]\s*|.|g;
123 } elsif (m|^(?:# )*(not )?ok (?:\d+) ([^#]+)(# (SKIP)?)?|) {
124 $not = $1;
125 $test = $2;
126 $skip = $4;
127 $test =~ s|\s+$||;
128 # If the test name starts with "selftests: " it is "fully qualified".
129 if ($test =~ /selftests: (.*)/) {
130 $test = $1;
131 $test =~ s|[:/]\s*|.|g;
132 } else {
133 # Otherwise, it likely needs the testdir prepended.
134 $test = "$testdir.$test";
135 }
136 # Any appearance of the SKIP is a skip.
137 if ($skip eq "SKIP") {
138 $result="skip";
139 } elsif ($not eq "not ") {
140 $result="fail";
141 } else {
142 $result="pass";
143 }
144 print "$test $result\n";
145 }
Kees Cookeb1a4672020-09-15 01:28:05 -0700146' "${LOGFILE}" >> "${RESULT_FILE}"
Naresh Kambojuc6462732017-01-11 16:26:30 +0530147}
148
149install() {
150 dist_name
151 # shellcheck disable=SC2154
152 case "${dist}" in
Kees Cook528efb02020-09-14 23:55:48 -0700153 debian|ubuntu) install_deps "sed perl wget xz-utils iproute2" "${SKIP_INSTALL}" ;;
154 centos|fedora) install_deps "sed perl wget xz iproute" "${SKIP_INSTALL}" ;;
Naresh Kambojuc6462732017-01-11 16:26:30 +0530155 unknown) warn_msg "Unsupported distro: package install skipped" ;;
156 esac
157}
158
159! check_root && error_msg "You need to be root to run this script."
160create_out_dir "${OUTPUT}"
161# shellcheck disable=SC2164
162cd "${OUTPUT}"
163
164install
165
166if [ -d "${KSELFTEST_PATH}" ]; then
167 echo "kselftests found on rootfs"
168 # shellcheck disable=SC2164
169 cd "${KSELFTEST_PATH}"
170else
Kees Cook888eda42020-09-14 22:52:00 -0700171 # Fetch whatever we have been aimed at, assuming only that it can
172 # be handled by "tar". Do not assume anything about the compression.
173 wget "${TESTPROG_URL}"
174 tar -xaf "$(basename "${TESTPROG_URL}")"
Naresh Kambojuc6462732017-01-11 16:26:30 +0530175 # shellcheck disable=SC2164
Kevin Hilman96efe162020-07-14 11:59:30 -0700176 if [ ! -e "run_kselftest.sh" ]; then cd "kselftest"; fi
Naresh Kambojuc6462732017-01-11 16:26:30 +0530177fi
178
Kees Cook528efb02020-09-14 23:55:48 -0700179skips=$(mktemp -p . -t skip-XXXXXX)
180
Naresh Kamboju687ede42017-05-30 15:02:23 +0530181if [ -n "${SKIPLIST}" ]; then
182 # shellcheck disable=SC2086
Kees Cook528efb02020-09-14 23:55:48 -0700183 for skip_regex in ${SKIPLIST}; do
184 echo "${skip_regex}" >> "$skips"
Naresh Kamboju687ede42017-05-30 15:02:23 +0530185 done
186fi
187
188# Ignore SKIPFILE when SKIPLIST provided
189if [ -f "${SKIPFILE}" ] && [ -z "${SKIPLIST}" ]; then
Kees Cook528efb02020-09-14 23:55:48 -0700190 while read -r skip_regex; do
191 case "${skip_regex}" in \#*) continue ;; esac
192 echo "${skip_regex}" >> "$skips"
Naresh Kamboju687ede42017-05-30 15:02:23 +0530193 done < "${SKIPFILE}"
194fi
195
Anders Roxell5c160f72019-05-14 13:38:36 +0200196echo "========================================"
197echo "skiplist:"
Kees Cook528efb02020-09-14 23:55:48 -0700198while read -r skip_regex; do
199 echo "${skip_regex}"
Kees Cookeb1a4672020-09-15 01:28:05 -0700200 perl -pi -e '
Kees Cook528efb02020-09-14 23:55:48 -0700201# Discover top-level test directory from: cd TESTDIR
202$testdir=$1 if m|^cd ([^\$]+)\b|;
203# Process each test from: \t"TESTNAME"
204if (m|^\t"([^"]+)"|) {
Kees Cookeb1a4672020-09-15 01:28:05 -0700205 $test = $1;
Kees Cook528efb02020-09-14 23:55:48 -0700206 # If the test_regex matches TESTDIR/TESTNAME,
207 # remove it from the run script.
Kees Cookeb1a4672020-09-15 01:28:05 -0700208 $name = "$testdir/$test";
209 if ("$name" =~ m|^'"${skip_regex}"'$|) {
Kees Cook528efb02020-09-14 23:55:48 -0700210 s|^\t"[^"]+"|\t|;
Kees Cookeb1a4672020-09-15 01:28:05 -0700211 $name =~ s|/|.|g;
212 # Record each skipped test as having been skipped.
213 open(my $fd, ">>'"${RESULT_FILE}"'");
214 print $fd "$name skip\n";
215 close($fd);
Kees Cook528efb02020-09-14 23:55:48 -0700216 }
217}' run_kselftest.sh
218done < "${skips}"
Anders Roxell5c160f72019-05-14 13:38:36 +0200219echo "========================================"
Kees Cook528efb02020-09-14 23:55:48 -0700220rm -f "${skips}"
221
Naresh Kambojuc6462732017-01-11 16:26:30 +0530222# run_kselftest.sh file generated by kselftest Makefile and included in tarball
Kees Cook8bd338b2020-09-15 00:49:06 -0700223./run_kselftest.sh 2>&1 | tee "${LOGFILE}"
Naresh Kambojuc6462732017-01-11 16:26:30 +0530224parse_output