From 0858ae1f6eb890c1e203a2aa21130ba34ca36a27 Mon Sep 17 00:00:00 2001 From: ulfjack Date: Fri, 27 Jul 2018 02:37:53 -0700 Subject: Add a flag to split test.xml generation into a separate Spawn At this time, this is only implemented for the StandaloneTestStrategy. This solves a race condition on Posix-like systems, where we cannot guarantee that the pipes are actually fully flushed to disk when the test process exits, and this can cause the test.xml to be empty, which makes it hard to debug issues. (The test.log files do not show up in normal CI systems, only the test.xml files.) Progress on #4608. PiperOrigin-RevId: 206292179 --- tools/test/BUILD | 6 +++++ tools/test/generate-xml.sh | 55 ++++++++++++++++++++++++++++++++++++++++++++++ tools/test/test-setup.sh | 21 ++++++++++++++---- 3 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 tools/test/generate-xml.sh (limited to 'tools') diff --git a/tools/test/BUILD b/tools/test/BUILD index 4a4e56b284..c6a8311033 100644 --- a/tools/test/BUILD +++ b/tools/test/BUILD @@ -2,6 +2,7 @@ package(default_visibility = ["//visibility:public"]) # Members of this filegroup shouldn't have duplicate basenames, otherwise # TestRunnerAction#getRuntimeArtifact() will get confused. +# Deprecated, do not use. filegroup( name = "runtime", srcs = ["test-setup.sh"], @@ -12,6 +13,11 @@ filegroup( srcs = ["test-setup.sh"], ) +filegroup( + name = "test_xml_generator", + srcs = ["generate-xml.sh"], +) + filegroup( name = "collect_coverage", srcs = ["collect_coverage.sh"], diff --git a/tools/test/generate-xml.sh b/tools/test/generate-xml.sh new file mode 100644 index 0000000000..730b131f38 --- /dev/null +++ b/tools/test/generate-xml.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +# Copyright 2018 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +TEST_LOG="$1" +XML_OUTPUT_FILE="$2" +DURATION_IN_SECONDS="$3" +EXIT_CODE="$4" + +# Keep this in sync with test-setup.sh! +function encode_output_file { + if [[ -f "$1" ]]; then + # Replace invalid XML characters and invalid sequence in CDATA + # cf. https://stackoverflow.com/a/7774512/4717701 + perl -CSDA -pe's/[^\x9\xA\xD\x20-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]+/?/g;' "$1" \ + | sed 's|]]>|]]>]]|g' + fi +} + +test_name="${TEST_BINARY#./}" +errors=0 +error_msg="" +if (( $EXIT_CODE != 0 )); then + errors=1 + 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 <${XML_OUTPUT_FILE} + + + + ${error_msg} + + + +EOF + diff --git a/tools/test/test-setup.sh b/tools/test/test-setup.sh index d8f2a47b67..d7bb931b5f 100755 --- a/tools/test/test-setup.sh +++ b/tools/test/test-setup.sh @@ -152,6 +152,7 @@ if [[ -z "$no_echo" ]]; then echo "-----------------------------------------------------------------------------" fi +# Unused if EXPERIMENTAL_SPLIT_XML_GENERATION is set. function encode_output_file { if [ -f "$1" ]; then # Replace invalid XML characters and invalid sequence in CDATA @@ -161,6 +162,8 @@ function encode_output_file { fi } +# Unused if EXPERIMENTAL_SPLIT_XML_GENERATION is set. +# Keep this in sync with generate-xml.sh! function write_xml_output_file { local duration=$(expr $(date +%s) - $start) local errors=0 @@ -268,17 +271,27 @@ if [ "$has_tail" == true ] && [ -z "$no_echo" ]; then wait $pid exitCode=$? else - if [ -z "$COVERAGE_DIR" ]; then - "${TEST_PATH}" "$@" 2> >(tee -a "${XML_OUTPUT_FILE}.log" >&2) 1> >(tee -a "${XML_OUTPUT_FILE}.log") 2>&1 || exitCode=$? + if [[ "${EXPERIMENTAL_SPLIT_XML_GENERATION}" == "1" ]]; then + if [ -z "$COVERAGE_DIR" ]; then + "${TEST_PATH}" "$@" 2>&1 || exitCode=$? + else + "$1" "$TEST_PATH" "${@:3}" 2>&1 || exitCode=$? + fi else - "$1" "$TEST_PATH" "${@:3}" 2> >(tee -a "${XML_OUTPUT_FILE}.log" >&2) 1> >(tee -a "${XML_OUTPUT_FILE}.log") 2>&1 || exitCode=$? + if [ -z "$COVERAGE_DIR" ]; then + "${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}" 2> >(tee -a "${XML_OUTPUT_FILE}.log" >&2) 1> >(tee -a "${XML_OUTPUT_FILE}.log") 2>&1 || exitCode=$? + fi fi fi for signal in $signals; do trap - ${signal} done -write_xml_output_file +if [[ "${EXPERIMENTAL_SPLIT_XML_GENERATION}" != "1" ]]; then + write_xml_output_file +fi # Add all of the files from the undeclared outputs directory to the manifest. if [[ -n "$TEST_UNDECLARED_OUTPUTS_DIR" && -n "$TEST_UNDECLARED_OUTPUTS_MANIFEST" ]]; then -- cgit v1.2.3