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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
#!/bin/bash
TMPDIR=/dev/shm
if [[ ${PYTHON} == "" ]]
then
PYTHON=python
fi
function print_help
{
echo "Utility to run various tests"
echo
echo "Define variable PYTHON to point custom executable (if needed);"
echo "by default standard python command is invoked."
echo
echo "Current settings:"
echo "- Python interpreter: '${PYTHON}'"
echo "- CFLAGS: '${CFLAGS}'"
echo "- Selected unit tests: '${UNITTEST}' (empty value means 'all')"
echo " This flag is used just for 'mallocfaults' and 'pycallfaults'"
echo " as these tests might be really time consuming"
echo
usage
}
function usage
{
echo "$0 unit|unpickle|leaks|valgrind|mallocfaults|pycallfaults"
echo
echo "unit - run default unit tests"
echo "unpickle - run unpickle tests, which depend on machine"
echo "leaks - recompile module with flag MEMORY_DEBUG,"
echo " then run unittests and check if there were memory leaks"
echo "valgrind - run unittests in valgrind and check if there are"
echo " any leaks from pyahocorasick"
echo "mallocfaults - recompile module with flag MEMORY_DEBUG,"
echo " then run unnitests injecting malloc faults"
echo "reallocfaults - recompile module with flag MEMORY_DEBUG,"
echo " then run unnitests injecting realloc faults"
echo "pycallfaults - recompile module with flag MEMORY_DEBUG,"
echo " then run unnitests injecting faults in python C-API calls"
echo "coverage - create coverage report in 'coverage' subdir"
echo
echo "release - run unit, unpickle, leaks, mallocfaults and reallocfaults"
echo " meant to run before relese"
}
######################################################################
ACTIONS="unit unpickle leaks valgrind mallocfaults reallocfaults pycallfaults coverage release"
if [[ $# != 1 || $1 == '-h' || $1 == '--help' ]]
then
print_help
exit 1
fi
ACTION=
REBUILD=1
######################################################################
RED='\033[31m'
GREEN='\033[32m'
RESET='\033[0m'
MEMORY_DEBUG_PATH="${TMPDIR}/memory.dump"
MEMORY_DEBUG="-DMEMORY_DEBUG -DMEMORY_DUMP_PATH='\"${MEMORY_DEBUG_PATH}\"'"
function rebuild
{
${PYTHON} setup.py build_ext --inplace
if [[ $? != 0 ]]
then
echo -e "${RED}Build failed${RESET}"
exit 1
fi
}
function force_rebuild
{
if [[ ${REBUILD} == 1 ]]
then
rm -r build ahocorasick*.so 2> /dev/null
rebuild
fi
}
function run_unittests
{
${PYTHON} unittests.py ${UNITTEST}
if [[ $? != 0 ]]
then
echo -e "${RED}Unit tests failed${RESET}"
exit 1
fi
}
function handle_unit
{
force_rebuild
run_unittests
}
function run_unpickletests
{
${PYTHON} unpickle_test.py
if [[ $? != 0 ]]
then
echo -e "${RED}Unpickle tests failed${RESET}"
exit 1
fi
}
function handle_unpickle
{
force_rebuild
run_unpickletests
}
function run_leaktest
{
${PYTHON} tests/memdump_check.py ${MEMORY_DEBUG_PATH}
if [[ $? != 0 ]]
then
echo -e "${RED}Memory leaks detected${RESET}"
exit 1
fi
}
function handle_leaks
{
export CFLAGS="${CFLAGS} ${MEMORY_DEBUG}"
force_rebuild
run_unittests
run_leaktest
}
function handle_valgrind
{
if ! command -v valgrind > /dev/null
then
echo "Valgrind not found"
exit 1
fi
force_rebuild
local LOGFILE=${TMPDIR}/valgrind.log
echo "Running valgrind..."
valgrind --log-file=${LOGFILE} --leak-check=full --track-origins=yes ${PYTHON} unittests.py
${PYTHON} tests/valgrind_check.py . ${LOGFILE}
}
function run_mallocfaults
{
# obtain max allocation number
unset ALLOC_FAIL
unset REALLOC_FAIL
run_unittests
local MINID=0
echo ${MEMORY_DEBUG_PATH}
local MAXID=$(${PYTHON} tests/memdump_maxalloc.py ${MEMORY_DEBUG_PATH})
# simulate failures of all allocations
for ID in `seq ${MINID} ${MAXID}`
do
echo -ne "Checking memalloc fail ${ID} of ${MAXID}\r"
mallocfault ${ID}
done
echo
}
function mallocfault
{
export ALLOC_NODUMP=1
export ALLOC_FAIL=$1
local LOG=${TMPDIR}/mallocfault${ID}.log
${PYTHON} unittests.py ${UNITTEST} -q > ${LOG} 2>&1
if [[ $? == 139 ]]
then
echo -e "${RED}SEGFAULT${RESET}"
exit 1
fi
${PYTHON} tests/unittestlog_check.py ${LOG}
if [[ $? != 0 ]]
then
echo -e "${RED}Possible error${RESET}"
echo "Inspect ${LOG}, there are errors other than expected MemoryError"
exit 1
fi
}
function handle_mallocfaults
{
export CFLAGS=${MEMORY_DEBUG}
force_rebuild
run_mallocfaults
}
function run_reallocfaults
{
# obtain max allocation number
unset ALLOC_FAIL
unset REALLOC_FAIL
run_unittests
local MINID=0
echo ${MEMORY_DEBUG_PATH}
local MAXID=$(${PYTHON} tests/memdump_maxrealloc.py ${MEMORY_DEBUG_PATH})
# simulate failures of all allocations
for ID in `seq ${MINID} ${MAXID}`
do
echo -ne "\rChecking realloc fail ${ID} of ${MAXID}"
reallocfault ${ID}
done
echo
}
function reallocfault
{
export ALLOC_NODUMP=1
export REALLOC_FAIL=$1
local LOG=${TMPDIR}/reallocfault${ID}.log
${PYTHON} unittests.py ${UNITTEST} -q > ${LOG} 2>&1
if [[ $? == 139 ]]
then
echo -e "${RED}SEGFAULT${RESET}"
exit 1
fi
${PYTHON} tests/unittestlog_check.py ${LOG}
if [[ $? != 0 ]]
then
echo -e "${RED}Possible error${RESET}"
echo "Inspect ${LOG}, there are errors other than expected MemoryError"
exit 1
fi
}
function handle_reallocfaults
{
export CFLAGS=${MEMORY_DEBUG}
force_rebuild
run_reallocfaults
}
function handle_pycallfaults
{
export CFLAGS="-DPYCALLS_INJECT_FAULTS"
force_rebuild
local TMP=${TMPDIR}/pycallfaults
${PYTHON} unittests.py ${UNITTEST} > ${TMP}
local MINID=0
local MAXID=$(awk '
/^Fail ID: / {if ($3 > max) max=$3}
END {print max}
' ${TMP})
# simulate failures of all call to Python C-API
for ID in `seq 0 ${MAXID}`
do
echo -n "Checking Python C-API fail ${ID} of ${MAXID}"
local LOG=${TMPDIR}/pycallfaults${ID}.log
export PYCALL_FAIL=${ID}
${PYTHON} unittests.py ${UNITTEST} > ${LOG} 2>&1
echo " return code $?"
${PYTHON} tests/pyfault_check.py ${LOG}
done
}
function handle_coverage
{
if ! command -v gcovr > /dev/null
then
echo "gcovr not found"
exit 1
fi
export CFLAGS="--coverage"
force_rebuild
run_unittests
local DIR=coverage
local INDEX=pyahocorasick.html
mkdir ${DIR} 2> /dev/null
gcovr --html-details -o ${DIR}/${INDEX}
echo "Navigate to ${DIR}/${INDEX}"
}
function handle_release
{
unset ALLOC_FAIL
unset UNITTEST
unset CFLAGS
# 1. build with default settings and run unit tests and unpickle tests
if true
then
force_rebuild > /dev/null 2>&1
run_unittests
run_unpickletests
fi
# 2. build with memory debug and run unit tests and unpickle tests
if true
then
export CFLAGS="${MEMORY_DEBUG}"
force_rebuild > /dev/null 2>&1
rm -f ${MEMORY_DUMP_PATH}
run_unittests
run_leaktest
rm -f ${MEMORY_DUMP_PATH}
run_unpickletests
run_leaktest
fi
# 3. inject malloc faults
if true
then
export CFLAGS="${MEMORY_DEBUG}"
force_rebuild > /dev/null 2>&1
run_mallocfaults
fi
echo -e "${GREEN}All OK${RESET}"
}
###################################################
arg=$1
case "${arg}"
in
unit)
handle_unit
;;
unpickle)
handle_unpickle
;;
leaks)
handle_leaks
;;
valgrind)
handle_valgrind
;;
mallocfaults)
handle_mallocfaults
;;
reallocfaults)
handle_reallocfaults
;;
pycallfaults)
handle_pycallfaults
;;
coverage)
handle_coverage
;;
release)
handle_release
;;
*)
echo "Unknown action '${arg}'"
usage
exit 2
;;
esac
|