github.com/wozhu6104/docker@v20.10.10+incompatible/hack/make/.integration-test-helpers (about)

     1  #!/usr/bin/env bash
     2  #
     3  # For integration-cli test, we use [gocheck](https://labix.org/gocheck), if you want
     4  # to run certain tests on your local host, you should run with command:
     5  #
     6  #     TESTFLAGS='-test.run TestDockerSuite/TestBuild*' ./hack/make.sh binary test-integration
     7  #
     8  
     9  if [ -z "${MAKEDIR}" ]; then
    10  	MAKEDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
    11  	export MAKEDIR
    12  fi
    13  source "${MAKEDIR}/.go-autogen"
    14  
    15  # Set defaults
    16  : "${TEST_REPEAT:=1}"
    17  : "${TESTFLAGS:=}"
    18  : "${TESTDEBUG:=}"
    19  : "${GOCACHE:=$(go env GOCACHE)}"
    20  
    21  setup_integration_test_filter() {
    22  	if [ -z "${TEST_FILTER}" ]; then
    23  		return
    24  	fi
    25  
    26  	local dirs
    27  	dirs=$(grep -rIlE --include '*_test.go' "func .*${TEST_FILTER}.*\(. \*testing\.T\)" ./integration*/ | xargs -I file dirname file | uniq)
    28  	if [ -z "${TEST_SKIP_INTEGRATION}" ]; then
    29  		: "${TEST_INTEGRATION_DIR:=$(echo "$dirs" | grep -v '^\./integration-cli$')}"
    30  		if [ -z "${TEST_INTEGRATION_DIR}" ]; then
    31  			echo "Skipping integration tests since the supplied filter \"${TEST_FILTER}\" omits all integration tests"
    32  			TEST_SKIP_INTEGRATION=1
    33  		else
    34  			TESTFLAGS+=" -test.run ${TEST_FILTER}"
    35  		fi
    36  	fi
    37  
    38  	if [ -z "${TEST_SKIP_INTEGRATION_CLI}" ]; then
    39  		if echo "$dirs" | grep -vq '^./integration-cli$'; then
    40  			TEST_SKIP_INTEGRATION_CLI=1
    41  			echo "Skipping integration-cli tests since the supplied filter \"${TEST_FILTER}\" omits all integration-cli tests"
    42  		else
    43  			TESTFLAGS+=" -test.run /${TEST_FILTER}"
    44  		fi
    45  	fi
    46  }
    47  
    48  setup_integration_test_filter
    49  if [ -z "${TEST_SKIP_INTEGRATION}" ] && [ -z "${TEST_INTEGRATION_DIR}" ]; then
    50  	integration_api_dirs="$(go list -test -f '{{- if ne .ForTest "" -}}{{- .Dir -}}{{- end -}}' ./integration/...)"
    51  else
    52  	integration_api_dirs="${TEST_INTEGRATION_DIR}"
    53  fi
    54  
    55  run_test_integration() {
    56  	set_platform_timeout
    57  	if [ -z "${TEST_SKIP_INTEGRATION}" ]; then
    58  		run_test_integration_suites "${integration_api_dirs}"
    59  	fi
    60  	if [ -z "${TEST_SKIP_INTEGRATION_CLI}" ]; then
    61  		TIMEOUT=360m run_test_integration_suites integration-cli
    62  	fi
    63  }
    64  
    65  run_test_integration_suites() {
    66  	local flags="-test.v -test.timeout=${TIMEOUT} $TESTFLAGS"
    67  	local dirs="$1"
    68  	for dir in ${dirs}; do
    69  		if ! (
    70  			cd "$dir"
    71  			# Create a useful package name based on the tests's $dir. We need to take
    72  			# into account that  "$dir" can be either an absolute (/go/src/github.com/docker/docker/integration/foo)
    73  			# or relative (./integration/foo) path. To account for both, first we strip
    74  			# the absolute path, then remove any leading periods and slashes.
    75  			pkgname="${dir}"
    76  			pkgname="${pkgname#*${GOPATH}/src/${DOCKER_PKG}}"
    77  			pkgname="${pkgname#*.}"
    78  			pkgname="${pkgname#*\/}"
    79  
    80  			# Finally, we use periods as separator (instead of slashes) to be more
    81  			# in line with Java package names (which is what junit.xml was designed for)
    82  			pkgname="$(go env GOARCH).${pkgname//\//.}"
    83  			echo "Running $PWD (${pkgname}) flags=${flags}"
    84  			[ -n "$TESTDEBUG" ] && set -x
    85  			# shellcheck disable=SC2086
    86  			test_env gotestsum \
    87  				--format=standard-verbose \
    88  				--jsonfile="${ABS_DEST}/${pkgname//./-}-go-test-report.json" \
    89  				--junitfile="${ABS_DEST}/${pkgname//./-}-junit-report.xml" \
    90  				--raw-command \
    91  				-- go tool test2json -p "${pkgname}" -t ./test.main ${flags}
    92  		); then exit 1; fi
    93  	done
    94  }
    95  
    96  build_test_suite_binaries() {
    97  	if [ -n "${DOCKER_INTEGRATION_TESTS_VERIFIED}" ]; then
    98  		echo "Skipping building test binaries; as DOCKER_INTEGRATION_TESTS_VERIFIED is set"
    99  		return
   100  	fi
   101  	if [ -z "${TEST_SKIP_INTEGRATION_CLI}" ]; then
   102  		build_test_suite_binary ./integration-cli "test.main"
   103  	fi
   104  	if [ -z "${TEST_SKIP_INTEGRATION}" ]; then
   105  		for dir in ${integration_api_dirs}; do
   106  			build_test_suite_binary "$dir" "test.main"
   107  		done
   108  	fi
   109  }
   110  
   111  # Build a binary for a test suite package
   112  build_test_suite_binary() {
   113  	local dir="$1"
   114  	local out="$2"
   115  	echo Building test suite binary "$dir/$out"
   116  	go test -c -o "$dir/$out" -ldflags "$LDFLAGS" "${BUILDFLAGS[@]}" "$dir"
   117  }
   118  
   119  cleanup_test_suite_binaries() {
   120  	[ -n "$TESTDEBUG" ] && return
   121  	echo "Removing test suite binaries"
   122  	# shellcheck disable=SC2038
   123  	find integration* -name test.main | xargs -r rm
   124  }
   125  
   126  repeat() {
   127  	for i in $(seq 1 ${TEST_REPEAT}); do
   128  		echo "Running integration-test (iteration $i)"
   129  		$@
   130  	done
   131  }
   132  
   133  # use "env -i" to tightly control the environment variables that bleed into the tests
   134  test_env() {
   135  	(
   136  		set -e
   137  		[ -n "$TESTDEBUG" ] && set -x
   138  		env -i \
   139  			DEST="$ABS_DEST" \
   140  			DOCKER_API_VERSION="$DOCKER_API_VERSION" \
   141  			DOCKER_BUILDKIT="$DOCKER_BUILDKIT" \
   142  			DOCKER_INTEGRATION_DAEMON_DEST="$DOCKER_INTEGRATION_DAEMON_DEST" \
   143  			DOCKER_TLS_VERIFY="$DOCKER_TEST_TLS_VERIFY" \
   144  			DOCKER_CERT_PATH="$DOCKER_TEST_CERT_PATH" \
   145  			DOCKER_ENGINE_GOARCH="$DOCKER_ENGINE_GOARCH" \
   146  			DOCKER_GRAPHDRIVER="$DOCKER_GRAPHDRIVER" \
   147  			DOCKER_USERLANDPROXY="$DOCKER_USERLANDPROXY" \
   148  			DOCKER_HOST="$DOCKER_HOST" \
   149  			DOCKER_REMAP_ROOT="$DOCKER_REMAP_ROOT" \
   150  			DOCKER_REMOTE_DAEMON="$DOCKER_REMOTE_DAEMON" \
   151  			DOCKER_ROOTLESS="$DOCKER_ROOTLESS" \
   152  			DOCKERFILE="$DOCKERFILE" \
   153  			GOCACHE="$GOCACHE" \
   154  			GOPATH="$GOPATH" \
   155  			GOTRACEBACK=all \
   156  			HOME="$ABS_DEST/fake-HOME" \
   157  			PATH="$PATH" \
   158  			TEMP="$TEMP" \
   159  			TEST_CLIENT_BINARY="$TEST_CLIENT_BINARY" \
   160  			"$@"
   161  	)
   162  }
   163  
   164  error_on_leaked_containerd_shims() {
   165  	if [ "$(go env GOOS)" = 'windows' ]; then
   166  		return
   167  	fi
   168  
   169  	leftovers=$(ps -ax -o pid,cmd \
   170  		| awk '$2 == "containerd-shim" && $4 ~ /.*\/bundles\/.*\/test-integration/ { print $1 }')
   171  	if [ -n "$leftovers" ]; then
   172  		ps aux
   173  		# shellcheck disable=SC2086
   174  		kill -9 ${leftovers} 2> /dev/null
   175  		echo "!!!! WARNING you have left over shim(s), Cleanup your test !!!!"
   176  		exit 1
   177  	fi
   178  }
   179  
   180  set_platform_timeout() {
   181  	# Test timeout.
   182  	if [ "${DOCKER_ENGINE_GOARCH}" = "arm64" ] || [ "${DOCKER_ENGINE_GOARCH}" = "arm" ]; then
   183  		: "${TIMEOUT:=10m}"
   184  	elif [ "${DOCKER_ENGINE_GOARCH}" = "windows" ]; then
   185  		: "${TIMEOUT:=8m}"
   186  	else
   187  		: "${TIMEOUT:=5m}"
   188  	fi
   189  
   190  	if [ "${TEST_REPEAT}" -gt 1 ]; then
   191  		# TIMEOUT needs to take TEST_REPEAT into account, or a premature time out may happen.
   192  		# The following ugliness will:
   193  		# - remove last character (usually 'm' from '10m')
   194  		# - multiply by testcount
   195  		# - add last character back
   196  		TIMEOUT=$((${TIMEOUT::-1} * ${TEST_REPEAT}))${TIMEOUT:$((${#TIMEOUT} - 1)):1}
   197  	fi
   198  }