blob: c737a57384fc07a206910bf02965fd0965ac3839 [file] [log] [blame]
Chase Qi09edc7f2016-08-18 13:18:50 +08001#!/bin/sh
2
3LANG=C
4export LANG
5
Vishal Bhoj07294292017-12-05 17:46:23 +05306error_fatal() {
Karsten Tausche053c43d2018-04-24 12:10:39 +02007 local msg="$1"
Vishal Bhoj07294292017-12-05 17:46:23 +05308 [ -z "${msg}" ] && msg="Unknown error"
9 if which lava-test-raise;then
Vishal Bhoj8f433f92017-12-12 14:14:57 +053010 lava-test-raise "${msg}"
Vishal Bhoj07294292017-12-05 17:46:23 +053011 else
12 printf "FATAL ERROR: %s\n" "${msg}" >&2
13 fi
14 exit 1
15}
16
Chase Qi09edc7f2016-08-18 13:18:50 +080017error_msg() {
Karsten Tausche053c43d2018-04-24 12:10:39 +020018 local msg="$1"
Chase Qi3d1bc842016-08-26 14:47:16 +080019 [ -z "${msg}" ] && msg="Unknown error"
Chase Qi09edc7f2016-08-18 13:18:50 +080020 printf "ERROR: %s\n" "${msg}" >&2
Chase Qic25683a2017-01-12 20:46:29 +080021 exit 1
Chase Qi09edc7f2016-08-18 13:18:50 +080022}
23
24warn_msg() {
Karsten Tausche053c43d2018-04-24 12:10:39 +020025 local msg="$1"
Chase Qi3d1bc842016-08-26 14:47:16 +080026 [ -z "${msg}" ] && msg="Unknown error"
Chase Qi09edc7f2016-08-18 13:18:50 +080027 printf "WARNING: %s\n" "${msg}" >&2
28}
29
30info_msg() {
Karsten Tausche053c43d2018-04-24 12:10:39 +020031 local msg="$1"
Chase Qi3d1bc842016-08-26 14:47:16 +080032 [ -z "${msg}" ] && msg="Unknown info"
Chase Qi09edc7f2016-08-18 13:18:50 +080033 printf "INFO: %s\n" "${msg}" >&1
34}
35
Chase Qi09edc7f2016-08-18 13:18:50 +080036check_root() {
37 if [ "$(id -ru)" -eq 0 ]; then
38 return 0
39 else
40 return 1
41 fi
42}
43
Chase Qi3d1bc842016-08-26 14:47:16 +080044exit_on_fail() {
Karsten Tausche053c43d2018-04-24 12:10:39 +020045 local exit_code="$?"
Naresh Kambojufeb3a242016-11-18 13:02:34 +053046 [ "$#" -lt 1 ] && error_msg "Usage: exit_on_fail test_case [skip_list]"
Karsten Tausche053c43d2018-04-24 12:10:39 +020047 local test_case="$1"
48 local skip_list="$2"
Chase Qi09edc7f2016-08-18 13:18:50 +080049
Chase Qi3d1bc842016-08-26 14:47:16 +080050 if [ "${exit_code}" -ne 0 ]; then
Naresh Kambojufeb3a242016-11-18 13:02:34 +053051 echo "${test_case} fail" | tee -a "${RESULT_FILE}"
Chase Qi92bff7d2016-09-18 18:44:31 +080052
53 # skip_list is a list of tests sepereated by space. This might be
54 # useful when exiting on prerequisite not met.
55 if [ -n "${skip_list}" ]; then
56 for i in ${skip_list}; do
57 echo "$i skip" | tee -a "${RESULT_FILE}"
58 done
59 fi
60
Chase Qibcc91622016-09-08 16:44:25 +080061 # Exit normally to continue to run the following steps defined in test
62 # definition file.
63 exit 0
64 else
Naresh Kambojufeb3a242016-11-18 13:02:34 +053065 echo "${test_case} pass" | tee -a "${RESULT_FILE}"
Chase Qibcc91622016-09-08 16:44:25 +080066 return 0
Chase Qi3d1bc842016-08-26 14:47:16 +080067 fi
68}
69
Naresh Kambojub2a67fa2016-11-17 22:14:50 +053070exit_on_skip() {
Karsten Tausche053c43d2018-04-24 12:10:39 +020071 local exit_code="$?"
Naresh Kambojub2a67fa2016-11-17 22:14:50 +053072 [ "$#" -lt 1 ] && error_msg "Usage: exit_on_skip test_case [msg]"
Karsten Tausche053c43d2018-04-24 12:10:39 +020073 local test_case="$1"
74 local msg="$2"
Naresh Kambojub2a67fa2016-11-17 22:14:50 +053075
76 if [ "${exit_code}" -ne 0 ]; then
77 echo "${test_case} skip" | tee -a "${RESULT_FILE}"
78
79 if [ -n "${msg}" ]; then
Chase Qi7aa508c2017-01-20 16:45:54 +080080 warn_msg "${msg}"
Naresh Kambojub2a67fa2016-11-17 22:14:50 +053081 fi
82
83 # Exit normally to continue to run the following steps defined in test
84 # definition file.
85 exit 0
86 else
87 echo "${test_case} pass" | tee -a "${RESULT_FILE}"
88 return 0
89 fi
90}
91
Chase Qi3d1bc842016-08-26 14:47:16 +080092check_return() {
Karsten Tausche053c43d2018-04-24 12:10:39 +020093 local exit_code="$?"
Naresh Kambojufeb3a242016-11-18 13:02:34 +053094 [ "$#" -ne 1 ] && error_msg "Usage: check_return test_case"
Karsten Tausche053c43d2018-04-24 12:10:39 +020095 local test_case="$1"
Chase Qi09edc7f2016-08-18 13:18:50 +080096
97 if [ "${exit_code}" -ne 0 ]; then
Naresh Kambojufeb3a242016-11-18 13:02:34 +053098 echo "${test_case} fail" | tee -a "${RESULT_FILE}"
Chase Qi09edc7f2016-08-18 13:18:50 +080099 return "${exit_code}"
100 else
Naresh Kambojufeb3a242016-11-18 13:02:34 +0530101 echo "${test_case} pass" | tee -a "${RESULT_FILE}"
Chase Qi09edc7f2016-08-18 13:18:50 +0800102 return 0
103 fi
104}
105
Chase Qic2e2f692016-10-27 10:27:43 +0800106# When shell argument "-e" set in test script, check_return and exit_on_fail
107# would NOT work. run_test_case should be used instead.
108run_test_case() {
109 [ "$#" -lt 2 ] && error_msg "Usage: run_test_case <test_command> <test_case_id> [skip_list]"
Karsten Tausche053c43d2018-04-24 12:10:39 +0200110 local test_command="$1"
111 local test_case_id="$2"
112 local skip_list="$3"
Chase Qic2e2f692016-10-27 10:27:43 +0800113
114 if eval "${test_command}"; then
115 echo "${test_case_id} pass" | tee -a "${RESULT_FILE}"
116 else
117 echo "${test_case_id} fail" | tee -a "${RESULT_FILE}"
118 # When skip_list isn't empty, skip the tests and exit.
119 if [ -n "${skip_list}" ]; then
120 for i in ${skip_list}; do
121 echo "$i skip" | tee -a "${RESULT_FILE}"
122 done
123 exit 0
124 fi
125 fi
126
127 return 0
128}
129
Naresh Kamboju7f319ba2016-09-12 15:11:12 +0530130report_pass() {
Naresh Kambojufeb3a242016-11-18 13:02:34 +0530131 [ "$#" -ne 1 ] && error_msg "Usage: report_pass test_case"
Karsten Tausche053c43d2018-04-24 12:10:39 +0200132 local test_case="$1"
Naresh Kambojufeb3a242016-11-18 13:02:34 +0530133 echo "${test_case} pass" | tee -a "${RESULT_FILE}"
Naresh Kamboju7f319ba2016-09-12 15:11:12 +0530134}
135
136report_fail() {
Naresh Kambojufeb3a242016-11-18 13:02:34 +0530137 [ "$#" -ne 1 ] && error_msg "Usage: report_fail test_case"
Karsten Tausche053c43d2018-04-24 12:10:39 +0200138 local test_case="$1"
Naresh Kambojufeb3a242016-11-18 13:02:34 +0530139 echo "${test_case} fail" | tee -a "${RESULT_FILE}"
Naresh Kamboju7f319ba2016-09-12 15:11:12 +0530140}
141
Chase Qi09edc7f2016-08-18 13:18:50 +0800142add_metric() {
Nicolas Dechesne3d25d432017-01-19 15:47:09 +0100143 if [ "$#" -lt 3 ]; then
144 warn_msg "The number of parameters less then 3"
145 error_msg "Usage: add_metric test_case result measurement [units]"
Chase Qi3d1bc842016-08-26 14:47:16 +0800146 fi
Karsten Tausche053c43d2018-04-24 12:10:39 +0200147 local test_case="$1"
148 local result="$2"
149 local measurement="$3"
150 local units="$4"
Chase Qi09edc7f2016-08-18 13:18:50 +0800151
Naresh Kambojufeb3a242016-11-18 13:02:34 +0530152 echo "${test_case} ${result} ${measurement} ${units}" | tee -a "${RESULT_FILE}"
Chase Qi09edc7f2016-08-18 13:18:50 +0800153}
154
Chase Qi38ea4d32016-10-27 18:16:10 +0800155detect_abi() {
156 abi=$(uname -m)
157 case "${abi}" in
158 armv7|armv7l|armv7el|armv7lh) abi="armeabi" ;;
159 arm64|armv8|arm64-v8a|aarch64) abi="arm64" ;;
Dan Rue552c9f52018-01-04 11:59:02 -0600160 x86_64) abi="x86_64" ;;
Chase Qifa9f33e2017-09-14 10:36:49 +0800161 *) error_msg "Unsupported architecture: ${abi}" ;;
Chase Qi38ea4d32016-10-27 18:16:10 +0800162 esac
163}
164
Chase Qi09edc7f2016-08-18 13:18:50 +0800165dist_name() {
Nicolas Dechesne14c77d92017-01-24 22:02:15 +0100166 if [ -f /etc/os-release ]; then
167 # shellcheck disable=SC1091
168 dist=$(. /etc/os-release && echo "${ID}")
169 elif [ -x /usr/bin/lsb_release ]; then
Chase Qi09edc7f2016-08-18 13:18:50 +0800170 dist="$(lsb_release -si)"
171 elif [ -f /etc/lsb-release ]; then
Nicolas Dechesneec8e5a82017-01-25 09:18:16 +0100172 # shellcheck disable=SC1091
173 dist="$(. /etc/lsb-release && echo "${DISTRIB_ID}")"
Chase Qi09edc7f2016-08-18 13:18:50 +0800174 elif [ -f /etc/debian_version ]; then
Nicolas Dechesneb7e38762017-01-25 12:07:08 +0100175 dist="debian"
Chase Qi09edc7f2016-08-18 13:18:50 +0800176 elif [ -f /etc/fedora-release ]; then
Nicolas Dechesneb7e38762017-01-25 12:07:08 +0100177 dist="fedora"
Chase Qi09edc7f2016-08-18 13:18:50 +0800178 elif [ -f /etc/centos-release ]; then
Nicolas Dechesneb7e38762017-01-25 12:07:08 +0100179 dist="centos"
Chase Qi09edc7f2016-08-18 13:18:50 +0800180 else
Nicolas Dechesneb7e38762017-01-25 12:07:08 +0100181 dist="unknown"
Chase Qi09edc7f2016-08-18 13:18:50 +0800182 warn_msg "Unsupported distro: cannot determine distribution name"
183 fi
Nicolas Dechesne14c77d92017-01-24 22:02:15 +0100184
Fathi Boudra7a178e12017-01-25 15:55:04 +0200185 # convert dist to lower case
186 dist=$(echo ${dist} | tr '[:upper:]' '[:lower:]')
Nicolas Dechesne14c77d92017-01-24 22:02:15 +0100187 case "${dist}" in
Fathi Boudrac917abd2017-01-25 15:56:04 +0200188 rpb*) dist="oe-rpb" ;;
Nicolas Dechesne14c77d92017-01-24 22:02:15 +0100189 esac
Chase Qi09edc7f2016-08-18 13:18:50 +0800190}
191
192install_deps() {
Karsten Tausche053c43d2018-04-24 12:10:39 +0200193 local pkgs="$1"
Chase Qi3d1bc842016-08-26 14:47:16 +0800194 [ -z "${pkgs}" ] && error_msg "Usage: install_deps pkgs"
195 # skip_install parmater is optional.
Karsten Tausche053c43d2018-04-24 12:10:39 +0200196 local skip_install="$2"
Chase Qi09edc7f2016-08-18 13:18:50 +0800197
198 if [ "${skip_install}" = "True" ] || [ "${skip_install}" = "true" ]; then
199 info_msg "install_deps skipped"
200 else
Nicolas Dechesne7a695312017-01-25 14:15:43 +0100201 ! check_root && \
202 error_msg "About to install packages, please run this script as root."
Chase Qi09edc7f2016-08-18 13:18:50 +0800203 info_msg "Installing ${pkgs}"
204 dist_name
205 case "${dist}" in
Nicolas Dechesneb7e38762017-01-25 12:07:08 +0100206 debian|ubuntu)
Dan Rue61c3a642017-12-01 10:40:07 -0600207 last_apt_time=/tmp/apt-get-updated.last
208 apt_cache_time=21600 # 6 hours
209 # Only run apt-get update if it hasn't been run in $apt_cache_time seconds
210 if [ ! -e ${last_apt_time} ] || \
211 [ "$(stat --format=%Y ${last_apt_time})" -lt $(( $(date +%s) - apt_cache_time )) ]; then
212 DEBIAN_FRONTEND=noninteractive apt-get update -q -y && touch ${last_apt_time}
213 fi
Chase Qi191ede52016-10-24 09:26:27 +0800214 # shellcheck disable=SC2086
Chase Qibcc91622016-09-08 16:44:25 +0800215 DEBIAN_FRONTEND=noninteractive apt-get install -q -y ${pkgs}
Chase Qi09edc7f2016-08-18 13:18:50 +0800216 ;;
Nicolas Dechesneb7e38762017-01-25 12:07:08 +0100217 centos)
Chase Qi191ede52016-10-24 09:26:27 +0800218 # shellcheck disable=SC2086
Chase Qi09edc7f2016-08-18 13:18:50 +0800219 yum -e 0 -y install ${pkgs}
220 ;;
Nicolas Dechesneb7e38762017-01-25 12:07:08 +0100221 fedora)
Chase Qi191ede52016-10-24 09:26:27 +0800222 # shellcheck disable=SC2086
Chase Qi09edc7f2016-08-18 13:18:50 +0800223 dnf -e 0 -y install ${pkgs}
224 ;;
Chase Qi475120c2017-02-06 12:06:01 +0800225 *)
226 warn_msg "Unsupported distro: ${dist}! Package installation skipped."
Chase Qi09edc7f2016-08-18 13:18:50 +0800227 ;;
228 esac
Chase Qiaed8b752017-05-03 21:01:54 +0800229 # shellcheck disable=SC2181
230 if [ $? -ne 0 ]; then
231 error_msg "Failed to install dependencies, exiting..."
232 fi
Chase Qi09edc7f2016-08-18 13:18:50 +0800233 fi
234}
Naresh Kambojuf88f4d72016-08-17 17:11:30 +0530235
Chase Qi50a5baa2016-10-20 10:17:05 +0800236# Return the exit code of the first command when using pipe.
237pipe0_status() {
238 [ "$#" -ne 2 ] && error_msg "Usage: pipe0_status cmd1 cmd2"
Karsten Tausche053c43d2018-04-24 12:10:39 +0200239 local cmd1="$1"
240 local cmd2="$2"
Chase Qi50a5baa2016-10-20 10:17:05 +0800241
242 exec 4>&1
Karsten Tausche053c43d2018-04-24 12:10:39 +0200243 local ret_val=$({ { eval "${cmd1}" 3>&-; echo "$?" 1>&3; } 4>&- \
Chase Qi50a5baa2016-10-20 10:17:05 +0800244 | eval "${cmd2}" 1>&4; } 3>&1)
245 exec 4>&-
246
247 return "${ret_val}"
248}
249
Naresh Kambojuf88f4d72016-08-17 17:11:30 +0530250validate_check_sum() {
251 if [ "$#" -ne 2 ]; then
252 warn_msg "The number of parameters should be 2"
253 error_msg "Usage: validate_check_sum filename known_sha256sum"
254 return 1
255 fi
Karsten Tausche053c43d2018-04-24 12:10:39 +0200256 local OUTPUT_FILE_NAME="$1"
257 local SHA256SUM_CHECK="$2"
Naresh Kambojuf88f4d72016-08-17 17:11:30 +0530258 # Get sha256sum of output_file
Karsten Tausche053c43d2018-04-24 12:10:39 +0200259 local GET_SHA256SUM=$(sha256sum "${OUTPUT_FILE_NAME}" | awk '{print $1}')
Chase Qiae7807f2016-10-21 08:43:25 +0800260 echo "GET_SHA256SUM is ${GET_SHA256SUM}"
Naresh Kambojuf88f4d72016-08-17 17:11:30 +0530261 if [ "${SHA256SUM_CHECK}" = "${GET_SHA256SUM}" ] ; then
262 return 0
263 else
264 echo "checksum did not match"
265 return 1
266 fi
267}
Chase Qie4cf6d42016-10-25 11:39:59 +0800268
269convert_to_mb() {
270 [ "$#" -ne 2 ] && error_msg "Usage: convert_to_mb value units"
Dan Ruef4970552018-01-26 17:28:35 -0600271 if ! echo "$1" | grep -E -q "^[0-9.]+$"; then
Chase Qie4cf6d42016-10-25 11:39:59 +0800272 error_msg "The first argument isn't a number"
273 fi
Karsten Tausche053c43d2018-04-24 12:10:39 +0200274 local value="$1"
275 local units="$2"
Chase Qie4cf6d42016-10-25 11:39:59 +0800276
277 case "${units}" in
278 KB|kb) value=$(echo "${value}" | awk '{print $1/1024}') ;;
279 MB|mb) ;;
280 GB|gb) value=$(echo "${value}" | awk '{print $1*1024}') ;;
281 TB|tb) value=$(echo "${value}" | awk '{print $1*1024*1024}') ;;
282 *) error_msg "Unsupported units" ;;
283 esac
284
285 echo "${value}"
286}
Chase Qie7eda1e2016-10-25 16:38:30 +0800287
288dist_info() {
289 if ! command -v lsb_release > /dev/null; then
290 dist_name
291 case "${dist}" in
Nicolas Dechesneb7e38762017-01-25 12:07:08 +0100292 debian|ubuntu) install_deps "lsb-release" ;;
293 centos|fedora) install_deps "redhat-lsb-core" ;;
Chase Qi7aa508c2017-01-20 16:45:54 +0800294 *) warn_msg "Unsupported distro: dist_info skipped"
Chase Qie7eda1e2016-10-25 16:38:30 +0800295 esac
296 fi
297
298 # shellcheck disable=SC2034
299 Release=$(lsb_release -r | awk '{print $2}')
300 Codename=$(lsb_release -c | awk '{print $2}')
301}
302
303add_key() {
304 [ "$#" -ne 1 ] && error_msg "Usage: add_key url"
Karsten Tausche053c43d2018-04-24 12:10:39 +0200305 local url="$1"
Chase Qie7eda1e2016-10-25 16:38:30 +0800306
Nicolas Dechesne7a695312017-01-25 14:15:43 +0100307 ! check_root && \
308 error_msg "About to use apt-key, please run this script as root."
Chase Qie7eda1e2016-10-25 16:38:30 +0800309 dist_name
310 case "${dist}" in
Nicolas Dechesneb7e38762017-01-25 12:07:08 +0100311 debian|ubuntu) wget -O - "${url}" | apt-key add - ;;
312 centos|fedora) infor_msg "add_key isn't needed on ${dist}" ;;
Chase Qi7aa508c2017-01-20 16:45:54 +0800313 *) warn_msg "Unsupported distro: add_key skipped"
Chase Qie7eda1e2016-10-25 16:38:30 +0800314 esac
315}
316
317add_repo() {
318 [ "$#" -lt 1 ] && error_msg "Usage: add_repo <url> [backports]"
Karsten Tausche053c43d2018-04-24 12:10:39 +0200319 local url="$1"
Chase Qie7eda1e2016-10-25 16:38:30 +0800320
Nicolas Dechesne7a695312017-01-25 14:15:43 +0100321 ! check_root && \
322 error_msg "About to add a repo, please run this script as root."
Chase Qie7eda1e2016-10-25 16:38:30 +0800323 dist_name
324 case "${dist}" in
325 # Detect Debian/Ubuntu codename and add repo automatically. The same url
326 # should work on all distributions supported by the repo.
Nicolas Dechesneb7e38762017-01-25 12:07:08 +0100327 debian|ubuntu)
Chase Qie7eda1e2016-10-25 16:38:30 +0800328 dist_info
329 if [ -z "$2" ]; then
Karsten Tausche053c43d2018-04-24 12:10:39 +0200330 local backports=""
Chase Qie7eda1e2016-10-25 16:38:30 +0800331 elif [ "$2" = "backports" ]; then
Karsten Tausche053c43d2018-04-24 12:10:39 +0200332 local backports="-backports"
Chase Qie7eda1e2016-10-25 16:38:30 +0800333 else
334 echo "Usage: add_repo <url> [backports]"
335 error_msg "$2 is not a supported argument, should be 'backports'"
336 fi
337 echo "deb ${url} ${Codename}${backports} main" \
338 >> "/etc/apt/sources.list.d/3rd-party-repo.list"
339 ;;
340 # It is not easy to amend url with distro version as its format may vary
341 # by repo. Test definition/plan should provide a correct repo url.
Nicolas Dechesneb7e38762017-01-25 12:07:08 +0100342 centos|fedora)
Chase Qie7eda1e2016-10-25 16:38:30 +0800343 wget -O - "${url}" >> "/etc/yum.repos.d/3rd-party.repo"
344 ;;
345 *)
Chase Qi475120c2017-02-06 12:06:01 +0800346 warn_msg "Unsupported distro: ${dist}! add_repo skipped"
Chase Qie7eda1e2016-10-25 16:38:30 +0800347 ;;
348 esac
349}
Daniel Díaz215b4442017-02-15 18:56:58 -0600350
351create_out_dir() {
352 [ -z "$1" ] && error_msg "Usage: create_out_dir output_dir"
Karsten Tausche053c43d2018-04-24 12:10:39 +0200353 local OUTPUT=$1
Daniel Díaz215b4442017-02-15 18:56:58 -0600354 [ -d "${OUTPUT}" ] &&
355 mv "${OUTPUT}" "${OUTPUT}_$(date -r "${OUTPUT}" +%Y%m%d%H%M%S)"
356 mkdir -p "${OUTPUT}"
357 [ -d "${OUTPUT}" ] || error_msg "Could not create output directory ${OUTPUT}"
358}
Dan Ruef4970552018-01-26 17:28:35 -0600359
360generate_skipfile() {
361 # Generate a skipfile and set the SKIPFILE variable based on SKIPFILE_YAML,
362 # BOARD, BRANCH, and ENVIRONMENT.
363 #
364 # In:
365 # SKIPFILE_YAML: (required) skipgen/yaml formatted skipfile
366 # SKIPFILE_PATH: (required) destination file for generated skipfile
367 # BOARD: (optional) board name to pass to skipgen
368 # BRANCH: (optional) branch name to pass to skipgen
369 # ENVIRONMENT: (optional) environment name to pass to skipgen
370 #
371 info_msg "Generating a skipfile based on ${SKIPFILE_YAML}"
372 detect_abi
Karsten Tausche053c43d2018-04-24 12:10:39 +0200373 local SKIPGEN_ARGS=""
Dan Ruef4970552018-01-26 17:28:35 -0600374 test -n "${BOARD}" && SKIPGEN_ARGS="${SKIPGEN_ARGS} --board ${BOARD}"
375 test -n "${BRANCH}" && SKIPGEN_ARGS="${SKIPGEN_ARGS} --branch ${BRANCH}"
376 test -n "${ENVIRONMENT}" && SKIPGEN_ARGS="${SKIPGEN_ARGS} --environment ${ENVIRONMENT}"
377 # shellcheck disable=SC2086
378 ../../bin/${abi}/skipgen ${SKIPGEN_ARGS} "${SKIPFILE_YAML}" > "${SKIPFILE_PATH}"
379 test $? -eq 0 || error_msg "skipgen failed to generate a skipfile"
380 info_msg "Using the following generated skipfile contents (until EOF):"
381 cat "${SKIPFILE_PATH}"
382 info_msg "EOF"
383}