Skip to content

Commit

Permalink
Add stdout to default XML file and generate XML file on timeout
Browse files Browse the repository at this point in the history
This should fix #1027 and get better error result on Jenkins.

Change-Id: I5ce30b64f634e01dd350af10748c4a9455a6bea8
PiperOrigin-RevId: 162474168
  • Loading branch information
damienmg authored and aehlig committed Jul 19, 2017
1 parent 29dc6f8 commit b8514f5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 27 deletions.
27 changes: 27 additions & 0 deletions src/test/shell/bazel/bazel_test_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,33 @@ EOF
[ -s $xml_log ] || fail "$xml_log was not present after test"
}

# Tests that the test.xml is here in case of timeout
function test_xml_is_present_when_timingout() {
mkdir -p dir

cat <<'EOF' > dir/test.sh
#!/bin/sh
sleep 10
EOF

chmod +x dir/test.sh

cat <<'EOF' > dir/BUILD
sh_test(
name = "test",
srcs = [ "test.sh" ],
)
EOF

bazel test -s --test_timeout=1 \
//dir:test &> $TEST_log && fail "should have failed" || true

xml_log=bazel-testlogs/dir/test/test.xml
[ -s "${xml_log}" ] || fail "${xml_log} was not present after test"
cat "${xml_log}" > $TEST_log
expect_log '"Timed out"'
}

# Check that fallback xml output is correctly generated for sharded tests.
function test_xml_fallback_for_sharded_test() {
mkdir -p dir
Expand Down
74 changes: 47 additions & 27 deletions tools/test/test-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,42 @@ if [[ -z "$no_echo" ]]; then
echo "-----------------------------------------------------------------------------"
fi

function write_xml_output_file {
local duration=$(expr $(date +%s) - $start)
local errors=0
local error_msg=
local signal="${1-}"
if [ -n "${XML_OUTPUT_FILE-}" -a ! -f "${XML_OUTPUT_FILE-}" ]; then
# Create a default XML output file if the test runner hasn't generated it
if [ -n "${signal}" ]; then
errors=1
if [ "${signal}" = "SIGTERM" ]; then
error_msg="<error message=\"Timed out\"></error>"
else
error_msg="<error message=\"Terminated by signal ${signal}\"></error>"
fi
elif (( $exitCode != 0 )); then
errors=1
error_msg="<error message=\"exited with error code $exitCode\"></error>"
fi
# Ensure that test shards have unique names in the xml output.
if [[ -n "${TEST_TOTAL_SHARDS+x}" ]] && ((TEST_TOTAL_SHARDS != 0)); then
((shard_num=TEST_SHARD_INDEX+1))
TEST_NAME="$TEST_NAME"_shard_"$shard_num"/"$TEST_TOTAL_SHARDS"
fi
cat <<EOF >${XML_OUTPUT_FILE}
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="$TEST_NAME" tests="1" failures="0" errors="${errors}">
<testcase name="$TEST_NAME" status="run" duration="${duration}">${error_msg}</testcase>
<system-out>$(<"${XML_OUTPUT_FILE}.log")</system-out>
</testsuite>
</testsuites>
EOF
fi
rm -f "${XML_OUTPUT_FILE}.log"
}

# The path of this command-line is usually relative to the exec-root,
# but when using --run_under it can be a "/bin/bash -c" command-line.

Expand All @@ -161,37 +197,21 @@ fi
[[ -n "$RUNTEST_PRESERVE_CWD" ]] && EXE="${TEST_NAME}"

exitCode=0
signals="$(trap -l | sed -E 's/[0-9]+\)//g')"
for signal in $signals; do
trap "write_xml_output_file ${signal}" ${signal}
done
start=$(date +%s)

if [ -z "$COVERAGE_DIR" ]; then
"${TEST_PATH}" "$@" || exitCode=$?
"${TEST_PATH}" "$@" 2> >(tee -a "${XML_OUTPUT_FILE}.log" >&2) 1> >(tee -a "${XML_OUTPUT_FILE}.log") 2>&1 || exitCode=$?
else
"$1" "$TEST_PATH" "${@:3}" || exitCode=$?
"$1" "$TEST_PATH" "${@:3}" 2> >(tee -a "${XML_OUTPUT_FILE}.log" >&2) 1> >(tee -a "${XML_OUTPUT_FILE}.log") 2>&1 || exitCode=$?
fi
duration=$(expr $(date +%s) - $start)


if [ -n "${XML_OUTPUT_FILE-}" -a ! -f "${XML_OUTPUT_FILE-}" ]; then
# Create a default XML output file if the test runner hasn't generated it
if (( $exitCode != 0 )); then
errors=1
error_msg="<error message=\"exited with error code $exitCode\"></error>"
else
errors=0
error_msg=
fi
# Ensure that test shards have unique names in the xml output.
if [[ -n "${TEST_TOTAL_SHARDS+x}" ]] && ((TEST_TOTAL_SHARDS != 0)); then
((shard_num=TEST_SHARD_INDEX+1))
TEST_NAME="$TEST_NAME"_shard_"$shard_num"/"$TEST_TOTAL_SHARDS"
fi
cat <<EOF >${XML_OUTPUT_FILE}
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="$TEST_NAME" tests="1" failures="0" errors="$errors">
<testcase name="$TEST_NAME" status="run" duration="$duration">$error_msg</testcase>
</testsuite>
</testsuites>
EOF
fi
for signal in $signals; do
trap - ${signal}
done
write_xml_output_file

exit $exitCode

0 comments on commit b8514f5

Please sign in to comment.