github.com/eikeon/docker@v1.5.0-rc4/project/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  			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" --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" || true
    47  					# don't let one directory that fails to build tank _all_ our tests!
    48  				done
    49  			fi
    50  		)
    51  		echo "$TESTDIRS" | go_run_test_dir
    52  	}
    53  }
    54  
    55  go_run_test_dir() {
    56  	TESTS_FAILED=()
    57  	while read dir; do
    58  		echo
    59  		echo '+ go test' $TESTFLAGS "${DOCKER_PKG}${dir#.}"
    60  		precompiled="$DEST/precompiled/$dir.test$(binary_extension)"
    61  		if ! ( cd "$dir" && "$precompiled" $TESTFLAGS ); then
    62  			TESTS_FAILED+=("$dir")
    63  			echo
    64  			echo "${RED}Tests failed: $dir${TEXTRESET}"
    65  			sleep 1 # give it a second, so observers watching can take note
    66  		fi
    67  	done
    68  
    69  	echo
    70  	echo
    71  	echo
    72  
    73  	# if some tests fail, we want the bundlescript to fail, but we want to
    74  	# try running ALL the tests first, hence TESTS_FAILED
    75  	if [ "${#TESTS_FAILED[@]}" -gt 0 ]; then
    76  		echo "${RED}Test failures in: ${TESTS_FAILED[@]}${TEXTRESET}"
    77  		echo
    78  		false
    79  	else
    80  		echo "${GREEN}Test success${TEXTRESET}"
    81  		echo
    82  		true
    83  	fi
    84  }
    85  
    86  bundle_test_unit 2>&1 | tee -a $DEST/test.log