| #!/bin/sh |
| |
| LANG=C |
| export LANG |
| |
| error_msg() { |
| msg="$1" |
| [ -z "${msg}" ] && msg="Unknown error" |
| printf "ERROR: %s\n" "${msg}" >&2 |
| exit 1 |
| } |
| |
| warn_msg() { |
| msg="$1" |
| [ -z "${msg}" ] && msg="Unknown error" |
| printf "WARNING: %s\n" "${msg}" >&2 |
| } |
| |
| info_msg() { |
| msg="$1" |
| [ -z "${msg}" ] && msg="Unknown info" |
| printf "INFO: %s\n" "${msg}" >&1 |
| } |
| |
| check_root() { |
| if [ "$(id -ru)" -eq 0 ]; then |
| return 0 |
| else |
| return 1 |
| fi |
| } |
| |
| exit_on_fail() { |
| exit_code="$?" |
| [ "$#" -lt 1 ] && error_msg "Usage: exit_on_fail test_case [skip_list]" |
| test_case="$1" |
| skip_list="$2" |
| |
| if [ "${exit_code}" -ne 0 ]; then |
| echo "${test_case} fail" | tee -a "${RESULT_FILE}" |
| |
| # skip_list is a list of tests sepereated by space. This might be |
| # useful when exiting on prerequisite not met. |
| if [ -n "${skip_list}" ]; then |
| for i in ${skip_list}; do |
| echo "$i skip" | tee -a "${RESULT_FILE}" |
| done |
| fi |
| |
| # Exit normally to continue to run the following steps defined in test |
| # definition file. |
| exit 0 |
| else |
| echo "${test_case} pass" | tee -a "${RESULT_FILE}" |
| return 0 |
| fi |
| } |
| |
| exit_on_skip() { |
| exit_code="$?" |
| [ "$#" -lt 1 ] && error_msg "Usage: exit_on_skip test_case [msg]" |
| test_case="$1" |
| msg="$2" |
| |
| if [ "${exit_code}" -ne 0 ]; then |
| echo "${test_case} skip" | tee -a "${RESULT_FILE}" |
| |
| if [ -n "${msg}" ]; then |
| warn_msg "${msg}" |
| fi |
| |
| # Exit normally to continue to run the following steps defined in test |
| # definition file. |
| exit 0 |
| else |
| echo "${test_case} pass" | tee -a "${RESULT_FILE}" |
| return 0 |
| fi |
| } |
| |
| check_return() { |
| exit_code="$?" |
| [ "$#" -ne 1 ] && error_msg "Usage: check_return test_case" |
| test_case="$1" |
| |
| if [ "${exit_code}" -ne 0 ]; then |
| echo "${test_case} fail" | tee -a "${RESULT_FILE}" |
| return "${exit_code}" |
| else |
| echo "${test_case} pass" | tee -a "${RESULT_FILE}" |
| return 0 |
| fi |
| } |
| |
| # When shell argument "-e" set in test script, check_return and exit_on_fail |
| # would NOT work. run_test_case should be used instead. |
| run_test_case() { |
| [ "$#" -lt 2 ] && error_msg "Usage: run_test_case <test_command> <test_case_id> [skip_list]" |
| test_command="$1" |
| test_case_id="$2" |
| skip_list="$3" |
| |
| if eval "${test_command}"; then |
| echo "${test_case_id} pass" | tee -a "${RESULT_FILE}" |
| else |
| echo "${test_case_id} fail" | tee -a "${RESULT_FILE}" |
| # When skip_list isn't empty, skip the tests and exit. |
| if [ -n "${skip_list}" ]; then |
| for i in ${skip_list}; do |
| echo "$i skip" | tee -a "${RESULT_FILE}" |
| done |
| exit 0 |
| fi |
| fi |
| |
| return 0 |
| } |
| |
| report_pass() { |
| [ "$#" -ne 1 ] && error_msg "Usage: report_pass test_case" |
| test_case="$1" |
| echo "${test_case} pass" | tee -a "${RESULT_FILE}" |
| } |
| |
| report_fail() { |
| [ "$#" -ne 1 ] && error_msg "Usage: report_fail test_case" |
| test_case="$1" |
| echo "${test_case} fail" | tee -a "${RESULT_FILE}" |
| } |
| |
| add_metric() { |
| if [ "$#" -lt 3 ]; then |
| warn_msg "The number of parameters less then 3" |
| error_msg "Usage: add_metric test_case result measurement [units]" |
| fi |
| test_case="$1" |
| result="$2" |
| measurement="$3" |
| units="$4" |
| |
| echo "${test_case} ${result} ${measurement} ${units}" | tee -a "${RESULT_FILE}" |
| } |
| |
| detect_abi() { |
| abi=$(uname -m) |
| case "${abi}" in |
| armv7|armv7l|armv7el|armv7lh) abi="armeabi" ;; |
| arm64|armv8|arm64-v8a|aarch64) abi="arm64" ;; |
| *) error_msg "Unknown architecture: ${abi}" ;; |
| esac |
| } |
| |
| dist_name() { |
| if [ -x /usr/bin/lsb_release ]; then |
| dist="$(lsb_release -si)" |
| elif [ -f /etc/lsb-release ]; then |
| # shellcheck disable=SC1091 |
| dist="$(. /etc/lsb-release && echo "${DISTRIB_ID}")" |
| elif [ -f /etc/debian_version ]; then |
| dist="Debian" |
| elif [ -f /etc/fedora-release ]; then |
| dist="Fedora" |
| elif [ -f /etc/centos-release ]; then |
| dist="CentOS" |
| else |
| dist="Unknown" |
| warn_msg "Unsupported distro: cannot determine distribution name" |
| fi |
| } |
| |
| install_deps() { |
| pkgs="$1" |
| [ -z "${pkgs}" ] && error_msg "Usage: install_deps pkgs" |
| # skip_install parmater is optional. |
| skip_install="$2" |
| |
| if [ "${skip_install}" = "True" ] || [ "${skip_install}" = "true" ]; then |
| info_msg "install_deps skipped" |
| else |
| info_msg "Installing ${pkgs}" |
| dist_name |
| case "${dist}" in |
| Debian|Ubuntu) |
| # Use the default answers for all questions. |
| DEBIAN_FRONTEND=noninteractive apt-get update -q -y |
| # shellcheck disable=SC2086 |
| DEBIAN_FRONTEND=noninteractive apt-get install -q -y ${pkgs} |
| ;; |
| CentOS) |
| # shellcheck disable=SC2086 |
| yum -e 0 -y install ${pkgs} |
| ;; |
| Fedora) |
| # shellcheck disable=SC2086 |
| dnf -e 0 -y install ${pkgs} |
| ;; |
| Unknown) |
| warn_msg "Unsupported distro: package install skipped" |
| ;; |
| esac |
| fi |
| } |
| |
| # Return the exit code of the first command when using pipe. |
| pipe0_status() { |
| [ "$#" -ne 2 ] && error_msg "Usage: pipe0_status cmd1 cmd2" |
| cmd1="$1" |
| cmd2="$2" |
| |
| exec 4>&1 |
| ret_val=$({ { eval "${cmd1}" 3>&-; echo "$?" 1>&3; } 4>&- \ |
| | eval "${cmd2}" 1>&4; } 3>&1) |
| exec 4>&- |
| |
| return "${ret_val}" |
| } |
| |
| validate_check_sum() { |
| if [ "$#" -ne 2 ]; then |
| warn_msg "The number of parameters should be 2" |
| error_msg "Usage: validate_check_sum filename known_sha256sum" |
| return 1 |
| fi |
| OUTPUT_FILE_NAME="$1" |
| SHA256SUM_CHECK="$2" |
| # Get sha256sum of output_file |
| GET_SHA256SUM=$(sha256sum "${OUTPUT_FILE_NAME}" | awk '{print $1}') |
| echo "GET_SHA256SUM is ${GET_SHA256SUM}" |
| if [ "${SHA256SUM_CHECK}" = "${GET_SHA256SUM}" ] ; then |
| return 0 |
| else |
| echo "checksum did not match" |
| return 1 |
| fi |
| } |
| |
| convert_to_mb() { |
| [ "$#" -ne 2 ] && error_msg "Usage: convert_to_mb value units" |
| if ! echo "$1" | egrep -q "^[0-9.]+$"; then |
| error_msg "The first argument isn't a number" |
| fi |
| value="$1" |
| units="$2" |
| |
| case "${units}" in |
| KB|kb) value=$(echo "${value}" | awk '{print $1/1024}') ;; |
| MB|mb) ;; |
| GB|gb) value=$(echo "${value}" | awk '{print $1*1024}') ;; |
| TB|tb) value=$(echo "${value}" | awk '{print $1*1024*1024}') ;; |
| *) error_msg "Unsupported units" ;; |
| esac |
| |
| echo "${value}" |
| } |
| |
| dist_info() { |
| if ! command -v lsb_release > /dev/null; then |
| dist_name |
| case "${dist}" in |
| Debian|Ubuntu) install_deps "lsb-release" ;; |
| CentOS|Fedora) install_deps "redhat-lsb-core" ;; |
| *) warn_msg "Unsupported distro: dist_info skipped" |
| esac |
| fi |
| |
| # shellcheck disable=SC2034 |
| Release=$(lsb_release -r | awk '{print $2}') |
| Codename=$(lsb_release -c | awk '{print $2}') |
| } |
| |
| add_key() { |
| [ "$#" -ne 1 ] && error_msg "Usage: add_key url" |
| url="$1" |
| |
| dist_name |
| case "${dist}" in |
| Debian|Ubuntu) wget -O - "${url}" | apt-key add - ;; |
| CentOS|Fedora) infor_msg "add_key isn't needed on ${dist}" ;; |
| *) warn_msg "Unsupported distro: add_key skipped" |
| esac |
| } |
| |
| add_repo() { |
| [ "$#" -lt 1 ] && error_msg "Usage: add_repo <url> [backports]" |
| url="$1" |
| |
| dist_name |
| case "${dist}" in |
| # Detect Debian/Ubuntu codename and add repo automatically. The same url |
| # should work on all distributions supported by the repo. |
| Debian|Ubuntu) |
| dist_info |
| if [ -z "$2" ]; then |
| backports="" |
| elif [ "$2" = "backports" ]; then |
| backports="-backports" |
| else |
| echo "Usage: add_repo <url> [backports]" |
| error_msg "$2 is not a supported argument, should be 'backports'" |
| fi |
| echo "deb ${url} ${Codename}${backports} main" \ |
| >> "/etc/apt/sources.list.d/3rd-party-repo.list" |
| ;; |
| # It is not easy to amend url with distro version as its format may vary |
| # by repo. Test definition/plan should provide a correct repo url. |
| CentOS|Fedora) |
| wget -O - "${url}" >> "/etc/yum.repos.d/3rd-party.repo" |
| ;; |
| *) |
| warn_msg "Unsupported distro: add_repo skipped" |
| ;; |
| esac |
| } |