summaryrefslogtreecommitdiff
path: root/common/scripts/include/sh-test-lib
blob: 5c1e4a15b260805666babc56251ab7291eff4416 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/sh
#
# Shared shell library for test management
#
# Copyright (C) 2010 - 2014, Linaro Limited.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# Author: Ricardo Salveti <rsalveti@linaro.org>
# Author: Botao Sun <botao.sun@linaro.org>
# Maintainer: Chase Qi <chase.qi@linaro.org>

error_msg() {
    local msg="$1"
    test -z "${msg}" && msg="Unknown error"
    echo "ERROR: ${msg}" >&2
    exit 1
}

warn_msg() {
    local msg="$1"
    test -z "${msg}" && msg="Unknown error"
    printf "WARNING: %s\n\n" "${msg}" >&2
}

info_msg() {
    local msg="$1"
    test -z "${msg}" && msg="Unknown info"
    printf "INFO: %s\n\n" "${msg}" >&1
}

check_return_fail() {
    if [ $? -ne 0 ]; then
        fail_test "$1"
        return 0
    else
        return 1
    fi
}

fail_test() {
    local reason="$1"
    echo "${test}: fail - ${reason}"
}

pass_test() {
    echo "${test}: pass"
}

check_root() {
    if [ "$(id -ru)" -eq 0 ]; then
        return 0
    else
        return 1
    fi
}

check_return() {
    local exit_code="$?"
    local test="$1"

    test -z "${test}" && warn_msg "Test name is empty"

    if [ "${exit_code}" -ne 0 ]; then
        echo "${test} fail" | tee -a "${RESULT_FILE}"
    else
        echo "${test} pass" | tee -a "${RESULT_FILE}"
    fi
}

add_metric() {
    local test="$1"
    local measurement="$2"
    local units="$3"

    test -z "${test}" && warn_msg "Test name is empty"
    test -z "${measurement}" && warn_msg "Test measurement is empty"
    test -z "${units}" && warn_msg "Test units is empty"

    echo "${test} pass ${measurement} ${units}" | tee -a "${RESULT_FILE}"
}

dist_name() {
    if [ -x /usr/bin/lsb_release ]; then
        dist="$(lsb_release -si)"
    elif [ -f /etc/lsb-release ]; then
        . /etc/lsb-release
        dist="${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() {
    local pkgs="$1"
    dist_name
    case "${dist}" in
      Debian|Ubuntu)
        apt-get update
        apt-get install -y -q ${pkgs}
        ;;
      CentOS)
        yum -e 0 -y install ${pkgs}
        ;;
      Fedora)
        dnf -e 0 -y install ${pkgs}
        ;;
      Unknown)
        warn_msg "Unsupported distro: package install skipped"
        ;;
    esac
}