From ed345fb18ee9d003f30884b696628b75380a426a Mon Sep 17 00:00:00 2001 From: Tianon Gravi Date: Sat, 14 Feb 2015 03:27:31 -0700 Subject: [PATCH] Run tests in stricter environment Use `env -i` to very explicitly control exactly which environment variables leak into our tests. This enforces a clean separation of "build environment knobs" versus "test suite knobs". This also includes a minor tweak to how we handle starting our integration daemon, especially to catch failure to start sooner than failing tests. Signed-off-by: Andrew "Tianon" Page --- Dockerfile | 2 +- integration-cli/docker_utils.go | 12 ++++++++++-- project/make.sh | 15 ++++++++++++++- project/make/.integration-daemon-start | 19 +++++++++++++++++++ project/make/test-docker-py | 4 ++-- project/make/test-integration-cli | 3 --- project/make/test-unit | 2 +- 7 files changed, 47 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index b097082ec2..6ba56258e0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -122,7 +122,7 @@ RUN set -x \ go build -o /go/bin/registry-v2 github.com/docker/distribution/cmd/registry # Get the "docker-py" source so we can run their integration tests -ENV DOCKER_PY_COMMIT aa19d7b6609c6676e8258f6b900dea2eda1dbe95 +ENV DOCKER_PY_COMMIT d39da1167975aaeb6c423b99621ecda1223477b8 RUN git clone https://github.com/docker/docker-py.git /docker-py \ && cd /docker-py \ && git checkout -q $DOCKER_PY_COMMIT diff --git a/integration-cli/docker_utils.go b/integration-cli/docker_utils.go index a096fa3324..87d39caa5e 100644 --- a/integration-cli/docker_utils.go +++ b/integration-cli/docker_utils.go @@ -20,6 +20,8 @@ import ( "strings" "testing" "time" + + "github.com/docker/docker/api" ) // Daemon represents a Docker daemon for the testing framework. @@ -266,8 +268,8 @@ func (d *Daemon) Cmd(name string, arg ...string) (string, error) { } func daemonHost() string { - daemonUrlStr := "unix:///var/run/docker.sock" - if daemonHostVar := os.Getenv("DOCKER_TEST_HOST"); daemonHostVar != "" { + daemonUrlStr := "unix://" + api.DEFAULTUNIXSOCKET + if daemonHostVar := os.Getenv("DOCKER_HOST"); daemonHostVar != "" { daemonUrlStr = daemonHostVar } return daemonUrlStr @@ -772,6 +774,12 @@ func fakeGIT(name string, files map[string]string) (*FakeGIT, error) { if err != nil { return nil, err } + if output, err := exec.Command("git", "config", "user.name", "Fake User").CombinedOutput(); err != nil { + return nil, fmt.Errorf("error trying to set 'user.name': %s (%s)", err, output) + } + if output, err := exec.Command("git", "config", "user.email", "fake.user@example.com").CombinedOutput(); err != nil { + return nil, fmt.Errorf("error trying to set 'user.email': %s (%s)", err, output) + } if output, err := exec.Command("git", "add", "*").CombinedOutput(); err != nil { return nil, fmt.Errorf("error trying to add files to repo: %s (%s)", err, output) } diff --git a/project/make.sh b/project/make.sh index f1e60cc34a..264ed37dd2 100755 --- a/project/make.sh +++ b/project/make.sh @@ -178,9 +178,22 @@ go_test_dir() { export DEST echo '+ go test' $TESTFLAGS "${DOCKER_PKG}${dir#.}" cd "$dir" - go test ${testcover[@]} -ldflags "$LDFLAGS" "${BUILDFLAGS[@]}" $TESTFLAGS + test_env go test ${testcover[@]} -ldflags "$LDFLAGS" "${BUILDFLAGS[@]}" $TESTFLAGS ) } +test_env() { + # use "env -i" to tightly control the environment variables that bleed into the tests + env -i \ + DEST="$DEST" \ + DOCKER_EXECDRIVER="$DOCKER_EXECDRIVER" \ + DOCKER_GRAPHDRIVER="$DOCKER_GRAPHDRIVER" \ + DOCKER_HOST="$DOCKER_HOST" \ + GOPATH="$GOPATH" \ + HOME="$DEST/fake-HOME" \ + PATH="$PATH" \ + TEST_DOCKERINIT_PATH="$TEST_DOCKERINIT_PATH" \ + "$@" +} # a helper to provide ".exe" when it's appropriate binary_extension() { diff --git a/project/make/.integration-daemon-start b/project/make/.integration-daemon-start index 5d3cd94140..a1e390bde2 100644 --- a/project/make/.integration-daemon-start +++ b/project/make/.integration-daemon-start @@ -16,8 +16,10 @@ export DOCKER_GRAPHDRIVER=${DOCKER_GRAPHDRIVER:-vfs} export DOCKER_EXECDRIVER=${DOCKER_EXECDRIVER:-native} if [ -z "$DOCKER_TEST_HOST" ]; then + 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 \ docker --daemon --debug \ + --host "$DOCKER_HOST" \ --storage-driver "$DOCKER_GRAPHDRIVER" \ --exec-driver "$DOCKER_EXECDRIVER" \ --pidfile "$DEST/docker.pid" \ @@ -26,3 +28,20 @@ if [ -z "$DOCKER_TEST_HOST" ]; then else export DOCKER_HOST="$DOCKER_TEST_HOST" fi + +# give it a second to come up so it's "ready" +tries=10 +while ! docker version &> /dev/null; do + (( tries-- )) + if [ $tries -le 0 ]; then + if [ -z "$DOCKER_HOST" ]; then + echo >&2 "error: daemon failed to start" + echo >&2 " check $DEST/docker.log for details" + else + echo >&2 "error: daemon at $DOCKER_HOST fails to 'docker version':" + docker version >&2 || true + fi + false + fi + sleep 2 +done diff --git a/project/make/test-docker-py b/project/make/test-docker-py index 104842a423..b95cf40af5 100644 --- a/project/make/test-docker-py +++ b/project/make/test-docker-py @@ -18,8 +18,8 @@ DEST=$1 git clone https://github.com/docker/docker-py.git "$dockerPy" } - export PYTHONPATH="$dockerPy" # import "docker" from our local docker-py - python "$dockerPy/tests/integration_test.py" + # exporting PYTHONPATH to import "docker" from our local docker-py + test_env PYTHONPATH="$dockerPy" python "$dockerPy/tests/integration_test.py" }; then didFail=1 fi diff --git a/project/make/test-integration-cli b/project/make/test-integration-cli index 7c5e02437f..5ea3be3872 100644 --- a/project/make/test-integration-cli +++ b/project/make/test-integration-cli @@ -16,9 +16,6 @@ bundle_test_integration_cli() { # even and especially on test failures didFail= if ! { - # pull the busybox image before running the tests - sleep 2 - source "$(dirname "$BASH_SOURCE")/.ensure-busybox" source "$(dirname "$BASH_SOURCE")/.ensure-emptyfs" diff --git a/project/make/test-unit b/project/make/test-unit index 9225b33a06..2b0927526b 100644 --- a/project/make/test-unit +++ b/project/make/test-unit @@ -58,7 +58,7 @@ go_run_test_dir() { echo echo '+ go test' $TESTFLAGS "${DOCKER_PKG}${dir#.}" precompiled="$DEST/precompiled/$dir.test$(binary_extension)" - if ! ( cd "$dir" && "$precompiled" $TESTFLAGS ); then + if ! ( cd "$dir" && test_env "$precompiled" $TESTFLAGS ); then TESTS_FAILED+=("$dir") echo echo "${RED}Tests failed: $dir${TEXTRESET}"