From 4308e881cc00141a1dd187120d1c4e2106148282 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Thu, 6 Jul 2017 17:20:41 -0400 Subject: [PATCH] Cleanup test output, and binaries. Signed-off-by: Daniel Nephin --- .gitignore | 2 +- hack/make/.ensure-emptyfs | 5 +- hack/make/.integration-daemon-setup | 2 +- hack/make/.integration-daemon-start | 28 +++++---- hack/make/.integration-daemon-stop | 11 ++-- hack/make/.integration-test-helpers | 96 +++++++++++++++++------------ hack/make/test-integration | 23 +++---- hack/make/test-integration-cli | 4 +- 8 files changed, 94 insertions(+), 77 deletions(-) diff --git a/.gitignore b/.gitignore index 943e7f3f68..93a6df48e7 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,7 @@ *.exe *.exe~ *.orig -*.test +test.main .*.swp .DS_Store # a .bashrc may be added to customize the build environment diff --git a/hack/make/.ensure-emptyfs b/hack/make/.ensure-emptyfs index 7b00b9d455..f237905dce 100644 --- a/hack/make/.ensure-emptyfs +++ b/hack/make/.ensure-emptyfs @@ -18,6 +18,9 @@ if ! docker inspect -t image emptyfs &> /dev/null; then tar -cf layer.tar --files-from /dev/null ) ) - ( set -x; tar -cC "$dir" . | docker load ) + ( + [ -n "$TESTDEBUG" ] && set -x + tar -cC "$dir" . | docker load + ) rm -rf "$dir" fi diff --git a/hack/make/.integration-daemon-setup b/hack/make/.integration-daemon-setup index 5134e4c2db..c130e23560 100644 --- a/hack/make/.integration-daemon-setup +++ b/hack/make/.integration-daemon-setup @@ -1,7 +1,7 @@ #!/usr/bin/env bash set -e -bundle .detect-daemon-osarch +source "$MAKEDIR/.detect-daemon-osarch" if [ "$DOCKER_ENGINE_GOOS" != "windows" ]; then bundle .ensure-emptyfs fi diff --git a/hack/make/.integration-daemon-start b/hack/make/.integration-daemon-start index 0e8573ede1..b4e6684854 100644 --- a/hack/make/.integration-daemon-start +++ b/hack/make/.integration-daemon-start @@ -76,24 +76,26 @@ if [ -z "$DOCKER_TEST_HOST" ]; then # see https://github.com/docker/libcontainer/blob/master/apparmor/apparmor.go#L16 export container="" ( - set -x + [ -n "$TESTDEBUG" ] && set -x /etc/init.d/apparmor start ) fi - export DOCKER_HOST="unix://$(cd "$DEST" && pwd)/docker.sock" # "pwd" tricks to make sure $DEST is an absolute path, not a relative one - ( set -x; exec \ - dockerd --debug \ - --host "$DOCKER_HOST" \ - --storage-driver "$DOCKER_GRAPHDRIVER" \ - --pidfile "$DEST/docker.pid" \ - --userland-proxy="$DOCKER_USERLANDPROXY" \ - $storage_params \ - $extra_params \ - &> "$DEST/docker.log" + # "pwd" tricks to make sure $DEST is an absolute path, not a relative one + export DOCKER_HOST="unix://$(cd "$DEST" && pwd)/docker.sock" + ( + echo "Starting dockerd" + [ -n "$TESTDEBUG" ] && set -x + exec \ + dockerd --debug \ + --host "$DOCKER_HOST" \ + --storage-driver "$DOCKER_GRAPHDRIVER" \ + --pidfile "$DEST/docker.pid" \ + --userland-proxy="$DOCKER_USERLANDPROXY" \ + $storage_params \ + $extra_params \ + &> "$DEST/docker.log" ) & - # make sure that if the script exits unexpectedly, we stop this daemon we just started - trap 'bundle .integration-daemon-stop' EXIT else export DOCKER_HOST="$DOCKER_TEST_HOST" fi diff --git a/hack/make/.integration-daemon-stop b/hack/make/.integration-daemon-stop index 591a8d6bec..c1d43e1a5e 100644 --- a/hack/make/.integration-daemon-stop +++ b/hack/make/.integration-daemon-stop @@ -1,11 +1,12 @@ #!/usr/bin/env bash if [ ! "$(go env GOOS)" = 'windows' ]; then - trap - EXIT # reset EXIT trap applied in .integration-daemon-start - for pidFile in $(find "$DEST" -name docker.pid); do - pid=$(set -x; cat "$pidFile") - ( set -x; kill "$pid" ) + pid=$([ -n "$TESTDEBUG" ] && set -x; cat "$pidFile") + ( + [ -n "$TESTDEBUG" ] && set -x + kill "$pid" + ) if ! wait "$pid"; then echo >&2 "warning: PID $pid from $pidFile had a nonzero exit code" fi @@ -15,7 +16,7 @@ if [ ! "$(go env GOOS)" = 'windows' ]; then # Stop apparmor if it is enabled if [ -e "/sys/module/apparmor/parameters/enabled" ] && [ "$(cat /sys/module/apparmor/parameters/enabled)" == "Y" ]; then ( - set -x + [ -n "$TESTDEBUG" ] && set -x /etc/init.d/apparmor stop ) fi diff --git a/hack/make/.integration-test-helpers b/hack/make/.integration-test-helpers index 0156e8d4ea..9a8abd3633 100644 --- a/hack/make/.integration-test-helpers +++ b/hack/make/.integration-test-helpers @@ -1,26 +1,38 @@ #!/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 +# source "$SCRIPTDIR/make/.go-autogen" -: ${TEST_REPEAT:=0} +: ${TEST_REPEAT:=1} + +integration_api_dirs=("$(find ./integration -type d | grep -vE '^./integration$')") + +run_test_integration() { + local flags="-test.v -test.timeout=${TIMEOUT} $TESTFLAGS" + for dir in $integration_api_dirs; do + ( + cd $dir + echo "Running $PWD" + test_env ./test.main $flags + ) + done -bundle_test_integration() { ( - local flags="-v -test.timeout=${TIMEOUT} $TESTFLAGS" - cd integration - set -ex - # TODO: run existing binary? - go test -ldflags "$LDFLAGS" "${BUILDFLAGS[@]}" $flags ./... - ) - ( - local flags="$TESTFLAGS -check.v -check.timeout=${TIMEOUT} -test.timeout=360m" - go_test_dir integration-cli + flags="-check.v -check.timeout=${TIMEOUT} -test.timeout=360m $TESTFLAGS" + cd integration-cli > /dev/null + echo "Running $PWD" + test_env ./test.main $flags ) } build_test_suite_binaries() { - build_test_suite_binary integration-cli "test.main" - for dir in $(find integration -type d); do + build_test_suite_binary ./integration-cli "test.main" + for dir in $integration_api_dirs; do build_test_suite_binary "$dir" "test.main" done } @@ -30,39 +42,29 @@ 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" + go test -c -o "$dir/$out" -ldflags "$LDFLAGS" "${BUILDFLAGS[@]}" "$dir" } -# If $TESTFLAGS is set in the environment, it is passed as extra arguments to 'go test'. -# You can use this to select certain tests to run, e.g. -# -# TESTFLAGS='-test.run ^TestBuild$' ./hack/make.sh test-unit -# -# 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 -# -go_test_dir() { - local dir=$1 - ( - set -e - # DEST is used by the test suite - export DEST="$ABS_DEST" - cd "$dir" - for i in $(seq 0 $TEST_REPEAT); do - echo "Repeating integration-test ($i)" - test_env "./test.main" $TESTFLAGS - done - ) +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 -xe - # use "env -i" to tightly control the environment variables that bleed into the tests + set -e + [ -n "$TESTDEBUG" ] && set -x env -i \ - DEST="$DEST" \ + DEST="$ABS_DEST" \ DOCKER_CLI_VERSION="$DOCKER_CLI_VERSION" \ DOCKER_API_VERSION="$DOCKER_API_VERSION" \ DOCKER_INTEGRATION_DAEMON_DEST="$DOCKER_INTEGRATION_DAEMON_DEST" \ @@ -85,3 +87,19 @@ test_env() { "$@" ) } + + +error_on_leaked_containerd_shims() { + if [ "$(go env GOOS)" == 'windows' ]; then + return + fi + + leftovers=$(ps -ax -o pid,cmd | + awk '$2 == "docker-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 +} diff --git a/hack/make/test-integration b/hack/make/test-integration index 8b8440cb3a..e419d66c6e 100755 --- a/hack/make/test-integration +++ b/hack/make/test-integration @@ -4,28 +4,19 @@ set -e source "${MAKEDIR}/.go-autogen" source hack/make/.integration-test-helpers -# subshell so that we can export PATH without breaking other things ( - build_test_suite_binaries - + build_test_suite_binaries bundle .integration-daemon-start - bundle .integration-daemon-setup - bundle_test_integration + local testexit=0 + ( repeat run_test_integration ) || testexit=$? + # Always run cleanup, even if the subshell fails bundle .integration-daemon-stop + cleanup_test_suite_binaries + error_on_leaked_containerd_shims - if [ "$(go env GOOS)" != 'windows' ] - then - leftovers=$(ps -ax -o pid,cmd | awk '$2 == "docker-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 - fi + exit $testexit ) 2>&1 | tee -a "$DEST/test.log" diff --git a/hack/make/test-integration-cli b/hack/make/test-integration-cli index 9282b4fda1..4cc79d095d 100755 --- a/hack/make/test-integration-cli +++ b/hack/make/test-integration-cli @@ -1,4 +1,6 @@ #!/usr/bin/env bash set -e echo "WARNING: test-integration-cli is DEREPCATED. Use test-integration." >&2 -exit 1 + +# TODO: remove this and exit 1 once CI has changed to use test-integration +bundle test-integration