Files
bind9/lib/unit-test-driver.sh.in
Michał Kępień 6bdd55a9b3 Enforce a run time limit on unit test binaries
When a unit test binary hangs, the GitLab CI job in which it is run is
stuck until its run time limit is exceeded.  Furthermore, it is not
trivial to determine which test(s) hung in a given GitLab CI job based
on its log.  To prevent these issues, enforce a run time limit on every
binary executed by the lib/unit-test-driver.sh script.  Use a timeout of
5 minutes for consistency with older BIND 9 branches, which employed
Kyua for running unit tests.  Report an exit code of 124 when the run
time limit is exceeded for a unit test binary, for consistency with the
"timeout" tool included in GNU coreutils.
2021-04-07 11:41:45 +02:00

50 lines
1.5 KiB
Bash

#!/bin/sh
TOP_BUILDDIR=@abs_top_builddir@
TOP_SRCDIR=@abs_top_srcdir@
if [ -z "${1}" ]; then
echo "Usage: ${0} test_program" >&2
exit 1
fi
TEST_PROGRAM="${1}"
TIMEOUT=300
"${TEST_PROGRAM}" &
TEST_PROGRAM_PID=${!}
STATUS=124
while [ ${TIMEOUT} -gt 0 ]; do
if ! kill -0 "${TEST_PROGRAM_PID}" 2>/dev/null; then
wait "${TEST_PROGRAM_PID}"
STATUS=${?}
break
fi
sleep 1
TIMEOUT=$((TIMEOUT - 1))
done
if [ ${TIMEOUT} -eq 0 ]; then
echo "PID ${TEST_PROGRAM_PID} exceeded run time limit, sending SIGKILL" >&2
kill -KILL "${TEST_PROGRAM_PID}" 2>/dev/null
fi
TEST_PROGRAM_NAME=$(basename "${TEST_PROGRAM}")
TEST_PROGRAM_WORK_DIR=$(dirname "${TEST_PROGRAM}")
find "${TEST_PROGRAM_WORK_DIR}" -name 'core*' -or -name '*.core' | while read -r CORE_DUMP; do
BINARY=$(gdb --batch --core="${CORE_DUMP}" 2>/dev/null | sed -n "s/^Core was generated by \`\(.*\)'\.\$/\1/p")
if ! echo "${BINARY}" | grep -q "${TEST_PROGRAM_NAME}\$"; then
continue
fi
echo "I:${TEST_PROGRAM_NAME}:Core dump found: ${CORE_DUMP}"
echo "D:${TEST_PROGRAM_NAME}:backtrace from ${CORE_DUMP} start"
"${TOP_BUILDDIR}/libtool" --mode=execute gdb \
--batch \
--command="${TOP_SRCDIR}/bin/tests/system/run.gdb" \
--core="${CORE_DUMP}" \
-- \
"${BINARY}"
echo "D:${TEST_PROGRAM_NAME}:backtrace from ${CORE_DUMP} end"
done
exit ${STATUS}