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