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