-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-tests.sh
executable file
·238 lines (196 loc) · 5.96 KB
/
run-tests.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
#!/usr/bin/env bash
#
# © 2024 AO Kaspersky Lab
# Licensed under the OpenSSL License
set -e
export LANG=C
export PKG_CONFIG=""
export BUILD_WITH_CLANG=
export BUILD_WITH_GCC=
TOOLCHAIN_SUFFIX=""
if [ "${BUILD_WITH_CLANG}" == "y" ]; then
TOOLCHAIN_SUFFIX="-clang"
fi
if [ "${BUILD_WITH_GCC}" == "y" ]; then
TOOLCHAIN_SUFFIX="-gcc"
fi
PROJECT_NAME=BoringSSL
KOS_DIR="$(dirname "$(realpath "${0}")")"
ROOT_DIR="$(dirname "${KOS_DIR}")"
BUILD=${KOS_DIR}/build_tests
TARGET=${TARGET:-"aarch64-kos"}
export GENERATED_DIR="${BUILD}/generated"
export TEST_LOGS_DIR="${BUILD}/logs"
export FAILED_TESTS="${TEST_LOGS_DIR}/failed_tests"
export TEST_TARGET_PREFIX="kos-qemu-image-"
export TEST_TARGET_SUFFIX="-sim"
export TEST_TIMEOUT=3000
export JOBS=`nproc`
export CMAKE_PID=
export ALL_TESTS=
export TESTS=
function KillQemu() {
PID_TO_KILL=$(pgrep qemu-system.*)
kill $PID_TO_KILL 2>/dev/null
}
function PrintHelp() {
cat<<HELP
Run ${PROJECT_NAME} unit tests in QEMU.
USAGE:
${0} [OPTIONS]
OPTIONS:
-h, --help
Help text.
-s, --sdk PATH
Path to the installed version of the KasperskyOS Community Edition SDK.
The value specified in the -s option takes precedence over
the value of the SDK_PREFIX environment variable.
-l, --list
List of tests that can be run.
-n, --name TEST
Test name to execute. The parameter can be repeated multiple times.
If not specified, all tests will be executed.
-t, --timeout SEC
Time, in seconds, allotted to start and execute a single test case.
Default value is ${TEST_TIMEOUT} seconds.
-o, --out PATH
Path where the results of the test run will be stored.
If not specified, the results will be stored in the ${TEST_LOGS_DIR} directory.
-j, --jobs N
Number of jobs for parallel build.
If not specified, the default value obtained from the nproc command is used.
HELP
}
function ParsArguments() {
local LIST_TESTS=""
while [ -n "${1}" ]; do
case "${1}" in
-h | --help) PrintHelp
exit 0;;
-l | --list) LIST_TESTS=YES;;
-s | --sdk) SDK_PREFIX="${2}"
shift;;
-n | --name) TESTS="${TESTS} ${2}"
shift ;;
-t | --timeout) TEST_TIMEOUT="${2}"
shift ;;
-o | --out) TEST_LOGS_DIR="${2}";
shift ;;
-j | --jobs) JOBS="${2}";
shift ;;
*) echo "Unknown option - '${1}'."
exit 1;;
esac
shift
done
if [ -z "${SDK_PREFIX}" ];then
echo "Can't get path to the installed KasperskyOS SDK."
PrintHelp
exit 1
fi
export PATH="${SDK_PREFIX}/toolchain/bin:${PATH}"
if [ ! -z "${LIST_TESTS}" ]; then
PrintTestNames
exit 0
fi
}
function Generate() {
cmake -G "Unix Makefiles" -B "${BUILD}" \
-D CMAKE_BUILD_TYPE:STRING=Debug \
-D CMAKE_INSTALL_PREFIX:STRING="${INSTALL_PREFIX}" \
-D KOS_DIR="${KOS_DIR}" \
-D CMAKE_FIND_ROOT_PATH="${PREFIX_DIR}/sysroot-${TARGET}" \
-D CMAKE_TOOLCHAIN_FILE="${SDK_PREFIX}/toolchain/share/toolchain-${TARGET}${TOOLCHAIN_SUFFIX}.cmake" \
"${ROOT_DIR}/src"
if [ $? -ne 0 ]; then
echo "Can't generate make files.";
rm -rf "${BUILD}"
exit 1
fi
}
function ListTests() {
[ ! -e ${BUILD} ] && Generate
ALL_TESTS=$("cmake" --build ${BUILD} --target help | \
grep -wo ${TEST_TARGET_PREFIX}.*${TEST_TARGET_SUFFIX} | \
sed "s|${TEST_TARGET_PREFIX}\(.*\)${TEST_TARGET_SUFFIX}|\1|")
if [ -z "${ALL_TESTS}" ]; then
echo "No test targets found - nothing to do."
exit 0
fi
}
function PrintTestNames() {
ListTests
echo "Tests available:"
echo "${ALL_TESTS}" | sed 's/\s\+/\n/g' | sort | sed 's/^/ /'
}
function GetTests() {
ListTests
if [ -z "${TESTS}" ]; then
TESTS="${ALL_TESTS}"
else
TESTS=$(echo "${TESTS}" | sed 's/ /\n/g' | sort | uniq)
for TEST in ${TESTS}; do
if ! echo "${ALL_TESTS}" | grep -q "${TEST}"; then
echo "Unknown test: ${TEST}."
exit 1;
fi
done
fi
}
function SetupEnvironment() {
# TEST_LOGS_DIR should be a full path, no matter relative or absolute.
if [[ "${TEST_LOGS_DIR}" != /* ]]; then
TEST_LOGS_DIR="${PWD}/${TEST_LOGS_DIR}"
fi
if [ -e "${TEST_LOGS_DIR}" ]; then
rm -rf "${TEST_LOGS_DIR}"
fi
mkdir -p ${TEST_LOGS_DIR} &> /dev/null
if [ -e "${FAILED_TESTS}" ]; then
rm -rf "${FAILED_TESTS}"
fi
}
function RunTests() {
# Run all specified tests.
for TEST in ${TESTS}; do
TEST_LOG="${TEST_LOGS_DIR}/${TEST}.result"
TEST_TARGET=${TEST_TARGET_PREFIX}${TEST}${TEST_TARGET_SUFFIX}
# Build test.
"cmake" --build ${BUILD} --target ${TEST_TARGET} -j ${JOBS} &> ${TEST_LOG} &
CMAKE_PID=`echo $!`
FAILED=YES
tail -F -n +1 --pid="${CMAKE_PID}" "${TEST_LOG}" 2>/dev/null \
| while IFS= read -t ${TEST_TIMEOUT} -r STR; do
echo ${STR}
if [[ ${STR} == *"ALL-KTEST-FINISHED"* ]]; then
FAILED=NO
break;
elif [[ ${STR} == *"FAILED TEST"* ]]; then
echo " ${TEST}" >> "${FAILED_TESTS}"
break;
fi
done;
KillQemu
# Cleanup.
if [[ "${FAILED}" == NO ]]; then
rm -rf "${GENERATED_DIR}/*_${TEST}" "${BUILD}/${TEST}*"
fi
done
}
function PrintResult() {
if [[ -e "${FAILED_TESTS}" ]]; then
echo "Some tests have failed. See the logs for more details."
echo "List of failed tests can be found at ${FAILED_TESTS}."
echo "Failed tests:"
cat "${FAILED_TESTS}"
else
echo "All tests are passed."
fi
}
# Main.
trap KillQemu EXIT HUP INT QUIT PIPE TERM
ParsArguments $@
GetTests
SetupEnvironment
RunTests
PrintResult