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