1#!/bin/bash 2 3# Copyright 2014 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17readonly PREFIX="#####" 18readonly RETRIES=2 19test_prefix= 20 21# The tests currently have hundreds of ResourceWarnings that make it hard 22# to see errors/failures. Disable this warning for now. 23export PYTHONWARNINGS="ignore::ResourceWarning" 24 25function checkArgOrExit() { 26 if [[ $# -lt 2 ]]; then 27 echo "Missing argument for option $1" >&2 28 exit 1 29 fi 30} 31 32function usageAndExit() { 33 cat >&2 << EOF 34 all_tests.sh - test runner with support for flake testing 35 36 all_tests.sh [options] 37 38 options: 39 -h, --help show this menu 40 -p, --prefix=TEST_PREFIX specify a prefix for the tests to be run 41EOF 42 exit 0 43} 44 45function maybePlural() { 46 # $1 = integer to use for plural check 47 # $2 = singular string 48 # $3 = plural string 49 if [ $1 -ne 1 ]; then 50 echo "$3" 51 else 52 echo "$2" 53 fi 54} 55 56function runTest() { 57 local cmd="$1" 58 59 if $cmd; then 60 return 0 61 fi 62 63 for iteration in $(seq $RETRIES); do 64 if ! $cmd; then 65 echo >&2 "'$cmd' failed more than once, giving up" 66 return 1 67 fi 68 done 69 70 echo >&2 "Warning: '$cmd' FLAKY, passed $RETRIES/$((RETRIES + 1))" 71} 72 73# Parse arguments 74while [ -n "$1" ]; do 75 case "$1" in 76 -h|--help) 77 usageAndExit 78 ;; 79 -p|--prefix) 80 checkArgOrExit $@ 81 test_prefix=$2 82 shift 2 83 ;; 84 *) 85 echo "Unknown option $1" >&2 86 echo >&2 87 usageAndExit 88 ;; 89 esac 90done 91 92# Find the relevant tests 93if [[ -z $test_prefix ]]; then 94 readonly tests=$(eval "find . -name '*_test.py' -type f -executable") 95else 96 readonly tests=$(eval "find . -name '$test_prefix*' -type f -executable") 97fi 98 99# Give some readable status messages 100readonly count=$(echo $tests | wc -w) 101if [[ -z $test_prefix ]]; then 102 echo "$PREFIX Found $count $(maybePlural $count test tests)." 103else 104 echo "$PREFIX Found $count $(maybePlural $count test tests) with prefix $test_prefix." 105fi 106 107exit_code=0 108 109i=0 110for test in $tests; do 111 i=$((i + 1)) 112 echo "" 113 echo "$PREFIX $test ($i/$count)" 114 echo "" 115 runTest $test || exit_code=$(( exit_code + 1 )) 116 echo "" 117done 118 119echo "$PREFIX $exit_code failed $(maybePlural $exit_code test tests)." 120exit $exit_code 121