Browse code

compile unit tests in parallel with GNU parallel

Docker-DCO-1.1-Signed-off-by: Tibor Vass <teabee89@gmail.com> (github: tiborvass)

Tibor Vass authored on 2014/06/28 04:55:36
Showing 2 changed files
... ...
@@ -156,6 +156,25 @@ go_test_dir() {
156 156
 	)
157 157
 }
158 158
 
159
+# Compile phase run by parallel in test-unit. No support for coverpkg
160
+go_compile_test_dir() {
161
+	dir=$1
162
+	testcover=()
163
+	if [ "$HAVE_GO_TEST_COVER" ]; then
164
+		# if our current go install has -cover, we want to use it :)
165
+		mkdir -p "$DEST/coverprofiles"
166
+		coverprofile="docker${dir#.}"
167
+		coverprofile="$DEST/coverprofiles/${coverprofile//\//-}"
168
+		testcover=( -cover -coverprofile "$coverprofile" ) # missing $coverpkg
169
+	fi
170
+	(
171
+		readarray -t BUILDFLAGS < "$BUILDFLAGS_FILE"
172
+		cd "$dir"
173
+		go test "${testcover[@]}" -ldflags "$LDFLAGS" "${BUILDFLAGS[@]}" $TESTFLAGS -c
174
+		echo "$dir"
175
+	)
176
+}
177
+
159 178
 # This helper function walks the current directory looking for directories
160 179
 # holding certain files ($1 parameter), and prints their paths on standard
161 180
 # output, one per line.
... ...
@@ -6,6 +6,7 @@ DEST=$1
6 6
 RED=$'\033[31m'
7 7
 GREEN=$'\033[32m'
8 8
 TEXTRESET=$'\033[0m' # reset the foreground colour
9
+: ${PARALLEL_JOBS:=0}
9 10
 
10 11
 # Run Docker's test suite, including sub-packages, and store their output as a bundle
11 12
 # If $TESTFLAGS is set in the environment, it is passed as extra arguments to 'go test'.
... ...
@@ -22,34 +23,58 @@ bundle_test_unit() {
22 22
 			TESTDIRS=$(find_dirs '*_test.go')
23 23
 		fi
24 24
 
25
-		TESTS_FAILED=()
26
-		for test_dir in $TESTDIRS; do
27
-			echo
25
+		(
26
+			# accomodate parallel to be able to access variables
27
+			export SHELL=/bin/bash 
28
+			export HOME=/tmp
29
+			mkdir -p "$HOME/.parallel"
30
+			touch "$HOME/.parallel/ignored_vars"
31
+			export -f go_compile_test_dir
32
+			export LDFLAGS="$LDFLAGS $LDFLAGS_STATIC_DOCKER"
33
+			export TESTFLAGS
34
+			export HAVE_GO_TEST_COVER
35
+			export DEST
36
+			# some hack to export array variables
37
+			export BUILDFLAGS_FILE="$HOME/buildflags_file"
38
+			( IFS=$'\n'; echo "${BUILDFLAGS[*]}" ) > "$BUILDFLAGS_FILE"
28 39
 
29
-			if ! LDFLAGS="$LDFLAGS $LDFLAGS_STATIC_DOCKER" go_test_dir "$test_dir"; then
30
-				TESTS_FAILED+=("$test_dir")
31
-				echo
32
-				echo "${RED}Tests failed: $test_dir${TEXTRESET}"
33
-				sleep 1 # give it a second, so observers watching can take note
34
-			fi
35
-		done
40
+			echo "$TESTDIRS" | parallel --jobs "$PARALLEL_JOBS" --env _ go_compile_test_dir | go_run_test_dir
41
+		)
42
+	}
43
+}
36 44
 
45
+go_run_test_dir() {
46
+	TESTS_FAILED=()
47
+	while read dir; do
37 48
 		echo
38
-		echo
39
-		echo
40
-
41
-		# if some tests fail, we want the bundlescript to fail, but we want to
42
-		# try running ALL the tests first, hence TESTS_FAILED
43
-		if [ "${#TESTS_FAILED[@]}" -gt 0 ]; then
44
-			echo "${RED}Test failures in: ${TESTS_FAILED[@]}${TEXTRESET}"
49
+		echo '+ go test' $TESTFLAGS "github.com/dotcloud/docker${dir#.}"
50
+		pushd "$dir" > /dev/null
51
+		t=$(basename "$dir").test
52
+		if ! ./$t; then
53
+			TESTS_FAILED+=("$dir")
45 54
 			echo
46
-			false
47
-		else
48
-			echo "${GREEN}Test success${TEXTRESET}"
49
-			echo
50
-			true
55
+			echo "${RED}Tests failed: $dir${TEXTRESET}"
56
+			sleep 1 # give it a second, so observers watching can take note
51 57
 		fi
52
-	}
58
+		rm ./$t || true
59
+		popd > /dev/null
60
+	done
61
+
62
+	echo
63
+	echo
64
+	echo
65
+
66
+	# if some tests fail, we want the bundlescript to fail, but we want to
67
+	# try running ALL the tests first, hence TESTS_FAILED
68
+	if [ "${#TESTS_FAILED[@]}" -gt 0 ]; then
69
+		echo "${RED}Test failures in: ${TESTS_FAILED[@]}${TEXTRESET}"
70
+		echo
71
+		false
72
+	else
73
+		echo "${GREEN}Test success${TEXTRESET}"
74
+		echo
75
+		true
76
+	fi
53 77
 }
54 78
 
55 79
 exec > >(tee -a $DEST/test.log) 2>&1