#!/usr/bin/env bash
#
# For integration-cli test, we use [gocheck](https://labix.org/gocheck), if you want
# to run certain tests on your local host, you should run with command:
#
#     TESTFLAGS='-check.f DockerSuite.TestBuild*' ./hack/make.sh binary test-integration
#
if [ -z ${MAKEDIR} ]; then
	export MAKEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
fi
source "$MAKEDIR/.go-autogen"

# Set defaults
: ${TEST_REPEAT:=1}
: ${TESTFLAGS:=}
: ${TESTDEBUG:=}

integration_api_dirs=${TEST_INTEGRATION_DIR:-"$(
	find ./integration -type d |
	grep -vE '(^./integration($|/internal)|/testdata)')"}

run_test_integration() {
	[[ "$TESTFLAGS" != *-check.f* ]] && run_test_integration_suites
	run_test_integration_legacy_suites
}

run_test_integration_suites() {
	local flags="-test.v -test.timeout=${TIMEOUT} $TESTFLAGS"
	for dir in ${integration_api_dirs}; do
		if ! (
			cd "$dir"
			echo "Running $PWD"
			test_env ./test.main ${flags}
		); then exit 1; fi
	done
}

run_test_integration_legacy_suites() {
	(
		flags="-check.v -check.timeout=${TIMEOUT} -test.timeout=360m $TESTFLAGS"
		cd integration-cli
		echo "Running $PWD"
		test_env ./test.main $flags
	)
}

build_test_suite_binaries() {
	if [ ${DOCKER_INTEGRATION_TESTS_VERIFIED-} ]; then
		echo "Skipping building test binaries; as DOCKER_INTEGRATION_TESTS_VERIFIED is set"
		return
	fi
	build_test_suite_binary ./integration-cli "test.main"
	for dir in ${integration_api_dirs}; do
		build_test_suite_binary "$dir" "test.main"
	done
}

# Build a binary for a test suite package
build_test_suite_binary() {
	local dir="$1"
	local out="$2"
	echo Building test suite binary "$dir/$out"
	go test -c -o "$dir/$out" -ldflags "$LDFLAGS" "${BUILDFLAGS[@]}" "$dir"
}

cleanup_test_suite_binaries() {
	[ -n "$TESTDEBUG" ] && return
	echo "Removing test suite binaries"
	find integration* -name test.main | xargs -r rm
}

repeat() {
	for i in $(seq 1 ${TEST_REPEAT}); do
		echo "Running integration-test (iteration $i)"
		$@
	done
}

# use "env -i" to tightly control the environment variables that bleed into the tests
test_env() {
	(
		set -e
		[ -n "$TESTDEBUG" ] && set -x
		env -i \
			DEST="$ABS_DEST" \
			DOCKER_API_VERSION="$DOCKER_API_VERSION" \
			DOCKER_BUILDKIT="$DOCKER_BUILDKIT" \
			DOCKER_INTEGRATION_DAEMON_DEST="$DOCKER_INTEGRATION_DAEMON_DEST" \
			DOCKER_TLS_VERIFY="$DOCKER_TEST_TLS_VERIFY" \
			DOCKER_CERT_PATH="$DOCKER_TEST_CERT_PATH" \
			DOCKER_ENGINE_GOARCH="$DOCKER_ENGINE_GOARCH" \
			DOCKER_GRAPHDRIVER="$DOCKER_GRAPHDRIVER" \
			DOCKER_USERLANDPROXY="$DOCKER_USERLANDPROXY" \
			DOCKER_HOST="$DOCKER_HOST" \
			DOCKER_REMAP_ROOT="$DOCKER_REMAP_ROOT" \
			DOCKER_REMOTE_DAEMON="$DOCKER_REMOTE_DAEMON" \
			DOCKERFILE="$DOCKERFILE" \
			GOPATH="$GOPATH" \
			GOTRACEBACK=all \
			HOME="$ABS_DEST/fake-HOME" \
			PATH="$PATH" \
			TEMP="$TEMP" \
			TEST_CLIENT_BINARY="$TEST_CLIENT_BINARY" \
			"$@"
	)
}
   

error_on_leaked_containerd_shims() {
	if [ "$(go env GOOS)" == 'windows' ]; then
		return
	fi

	leftovers=$(ps -ax -o pid,cmd |
	            awk '$2 == "containerd-shim" && $4 ~ /.*\/bundles\/.*\/test-integration/ { print $1 }')
	if [ -n "$leftovers" ]; then
		ps aux
		kill -9 ${leftovers} 2> /dev/null
		echo "!!!! WARNING you have left over shim(s), Cleanup your test !!!!"
		exit 1
	fi
}