| #!/bin/bash |
| # |
| # 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. |
| |
| if [ "${TESTPATCHDEBUG}" == "true" ] ; then |
| set -x |
| fi |
| |
| TASKNAME="TESTS" |
| OP="" |
| TEMPDIR="" |
| REPORTDIR="" |
| SUMMARYFILE="" |
| SUMMARYFILEFULL="" |
| STDOUT="/dev/null" |
| MVNPASSTHRU="" |
| FLAKY_LIMIT=10 |
| |
| ############################################################################### |
| cleanupAndExit() { |
| exit "$1" |
| } |
| |
| printUsage() { |
| echo "Usage: $0 --taskname | (--op=pre|post|report --tempdir=<TEMP DIR> --reportdir=<REPORT DIR> --summaryfile=<SUMMARY FILE> --summaryfile-full=<FULL SUMMARY FILE>) [--verbose] [-D<VALUE>...] [-P<VALUE>...]" |
| echo |
| } |
| |
| parseArgs() { |
| for i in "$@" |
| do |
| case $i in |
| --taskname) |
| echo ${TASKNAME} |
| exit 0 |
| ;; |
| --op=*) |
| OP=${i#*=} |
| ;; |
| --tempdir=*) |
| TEMPDIR=${i#*=} |
| ;; |
| --reportdir=*) |
| REPORTDIR=${i#*=} |
| ;; |
| --summaryfile=*) |
| SUMMARYFILE=${i#*=} |
| ;; |
| --summaryfile-full=*) |
| SUMMARYFILEFULL=${i#*=} |
| ;; |
| --verbose) |
| STDOUT="/dev/stdout" |
| ;; |
| -D*) |
| MVNPASSTHRU="${MVNPASSTHRU} $i" |
| ;; |
| -P*) |
| MVNPASSTHRU="${MVNPASSTHRU} $i" |
| ;; |
| esac |
| done |
| if [[ "${TASKNAME}" == "" || "${OP}" == "" || "${TEMPDIR}" == "" || "${REPORTDIR}" == "" || "${SUMMARYFILE}" == "" ]] ; then |
| echo "Missing options" |
| echo |
| printUsage |
| cleanupAndExit 1 |
| fi |
| if [[ "${OP}" != "pre" && "${OP}" != "post" && "${OP}" != "report" ]] ; then |
| echo "Invalid operation" |
| echo |
| printUsage |
| cleanupAndExit 1 |
| fi |
| } |
| |
| count_test_result_type() { |
| local type=$1 |
| find . -name "TEST-*.xml" -exec sh -c 'xmllint --xpath "string(//testsuite/@'"${type}"')" $1; echo' _ {} \; \ |
| | awk 'BEGIN {count=0} {count=count+$1} END {print count}'; |
| } |
| |
| run_tests() { |
| export MAVEN_OPTS="${MVNPASSTHRU} \ |
| -Dmaven.test.failure.ignore=true \ |
| -Doozie.test.waitfor.ratio=3 \ |
| -Dtest.timeout=7200 \ |
| -Dsurefire.rerunFailingTestsCount=1 \ |
| ${MAVEN_OPTS}" |
| mvn test -fae | tee "${TEMPDIR}/${TASKNAME}.out" >> "$STDOUT" |
| local exitCode=${PIPESTATUS[0]} |
| |
| grep -b1 -e "Run .: PASS" "${TEMPDIR}/${TASKNAME}.out" | grep Test | cut -d':' -f2 \ |
| | awk '{print $1}' > "${TEMPDIR}/${TASKNAME}.flakies.out" |
| |
| if [ "${PIPESTATUS[0]}" -eq "0" ]; then |
| isTestsFlaky=1 |
| sed -i -e "s/\./#/g" "${TEMPDIR}/${TASKNAME}.flakies.out" |
| else |
| isTestsFlaky=0 |
| fi |
| |
| if [ "${isTestsFlaky}" -eq "1" ]; then |
| echo "There are flaky tests." |
| else |
| rm "${TEMPDIR}/${TASKNAME}.flakies.out" |
| |
| if [ "${exitCode}" -eq 0 ]; then |
| echo "All tests passed." |
| else |
| echo "Tests failed." |
| fi |
| fi |
| |
| echo "$exitCode" > "${TEMPDIR}/${TASKNAME}.exitCode" |
| } |
| |
| print_flakies() { |
| if [ -f "${TEMPDIR}/${TASKNAME}.flakies.out" ]; then |
| echo ". {color:orange}Tests failed at first run:{color}" |
| local limit="${1}" |
| if [ -n "${limit}" ]; then |
| cat "${TEMPDIR}/${TASKNAME}.flakies.out" | head -"${limit}" |
| echo ". For the complete list of flaky tests, see TEST-SUMMARY-FULL files." |
| else |
| cat "${TEMPDIR}/${TASKNAME}.flakies.out" |
| fi |
| fi |
| } |
| |
| print_flakies_short() { |
| print_flakies "${FLAKY_LIMIT}" |
| } |
| |
| print_flakies_full() { |
| print_flakies |
| } |
| |
| generate_report() { |
| failedTests=$(find . -name '*\.txt' | grep target/surefire-reports | xargs grep "<<< FAILURE" | grep -v "Tests run:" | sed 's/.*\.txt\://' | sed 's/ .*//') |
| testsWithError=$(find . -name '*\.txt' | grep target/surefire-reports | xargs grep "<<< ERROR" | grep -v "Tests run:" | sed 's/.*\.txt\://' | sed 's/ .*//') |
| testsRun=$(grep "Tests run:" "${TEMPDIR}/${TASKNAME}.out" | grep -v " Time elapsed:" | awk '{print $4}' | sed 's/,//' | awk 'BEGIN {count=0} {count=count+$1} END {print count}') |
| testsFailed=$(count_test_result_type failures) |
| testsErrors=$(count_test_result_type errors) |
| hasFailures=$((testsFailed + testsErrors)) |
| testsExitCode=$(cat "${TEMPDIR}/${TASKNAME}.exitCode") |
| |
| if [[ ${hasFailures} != 0 ]] ; then |
| echo "{color:red}-1 ${TASKNAME}{color}" |
| echo ". Tests run: $testsRun" |
| echo ". Tests failed: $testsFailed" |
| echo ". Tests errors: $testsErrors" |
| echo "" |
| echo ". The patch failed the following testcases:" |
| echo "" |
| echo "${failedTests//#/. /}" |
| echo "" |
| echo ". Tests failing with errors:" |
| echo "${testsWithError//#/. /}" |
| echo "" |
| else |
| if [[ "${testsExitCode}" != "0" ]] ; then |
| echo "{color:red}-1 ${TASKNAME}{color} - patch does not compile, cannot run testcases" |
| else |
| echo "{color:green}+1 ${TASKNAME}{color}" |
| echo ". Tests run: $testsRun" |
| fi |
| fi |
| } |
| ############################################################################### |
| |
| parseArgs "$@" |
| |
| case $OP in |
| pre) |
| ;; |
| post) |
| run_tests |
| ;; |
| report) |
| generate_report >> "${SUMMARYFILE}" |
| print_flakies_short >> "${SUMMARYFILE}" |
| print_flakies_full >> "${SUMMARYFILEFULL}" |
| ;; |
| esac |
| |
| exit 0 |