Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | LANG=C |
| 4 | export LANG |
| 5 | |
Vishal Bhoj | 0729429 | 2017-12-05 17:46:23 +0530 | [diff] [blame] | 6 | error_fatal() { |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 7 | local msg="$1" |
Vishal Bhoj | 0729429 | 2017-12-05 17:46:23 +0530 | [diff] [blame] | 8 | [ -z "${msg}" ] && msg="Unknown error" |
| 9 | if which lava-test-raise;then |
Vishal Bhoj | 8f433f9 | 2017-12-12 14:14:57 +0530 | [diff] [blame] | 10 | lava-test-raise "${msg}" |
Vishal Bhoj | 0729429 | 2017-12-05 17:46:23 +0530 | [diff] [blame] | 11 | else |
| 12 | printf "FATAL ERROR: %s\n" "${msg}" >&2 |
| 13 | fi |
| 14 | exit 1 |
| 15 | } |
| 16 | |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 17 | error_msg() { |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 18 | local msg="$1" |
Chase Qi | 3d1bc84 | 2016-08-26 14:47:16 +0800 | [diff] [blame] | 19 | [ -z "${msg}" ] && msg="Unknown error" |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 20 | printf "ERROR: %s\n" "${msg}" >&2 |
Chase Qi | c25683a | 2017-01-12 20:46:29 +0800 | [diff] [blame] | 21 | exit 1 |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | warn_msg() { |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 25 | local msg="$1" |
Chase Qi | 3d1bc84 | 2016-08-26 14:47:16 +0800 | [diff] [blame] | 26 | [ -z "${msg}" ] && msg="Unknown error" |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 27 | printf "WARNING: %s\n" "${msg}" >&2 |
| 28 | } |
| 29 | |
| 30 | info_msg() { |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 31 | local msg="$1" |
Chase Qi | 3d1bc84 | 2016-08-26 14:47:16 +0800 | [diff] [blame] | 32 | [ -z "${msg}" ] && msg="Unknown info" |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 33 | printf "INFO: %s\n" "${msg}" >&1 |
| 34 | } |
| 35 | |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 36 | check_root() { |
| 37 | if [ "$(id -ru)" -eq 0 ]; then |
| 38 | return 0 |
| 39 | else |
| 40 | return 1 |
| 41 | fi |
| 42 | } |
| 43 | |
Chase Qi | 3d1bc84 | 2016-08-26 14:47:16 +0800 | [diff] [blame] | 44 | exit_on_fail() { |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 45 | local exit_code="$?" |
Naresh Kamboju | feb3a24 | 2016-11-18 13:02:34 +0530 | [diff] [blame] | 46 | [ "$#" -lt 1 ] && error_msg "Usage: exit_on_fail test_case [skip_list]" |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 47 | local test_case="$1" |
| 48 | local skip_list="$2" |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 49 | |
Chase Qi | 3d1bc84 | 2016-08-26 14:47:16 +0800 | [diff] [blame] | 50 | if [ "${exit_code}" -ne 0 ]; then |
Naresh Kamboju | feb3a24 | 2016-11-18 13:02:34 +0530 | [diff] [blame] | 51 | echo "${test_case} fail" | tee -a "${RESULT_FILE}" |
Chase Qi | 92bff7d | 2016-09-18 18:44:31 +0800 | [diff] [blame] | 52 | |
| 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 Qi | bcc9162 | 2016-09-08 16:44:25 +0800 | [diff] [blame] | 61 | # Exit normally to continue to run the following steps defined in test |
| 62 | # definition file. |
| 63 | exit 0 |
| 64 | else |
Naresh Kamboju | feb3a24 | 2016-11-18 13:02:34 +0530 | [diff] [blame] | 65 | echo "${test_case} pass" | tee -a "${RESULT_FILE}" |
Chase Qi | bcc9162 | 2016-09-08 16:44:25 +0800 | [diff] [blame] | 66 | return 0 |
Chase Qi | 3d1bc84 | 2016-08-26 14:47:16 +0800 | [diff] [blame] | 67 | fi |
| 68 | } |
| 69 | |
Naresh Kamboju | b2a67fa | 2016-11-17 22:14:50 +0530 | [diff] [blame] | 70 | exit_on_skip() { |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 71 | local exit_code="$?" |
Naresh Kamboju | b2a67fa | 2016-11-17 22:14:50 +0530 | [diff] [blame] | 72 | [ "$#" -lt 1 ] && error_msg "Usage: exit_on_skip test_case [msg]" |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 73 | local test_case="$1" |
| 74 | local msg="$2" |
Naresh Kamboju | b2a67fa | 2016-11-17 22:14:50 +0530 | [diff] [blame] | 75 | |
| 76 | if [ "${exit_code}" -ne 0 ]; then |
| 77 | echo "${test_case} skip" | tee -a "${RESULT_FILE}" |
| 78 | |
| 79 | if [ -n "${msg}" ]; then |
Chase Qi | 7aa508c | 2017-01-20 16:45:54 +0800 | [diff] [blame] | 80 | warn_msg "${msg}" |
Naresh Kamboju | b2a67fa | 2016-11-17 22:14:50 +0530 | [diff] [blame] | 81 | 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 Qi | 3d1bc84 | 2016-08-26 14:47:16 +0800 | [diff] [blame] | 92 | check_return() { |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 93 | local exit_code="$?" |
Naresh Kamboju | feb3a24 | 2016-11-18 13:02:34 +0530 | [diff] [blame] | 94 | [ "$#" -ne 1 ] && error_msg "Usage: check_return test_case" |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 95 | local test_case="$1" |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 96 | |
| 97 | if [ "${exit_code}" -ne 0 ]; then |
Naresh Kamboju | feb3a24 | 2016-11-18 13:02:34 +0530 | [diff] [blame] | 98 | echo "${test_case} fail" | tee -a "${RESULT_FILE}" |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 99 | return "${exit_code}" |
| 100 | else |
Naresh Kamboju | feb3a24 | 2016-11-18 13:02:34 +0530 | [diff] [blame] | 101 | echo "${test_case} pass" | tee -a "${RESULT_FILE}" |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 102 | return 0 |
| 103 | fi |
| 104 | } |
| 105 | |
Chase Qi | c2e2f69 | 2016-10-27 10:27:43 +0800 | [diff] [blame] | 106 | # 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. |
| 108 | run_test_case() { |
| 109 | [ "$#" -lt 2 ] && error_msg "Usage: run_test_case <test_command> <test_case_id> [skip_list]" |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 110 | local test_command="$1" |
| 111 | local test_case_id="$2" |
| 112 | local skip_list="$3" |
Chase Qi | c2e2f69 | 2016-10-27 10:27:43 +0800 | [diff] [blame] | 113 | |
| 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 Kamboju | 7f319ba | 2016-09-12 15:11:12 +0530 | [diff] [blame] | 130 | report_pass() { |
Naresh Kamboju | feb3a24 | 2016-11-18 13:02:34 +0530 | [diff] [blame] | 131 | [ "$#" -ne 1 ] && error_msg "Usage: report_pass test_case" |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 132 | local test_case="$1" |
Naresh Kamboju | feb3a24 | 2016-11-18 13:02:34 +0530 | [diff] [blame] | 133 | echo "${test_case} pass" | tee -a "${RESULT_FILE}" |
Naresh Kamboju | 7f319ba | 2016-09-12 15:11:12 +0530 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | report_fail() { |
Naresh Kamboju | feb3a24 | 2016-11-18 13:02:34 +0530 | [diff] [blame] | 137 | [ "$#" -ne 1 ] && error_msg "Usage: report_fail test_case" |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 138 | local test_case="$1" |
Naresh Kamboju | feb3a24 | 2016-11-18 13:02:34 +0530 | [diff] [blame] | 139 | echo "${test_case} fail" | tee -a "${RESULT_FILE}" |
Naresh Kamboju | 7f319ba | 2016-09-12 15:11:12 +0530 | [diff] [blame] | 140 | } |
| 141 | |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 142 | add_metric() { |
Nicolas Dechesne | 3d25d43 | 2017-01-19 15:47:09 +0100 | [diff] [blame] | 143 | 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 Qi | 3d1bc84 | 2016-08-26 14:47:16 +0800 | [diff] [blame] | 146 | fi |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 147 | local test_case="$1" |
| 148 | local result="$2" |
| 149 | local measurement="$3" |
| 150 | local units="$4" |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 151 | |
Naresh Kamboju | feb3a24 | 2016-11-18 13:02:34 +0530 | [diff] [blame] | 152 | echo "${test_case} ${result} ${measurement} ${units}" | tee -a "${RESULT_FILE}" |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 153 | } |
| 154 | |
Chase Qi | 38ea4d3 | 2016-10-27 18:16:10 +0800 | [diff] [blame] | 155 | detect_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 Rue | 552c9f5 | 2018-01-04 11:59:02 -0600 | [diff] [blame] | 160 | x86_64) abi="x86_64" ;; |
Chase Qi | fa9f33e | 2017-09-14 10:36:49 +0800 | [diff] [blame] | 161 | *) error_msg "Unsupported architecture: ${abi}" ;; |
Chase Qi | 38ea4d3 | 2016-10-27 18:16:10 +0800 | [diff] [blame] | 162 | esac |
| 163 | } |
| 164 | |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 165 | dist_name() { |
Nicolas Dechesne | 14c77d9 | 2017-01-24 22:02:15 +0100 | [diff] [blame] | 166 | 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 Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 170 | dist="$(lsb_release -si)" |
| 171 | elif [ -f /etc/lsb-release ]; then |
Nicolas Dechesne | ec8e5a8 | 2017-01-25 09:18:16 +0100 | [diff] [blame] | 172 | # shellcheck disable=SC1091 |
| 173 | dist="$(. /etc/lsb-release && echo "${DISTRIB_ID}")" |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 174 | elif [ -f /etc/debian_version ]; then |
Nicolas Dechesne | b7e3876 | 2017-01-25 12:07:08 +0100 | [diff] [blame] | 175 | dist="debian" |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 176 | elif [ -f /etc/fedora-release ]; then |
Nicolas Dechesne | b7e3876 | 2017-01-25 12:07:08 +0100 | [diff] [blame] | 177 | dist="fedora" |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 178 | elif [ -f /etc/centos-release ]; then |
Nicolas Dechesne | b7e3876 | 2017-01-25 12:07:08 +0100 | [diff] [blame] | 179 | dist="centos" |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 180 | else |
Nicolas Dechesne | b7e3876 | 2017-01-25 12:07:08 +0100 | [diff] [blame] | 181 | dist="unknown" |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 182 | warn_msg "Unsupported distro: cannot determine distribution name" |
| 183 | fi |
Nicolas Dechesne | 14c77d9 | 2017-01-24 22:02:15 +0100 | [diff] [blame] | 184 | |
Fathi Boudra | 7a178e1 | 2017-01-25 15:55:04 +0200 | [diff] [blame] | 185 | # convert dist to lower case |
| 186 | dist=$(echo ${dist} | tr '[:upper:]' '[:lower:]') |
Nicolas Dechesne | 14c77d9 | 2017-01-24 22:02:15 +0100 | [diff] [blame] | 187 | case "${dist}" in |
Fathi Boudra | c917abd | 2017-01-25 15:56:04 +0200 | [diff] [blame] | 188 | rpb*) dist="oe-rpb" ;; |
Nicolas Dechesne | 14c77d9 | 2017-01-24 22:02:15 +0100 | [diff] [blame] | 189 | esac |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | install_deps() { |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 193 | local pkgs="$1" |
Chase Qi | 3d1bc84 | 2016-08-26 14:47:16 +0800 | [diff] [blame] | 194 | [ -z "${pkgs}" ] && error_msg "Usage: install_deps pkgs" |
| 195 | # skip_install parmater is optional. |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 196 | local skip_install="$2" |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 197 | |
| 198 | if [ "${skip_install}" = "True" ] || [ "${skip_install}" = "true" ]; then |
| 199 | info_msg "install_deps skipped" |
| 200 | else |
Nicolas Dechesne | 7a69531 | 2017-01-25 14:15:43 +0100 | [diff] [blame] | 201 | ! check_root && \ |
| 202 | error_msg "About to install packages, please run this script as root." |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 203 | info_msg "Installing ${pkgs}" |
| 204 | dist_name |
| 205 | case "${dist}" in |
Nicolas Dechesne | b7e3876 | 2017-01-25 12:07:08 +0100 | [diff] [blame] | 206 | debian|ubuntu) |
Dan Rue | 61c3a64 | 2017-12-01 10:40:07 -0600 | [diff] [blame] | 207 | 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 Qi | 191ede5 | 2016-10-24 09:26:27 +0800 | [diff] [blame] | 214 | # shellcheck disable=SC2086 |
Chase Qi | bcc9162 | 2016-09-08 16:44:25 +0800 | [diff] [blame] | 215 | DEBIAN_FRONTEND=noninteractive apt-get install -q -y ${pkgs} |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 216 | ;; |
Nicolas Dechesne | b7e3876 | 2017-01-25 12:07:08 +0100 | [diff] [blame] | 217 | centos) |
Chase Qi | 191ede5 | 2016-10-24 09:26:27 +0800 | [diff] [blame] | 218 | # shellcheck disable=SC2086 |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 219 | yum -e 0 -y install ${pkgs} |
| 220 | ;; |
Nicolas Dechesne | b7e3876 | 2017-01-25 12:07:08 +0100 | [diff] [blame] | 221 | fedora) |
Chase Qi | 191ede5 | 2016-10-24 09:26:27 +0800 | [diff] [blame] | 222 | # shellcheck disable=SC2086 |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 223 | dnf -e 0 -y install ${pkgs} |
| 224 | ;; |
Chase Qi | 475120c | 2017-02-06 12:06:01 +0800 | [diff] [blame] | 225 | *) |
| 226 | warn_msg "Unsupported distro: ${dist}! Package installation skipped." |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 227 | ;; |
| 228 | esac |
Chase Qi | aed8b75 | 2017-05-03 21:01:54 +0800 | [diff] [blame] | 229 | # shellcheck disable=SC2181 |
| 230 | if [ $? -ne 0 ]; then |
| 231 | error_msg "Failed to install dependencies, exiting..." |
| 232 | fi |
Chase Qi | 09edc7f | 2016-08-18 13:18:50 +0800 | [diff] [blame] | 233 | fi |
| 234 | } |
Naresh Kamboju | f88f4d7 | 2016-08-17 17:11:30 +0530 | [diff] [blame] | 235 | |
Chase Qi | 50a5baa | 2016-10-20 10:17:05 +0800 | [diff] [blame] | 236 | # Return the exit code of the first command when using pipe. |
| 237 | pipe0_status() { |
| 238 | [ "$#" -ne 2 ] && error_msg "Usage: pipe0_status cmd1 cmd2" |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 239 | local cmd1="$1" |
| 240 | local cmd2="$2" |
Chase Qi | 50a5baa | 2016-10-20 10:17:05 +0800 | [diff] [blame] | 241 | |
| 242 | exec 4>&1 |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 243 | local ret_val=$({ { eval "${cmd1}" 3>&-; echo "$?" 1>&3; } 4>&- \ |
Chase Qi | 50a5baa | 2016-10-20 10:17:05 +0800 | [diff] [blame] | 244 | | eval "${cmd2}" 1>&4; } 3>&1) |
| 245 | exec 4>&- |
| 246 | |
| 247 | return "${ret_val}" |
| 248 | } |
| 249 | |
Naresh Kamboju | f88f4d7 | 2016-08-17 17:11:30 +0530 | [diff] [blame] | 250 | validate_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 Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 256 | local OUTPUT_FILE_NAME="$1" |
| 257 | local SHA256SUM_CHECK="$2" |
Naresh Kamboju | f88f4d7 | 2016-08-17 17:11:30 +0530 | [diff] [blame] | 258 | # Get sha256sum of output_file |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 259 | local GET_SHA256SUM=$(sha256sum "${OUTPUT_FILE_NAME}" | awk '{print $1}') |
Chase Qi | ae7807f | 2016-10-21 08:43:25 +0800 | [diff] [blame] | 260 | echo "GET_SHA256SUM is ${GET_SHA256SUM}" |
Naresh Kamboju | f88f4d7 | 2016-08-17 17:11:30 +0530 | [diff] [blame] | 261 | if [ "${SHA256SUM_CHECK}" = "${GET_SHA256SUM}" ] ; then |
| 262 | return 0 |
| 263 | else |
| 264 | echo "checksum did not match" |
| 265 | return 1 |
| 266 | fi |
| 267 | } |
Chase Qi | e4cf6d4 | 2016-10-25 11:39:59 +0800 | [diff] [blame] | 268 | |
| 269 | convert_to_mb() { |
| 270 | [ "$#" -ne 2 ] && error_msg "Usage: convert_to_mb value units" |
Dan Rue | f497055 | 2018-01-26 17:28:35 -0600 | [diff] [blame] | 271 | if ! echo "$1" | grep -E -q "^[0-9.]+$"; then |
Chase Qi | e4cf6d4 | 2016-10-25 11:39:59 +0800 | [diff] [blame] | 272 | error_msg "The first argument isn't a number" |
| 273 | fi |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 274 | local value="$1" |
| 275 | local units="$2" |
Chase Qi | e4cf6d4 | 2016-10-25 11:39:59 +0800 | [diff] [blame] | 276 | |
| 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 Qi | e7eda1e | 2016-10-25 16:38:30 +0800 | [diff] [blame] | 287 | |
| 288 | dist_info() { |
| 289 | if ! command -v lsb_release > /dev/null; then |
| 290 | dist_name |
| 291 | case "${dist}" in |
Nicolas Dechesne | b7e3876 | 2017-01-25 12:07:08 +0100 | [diff] [blame] | 292 | debian|ubuntu) install_deps "lsb-release" ;; |
| 293 | centos|fedora) install_deps "redhat-lsb-core" ;; |
Chase Qi | 7aa508c | 2017-01-20 16:45:54 +0800 | [diff] [blame] | 294 | *) warn_msg "Unsupported distro: dist_info skipped" |
Chase Qi | e7eda1e | 2016-10-25 16:38:30 +0800 | [diff] [blame] | 295 | 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 | |
| 303 | add_key() { |
| 304 | [ "$#" -ne 1 ] && error_msg "Usage: add_key url" |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 305 | local url="$1" |
Chase Qi | e7eda1e | 2016-10-25 16:38:30 +0800 | [diff] [blame] | 306 | |
Nicolas Dechesne | 7a69531 | 2017-01-25 14:15:43 +0100 | [diff] [blame] | 307 | ! check_root && \ |
| 308 | error_msg "About to use apt-key, please run this script as root." |
Chase Qi | e7eda1e | 2016-10-25 16:38:30 +0800 | [diff] [blame] | 309 | dist_name |
| 310 | case "${dist}" in |
Nicolas Dechesne | b7e3876 | 2017-01-25 12:07:08 +0100 | [diff] [blame] | 311 | debian|ubuntu) wget -O - "${url}" | apt-key add - ;; |
| 312 | centos|fedora) infor_msg "add_key isn't needed on ${dist}" ;; |
Chase Qi | 7aa508c | 2017-01-20 16:45:54 +0800 | [diff] [blame] | 313 | *) warn_msg "Unsupported distro: add_key skipped" |
Chase Qi | e7eda1e | 2016-10-25 16:38:30 +0800 | [diff] [blame] | 314 | esac |
| 315 | } |
| 316 | |
| 317 | add_repo() { |
| 318 | [ "$#" -lt 1 ] && error_msg "Usage: add_repo <url> [backports]" |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 319 | local url="$1" |
Chase Qi | e7eda1e | 2016-10-25 16:38:30 +0800 | [diff] [blame] | 320 | |
Nicolas Dechesne | 7a69531 | 2017-01-25 14:15:43 +0100 | [diff] [blame] | 321 | ! check_root && \ |
| 322 | error_msg "About to add a repo, please run this script as root." |
Chase Qi | e7eda1e | 2016-10-25 16:38:30 +0800 | [diff] [blame] | 323 | 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 Dechesne | b7e3876 | 2017-01-25 12:07:08 +0100 | [diff] [blame] | 327 | debian|ubuntu) |
Chase Qi | e7eda1e | 2016-10-25 16:38:30 +0800 | [diff] [blame] | 328 | dist_info |
| 329 | if [ -z "$2" ]; then |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 330 | local backports="" |
Chase Qi | e7eda1e | 2016-10-25 16:38:30 +0800 | [diff] [blame] | 331 | elif [ "$2" = "backports" ]; then |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 332 | local backports="-backports" |
Chase Qi | e7eda1e | 2016-10-25 16:38:30 +0800 | [diff] [blame] | 333 | 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 Dechesne | b7e3876 | 2017-01-25 12:07:08 +0100 | [diff] [blame] | 342 | centos|fedora) |
Chase Qi | e7eda1e | 2016-10-25 16:38:30 +0800 | [diff] [blame] | 343 | wget -O - "${url}" >> "/etc/yum.repos.d/3rd-party.repo" |
| 344 | ;; |
| 345 | *) |
Chase Qi | 475120c | 2017-02-06 12:06:01 +0800 | [diff] [blame] | 346 | warn_msg "Unsupported distro: ${dist}! add_repo skipped" |
Chase Qi | e7eda1e | 2016-10-25 16:38:30 +0800 | [diff] [blame] | 347 | ;; |
| 348 | esac |
| 349 | } |
Daniel Díaz | 215b444 | 2017-02-15 18:56:58 -0600 | [diff] [blame] | 350 | |
| 351 | create_out_dir() { |
| 352 | [ -z "$1" ] && error_msg "Usage: create_out_dir output_dir" |
Karsten Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 353 | local OUTPUT=$1 |
Daniel Díaz | 215b444 | 2017-02-15 18:56:58 -0600 | [diff] [blame] | 354 | [ -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 Rue | f497055 | 2018-01-26 17:28:35 -0600 | [diff] [blame] | 359 | |
| 360 | generate_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 Tausche | 053c43d | 2018-04-24 12:10:39 +0200 | [diff] [blame] | 373 | local SKIPGEN_ARGS="" |
Dan Rue | f497055 | 2018-01-26 17:28:35 -0600 | [diff] [blame] | 374 | 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 | } |