Add option to generate junit xml reports

This commit is contained in:
Rajalakshmi-Girish
2021-06-15 19:18:48 +00:00
parent 56678038b5
commit 8bfce5dc01
4 changed files with 88 additions and 1 deletions

View File

@@ -212,6 +212,39 @@ function run_for_modules {
fi
}
junitFilenamePrefix() {
if [[ -z "${JUNIT_REPORT_DIR}" ]]; then
echo ""
return
fi
mkdir -p "${JUNIT_REPORT_DIR}"
DATE=$( date +%s | base64 | head -c 15 )
echo "${JUNIT_REPORT_DIR}/junit_$DATE"
}
function produce_junit_xmlreport {
local -r junit_filename_prefix=$1
if [[ -z "${junit_filename_prefix}" ]]; then
return
fi
local junit_xml_filename
junit_xml_filename="${junit_filename_prefix}.xml"
if ! command -v gotestsum >/dev/null 2>&1; then
log_callout "gotestsum not found; installing now"
pushd "${ETCD_ROOT_DIR}/tools/mod" >/dev/null || return
GO111MODULE=on go install gotest.tools/gotestsum
popd >/dev/null || return
fi
gotestsum --junitfile "${junit_xml_filename}" --raw-command cat "${junit_filename_prefix}"*.stdout
if [ "${VERBOSE}" != "1" ]; then
rm "${junit_filename_prefix}"*.stdout
fi
log_callout "Saved JUnit XML test report to ${junit_xml_filename}"
}
#### Running go test ########
@@ -236,11 +269,33 @@ function go_test {
local packages="${1}"
local mode="${2}"
local flags_for_package_func="${3}"
local junit_filename_prefix
shift 3
local goTestFlags=""
local goTestEnv=""
##### Create a junit-style XML test report in this directory if set. #####
JUNIT_REPORT_DIR=${JUNIT_REPORT_DIR:-}
# If JUNIT_REPORT_DIR is unset, and ARTIFACTS is set, then have them match.
if [[ -z "${JUNIT_REPORT_DIR:-}" && -n "${ARTIFACTS:-}" ]]; then
export JUNIT_REPORT_DIR="${ARTIFACTS}"
fi
# Used to filter verbose test output.
go_test_grep_pattern=".*"
if [[ -n "${JUNIT_REPORT_DIR}" ]] ; then
goTestFlags+="-v "
goTestFlags+="-json "
# Show only summary lines by matching lines like "status package/test"
go_test_grep_pattern="^[^[:space:]]\+[[:space:]]\+[^[:space:]]\+/[^[[:space:]]\+"
fi
junit_filename_prefix=$(junitFilenamePrefix)
if [ "${VERBOSE}" == "1" ]; then
goTestFlags="-v"
fi
@@ -269,13 +324,15 @@ function go_test {
local cmd=( go test ${goTestFlags} ${additional_flags} "$@" ${pkg} )
# shellcheck disable=SC2086
if ! run env ${goTestEnv} "${cmd[@]}" ; then
if ! run env ${goTestEnv} "${cmd[@]}" | tee ${junit_filename_prefix:+"${junit_filename_prefix}.stdout"} | grep --binary-files=text "${go_test_grep_pattern}" ; then
if [ "${mode}" != "keep_going" ]; then
produce_junit_xmlreport "${junit_filename_prefix}"
return 2
else
failures=("${failures[@]}" "${pkg}")
fi
fi
produce_junit_xmlreport "${junit_filename_prefix}"
done
if [ -n "${failures[*]}" ] ; then