github.com/fcwu/docker@v1.4.2-0.20150115145920-2a69ca89f0df/project/make/test-unit (about)

     1  #!/bin/bash
     2  set -e
     3  
     4  DEST=$1
     5  : ${PARALLEL_JOBS:=$(nproc)}
     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="$LDFLAGS $LDFLAGS_STATIC_DOCKER"
    27  			export TESTFLAGS
    28  			export HAVE_GO_TEST_COVER
    29  			export DEST
    30  			if command -v parallel &> /dev/null; then
    31  				# accomodate parallel to be able to access variables
    32  				export SHELL="$BASH"
    33  				export HOME="$(mktemp -d)"
    34  				mkdir -p "$HOME/.parallel"
    35  				touch "$HOME/.parallel/ignored_vars"
    36  
    37  				# some hack to export array variables
    38  				export BUILDFLAGS_FILE="$HOME/buildflags_file"
    39  				( IFS=$'\n'; echo "${BUILDFLAGS[*]}" ) > "$BUILDFLAGS_FILE"
    40  
    41  				echo "$TESTDIRS" | parallel --jobs "$PARALLEL_JOBS" --halt 2 --env _ "$(dirname "$BASH_SOURCE")/.go-compile-test-dir"
    42  				rm -rf "$HOME"
    43  			else
    44  				# aww, no "parallel" available - fall back to boring
    45  				for test_dir in $TESTDIRS; do
    46  					"$(dirname "$BASH_SOURCE")/.go-compile-test-dir" "$test_dir"
    47  				done
    48  			fi
    49  		)
    50  		echo "$TESTDIRS" | go_run_test_dir
    51  	}
    52  }
    53  
    54  go_run_test_dir() {
    55  	TESTS_FAILED=()
    56  	while read dir; do
    57  		echo
    58  		echo '+ go test' $TESTFLAGS "${DOCKER_PKG}${dir#.}"
    59  		precompiled="$DEST/precompiled/$dir.test"
    60  		if ! ( cd "$dir" && "$precompiled" $TESTFLAGS ); then
    61  			TESTS_FAILED+=("$dir")
    62  			echo
    63  			echo "${RED}Tests failed: $dir${TEXTRESET}"
    64  			sleep 1 # give it a second, so observers watching can take note
    65  		fi
    66  	done
    67  
    68  	echo
    69  	echo
    70  	echo
    71  
    72  	# if some tests fail, we want the bundlescript to fail, but we want to
    73  	# try running ALL the tests first, hence TESTS_FAILED
    74  	if [ "${#TESTS_FAILED[@]}" -gt 0 ]; then
    75  		echo "${RED}Test failures in: ${TESTS_FAILED[@]}${TEXTRESET}"
    76  		echo
    77  		false
    78  	else
    79  		echo "${GREEN}Test success${TEXTRESET}"
    80  		echo
    81  		true
    82  	fi
    83  }
    84  
    85  exec > >(tee -a $DEST/test.log) 2>&1
    86  bundle_test_unit