summaryrefslogtreecommitdiff
path: root/automated/lib/sh-test-lib
blob: 39a7e922c70ce74d868c2c6cdbf16bf14f96ed0c (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/sh

LANG=C
export LANG

error_msg() {
    local msg="$1"
    [ -z "${msg}" ] && msg="Unknown error"
    printf "ERROR: %s\n" "${msg}" >&2
    exit 0
}

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

info_msg() {
    local 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() {
    local exit_code="$?"
    [ "$#" -lt 1 ] && error_msg "Usage: exit_on_fail test [skip_list]"
    local test="$1"
    local skip_list="$2"

    if [ "${exit_code}" -ne 0 ]; then
        echo "${test} 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} pass" | tee -a "${RESULT_FILE}"
        return 0
    fi
}

check_return() {
    local exit_code="$?"
    [ "$#" -ne 1 ] && error_msg "Usage: check_return test"
    local test="$1"

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

report_pass() {
    [ "$#" -ne 1 ] && error_msg "Usage: report_pass test"
    local test="$1"
    echo "${test} pass" | tee -a "${RESULT_FILE}"
}

report_fail() {
    [ "$#" -ne 1 ] && error_msg "Usage: report_fail test"
    local test="$1"
    echo "${test} fail" | tee -a "${RESULT_FILE}"
}

add_metric() {
    if [ "$#" -ne 4 ]; then
        warn_msg "The number of parameters less then 4"
        error_msg "Usage: add_metric test result measurement units"
    fi
    local test="$1"
    local result="$2"
    local measurement="$3"
    local units="$4"

    echo "${test} ${result} ${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"
    [ -z "${pkgs}" ] && error_msg "Usage: install_deps pkgs"
    # skip_install parmater is optional.
    local 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
            DEBIAN_FRONTEND=noninteractive apt-get install -q -y ${pkgs}
            ;;
          CentOS)
            yum -e 0 -y install ${pkgs}
            ;;
          Fedora)
            dnf -e 0 -y install ${pkgs}
            ;;
          Unknown)
            warn_msg "Unsupported distro: package install skipped"
            ;;
        esac
    fi
}

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
}