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