2023-08-31 11:43:23 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -o pipefail
|
|
|
|
|
|
|
|
GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel)
|
|
|
|
|
|
|
|
source "${GIT_ROOT}/_assets/scripts/colors.sh"
|
|
|
|
|
2023-11-21 15:39:11 +00:00
|
|
|
if [[ $UNIT_TEST_FAILFAST == 'true' ]]; then
|
2023-11-22 16:48:04 +00:00
|
|
|
GOTEST_EXTRAFLAGS="${GOTEST_EXTRAFLAGS} -failfast"
|
2023-11-21 15:39:11 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z "${UNIT_TEST_COUNT}" ]]; then
|
|
|
|
UNIT_TEST_COUNT=1
|
|
|
|
fi
|
|
|
|
|
2024-01-24 13:25:58 +00:00
|
|
|
UNIT_TEST_PACKAGE_TIMEOUT="$((UNIT_TEST_COUNT * 2))m"
|
|
|
|
UNIT_TEST_PACKAGE_TIMEOUT_EXTENDED="$((UNIT_TEST_COUNT * 30))m"
|
2023-11-22 16:48:04 +00:00
|
|
|
|
2023-11-21 15:39:11 +00:00
|
|
|
redirect_stdout() {
|
|
|
|
output_file=$1
|
|
|
|
|
|
|
|
if [[ "${CI}" == 'true' ]];
|
|
|
|
then
|
|
|
|
cat > "${output_file}";
|
|
|
|
else
|
|
|
|
tee "${output_file}";
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-01-24 13:25:58 +00:00
|
|
|
has_extended_timeout() {
|
|
|
|
local package
|
|
|
|
for package in ${UNIT_TEST_PACKAGES_WITH_EXTENDED_TIMEOUT}; do
|
|
|
|
if [[ "$1" == "${package}" ]]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2023-11-21 15:39:11 +00:00
|
|
|
last_failing_exit_code=0
|
|
|
|
|
2023-08-31 11:43:23 +00:00
|
|
|
for package in ${UNIT_TEST_PACKAGES}; do
|
|
|
|
echo -e "${GRN}Testing:${RST} ${package}"
|
|
|
|
package_dir=$(go list -f "{{.Dir}}" "${package}")
|
|
|
|
output_file=${package_dir}/test.log
|
|
|
|
|
2024-01-24 13:25:58 +00:00
|
|
|
if has_extended_timeout "${package}"; then
|
|
|
|
package_timeout="${UNIT_TEST_PACKAGE_TIMEOUT_EXTENDED}"
|
|
|
|
else
|
|
|
|
package_timeout="${UNIT_TEST_PACKAGE_TIMEOUT}"
|
|
|
|
fi
|
|
|
|
|
2023-11-22 16:48:04 +00:00
|
|
|
go test "${package}" -v ${GOTEST_EXTRAFLAGS} \
|
2024-01-24 13:25:58 +00:00
|
|
|
-timeout "${package_timeout}" \
|
2023-11-22 16:48:04 +00:00
|
|
|
-count "${UNIT_TEST_COUNT}" \
|
|
|
|
-tags "${BUILD_TAGS}" | \
|
2023-11-21 15:39:11 +00:00
|
|
|
redirect_stdout "${output_file}"
|
2023-08-31 11:43:23 +00:00
|
|
|
go_test_exit=$?
|
|
|
|
|
2023-11-21 15:39:11 +00:00
|
|
|
if [[ "${CI}" == 'true' ]]; then
|
2023-08-31 11:43:23 +00:00
|
|
|
go-junit-report -in "${output_file}" -out "${package_dir}"/report.xml
|
|
|
|
fi
|
|
|
|
|
2023-11-21 15:39:11 +00:00
|
|
|
if [[ "${go_test_exit}" -ne 0 ]]; then
|
2023-11-22 10:37:05 +00:00
|
|
|
if [[ "${CI}" == 'true' ]]; then
|
|
|
|
echo -e "${YLW}Failed, see the log:${RST} ${BLD}${output_file}${RST}"
|
|
|
|
fi
|
|
|
|
|
2023-11-21 15:39:11 +00:00
|
|
|
if [[ "$UNIT_TEST_FAILFAST" == 'true' ]]; then
|
|
|
|
exit "${go_test_exit}"
|
|
|
|
fi
|
2023-11-22 10:37:05 +00:00
|
|
|
|
2023-11-21 15:39:11 +00:00
|
|
|
last_failing_exit_code="${go_test_exit}"
|
2023-08-31 11:43:23 +00:00
|
|
|
fi
|
|
|
|
done
|
2023-11-21 15:39:11 +00:00
|
|
|
|
|
|
|
if [[ "${last_failing_exit_code}" -ne 0 ]]; then
|
|
|
|
if [[ "${UNIT_TEST_COUNT}" -gt 1 ]]; then
|
2023-11-22 10:37:05 +00:00
|
|
|
mkdir -p "${GIT_ROOT}/reports"
|
|
|
|
"${GIT_ROOT}/_assets/scripts/test_stats.py" | redirect_stdout "${GIT_ROOT}/reports/test_stats.txt"
|
2023-11-21 15:39:11 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
exit "${last_failing_exit_code}"
|
|
|
|
fi
|