Browse code

Merge pull request #10797 from tianon/strict-test-environment

Steve Francia authored on 2015/02/20 06:14:05
Showing 7 changed files
... ...
@@ -119,7 +119,7 @@ RUN set -x \
119 119
 		go build -o /go/bin/registry-v2 github.com/docker/distribution/cmd/registry
120 120
 
121 121
 # Get the "docker-py" source so we can run their integration tests
122
-ENV DOCKER_PY_COMMIT aa19d7b6609c6676e8258f6b900dea2eda1dbe95
122
+ENV DOCKER_PY_COMMIT d39da1167975aaeb6c423b99621ecda1223477b8
123 123
 RUN git clone https://github.com/docker/docker-py.git /docker-py \
124 124
 	&& cd /docker-py \
125 125
 	&& git checkout -q $DOCKER_PY_COMMIT
... ...
@@ -20,6 +20,8 @@ import (
20 20
 	"strings"
21 21
 	"testing"
22 22
 	"time"
23
+
24
+	"github.com/docker/docker/api"
23 25
 )
24 26
 
25 27
 // Daemon represents a Docker daemon for the testing framework.
... ...
@@ -266,8 +268,8 @@ func (d *Daemon) Cmd(name string, arg ...string) (string, error) {
266 266
 }
267 267
 
268 268
 func daemonHost() string {
269
-	daemonUrlStr := "unix:///var/run/docker.sock"
270
-	if daemonHostVar := os.Getenv("DOCKER_TEST_HOST"); daemonHostVar != "" {
269
+	daemonUrlStr := "unix://" + api.DEFAULTUNIXSOCKET
270
+	if daemonHostVar := os.Getenv("DOCKER_HOST"); daemonHostVar != "" {
271 271
 		daemonUrlStr = daemonHostVar
272 272
 	}
273 273
 	return daemonUrlStr
... ...
@@ -772,6 +774,12 @@ func fakeGIT(name string, files map[string]string) (*FakeGIT, error) {
772 772
 	if err != nil {
773 773
 		return nil, err
774 774
 	}
775
+	if output, err := exec.Command("git", "config", "user.name", "Fake User").CombinedOutput(); err != nil {
776
+		return nil, fmt.Errorf("error trying to set 'user.name': %s (%s)", err, output)
777
+	}
778
+	if output, err := exec.Command("git", "config", "user.email", "fake.user@example.com").CombinedOutput(); err != nil {
779
+		return nil, fmt.Errorf("error trying to set 'user.email': %s (%s)", err, output)
780
+	}
775 781
 	if output, err := exec.Command("git", "add", "*").CombinedOutput(); err != nil {
776 782
 		return nil, fmt.Errorf("error trying to add files to repo: %s (%s)", err, output)
777 783
 	}
... ...
@@ -178,9 +178,22 @@ go_test_dir() {
178 178
 		export DEST
179 179
 		echo '+ go test' $TESTFLAGS "${DOCKER_PKG}${dir#.}"
180 180
 		cd "$dir"
181
-		go test ${testcover[@]} -ldflags "$LDFLAGS" "${BUILDFLAGS[@]}" $TESTFLAGS
181
+		test_env go test ${testcover[@]} -ldflags "$LDFLAGS" "${BUILDFLAGS[@]}" $TESTFLAGS
182 182
 	)
183 183
 }
184
+test_env() {
185
+	# use "env -i" to tightly control the environment variables that bleed into the tests
186
+	env -i \
187
+		DEST="$DEST" \
188
+		DOCKER_EXECDRIVER="$DOCKER_EXECDRIVER" \
189
+		DOCKER_GRAPHDRIVER="$DOCKER_GRAPHDRIVER" \
190
+		DOCKER_HOST="$DOCKER_HOST" \
191
+		GOPATH="$GOPATH" \
192
+		HOME="$DEST/fake-HOME" \
193
+		PATH="$PATH" \
194
+		TEST_DOCKERINIT_PATH="$TEST_DOCKERINIT_PATH" \
195
+		"$@"
196
+}
184 197
 
185 198
 # a helper to provide ".exe" when it's appropriate
186 199
 binary_extension() {
... ...
@@ -16,8 +16,10 @@ export DOCKER_GRAPHDRIVER=${DOCKER_GRAPHDRIVER:-vfs}
16 16
 export DOCKER_EXECDRIVER=${DOCKER_EXECDRIVER:-native}
17 17
 
18 18
 if [ -z "$DOCKER_TEST_HOST" ]; then
19
+	export DOCKER_HOST="unix://$(cd "$DEST" && pwd)/docker.sock" # "pwd" tricks to make sure $DEST is an absolute path, not a relative one
19 20
 	( set -x; exec \
20 21
 		docker --daemon --debug \
22
+		--host "$DOCKER_HOST" \
21 23
 		--storage-driver "$DOCKER_GRAPHDRIVER" \
22 24
 		--exec-driver "$DOCKER_EXECDRIVER" \
23 25
 		--pidfile "$DEST/docker.pid" \
... ...
@@ -26,3 +28,20 @@ if [ -z "$DOCKER_TEST_HOST" ]; then
26 26
 else
27 27
 	export DOCKER_HOST="$DOCKER_TEST_HOST"
28 28
 fi
29
+
30
+# give it a second to come up so it's "ready"
31
+tries=10
32
+while ! docker version &> /dev/null; do
33
+	(( tries-- ))
34
+	if [ $tries -le 0 ]; then
35
+		if [ -z "$DOCKER_HOST" ]; then
36
+			echo >&2 "error: daemon failed to start"
37
+			echo >&2 "  check $DEST/docker.log for details"
38
+		else
39
+			echo >&2 "error: daemon at $DOCKER_HOST fails to 'docker version':"
40
+			docker version >&2 || true
41
+		fi
42
+		false
43
+	fi
44
+	sleep 2
45
+done
... ...
@@ -18,8 +18,8 @@ DEST=$1
18 18
 			git clone https://github.com/docker/docker-py.git "$dockerPy"
19 19
 		}
20 20
 
21
-		export PYTHONPATH="$dockerPy" # import "docker" from our local docker-py
22
-		python "$dockerPy/tests/integration_test.py"
21
+		# exporting PYTHONPATH to import "docker" from our local docker-py
22
+		test_env PYTHONPATH="$dockerPy" python "$dockerPy/tests/integration_test.py"
23 23
 	}; then
24 24
 		didFail=1
25 25
 	fi
... ...
@@ -16,9 +16,6 @@ bundle_test_integration_cli() {
16 16
 	# even and especially on test failures
17 17
 	didFail=
18 18
 	if ! {
19
-		# pull the busybox image before running the tests
20
-		sleep 2
21
-
22 19
 		source "$(dirname "$BASH_SOURCE")/.ensure-busybox"
23 20
 		source "$(dirname "$BASH_SOURCE")/.ensure-emptyfs"
24 21
 
... ...
@@ -58,7 +58,7 @@ go_run_test_dir() {
58 58
 		echo
59 59
 		echo '+ go test' $TESTFLAGS "${DOCKER_PKG}${dir#.}"
60 60
 		precompiled="$DEST/precompiled/$dir.test$(binary_extension)"
61
-		if ! ( cd "$dir" && "$precompiled" $TESTFLAGS ); then
61
+		if ! ( cd "$dir" && test_env "$precompiled" $TESTFLAGS ); then
62 62
 			TESTS_FAILED+=("$dir")
63 63
 			echo
64 64
 			echo "${RED}Tests failed: $dir${TEXTRESET}"