blob: 9d64c5e921c50cb7a82504249fa883e0076d583e [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 Kamboju7b9fd952020-12-03 17:26:53 +053022TST_CMDFILES=""
23TST_CASENAME=""
Naresh Kamboju687ede42017-05-30 15:02:23 +053024
Kees Cook888eda42020-09-14 22:52:00 -070025# Architecture-specific tarball name defaults.
26if [ "$(uname -m)" = "aarch64" ]; then
Riku Voipio85cdee42017-05-15 16:01:06 +030027 TESTPROG="kselftest_aarch64.tar.gz"
Kees Cook888eda42020-09-14 22:52:00 -070028else
29 TESTPROG="kselftest_armhf.tar.gz"
Riku Voipio85cdee42017-05-15 16:01:06 +030030fi
Naresh Kambojuc6462732017-01-11 16:26:30 +053031
32usage() {
Naresh Kamboju7b9fd952020-12-03 17:26:53 +053033 echo "Usage: $0 [-c bpf cpufreq net timers]
34 [-T cpu-hotplug:cpu-on-off-test.sh]
35 [-t kselftest_aarch64.tar.gz | kselftest_armhf.tar.gz]
Naresh Kamboju687ede42017-05-30 15:02:23 +053036 [-s True|False]
Naresh Kamboju92375fe2017-06-27 17:16:21 +053037 [-u url]
Milosz Wasilewski597e24c2017-09-04 14:06:36 +010038 [-p path]
Naresh Kamboju687ede42017-05-30 15:02:23 +053039 [-L List of skip test cases]
Dan Ruea12a8382018-03-05 12:03:15 -060040 [-S kselftest-skipfile]
41 [-b board]
42 [-g branch]
43 [-e environment]" 1>&2
Naresh Kambojuc6462732017-01-11 16:26:30 +053044 exit 1
45}
46
Naresh Kamboju7b9fd952020-12-03 17:26:53 +053047while getopts "c:T:t:s:u:p:L:S:b:g:e:h" opt; do
Naresh Kambojuc6462732017-01-11 16:26:30 +053048 case "${opt}" in
Naresh Kamboju7b9fd952020-12-03 17:26:53 +053049 c) TST_CMDFILES="${OPTARG}" ;;
50 T) TST_CASENAME="${OPTARG}" ;;
Naresh Kambojuc6462732017-01-11 16:26:30 +053051 t) TESTPROG="${OPTARG}" ;;
52 s) SKIP_INSTALL="${OPTARG}" ;;
Naresh Kamboju92375fe2017-06-27 17:16:21 +053053 # Download kselftest tarball from given URL
54 u) TESTPROG_URL="${OPTARG}" ;;
Naresh Kamboju687ede42017-05-30 15:02:23 +053055 # List of known unsupported test cases to be skipped
56 L) SKIPLIST="${OPTARG}" ;;
Milosz Wasilewski597e24c2017-09-04 14:06:36 +010057 p) KSELFTEST_PATH="${OPTARG}" ;;
Naresh Kamboju687ede42017-05-30 15:02:23 +053058 S)
Dan Ruea12a8382018-03-05 12:03:15 -060059
60 #OPT=$(echo "${OPTARG}" | grep "http")
61 #if [ -z "${OPT}" ] ; then
62 ## kselftest skipfile
63 # SKIPFILE="${SCRIPTPATH}/${OPTARG}"
64 #else
65 ## Download kselftest skipfile from speficied URL
66 # wget "${OPTARG}" -O "skipfile"
67 # SKIPFILE="skipfile"
68 # SKIPFILE="${SCRIPTPATH}/${SKIPFILE}"
69 #fi
70
71 if [ -z "${OPTARG##*http*}" ]; then
72 if [ -z "${OPTARG##*yaml*}" ]; then
73 # Skipfile is of type yaml
74 SKIPFILE_TMP="http-skipfile.yaml"
75 SKIPFILE_YAML="${SCRIPTPATH}/${SKIPFILE_TMP}"
76 else
77 # Skipfile is normal skipfile
78 SKIPFILE_TMP="http-skipfile"
79 SKIPFILE="${SCRIPTPATH}/${SKIPFILE_TMP}"
80 fi
81 # Download LTP skipfile from specified URL
82 if ! wget "${OPTARG}" -O "${SKIPFILE_TMP}"; then
83 error_msg "Failed to fetch ${OPTARG}"
84 exit 1
85 fi
86 elif [ "${OPTARG##*.}" = "yaml" ]; then
87 # yaml skipfile; use skipgen to generate a skipfile
88 SKIPFILE_YAML="${SCRIPTPATH}/${OPTARG}"
Naresh Kamboju687ede42017-05-30 15:02:23 +053089 else
Dan Ruea12a8382018-03-05 12:03:15 -060090 # Regular LTP skipfile
91 SKIPFILE="${SCRIPTPATH}/${OPTARG}"
Naresh Kamboju687ede42017-05-30 15:02:23 +053092 fi
93 ;;
Dan Ruea12a8382018-03-05 12:03:15 -060094
95 b)
96 export BOARD="${OPTARG}"
97 ;;
98 g)
99 export BRANCH="${OPTARG}"
100 ;;
101 e)
102 export ENVIRONMENT="${OPTARG}"
103 ;;
Naresh Kambojuc6462732017-01-11 16:26:30 +0530104 h|*) usage ;;
105 esac
106done
107
Kees Cook888eda42020-09-14 22:52:00 -0700108# If no explicit URL given, use the default URL for the kselftest tarball.
109if [ -z "${TESTPROG_URL}" ]; then
110 TESTPROG_URL=http://testdata.validation.linaro.org/tests/kselftest/"${TESTPROG}"
111fi
112
Dan Ruea12a8382018-03-05 12:03:15 -0600113if [ -n "${SKIPFILE_YAML}" ]; then
114 export SKIPFILE_PATH="${SCRIPTPATH}/generated_skipfile"
115 generate_skipfile
116 if [ ! -f "${SKIPFILE_PATH}" ]; then
117 error_msg "Skipfile ${SKIPFILE} does not exist";
118 exit 1
119 fi
120 SKIPFILE="${SKIPFILE_PATH}"
121fi
122
123
Naresh Kambojuc6462732017-01-11 16:26:30 +0530124parse_output() {
Kees Cook8bd338b2020-09-15 00:49:06 -0700125 perl -ne '
126 if (m|^# selftests: (.*)$|) {
127 $testdir = $1;
128 $testdir =~ s|[:/]\s*|.|g;
129 } elsif (m|^(?:# )*(not )?ok (?:\d+) ([^#]+)(# (SKIP)?)?|) {
130 $not = $1;
131 $test = $2;
132 $skip = $4;
133 $test =~ s|\s+$||;
134 # If the test name starts with "selftests: " it is "fully qualified".
135 if ($test =~ /selftests: (.*)/) {
136 $test = $1;
137 $test =~ s|[:/]\s*|.|g;
138 } else {
139 # Otherwise, it likely needs the testdir prepended.
140 $test = "$testdir.$test";
141 }
142 # Any appearance of the SKIP is a skip.
143 if ($skip eq "SKIP") {
144 $result="skip";
145 } elsif ($not eq "not ") {
146 $result="fail";
147 } else {
148 $result="pass";
149 }
150 print "$test $result\n";
151 }
Kees Cookeb1a4672020-09-15 01:28:05 -0700152' "${LOGFILE}" >> "${RESULT_FILE}"
Naresh Kambojuc6462732017-01-11 16:26:30 +0530153}
154
155install() {
156 dist_name
157 # shellcheck disable=SC2154
158 case "${dist}" in
Kees Cook528efb02020-09-14 23:55:48 -0700159 debian|ubuntu) install_deps "sed perl wget xz-utils iproute2" "${SKIP_INSTALL}" ;;
160 centos|fedora) install_deps "sed perl wget xz iproute" "${SKIP_INSTALL}" ;;
Naresh Kambojuc6462732017-01-11 16:26:30 +0530161 unknown) warn_msg "Unsupported distro: package install skipped" ;;
162 esac
163}
164
165! check_root && error_msg "You need to be root to run this script."
166create_out_dir "${OUTPUT}"
167# shellcheck disable=SC2164
168cd "${OUTPUT}"
169
170install
171
172if [ -d "${KSELFTEST_PATH}" ]; then
173 echo "kselftests found on rootfs"
174 # shellcheck disable=SC2164
175 cd "${KSELFTEST_PATH}"
176else
Kees Cook888eda42020-09-14 22:52:00 -0700177 # Fetch whatever we have been aimed at, assuming only that it can
178 # be handled by "tar". Do not assume anything about the compression.
179 wget "${TESTPROG_URL}"
180 tar -xaf "$(basename "${TESTPROG_URL}")"
Naresh Kambojuc6462732017-01-11 16:26:30 +0530181 # shellcheck disable=SC2164
Kevin Hilman96efe162020-07-14 11:59:30 -0700182 if [ ! -e "run_kselftest.sh" ]; then cd "kselftest"; fi
Naresh Kambojuc6462732017-01-11 16:26:30 +0530183fi
184
Kees Cook528efb02020-09-14 23:55:48 -0700185skips=$(mktemp -p . -t skip-XXXXXX)
186
Naresh Kamboju687ede42017-05-30 15:02:23 +0530187if [ -n "${SKIPLIST}" ]; then
188 # shellcheck disable=SC2086
Kees Cook528efb02020-09-14 23:55:48 -0700189 for skip_regex in ${SKIPLIST}; do
190 echo "${skip_regex}" >> "$skips"
Naresh Kamboju687ede42017-05-30 15:02:23 +0530191 done
192fi
193
194# Ignore SKIPFILE when SKIPLIST provided
195if [ -f "${SKIPFILE}" ] && [ -z "${SKIPLIST}" ]; then
Kees Cook528efb02020-09-14 23:55:48 -0700196 while read -r skip_regex; do
197 case "${skip_regex}" in \#*) continue ;; esac
198 echo "${skip_regex}" >> "$skips"
Naresh Kamboju687ede42017-05-30 15:02:23 +0530199 done < "${SKIPFILE}"
200fi
201
Kees Cook3aa67522020-10-15 14:55:43 -0700202cp kselftest-list.txt kselftest-list.txt.orig
Anders Roxell5c160f72019-05-14 13:38:36 +0200203echo "skiplist:"
Kees Cook3aa67522020-10-15 14:55:43 -0700204echo "========================================"
Kees Cook528efb02020-09-14 23:55:48 -0700205while read -r skip_regex; do
Kees Cook3aa67522020-10-15 14:55:43 -0700206 echo "$skip_regex"
207 # Remove matching tests from list of tests to run and report it as skipped
208 perl -i -ne 'if (s|^('"${skip_regex}"')$|\1 skip|) { print STDERR; } else { print; }' kselftest-list.txt 2>>"${RESULT_FILE}"
Kees Cook528efb02020-09-14 23:55:48 -0700209done < "${skips}"
Anders Roxell5c160f72019-05-14 13:38:36 +0200210echo "========================================"
Kees Cook528efb02020-09-14 23:55:48 -0700211rm -f "${skips}"
212
Naresh Kamboju7b9fd952020-12-03 17:26:53 +0530213if [ -n "${TST_CASENAME}" ]; then
214 ./run_kselftest.sh -t "${TST_CASENAME}" 2>&1 | tee -a "${LOGFILE}"
215elif [ -n "${TST_CMDFILES}" ]; then
216 # shellcheck disable=SC2086
217 for test in ${TST_CMDFILES}; do
218 ./run_kselftest.sh -c ${test} 2>&1 | tee -a "${LOGFILE}"
219 done
220else
221 ./run_kselftest.sh 2>&1 | tee "${LOGFILE}"
222fi
Naresh Kambojuc6462732017-01-11 16:26:30 +0530223parse_output