github.com/amylindburg/docker@v1.7.0/hack/make/test-unit (about)

     1  #!/bin/bash
     2  set -e
     3  
     4  : ${PARALLEL_JOBS:=$(nproc 2>/dev/null || echo 1)} # if nproc fails (usually because we don't have it), let's not parallelize by default
     5  
     6  RED=$'\033[31m'
     7  GREEN=$'\033[32m'
     8  TEXTRESET=$'\033[0m' # reset the foreground colour
     9  
    10  # Run Docker's test suite, including sub-packages, and store their output as a bundle
    11  # If $TESTFLAGS is set in the environment, it is passed as extra arguments to 'go test'.
    12  # You can use this to select certain tests to run, eg.
    13  #
    14  #   TESTFLAGS='-test.run ^TestBuild$' ./hack/make.sh test-unit
    15  #
    16  bundle_test_unit() {
    17  	{
    18  		date
    19  
    20  		# Run all the tests if no TESTDIRS were specified.
    21  		if [ -z "$TESTDIRS" ]; then
    22  			TESTDIRS=$(find_dirs '*_test.go')
    23  		fi
    24  		(
    25  			export LDFLAGS
    26  			export TESTFLAGS
    27  			export HAVE_GO_TEST_COVER
    28  
    29  			# some hack to export array variables
    30  			export BUILDFLAGS_FILE="$DEST/buildflags-file"
    31  			( IFS=$'\n'; echo "${BUILDFLAGS[*]}" ) > "$BUILDFLAGS_FILE"
    32  
    33  			if command -v parallel &> /dev/null; then
    34  				# accomodate parallel to be able to access variables
    35  				export SHELL="$BASH"
    36  				export HOME="$(mktemp -d)"
    37  				mkdir -p "$HOME/.parallel"
    38  				touch "$HOME/.parallel/ignored_vars"
    39  
    40  				echo "$TESTDIRS" | parallel --jobs "$PARALLEL_JOBS" --env _ "${MAKEDIR}/.go-compile-test-dir"
    41  				rm -rf "$HOME"
    42  			else
    43  				# aww, no "parallel" available - fall back to boring
    44  				for test_dir in $TESTDIRS; do
    45  					"${MAKEDIR}/.go-compile-test-dir" "$test_dir" || true
    46  					# don't let one directory that fails to build tank _all_ our tests!
    47  				done
    48  			fi
    49  			rm -f "$BUILDFLAGS_FILE"
    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="$ABS_DEST/precompiled/$dir.test$(binary_extension)"
    61  		if ! ( cd "$dir" && test_env "$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"