提交 82a3b3b7 编写于 作者: O openeuler-ci-bot 提交者: Gitee

!3 add code of test frame

Merge pull request !3 from lemon.higgins/master
此差异已折叠。
export OET_PATH=/opt/litf
export FRAME=$(uname -i)
export LOCAL_IP=$(ip addr show | grep inet | grep -v inet6 | grep -Ewv "lo.*|docker.*|bond.*|vlan.*|virbr.*|br-.*" | awk '{print $2}' | awk -F "/" '{print $1}' | head -1)
export LOCAL_USER=$(echo ${USER})
export LOCAL_PASSWD=""
export REMOTE_IP=""
export REMOTE_USER=""
export REMOTE_PASSWD=""
#!/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
if SSH_CMD "echo '' > /dev/null 2>&1" $remoteip $remoteuser $remotepasswd; then
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() {
[[ -z $exec_result ]] && {
LOG_INFO "The case execute succeed."
exec_result=0
all_result=0
return 0
}
for ret in "${all_result[@]}"; do
LOG_ERROR "Test point $ret: execute failed."
done
exec_result=0
all_result=0
return 1
}
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
}
function main() {
trap post_test EXIT INT TERM
if ! rpm -qa | grep expect >/dev/null 2>&1; then
dnf install expect -y
fi
config_params
pre_test
run_test
CASE_RESULT
test $? -eq 0 || exit 1
}
#!/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-08 16:13:40
#@License : Mulan PSL v2
#@Version : 1.0
#@Desc : Encapsulate ssh, user t directly, and execute remote commands
####################################
source ${OET_PATH}/libs/locallibs/common_lib.sh
function sshcmd() {
cmd=$1
remoteip=$2
remotepasswd=${3}
remoteuser=${4}
timeout=${5}
connport=${6}
test "$cmd"x = ""x && LOG_ERROR "Lack of execute command." && exit 1
cmd=${cmd//\$/\\\$}
test "$remoteip"x = ""x && LOG_ERROR "Missing ip."
test "$(echo ${remoteip} | awk -F"." '{if ($1!=0 && $NF!=0) split ($0,IPNUM,".")} END { for (k in IPNUM) if (IPNUM[k]==0) print IPNUM[k]; else if (IPNUM[k]!=0 && IPNUM[k]!~/[a-z|A-Z]/ && length(IPNUM[k])<=3 && IPNUM[k]<255 && IPNUM[k]!~/^0/) print IPNUM[k]}' | wc -l)" -ne 4 && LOG_ERROR "the remote ip is Incorrect." && exit 1
if ping -c 1 ${remoteip} | grep "100% packet loss"; then
LOG_ERROR "connection to $remoteip failed."
exit 101
fi
test "$remoteuser"x = "root"x && LOG_WARN "the remote user uses the default configuration."
test "$remotepasswd"x = "openEuler12#$"x && LOG_WARN "the remote password uses the default configuration."
test "$timeout"x = "15"x && LOG_WARN "the timeout uses the default configuration."
test "$connport"x = "22"x && LOG_WARN "the connect port using the default configuration"
cmd_last_world=$(echo ${cmd} | awk '{print $NF}')
e_time=${timeout}
test "$cmd_last_world"x == "&"x && {
timeout=0
e_time=-1
}
expect <<-EOF
set timeout ${e_time}
spawn ssh -o "ConnectTimeout=${timeout}" -p ${connport} ${remoteuser}@${remoteip} "$cmd"
expect {
"Are you sure you want to continue connecting*"
{
send "yes\r"
expect "*\[P|p]assword:"
send "${remotepasswd}\r"
}
"*\[P|p]assword:"
{
send "${remotepasswd}\r"
}
timeout
{
end_user "connection to $remoteip timed out: \$expect_out(buffer)\n"
exit 101
}
eof
{
catch wait result
exit [lindex \$result 3]
}
}
expect {
eof {
catch wait result
exit [lindex \$result 3]
}
"\[P|p]assword:"
{
send_user "invalid password or account again.\$expect_out(buffer)\n"
send "${remotepasswd}\r"
}
timeout
{
send_user "connection to $remoteip timed out: \$expect_out(buffer)\n"
exit 101
}
}
}
EOF
exit $?
}
usage() {
printf "Usage: sshcmd.sh -c \"command\" -i \"remote machinet ip\" [-u login_user] [-p login_password] [-o port] [-t timeout]"
}
while getopts "c:i:p:u:t:o:h" OPTIONS; do
case $OPTIONS in
c) cmd="$OPTARG" ;;
i) remoteip="$OPTARG" ;;
u) remoteuser="$OPTARG" ;;
p) remotepasswd="$OPTARG" ;;
t) timeout="$OPTARG" ;;
o) connport="$OPTARG" ;;
\?)
printf "ERROR - Invalid parameter" >&2
usage
exit 1
;;
*)
printf "ERROR - Invalid parameter" >&2
usage
exit 1
;;
esac
done
if [ "$cmd"x = ""x ] || [ "$remoteip"x = ""x ]; then
usage
exit 1
fi
sshcmd "$cmd" "$remoteip" "${remotepasswd-openEuler12#$}" "${remoteuser-root}" "${timeout-300}" "${connport-22}"
exit $?
#!/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 17:58:35
#@License : Mulan PSL v2
#@Version : 1.0
#@Desc : 封装scp命令,供文件传输使用
####################################
source ${OET_PATH}/libs/locallibs/common_lib.sh
function sshscp() {
src=$1
dest=$2
remotepasswd=${3-openEuler12#$}
connport=${4-22}
test "$src"x = ""x && LOG_ERROR "No transfer file provided." && exit 1
test "$dest"x = ""x && LOG_ERROR "No file storage path provided." && exit 1
test "$remotepasswd"x = "openEuler12#$"x && LOG_WARN "the remote password uses the default configuration."
test "$connport"x = "22"x && LOG_WARN "the connect port using the default configuration"
expect <<-EOF
set timeout -1
spawn scp -P $connport -r $src $dest
expect {
"Are you sure you want to continue connecting*"
{
send "yes\r"
expect "\[P|p]assword:"
send "${remotepasswd}\r"
}
-re "\[P|p]assword:"
{
send "${remotepasswd}\r"
}
timeout
{
send_user "connection to remote timed out: \$expect_out(buffer)\n"
exit 101
}
eof
{
catch wait result
exit [lindex \$result 3]
}
}
expect {
eof
{
catch wait result
exit [lindex \$result 3]
}
-re "\[P|p]assword:"
{
send_user "invalid password or account. \$expect_out(buffer)\n"
exit 13
}
timeout
{
send_user "connection to remote timed out : \$expect_out(buffer)\n"
exit 101
}
}
EOF
exit $?
}
usage() {
printf "Usage: sshscp.sh -s src(user@ip:path) -d destination((user@ip:path)) [-p login_password] [-o port] -r -t timeout"
}
while getopts "p:s:d:o:h" OPTIONS; do
case $OPTIONS in
p) remotepasswd="$OPTARG" ;;
s) src="$OPTARG" ;;
d) dest="$OPTARG" ;;
o) connport="$OPTARG" ;;
h)
usage
exit 1
;;
\?)
printf "ERROR - Invalid parameter" >&2
usage
exit 1
;;
*)
printf "ERROR - Invalid parameter" >&2
usage
exit 1
;;
esac
done
sshscp "$src" "$dest" "$remotepasswd" "$connport"
exit $?
#!/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-14 16:45:43
# @License : Mulan PSL v2
# @Version : 1.0
# @Desc : Used to execute test cases
# ####################################
oet_path=$(
cd "$(dirname "$0")" || exit 1
pwd
)
source ${oet_path}/libs/locallibs/common_lib.sh
function usage() {
printf "Usage: \n
-c: configuration environment of test framework\n
-a: execute all use cases\n
-f:designated test suite\n
-r:designated test case\n
-x:the shell script is executed in debug mode\n
\n
Example: bash runoet.sh -f test_suite -r test_case -x\n"
}
function deploy_env() {
sed -i "s#OET_PATH.*#OET_PATH=${oet_path}#g" ${oet_path}/conf/env.conf
if ! grep "${oet_path}/conf/env.conf" ~/.bash_profile >/dev/null 2>&1; then
echo "source ${oet_path}/conf/env.conf" >>~/.bash_profile
fi
source ~/.bash_profile
}
function end_test_case() {
case_code=$1
exec 1>&6 6>&-
exec 2>&7 7>&-
LOG_INFO "The case exit by code ${case_code}"
LOG_INFO "End to run testcase:${test_case}"
test ${case_code} -ne 0 && ((fail_num++))
}
function run_all_cases() {
cd ${OET_PATH}/suite2cases || exit 1
mapfile test_suites < <(ls | sort -u)
test ${#test_suites[*]} -eq 0 && {
LOG_ERROR "Can't find recording about test_suites."
end_test_case 1
return 1
}
cd ${OET_PATH} || exit 1
for test_suite in ${test_suites[*]}; do
run_test_suite $test_suite
done
}
function run_test_suite() {
test_suite=$1
if ! find ${OET_PATH}/suite2cases -type f -name ${test_suite} >/dev/null 2>&1; then
LOG_ERROR "In the suite2cases directory, Can't find the file of testsuite:${test_suite}."
end_test_case 1
return 1
fi
while read -r test_case; do
run_test_case $test_suite $test_case
done <"${OET_PATH}/suite2cases/${test_suite}"
cd ${OET_PATH} || exit 1
}
function run_test_case() {
test_suite=$1
test_case=$2
LOG_INFO "start to run testcase:${test_case}"
log_path=${OET_PATH}/logs/${test_suite}/${test_case}
mkdir -p ${log_path}
exec 6>&1
exec 7>&2
exec >${log_path}/"$(date +%Y-%m-%d-%T)".log 2>&1
((case_num++))
if ! find ${OET_PATH}/suite2cases -type f -name ${test_suite} >/dev/null 2>&1; then
LOG_ERROR "In the suite2cases directory, Can't find the file of testsuite:${test_suite}."
end_test_case 1
return 1
elif ! find ${OET_PATH}/testcases -type d -name ${test_suite} >/dev/null 2>&1; then
LOG_ERROR "Can't find the dir of testsuite:${test_suite}."
end_test_case 1
return 1
fi
if ! grep -w ${test_case} ${OET_PATH}/suite2cases/${test_suite} >/dev/null 2>&1; then
LOG_ERROR "In the suite2cases directory, no testcase:${test_case} is found inside the ${test_suite} file."
end_test_case 1
return 1
elif ! find ${OET_PATH}/testcases -type d -name ${test_suite} >/dev/null 2>&1; then
LOG_ERROR "Can't find the dir of testcase:${test_case}."
end_test_case 1
return 1
elif ! find ${OET_PATH}/testcases/${test_suite} -type f -name "${test_case}*" >/dev/null 2>&1; then
LOG_ERROR "Can't find the script of testcase:${test_case}."
end_test_case 1
return 1
fi
case_path=$(find ${OET_PATH}/testcases/${test_suite} -type f -name "${test_case}*" | sed -e "s/${test_case}\..*//g")
cd ${case_path} || exit 1
script_type=$(ls ${test_case}* | awk -F '.' '{print $NF}')
if [[ "$script_type"x == "sh"x ]] || [[ "$script_type"x == "bash"x ]]; then
if [ "$command_x"x == "yes"x ]; then
bash -x ${test_case}.sh
case_result=$?
else
bash ${test_case}.sh
case_result=$?
fi
elif [ "$script_type"x == "py"x ]; then
python3 ${test_case}.py
case_result=$?
fi
if [ $case_result -eq 0 ]; then
touch ${OET_PATH}/results/succeed/${test_case}
else
touch ${OET_PATH}/results/failed/${test_case}
fi
test $case_result == 0 && {
end_test_case 0
((succee_num++))
}
test $case_result != 0 && {
end_test_case ${case_result}
}
}
function run_test() {
test_suite=$1
test_case=$2
case_num=0
fail_num=0
succee_num=0
if echo "$@" | grep -e '-a\|-f\|-r' >/dev/null 2>&1; then
rm -rf ${OET_PATH}/results
mkdir -p ${OET_PATH}/results/succeed
mkdir -p ${OET_PATH}/results/failed
fi
echo "$@" | grep -e '-f' >/dev/null 2>&1 && ! echo "$@" | grep -e '-r' >/dev/null 2>&1 && {
run_test_suite $test_suite
}
echo "$@" | grep -e '-f' | grep -e '-r' >/dev/null 2>&1 && {
run_test_case $test_suite $test_case
}
if echo "$@" | grep -e '-a\|-f\|-r' >/dev/null 2>&1; then
LOG_INFO "A total of ${case_num} use cases were executed, with ${succee_num} successes and ${fail_num} failures."
fi
if [ ${succee_num} != ${case_num} ]; then
return 1
fi
}
export command_x="no"
while getopts ":xachr:f:" arg; do
case $arg in
x)
command_x="yes"
;;
a)
run_all_cases
;;
c)
deploy_env
;;
f)
test_suite=$OPTARG
[[ -z "$test_suite" ]] && {
usage
exit 1
}
;;
r)
test_case=$OPTARG
[[ -z "$test_case" ]] && {
usage
exit 1
}
;;
h)
usage
;;
*)
usage
;;
esac
done
run_test $test_suite $test_case "$@"
oe_test_casename_01
oe_test_casename_02
#!/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 : Take the test ls command as an example
#####################################
source ${OET_PATH}/libs/locallibs/common_lib.sh
# 需要预加载的数据、参数配置
function config_params()
{
LOG_INFO "Start to config params of the case."
LOG_INFO "No params need to config."
LOG_INFO "End to config params of the case."
}
# 测试对象、测试需要的工具等安装准备
function pre_test()
{
LOG_INFO "Start to prepare the test environment."
LOG_INFO "No pkgs need to install."
LOG_INFO "End to prepare the test environment."
}
# 测试点的执行
function run_test()
{
LOG_INFO "Start to run test."
# 测试命令:ls
ls -CZl -all
CHECK_RESULT 0
# 测试/目录下是否存在proc|usr|roor|var|sys|etc|boot|dev目录
CHECK_RESULT "$(ls / | grep -cE 'proc|usr|roor|var|sys|etc|boot|dev')" 7
LOG_INFO "End to run test."
}
# 后置处理,恢复测试环境
function post_test()
{
LOG_INFO "Start to restore the test environment."
LOG_INFO "Need't to restore the tet environment."
LOG_INFO "End to restore the test environment."
}
main $@
#!/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 : Take the test ls command as an example
#####################################
import subprocess
ret = 0
cmd_status = subprocess.getstatusoutput("ls -CZl --all")[0]
if cmd_status != 0:
ret += 1
dir_num = subprocess.getoutput(
"ls / | grep -cE 'proc|usr|roor|var|sys|etc|boot|dev'")
if dir_num != "7":
ret += 1
exit(ret)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册