github.com/lafolle/docker@v1.6.0/hack/make/test-unit (about)

     1  #!/bin/bash
     2  set -e
     3  
     4  DEST=$1
     5  : ${PARALLEL_JOBS:=$(nproc 2>/dev/null || echo 1)} # if nproc fails (usually because we don't have it), let's not parallelize by default
     6  
     7  RED=$'\033[31m'
     8  GREEN=$'\033[32m'
     9  TEXTRESET=$'\033[0m' # reset the foreground colour
    10  
    11  # Run Docker's test suite, including sub-packages, and store their output as a bundle
    12  # If $TESTFLAGS is set in the environment, it is passed as extra arguments to 'go test'.
    13  # You can use this to select certain tests to run, eg.
    14  #
    15  #   TESTFLAGS='-run ^TestBuild$' ./hack/make.sh test-unit
    16  #
    17  bundle_test_unit() {
    18  	{
    19  		date
    20  
    21  		# Run all the tests if no TESTDIRS were specified.
    22  		if [ -z "$TESTDIRS" ]; then
    23  			TESTDIRS=$(find_dirs '*_test.go')
    24  		fi
    25  		(
    26  			export LDFLAGS
    27  			export TESTFLAGS
    28  			export HAVE_GO_TEST_COVER
    29  			export DEST
    30  
    31  			# some hack to export array variables
    32  			export BUILDFLAGS_FILE="buildflags_file"
    33  			( IFS=$'\n'; echo "${BUILDFLAGS[*]}" ) > "$BUILDFLAGS_FILE"
    34  
    35  			if command -v parallel &> /dev/null; then
    36  				# accomodate parallel to be able to access variables
    37  				export SHELL="$BASH"
    38  				export HOME="$(mktemp -d)"
    39  				mkdir -p "$HOME/.parallel"
    40  				touch "$HOME/.parallel/ignored_vars"
    41  
    42  				echo "$TESTDIRS" | parallel --jobs "$PARALLEL_JOBS" --env _ "$(dirname "$BASH_SOURCE")/.go-compile-test-dir"
    43  				rm -rf "$HOME"
    44  			else
    45  				# aww, no "parallel" available - fall back to boring
    46  				for test_dir in $TESTDIRS; do
    47  					"$(dirname "$BASH_SOURCE")/.go-compile-test-dir" "$test_dir" || true
    48  					# don't let one directory that fails to build tank _all_ our tests!
    49  				done
    50  			fi
    51  			rm -f "$BUILDFLAGS_FILE"
    52  		)
    53  		echo "$TESTDIRS" | go_run_test_dir
    54  	}
    55  }
    56  
    57  go_run_test_dir() {
    58  	TESTS_FAILED=()
    59  	while read dir; do
    60  		echo
    61  		echo '+ go test' $TESTFLAGS "${DOCKER_PKG}${dir#.}"
    62  		precompiled="$DEST/precompiled/$dir.test$(binary_extension)"
    63  		if ! ( cd "$dir" && test_env "$precompiled" $TESTFLAGS ); then
    64  			TESTS_FAILED+=("$dir")
    65  			echo
    66  			echo "${RED}Tests failed: $dir${TEXTRESET}"
    67  			sleep 1 # give it a second, so observers watching can take note
    68  		fi
    69  	done
    70  
    71  	echo
    72  	echo
    73  	echo
    74  
    75  	# if some tests fail, we want the bundlescript to fail, but we want to
    76  	# try running ALL the tests first, hence TESTS_FAILED
    77  	if [ "${#TESTS_FAILED[@]}" -gt 0 ]; then
    78  		echo "${RED}Test failures in: ${TESTS_FAILED[@]}${TEXTRESET}"
    79  		echo
    80  		false
    81  	else
    82  		echo "${GREEN}Test success${TEXTRESET}"
    83  		echo
    84  		true
    85  	fi
    86  }
    87  
    88  bundle_test_unit 2>&1 | tee -a $DEST/test.log