forked from intel/lkvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtests
executable file
·241 lines (208 loc) · 5.34 KB
/
runtests
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
#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-2.0-only
# Copyright (c) 2022 Intel Corporation.
source .env
START_TIME=""
SUMMRY_LOG=""
TESTS_CLIENT="tests-client"
TESTS_SERVER="tests-server"
TEST_FILES=""
SUB_FOLDERS=""
TEST_LIST_FILE="/tmp/tests"
OLD_IFS=""
usage() {
cat << _EOF
Usage: ${0##*/} [-f CMDFILES] [-c CMDLINE] [-o LOGFILE]
-f CMDFILES execute user defined list of tests in files separated by ','
-c CMDLINE execute test case
-o LOGFILE redirect output of tests to file
Examples:
./runtests -f cet/tests
./runtests -f cet/tests -o cet_tests.log
./runtests -f tests-client // exeucte tests-client/tests for all subfolders
./runtests -f tests-server // exeucte tests-server/tests for all subfolders
./runtests -c ./cet/quick_test -o cet_quick_test.log
_EOF
}
err() {
echo -e "\n$*" >&2
exit 1
}
runtest() {
local cmdline=$1
local logfile=$2
local subfolder=$3
local start
local stop
local duration
local code
local result
local case_result=""
if [[ -n "$logfile" ]]; then
echo "<<<test start - '$cmdline'>>" | tee -a "$logfile"
else
echo "<<<test start - '$cmdline'>>"
fi
if [[ -z "$subfolder" ]]; then
echo "LKVS tests: $cmdline" >> /dev/kmsg
else
echo "LKVS tests: ${subfolder}/${cmdline}" >> /dev/kmsg
fi
set -o pipefail
start=$(date +%s.%3N)
if [[ -n "$logfile" ]]; then
eval "$cmdline |& tee -a $logfile" &
else
eval "$cmdline" &
fi
wait $!
code=$?
stop=$(date +%s.%3N)
duration=$(printf '%.3f' "$(bc <<< "$stop-$start")")
set +o pipefail
case $code in
0)
result="PASS"
;;
2)
result="BLOCK"
;;
32)
result="NA"
;;
*)
result="FAIL"
;;
esac
if [[ -n "$logfile" ]]; then
echo -e "<<<test end, result: $result, duration: $duration>>\n" | tee -a "$logfile"
else
echo -e "<<<test end, result: $result, duration: ${duration}s>>\n"
fi
case_result=$(printf "[RESULT]%-68s%-11s%-12s%-8s" "[$cmdline]" " [$result]" "[$code]" "[${duration}s]")
echo "$case_result" >> "$SUMMRY_LOG"
}
runcmdfile() {
local cmdfile=$1
local logfile=$2
local subfolder=""
local file_type=""
local lines=""
local line=""
if [[ "$cmdfile" == *"/"* ]]; then
subfolder=${cmdfile%/*}
else
echo "cmdfile:$cmdfile(no '/') is not in a subfolder!"
fi
grep -v "^#.*" "$cmdfile" | grep -v "^$" > "$TEST_LIST_FILE"
lines=$(cat $TEST_LIST_FILE)
OLD_IFS="$IFS"
IFS=$'\n'
for line in $lines; do
runtest "$line" "$logfile" "$subfolder"
done
IFS="$OLD_IFS"
}
prepare_files_list() {
local test_file_type=$1
local sub_folder=""
for sub_folder in $SUB_FOLDERS; do
# Remov the / in the end
sub_folder="${sub_folder%/}"
[[ "$sub_folder" == "common" ]] && continue
if [[ -e "${sub_folder}/${test_file_type}" ]]; then
if [[ -z "$TEST_FILES" ]]; then
TEST_FILES="${sub_folder}/${test_file_type}"
else
TEST_FILES="${TEST_FILES},${sub_folder}/${test_file_type}"
fi
elif [[ -e "${sub_folder}/tests" ]]; then
if [[ -z "$TEST_FILES" ]]; then
TEST_FILES="${sub_folder}/tests"
else
TEST_FILES="${TEST_FILES},${sub_folder}/tests"
fi
else
echo "WARNING: No $test_file_type or tests file under $sub_folder folder."
fi
done
}
test_all() {
local test_file_type=$1
# Don't quote */, otherwise it could not list all folders.
SUB_FOLDERS=$(ls -1 -d */)
prepare_files_list "$test_file_type"
CMDFILES="$TEST_FILES"
if [[ -z "$LOGFILE" ]]; then
echo "Test files: $CMDFILES"
else
echo "Test files: $CMDFILES" | tee "$LOGFILE"
fi
}
# Default value
: "${LOGFILE:="./lkvs.log"}"
: CMDFILES=""
: CMDLINE=""
while getopts ":o:f:c:h" opt; do
case "$opt" in
o)
LOGFILE=$OPTARG
;;
f)
CMDFILES=$OPTARG
[[ "$CMDFILES" == "$TESTS_CLIENT" ]] && test_all "$TESTS_CLIENT"
[[ "$CMDFILES" == "$TESTS_SERVER" ]] && test_all "$TESTS_SERVER"
;;
c)
CMDLINE=$OPTARG
;;
h)
usage
exit 0
;;
\?)
usage
err "Invalid option: -$OPTARG"
;;
:)
usage
err "Option -$OPTARG requires an argument."
;;
esac
done
if [[ -z "$CMDFILES" ]] && [[ -z "$CMDLINE" ]]; then
usage
err "no test to run!"
fi
START_TIME="$(date +%Y-%m-%d_%H-%M-%S)"
SUMMRY_LOG="/tmp/lkvs_${START_TIME}_summary.log"
echo "Test Start Time: $START_TIME" > "$SUMMRY_LOG"
{
echo "--------------------------------------------------------"
printf "%-76s%-11s%-12s%-8s\n" "Testcase" "Result" "Exit Value" "Duration"
printf "%-76s%-11s%-12s%-8s\n" "--------" "------" "----------" "--------"
} >> "$SUMMRY_LOG"
for cmdfile in $(tr "," " " <<< "$CMDFILES"); do
if [[ ! -f "$cmdfile" ]]; then
echo "WARNING: $cmdfile not found!"
continue
fi
[[ $(which file) ]] && {
file_type=""
file_type=$(file "$cmdfile")
[[ "$file_type" == *"text"* ]] || {
echo "WARNING: -f $cmdfile is not a text type file, it's real type:|$file_type|"
echo "Please choose the correct tests file."
usage
continue
}
}
runcmdfile "$cmdfile" "$LOGFILE"
done
if [[ -n "$CMDLINE" ]]; then
runtest "$CMDLINE" "$LOGFILE"
fi
# Add the following print for the end of the case summary.
echo "--------------------------------------------------------" >> "$SUMMRY_LOG"
[[ -z "$LOGFILE" ]] || cat "$SUMMRY_LOG" >> "$LOGFILE"
cat "$SUMMRY_LOG"