|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# This script runs Go language unit tests for the image-inspector repository. Arguments to this script |
| 4 | +# are parsed as a list of packages to test until the first argument starting with '-' or '--' is |
| 5 | +# found. That argument and all following arguments are interpreted as flags to be passed directly |
| 6 | +# to `go test`. If no arguments are given, then "all" packages are tested. |
| 7 | +# |
| 8 | +# Coverage reports and jUnit XML reports can be generated by this script as well, but both cannot |
| 9 | +# be generated at once. |
| 10 | +# |
| 11 | +# This script consumes the following parameters as environment variables: |
| 12 | +# - DRY_RUN: prints all packages that would be tested with the args that would be used and exits |
| 13 | +# - TIMEOUT: the timeout for any one unit test (default '60s') |
| 14 | +# - DETECT_RACES: toggles the 'go test' race detector (defaults '-race') |
| 15 | +# - COVERAGE_SPEC: a set of flags for 'go test' that specify the coverage behavior (default '-cover -covermode=atomic') |
| 16 | +# - GOTEST_FLAGS: any other flags to be sent to 'go test' |
| 17 | +set -o errexit |
| 18 | +set -o nounset |
| 19 | +set -o pipefail |
| 20 | + |
| 21 | +function exit_trap() { |
| 22 | + local return_code=$? |
| 23 | + echo "[DEBUG] Exit trap handler got return code ${return_code}" |
| 24 | + |
| 25 | + end_time=$(date +%s) |
| 26 | + |
| 27 | + if [[ "${return_code}" -eq "0" ]]; then |
| 28 | + verb="succeeded" |
| 29 | + else |
| 30 | + verb="failed" |
| 31 | + fi |
| 32 | + |
| 33 | + echo "$0 ${verb} after $((${end_time} - ${start_time})) seconds" |
| 34 | + exit "${return_code}" |
| 35 | +} |
| 36 | + |
| 37 | +trap exit_trap EXIT |
| 38 | + |
| 39 | +start_time=$(date +%s) |
| 40 | +CODE_ROOT=$(dirname "${BASH_SOURCE}")/.. |
| 41 | +source "${CODE_ROOT}/hack/common.sh" |
| 42 | +source "${CODE_ROOT}/hack/util.sh" |
| 43 | +cd "${CODE_ROOT}" |
| 44 | +ii::log::install_errexit |
| 45 | +ii::build::setup_env |
| 46 | + |
| 47 | +# Internalize environment variables we consume and default if they're not set |
| 48 | +dry_run="${DRY_RUN:-}" |
| 49 | +test_timeout="${TIMEOUT:-120s}" |
| 50 | +detect_races="${DETECT_RACES:-true}" |
| 51 | +coverage_spec="${COVERAGE_SPEC:--cover -covermode atomic}" |
| 52 | +gotest_flags="${GOTEST_FLAGS:-}" |
| 53 | + |
| 54 | +# determine if user wanted verbosity |
| 55 | +verbose= |
| 56 | +if [[ "${gotest_flags}" =~ -v( |$) ]]; then |
| 57 | + verbose=true |
| 58 | +fi |
| 59 | + |
| 60 | +# Build arguments for 'go test' |
| 61 | +if [[ -z "${verbose}" ]]; then |
| 62 | + gotest_flags+=" -v" |
| 63 | +fi |
| 64 | + |
| 65 | +if [[ "${detect_races}" == "true" ]]; then |
| 66 | + gotest_flags+=" -race" |
| 67 | +fi |
| 68 | + |
| 69 | +# check to see if user has not disabled coverage mode |
| 70 | +if [[ -n "${coverage_spec}" ]]; then |
| 71 | + # if we have a coverage spec set, we add it. '-race' implies '-cover -covermode atomic' |
| 72 | + # but specifying both at the same time does not lead to an error so we can add both specs |
| 73 | + gotest_flags+=" ${coverage_spec}" |
| 74 | +fi |
| 75 | + |
| 76 | +# check to see if user has not disabled test timeouts |
| 77 | +if [[ -n "${test_timeout}" ]]; then |
| 78 | + gotest_flags+=" -timeout ${test_timeout}" |
| 79 | +fi |
| 80 | + |
| 81 | +# list_test_packages_under lists all packages containing Golang test files that we want to run as unit tests |
| 82 | +# under the given base dir in the OpenShift Origin tree |
| 83 | +function list_test_packages_under() { |
| 84 | + local basedir=$@ |
| 85 | + |
| 86 | + # we do not quote ${basedir} to allow for multiple arguments to be passed in as well as to allow for |
| 87 | + # arguments that use expansion, e.g. paths containing brace expansion or wildcards |
| 88 | + find ${basedir} -not \( \ |
| 89 | + \( \ |
| 90 | + -path 'Godeps' \ |
| 91 | + -o -path '*_output' \ |
| 92 | + -o -path '*.git' \ |
| 93 | + -o -path '*Godeps/*' \ |
| 94 | + -o -path '*test/*' \ |
| 95 | + \) -prune \ |
| 96 | + \) -name '*_test.go' | xargs -n1 dirname | sort -u | xargs -n1 printf "${II_GO_PACKAGE}/%s\n" |
| 97 | +} |
| 98 | + |
| 99 | +# Break up the positional arguments into packages that need to be tested and arguments that need to be passed to `go test` |
| 100 | +package_args= |
| 101 | +for arg in "$@"; do |
| 102 | + if [[ "${arg}" =~ -.* ]]; then |
| 103 | + # we found an arg that begins with a dash, so we stop interpreting arguments |
| 104 | + # henceforth as packages and instead interpret them as flags to give to `go test` |
| 105 | + break |
| 106 | + fi |
| 107 | + # an arg found before the first flag is a package |
| 108 | + package_args+=" ${arg}" |
| 109 | + shift |
| 110 | +done |
| 111 | +gotest_flags+=" $*" |
| 112 | + |
| 113 | +# Determine packages to test |
| 114 | +godeps_package_prefix="Godeps/_workspace/src/" |
| 115 | +test_packages= |
| 116 | +if [[ -n "${package_args}" ]]; then |
| 117 | + for package in ${package_args}; do |
| 118 | + # If we're trying to recursively test a package under Godeps, strip the Godeps prefix so go test can find the packages correctly |
| 119 | + if [[ "${package}" == "${godeps_package_prefix}"*"/..." ]]; then |
| 120 | + test_packages="${test_packages} ${package:${#godeps_package_prefix}}" |
| 121 | + else |
| 122 | + test_packages="${test_packages} ${II_GO_PACKAGE}/${package}" |
| 123 | + fi |
| 124 | + done |
| 125 | +else |
| 126 | + # If no packages are given to test, we need to generate a list of all packages with unit tests |
| 127 | + ii_test_packages="$(list_test_packages_under '*')" |
| 128 | + test_packages="${ii_test_packages}" |
| 129 | +fi |
| 130 | + |
| 131 | +go test ${gotest_flags} ${test_packages} |
0 commit comments