Browse Source

Convert "test-pr.sh" to use a two-stage containerized approach to actually check out the tests from the PR we're testing

Tianon Gravi 10 years ago
parent
commit
0581dea5cd
1 changed files with 47 additions and 15 deletions
  1. 47 15
      test-pr.sh

+ 47 - 15
test-pr.sh

@@ -1,13 +1,14 @@
 #!/bin/bash
 set -eo pipefail
 
-cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
+dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
 
 usage() {
 	cat <<-EOUSAGE
 		usage: $0 [PR number] [repo[:tag]]
 		   ie: $0 1024
 		       $0 9001 debian php django
+		       $0 0 hylang # special case that runs against local directory
 		
 		This script builds and tests the specified pull request to official-images and
 		provides ouput in markdown for commenting on the pull request.
@@ -17,14 +18,51 @@ usage() {
 pull="$1"
 shift || { usage >&2 && exit 1; }
 
-url="https://github.com/docker-library/official-images/pull/$pull.patch"
-pat="$(curl -fsSL --compressed "$url")"
-commit="$(echo "$pat" | grep -E '^From [0-9a-f]+ ' | tail -n1 | cut -d' ' -f2)"
+if [ -z "$BASHBREW_SECOND_STAGE" ]; then
+	docker build --pull -t bashbrew "$dir" > /dev/null
+	if [ "$pull" = '0' ]; then
+		name="bashbrew-test-local-$RANDOM"
+	else
+		name="bashbrew-test-pr-$pull"
+	fi
+	exec docker run \
+		-it --rm \
+		--name "$name" \
+		-e BASHBREW_SECOND_STAGE=1 \
+		-v /var/run/docker.sock:/var/run/docker.sock \
+		-w /usr/src/pr \
+		bashbrew /usr/src/official-images/test-pr.sh "$pull" "$@"
+fi
+
+if [ -d .git ]; then
+	echo >&2 'error: something has gone horribly wrong; .git already exists'
+	echo >&2 '  why do you have BASHBREW_SECOND_STAGE set?'
+	exit 1
+fi
+
+if [ "$pull" = '0' ]; then
+	cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
+	commit='FAKE'
+else
+	# TODO we only have "git version 2.4.1" which doesn't support "clone -q" :(
+	git init -q .
+	git remote add origin https://github.com/docker-library/official-images.git
+	git fetch -q origin
+	git reset -q --hard origin/master
+	git config user.name 'nobody'
+	git config user.email '[email protected]'
+	git fetch -q origin "pull/$pull/head:pr-$pull"
+	git merge -q --no-edit "pr-$pull"
+
+	commit="$(git log -1 --format=format:%h "pr-$pull")"
+fi
 
 if [ "$#" -eq 0 ]; then
 	IFS=$'\n'
-	files=( $(echo "$pat" | awk -F '/' '$1 == "+++ b" && $2 == "library" { print $3 }' | sort -u) )
+	files=( $(git diff --name-only origin/master...HEAD -- library | xargs -n1 basename) )
 	unset IFS
+
+	# TODO narrow this down into groups of the exact tags for each image that changed >:)
 else
 	files=( "$@" )
 fi
@@ -34,11 +72,6 @@ if [ ${#files[@]} -eq 0 ]; then
 	exit 0
 fi
 
-urls=()
-for f in "${files[@]}"; do
-	urls+=( "https://raw.githubusercontent.com/docker-library/official-images/$commit/library/$f" )
-done
-
 join() {
 	sep="$1"
 	arg1="$2"
@@ -49,14 +82,13 @@ join() {
 
 echo 'Build test of' '#'"$pull"';' "$commit" '(`'"$(join '`, `' "${files[@]}")"'`):'
 failed=
-for url in "${urls[@]}"; do
+for img in "${files[@]}"; do
 	echo
 	echo '```console'
-	echo '$ url="'"$url"'"'
-	echo '$ bashbrew build "$url"'
-	if ./bashbrew/bashbrew.sh build "$url"; then
+	echo '$ bashbrew build "'"$img"'"'
+	if ./bashbrew/bashbrew.sh build "$img"; then
 		echo '$ bashbrew list --uniq "$url" | xargs test/run.sh'
-		if ! ./bashbrew/bashbrew.sh list --uniq "$url" | xargs ./test/run.sh; then
+		if ! ./bashbrew/bashbrew.sh list --uniq "$img" | xargs ./test/run.sh; then
 			failed=1
 		fi
 	else