Erik Kline | 1b4e404 | 2015-02-04 17:33:41 +0900 | [diff] [blame] | 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 | |
| 17 | readonly PREFIX="#####" |
Erik Kline | 8dd83bd | 2016-08-26 16:13:36 +0900 | [diff] [blame] | 18 | readonly RETRIES=2 |
Benedict Wong | f23efc0 | 2018-01-09 15:16:01 -0800 | [diff] [blame] | 19 | test_prefix= |
| 20 | |
| 21 | function checkArgOrExit() { |
| 22 | if [[ $# -lt 2 ]]; then |
| 23 | echo "Missing argument for option $1" >&2 |
| 24 | exit 1 |
| 25 | fi |
| 26 | } |
| 27 | |
| 28 | function usageAndExit() { |
| 29 | cat >&2 << EOF |
| 30 | all_tests.sh - test runner with support for flake testing |
| 31 | |
| 32 | all_tests.sh [options] |
| 33 | |
| 34 | options: |
| 35 | -h, --help show this menu |
| 36 | -p, --prefix=TEST_PREFIX specify a prefix for the tests to be run |
| 37 | EOF |
| 38 | exit 0 |
| 39 | } |
Erik Kline | 1b4e404 | 2015-02-04 17:33:41 +0900 | [diff] [blame] | 40 | |
| 41 | function maybePlural() { |
| 42 | # $1 = integer to use for plural check |
| 43 | # $2 = singular string |
| 44 | # $3 = plural string |
| 45 | if [ $1 -ne 1 ]; then |
| 46 | echo "$3" |
| 47 | else |
| 48 | echo "$2" |
| 49 | fi |
| 50 | } |
| 51 | |
Erik Kline | 8dd83bd | 2016-08-26 16:13:36 +0900 | [diff] [blame] | 52 | function runTest() { |
| 53 | local cmd="$1" |
| 54 | |
| 55 | if $cmd; then |
| 56 | return 0 |
| 57 | fi |
| 58 | |
| 59 | for iteration in $(seq $RETRIES); do |
| 60 | if ! $cmd; then |
| 61 | echo >&2 "'$cmd' failed more than once, giving up" |
| 62 | return 1 |
| 63 | fi |
| 64 | done |
| 65 | |
| 66 | echo >&2 "Warning: '$cmd' FLAKY, passed $RETRIES/$((RETRIES + 1))" |
| 67 | } |
| 68 | |
Benedict Wong | f23efc0 | 2018-01-09 15:16:01 -0800 | [diff] [blame] | 69 | # Parse arguments |
| 70 | while [ -n "$1" ]; do |
| 71 | case "$1" in |
| 72 | -h|--help) |
| 73 | usageAndExit |
| 74 | ;; |
| 75 | -p|--prefix) |
| 76 | checkArgOrExit $@ |
| 77 | test_prefix=$2 |
| 78 | shift 2 |
| 79 | ;; |
| 80 | *) |
| 81 | echo "Unknown option $1" >&2 |
| 82 | echo >&2 |
| 83 | usageAndExit |
| 84 | ;; |
| 85 | esac |
| 86 | done |
Erik Kline | 8dd83bd | 2016-08-26 16:13:36 +0900 | [diff] [blame] | 87 | |
Benedict Wong | f23efc0 | 2018-01-09 15:16:01 -0800 | [diff] [blame] | 88 | # Find the relevant tests |
| 89 | if [[ -z $test_prefix ]]; then |
| 90 | readonly tests=$(eval "find . -name '*_test.py' -type f -executable") |
| 91 | else |
| 92 | readonly tests=$(eval "find . -name '$test_prefix*' -type f -executable") |
| 93 | fi |
| 94 | |
| 95 | # Give some readable status messages |
Erik Kline | 1b4e404 | 2015-02-04 17:33:41 +0900 | [diff] [blame] | 96 | readonly count=$(echo $tests | wc -w) |
Benedict Wong | f23efc0 | 2018-01-09 15:16:01 -0800 | [diff] [blame] | 97 | if [[ -z $test_prefix ]]; then |
| 98 | echo "$PREFIX Found $count $(maybePlural $count test tests)." |
| 99 | else |
| 100 | echo "$PREFIX Found $count $(maybePlural $count test tests) with prefix $test_prefix." |
| 101 | fi |
Erik Kline | 1b4e404 | 2015-02-04 17:33:41 +0900 | [diff] [blame] | 102 | |
| 103 | exit_code=0 |
| 104 | |
| 105 | i=0 |
| 106 | for test in $tests; do |
| 107 | i=$((i + 1)) |
| 108 | echo "" |
| 109 | echo "$PREFIX $test ($i/$count)" |
| 110 | echo "" |
Erik Kline | 8dd83bd | 2016-08-26 16:13:36 +0900 | [diff] [blame] | 111 | runTest $test || exit_code=$(( exit_code + 1 )) |
Erik Kline | 1b4e404 | 2015-02-04 17:33:41 +0900 | [diff] [blame] | 112 | echo "" |
| 113 | done |
| 114 | |
| 115 | echo "$PREFIX $exit_code failed $(maybePlural $exit_code test tests)." |
| 116 | exit $exit_code |