1#!/bin/sh
2
3# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
4#
5# SPDX-License-Identifier: MPL-2.0
6#
7# This Source Code Form is subject to the terms of the Mozilla Public
8# License, v. 2.0.  If a copy of the MPL was not distributed with this
9# file, you can obtain one at https://mozilla.org/MPL/2.0/.
10#
11# See the COPYRIGHT file distributed with this work for additional
12# information regarding copyright ownership.
13
14# Find the top of the BIND9 tree.
15export TOP=@abs_top_builddir@
16KYUA=@KYUA@
17CMOCKA_MESSAGE_OUTPUT=TAP
18export CMOCKA_MESSAGE_OUTPUT
19GDB="$(command -v gdb)"
20
21kyua_report() {
22	${KYUA} --logfile /dev/null report --results-file "${KYUA_RESULT:-LATEST}"
23}
24
25clear_kyua_work_dir() {
26	KYUA_WORK_DIR="$(grep -i -m1 "failed" "${1}" | sed -n 's|.*\(/tmp/kyua\.[A-Za-z0-9]*\).*|\1|p')"
27	if [ -n "${CI}" ] && [ -d "${KYUA_WORK_DIR}" ]; then
28		find "${KYUA_WORK_DIR}" \( -name 'core*' -o -name '*.core' \) -exec mv -v {} . \;
29		rm -rf "${KYUA_WORK_DIR}"
30	fi
31}
32
33if [ -z "${KYUA}" ]; then
34	exit 0
35fi
36
37echo "S:unit:$(date)"
38echo "T:unit:1:A"
39echo "I:unit tests (using kyua)"
40
41${KYUA} -v parallelism="${TEST_PARALLEL_JOBS:-1}" --logfile kyua.log --loglevel debug test --results-file "${KYUA_RESULT:-NEW}"
42status=$?
43
44kyua_report
45
46clear_kyua_work_dir kyua.log
47
48# Use kyua-debug(1) facility to gather additional data on failed tests.
49# Some runs will just show verbose information from the run, some will
50# show backtrace via gdb(1).
51USER_ID=$(id -u)
52BROKEN_TESTS=$(kyua_report | awk '$2 == "->" && ( $3 == "broken:" || $3 == "failed:" ) { print $1 }')
53# Conditions for getting kyua debug info and GDB backtrace: runs under CI
54# (safety), GDB present, root privileges, failed tests.
55if [ -n "${CI}" ] && [ -n "${GDB}" ] && [ "${USER_ID:-1}" -eq 0 ] && [ -n "${BROKEN_TESTS}" ]; then
56	if [ "$(uname -s)" = "Linux" ] && ! sysctl -n "kernel.core_pattern" | grep -xq "core.%p"; then
57		echo "I:*** kernel.core_pattern is not set to 'core.%p'"
58		echo "I:*** kyua may not be able to find core dumps for broken tests"
59	fi
60	if [ "$(uname -s)" = "FreeBSD" ] && ! sysctl -n "kern.corefile" | grep -xq "core.%P"; then
61		echo "I:*** kern.corefile is not set to 'core.%P'"
62		echo "I:*** kyua may not be able to find core dumps for broken tests"
63	fi
64	if grep '^#define USE_LIBTOOL 1$' "${TOP}/config.h" >/dev/null; then
65		# kyua debug command misidentifies broken binaries when libtool
66		# is used (see https://github.com/jmmv/kyua/issues/207).
67		# Here we try to "trick" kyua to use our custom gdb script instead
68		# of using gdb(1) directly. Hence this part needs to be run as root
69		# and, for safety reasons, only in the CI.
70		mv "${GDB}" "${GDB}.orig"
71		cp "${TOP}/unit/gdb" "${GDB}"
72	fi
73	i=1
74	for test in ${BROKEN_TESTS}; do
75		echo
76		echo "----- $test -----"
77		KYUA_DEBUG_LOG="kyua.debug.log.${i}"
78		${KYUA} debug "${test}" 2>&1 | tee "${KYUA_DEBUG_LOG}"
79		clear_kyua_work_dir "${KYUA_DEBUG_LOG}"
80		i=$((i + 1))
81	done
82	if grep '^#define USE_LIBTOOL 1$' "${TOP}/config.h" >/dev/null; then
83		mv "${GDB}.orig" "${GDB}"
84	fi
85fi
86
87if [ "${status}" -eq 0 ]
88then
89	rm -f kyua.log
90	echo "R:PASS"
91else
92	echo "R:FAIL:status:${status}"
93fi
94echo "E:unit:$(date)"
95
96exit ${status}
97