common_lib.sh 5.0 KB
Newer Older
L
lemon.higgins 已提交
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
#!/usr/bin/bash
# Copyright (c) [2020] Huawei Technologies Co.,Ltd.ALL rights reserved.
# This program is licensed under Mulan PSL v2.
# You can use it according to the terms and conditions of the Mulan PSL v2.
#          http://license.coscl.org.cn/MulanPSL2
# THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
####################################
#@Author    	:   lemon.higgins
#@Contact   	:   lemon.higgins@aliyun.com
#@Date      	:   2020-04-09 09:39:43
#@License   	:   Mulan PSL v2
#@Version   	:   1.0
#@Desc      	:   Public function
#####################################

function LOG_INFO() {
    printf "$(date +%Y-%m-%d\ %T)  $0  [ INFO ]  %s\n" "$@"
}

function LOG_WARN() {
    printf "$(date +%Y-%m-%d\ %T)  $0  [ WARN ]  %s\n" "$@"
}

function LOG_ERROR() {
    printf "$(date +%Y-%m-%d\ %T)  $0  [ ERROR ]  %s\n" "$@"
}

function DNF_INSTALL() {
    __pkg_list=$1
    if [ -z "${__pkg_list}" ]; then
        LOG_ERROR "Wrong parameter."
        exit 1
    fi
    reponames=$(grep '^\[.*\]' /etc/yum.repos.d/*.repo | tr -d [] | sed -e ':a;N;$!ba;s/\n/ /g')
    mapfile -t __install_pkgs < <(dnf -y install ${__pkg_list[*]} | grep -wE "${reponames// /|}" | grep -wE "$(uname -m)|noarch" | awk '{print $1}')

    if ! dnf -y install ${__pkg_list[*]}; then
        LOG_ERROR "pkg_list:${__pkg_list[*]} install failed."
        exit 1
    fi

    __installed_pkgs+=" ${__install_pkgs[*]}"

    return 0
}

function DNF_REMOVE() {
    __pkg_list=$1
    type=${2-0}

    if [ ${type} -eq 0 ]; then
        if ! dnf -y remove ${__installed_pkgs[*]} ${__pkg_list[*]}; then
            LOG_ERROR "pkg_list:${__installed_pkgs[*]} ${__pkg_list[*]} remove failed."
            exit 1
        fi
    else
        if ! dnf -y remove ${__pkg_list}; then
            LOG_ERROR "pkg_list:${__pkg_list[*]} remove failed."
            exit 1
        fi
    fi
}

function SLEEP_WAIT() {
    wait_time=${1-1}
    cmd=$2
    sleep_time=0

    while [ $sleep_time -lt $wait_time ]; do
        sleep 1
        if [ -n "$cmd" ]; then
            if $cmd; then
                return 0
            fi
        fi
        ((sleep_time++))
    done
}

function REMOTE_REBOOT_WAIT() {
    remoteuser=$1
    remotepasswd=$2
    remoteip=$3
    count=0

    if [[ "$(dmidecode -s system-product-name)" =~ "KVM" ]]; then
        SLEEP_WAIT 60
    else
        SLEEP_WAIT 200
    fi

    while [ $count -lt 60 ]; do
        if ping -c 1 $remoteip; then
L
lemon.higgins 已提交
97
            if SSH_CMD "echo '' > /dev/null 2>&1" $remoteip $remotepasswd $remoteuser; then
L
lemon.higgins 已提交
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
                return 0
            else
                SLEEP_WAIT 10
                ((count++))
            fi
        else
            SLEEP_WAIT 10
            ((count++))
        fi
    done

    return 1
}

function CHECK_RESULT() {
    actual_result=$1
    expect_result=${2-0}
    mode=${3-0}
    error_log=$4

    if [ -z "$actual_result" ]; then
        LOG_ERROR "Missing actual error code."
        return 1
    fi

    if [ $mode -eq 0 ]; then
        test "$actual_result"x != "$expect_result"x && {
            LOG_ERROR "$error_log"
            ((exec_result++))
            all_result="$all_result $exec_result"
        }
    else
        test "$actual_result"x == "$expect_result"x && {
            LOG_ERROR "$error_log"
            ((exec_result++))
            all_result="$all_result $exec_result"
        }
    fi
}

function CASE_RESULT() {
139 140
    test $1 -ne 0 && ret_c=1

L
lemon.higgins 已提交
141 142 143 144
    [[ -z $exec_result ]] && {
        LOG_INFO "The case execute succeed."
        exec_result=0
        all_result=0
145
        return $ret_c
L
lemon.higgins 已提交
146 147 148 149 150 151 152
    }

    for ret in "${all_result[@]}"; do
        LOG_ERROR "Test point $ret: execute failed."
    done
    exec_result=0
    all_result=0
153
    return $ret
L
lemon.higgins 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
}

function SSH_CMD() {
    cmd=$1
    remoteip=$2
    remotepasswd=${3-openEuler12#$}
    remoteuser=${4-root}
    timeout=${5-300}
    connport=${6-22}

    bash ${OET_PATH}/libs/locallibs/sshcmd.sh -c "$cmd" -i "$remoteip" -u "$remoteuser" -p "$remotepasswd" -t "$timeout" -o "$connport"
    ret=$?
    test $ret -ne 0 && LOG_ERROR "Failed in remote CMD operation:$ret"
    return $ret
}

function SSH_SCP() {
    src=$1
    dest=$2
    remotepasswd=${3-openEuler12#$}
    connport=${4-22}

    bash ${OET_PATH}/libs/locallibs/sshscp.sh -p "$remotepasswd" -o "$connport" -s "$src" -d "$dest"
    ret=$?
    test $ret -ne 0 && LOG_ERROR "Failed in remote SCP operation: $ret"
    return $ret
}

L
ltx 已提交
182 183 184 185 186
function POST_TEST_DEFAULT()
{
    LOG_INFO "$0 post_test"
}

L
lemon.higgins 已提交
187
function main() {
L
ltx 已提交
188 189 190 191 192
    if [ -n "$(type -t post_test)" ];then
        trap post_test EXIT INT TERM
    else
        trap POST_TEST_DEFAULT EXIT INT TERM
    fi
L
lemon.higgins 已提交
193 194 195 196 197

    if ! rpm -qa | grep expect >/dev/null 2>&1; then
        dnf install expect -y
    fi

L
ltx 已提交
198 199 200
    if [ -n "$(type -t config_params)" ];then
        config_params
    fi
L
lemon.higgins 已提交
201

L
ltx 已提交
202 203 204
    if [ -n "$(type -t pre_test)" ];then
        pre_test
    fi
L
lemon.higgins 已提交
205

L
ltx 已提交
206
    run_test
207
    CASE_RESULT $?
L
lemon.higgins 已提交
208 209
    test $? -eq 0 || exit 1
}