summaryrefslogtreecommitdiffstats
path: root/unit/unittest.sh.in
blob: b707e702f53ad5392d2ef949c8a44714b9f7933f (plain)
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
#!/bin/sh

# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
#
# SPDX-License-Identifier: MPL-2.0
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0.  If a copy of the MPL was not distributed with this
# file, you can obtain one at https://mozilla.org/MPL/2.0/.
#
# See the COPYRIGHT file distributed with this work for additional
# information regarding copyright ownership.

# Find the top of the BIND9 tree.
export TOP=@abs_top_builddir@
KYUA=@KYUA@
CMOCKA_MESSAGE_OUTPUT=TAP
export CMOCKA_MESSAGE_OUTPUT
GDB="$(command -v gdb)"

kyua_report() {
	${KYUA} --logfile /dev/null report --results-file "${KYUA_RESULT:-LATEST}"
}

clear_kyua_work_dir() {
	KYUA_WORK_DIR="$(grep -i -m1 "failed" "${1}" | sed -n 's|.*\(/tmp/kyua\.[A-Za-z0-9]*\).*|\1|p')"
	if [ -n "${CI}" ] && [ -d "${KYUA_WORK_DIR}" ]; then
		find "${KYUA_WORK_DIR}" \( -name 'core*' -o -name '*.core' \) -exec mv -v {} . \;
		rm -rf "${KYUA_WORK_DIR}"
	fi
}

if [ -z "${KYUA}" ]; then
	exit 0
fi

echo "S:unit:$(date)"
echo "T:unit:1:A"
echo "I:unit tests (using kyua)"

${KYUA} -v parallelism="${TEST_PARALLEL_JOBS:-1}" --logfile kyua.log --loglevel debug test --results-file "${KYUA_RESULT:-NEW}"
status=$?

kyua_report

clear_kyua_work_dir kyua.log

# Use kyua-debug(1) facility to gather additional data on failed tests.
# Some runs will just show verbose information from the run, some will
# show backtrace via gdb(1).
USER_ID=$(id -u)
BROKEN_TESTS=$(kyua_report | awk '$2 == "->" && ( $3 == "broken:" || $3 == "failed:" ) { print $1 }')
# Conditions for getting kyua debug info and GDB backtrace: runs under CI
# (safety), GDB present, root privileges, failed tests.
if [ -n "${CI}" ] && [ -n "${GDB}" ] && [ "${USER_ID:-1}" -eq 0 ] && [ -n "${BROKEN_TESTS}" ]; then
	if [ "$(uname -s)" = "Linux" ] && ! sysctl -n "kernel.core_pattern" | grep -xq "core.%p"; then
		echo "I:*** kernel.core_pattern is not set to 'core.%p'"
		echo "I:*** kyua may not be able to find core dumps for broken tests"
	fi
	if [ "$(uname -s)" = "FreeBSD" ] && ! sysctl -n "kern.corefile" | grep -xq "core.%P"; then
		echo "I:*** kern.corefile is not set to 'core.%P'"
		echo "I:*** kyua may not be able to find core dumps for broken tests"
	fi
	if grep '^#define USE_LIBTOOL 1$' "${TOP}/config.h" >/dev/null; then
		# kyua debug command misidentifies broken binaries when libtool
		# is used (see https://github.com/jmmv/kyua/issues/207).
		# Here we try to "trick" kyua to use our custom gdb script instead
		# of using gdb(1) directly. Hence this part needs to be run as root
		# and, for safety reasons, only in the CI.
		mv "${GDB}" "${GDB}.orig"
		cp "${TOP}/unit/gdb" "${GDB}"
	fi
	i=1
	for test in ${BROKEN_TESTS}; do
		echo
		echo "----- $test -----"
		KYUA_DEBUG_LOG="kyua.debug.log.${i}"
		${KYUA} debug "${test}" 2>&1 | tee "${KYUA_DEBUG_LOG}"
		clear_kyua_work_dir "${KYUA_DEBUG_LOG}"
		i=$((i + 1))
	done
	if grep '^#define USE_LIBTOOL 1$' "${TOP}/config.h" >/dev/null; then
		mv "${GDB}.orig" "${GDB}"
	fi
fi

if [ "${status}" -eq 0 ]
then
	rm -f kyua.log
	echo "R:PASS"
else
	echo "R:FAIL:status:${status}"
fi
echo "E:unit:$(date)"

exit ${status}