forked from weilinfox/ruyi-mugen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runoet.sh
262 lines (210 loc) · 6.8 KB
/
runoet.sh
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/bash
# Copyright (c) [2021] 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
# @email : lemon.higgins@aliyun.com
# @Date : 2021-04-20 15:12:00
# @License : Mulan PSL v2
# @Version : 1.0
# @Desc :
#####################################
OET_PATH=$(
cd "$(dirname "$0")" || exit 1
pwd
)
export OET_PATH
source ${OET_PATH}/libs/locallibs/common_lib.sh
[[ -d "/etc/mugen" ]] && export conf_file="/etc/mugen/mugen.json" || export conf_file="${OET_PATH}/conf/mugen.json"
REPOSITORY="https://gitee.com/openeuler/integration-test.git"
TIMEOUT="30m"
COMMAND_X="no"
CASE_NUM=0
SUCCESS_NUM=0
FAIL_NUM=0
function usage() {
echo TODO
#TODO
}
function deploy_conf() {
python3 ${OET_PATH}/libs/locallibs/write_conf.py "$@"
test $? -ne 0 && exit 1
}
function load_conf() {
rm -rf ${OET_PATH}/results
export_var=$(python3 ${OET_PATH}/libs/locallibs/read_conf.py env-var)
test $? -ne 0 && exit 1
$export_var
env_conf="$(echo -e $conf_file | sed -e 's/json/env/')"
printf "%s\n" "$export_var" >$env_conf
}
function download_cases() {
local tmpdir
tmpdir=$(mktemp -d)
git clone "$REPOSITORY" "$tmpdir"
cp "$tmpdir"/* "$OET_PATH" -r
rm -rf "$tmpdir"
}
function exec_case() {
local cmd=$1
local log_path=$2
local case_name=$3
exec 6>&1
exec 7>&2
exec >"$log_path"/"$(date +%Y-%m-%d-%T)".log 2>&1
timeout --preserve-status $TIMEOUT $cmd
ret_code=$?
exec 1>&6 6>&-
exec 2>&7 7>&-
test "$ret_code"x == "143"x && {
LOG_WARN "The case execution timeout."
}
if [ $ret_code -eq 0 ]; then
LOG_INFO "The case exit by code $ret_code."
((SUCCESS_NUM++))
mkdir -p ${OET_PATH}/results/succeed
touch ${OET_PATH}/results/succeed/${case_name}
else
LOG_ERROR "The case exit by code $ret_code."
((FAIL_NUM++))
mkdir -p ${OET_PATH}/results/failed
touch ${OET_PATH}/results/failed/${case_name}
fi
}
function run_test_case() {
local test_suite=$1
local test_case=$2
if [[ -z "$test_suite" || -z "$test_case" ]]; then
LOG_ERROR "Parameter(test suite or test case) loss."
exit 1
fi
((CASE_NUM++))
suite_path=$(python3 ${OET_PATH}/libs/locallibs/suite_case.py --suite $test_suite --key path)
test $? -ne 0 && return 1
test -d "$suite_path" || {
LOG_ERROR "Path value:${suite_path} in a JSON file that does not exist in the environment."
return 1
}
if ! grep -q "$test_case" suite2cases/"${test_suite}.json"; then
LOG_ERROR "In the suite2cases directory, Can't find the case name:${test_case} in the file of testsuite:${test_suite}."
return 1
fi
case_path=($(find ${suite_path} -name "${test_case}.*" | sed -e "s#/${test_case}.\(sh\|py\)##"))
test ${#case_path[@]} -gt 1 && {
LOG_ERROR "Multiple identical test case scripts have been found under the test suite. Please check your use case scripts."
return 1
}
log_path=${OET_PATH}/logs/${test_suite}/${test_case}
mkdir -p ${log_path}
LOG_INFO "start to run testcase:$test_case."
pushd "$case_path" >/dev/null || return 1
local time_out
time_out=$(grep -w --fixed-strings EXECUTE_T ${test_case}.* 2>/dev/nul | awk -F '=' '{print $NF}' | tr -d '"')
test -n "$time_out" && TIMEOUT=$time_out
local script_type
script_type=$(find . -name "${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
exec_case "bash -x ${test_case}.sh" "$log_path" "$test_case"
else
exec_case "bash ${test_case}.sh" "$log_path" "$test_case"
fi
elif [ "$script_type"x == "py"x ]; then
exec_case "python3 ${test_case}.py" "$log_path" "$test_case"
fi
popd >/dev/nul || return 1
LOG_INFO "End to run testcase:$test_case."
}
function run_test_suite() {
local test_suite=$1
[ -z "$(find $OET_PATH/suite2cases -name ${test_suite}.json)" ] && {
LOG_ERROR "In the suite2cases directory, Can't find the file of testsuite:${test_suite}."
return 1
}
for test_case in $(python3 ${OET_PATH}/libs/locallibs/suite_case.py --suite $test_suite --key cases-name | shuf); do
run_test_case "$test_suite" "$test_case"
done
}
function run_all_cases() {
test_suites=($(find ${OET_PATH}/suite2cases/ -type f -name "*.json" | awk -F '/' '{print $NF}' | sed -e 's/.json$//g'))
test ${#test_suites[@]} -eq 0 && {
LOG_ERROR "Can't find recording about test_suites."
return 1
}
for test_suite in ${test_suites[*]}; do
run_test_suite "$test_suite"
done
}
function statistic_result() {
LOG_INFO "A total of ${CASE_NUM} use cases were executed, with ${SUCCESS_NUM} successes and ${FAIL_NUM} failures."
[ ${FAIL_NUM} -ne 0 ] && exit 1
}
while getopts "c:af:r:dx" option; do
case $option in
c)
deploy_conf ${*//-c/}
;;
d)
echo "$@" | grep -q -e '^ *-d *$' || {
usage
exit 1
}
download_cases
;;
a)
if echo "$@" | grep -q -e '-a *-x *$\|-x *-a *$\|-ax *$\|-xa *$'; then
COMMAND_X="yes"
elif ! echo "$@" | grep -q -e '-a *$'; then
usage
exit 1
fi
load_conf
run_all_cases
statistic_result
;;
f)
test_suite=$OPTARG
echo $test_suite | grep -q -e '-a\| -r \|-x\|-d' && {
usage
exit 1
}
echo "$@" | grep -q -e ' *-x *\| *-xf *' && {
COMMAND_X="yes"
}
echo "$@" | grep -q -e ' -r ' || {
load_conf
run_test_suite $test_suite
statistic_result
}
;;
r)
test_case=$OPTARG
echo $test_case | grep -q -e '-a\|-f\|-x\|-d' && {
usage
exit 1
}
echo "$@" | grep -q -e ' *-x *\| *-xr *\| *-xf *' && {
COMMAND_X="yes"
}
load_conf
run_test_case $test_suite $test_case
statistic_result
;;
x)
echo "$@" | grep -q -e '^ *-x *$' && {
LOG_ERROR "The -x parameter must be used in combination with -a, -f, and -r."
exit 1
}
;;
*)
usage
exit 1
;;
esac
done