Browse Source

Move retry functionality into its own script

Mike Dillon 10 years ago
parent
commit
cef058a195

+ 37 - 0
test/retry.sh

@@ -0,0 +1,37 @@
+#!/bin/bash
+
+set -e
+
+opts="$(getopt -o 'i:c:t:s:' --long 'image:,cid:,tries:,sleep:' -- "$@")"
+eval set -- "$opts"
+while true; do
+	flag=$1
+	shift
+	case "$flag" in
+		--image|-i) image="$1" && shift ;;
+		--cid|-c) cid="$1" && shift ;;
+		--tries|-t) tries="$1" && shift ;;
+		--sleep|-s) sleep="$1" && shift ;;
+		--) break;;
+	esac
+done
+
+if [ $# -eq 0 ]; then
+	echo >&2 'retry.sh requires a command to run'
+	false
+fi
+
+: ${tries:=10}
+: ${sleep:=2}
+
+while ! eval "$@" &> /dev/null; do
+	(( tries-- ))
+	if [ $tries -le 0 ]; then
+		echo >&2 "${image:-the container} failed to accept connections in a reasonable amount of time!"
+		[ "$cid" ] && ( set -x && docker logs "$cid" ) >&2 || true
+		eval "$@" # to hopefully get a useful error message
+		false
+	fi
+	echo >&2 -n .
+	sleep "$sleep"
+done

+ 0 - 37
test/run.sh

@@ -18,43 +18,6 @@ environments.
 EOUSAGE
 }
 
-retry() {
-	[ -z "$image" -o -z "$cid" ] && { echo >&2 'The retry function requires $image and $cid to be set'; false; }
-
-	opts="$(getopt -o 't:s:' --long 'tries:,sleep:' -- "$@")"
-	eval set -- "$opts"
-	while true; do
-		flag=$1
-		shift
-		case "$flag" in
-			--tries|-t) tries="$1" && shift ;;
-			--sleep|-s) sleep="$1" && shift ;;
-			--) break;;
-		esac
-	done
-
-	if [ $# -eq 0 ]; then
-		echo >&2 'The retry function requires a command to run'
-		false
-	fi
-
-	: ${tries:=10}
-	: ${sleep:=2}
-
-	while ! eval "$@" &> /dev/null; do
-		(( tries-- ))
-		if [ $tries -le 0 ]; then
-			echo >&2 "$image failed to accept connections in a reasonable amount of time!"
-			( set -x && docker logs "$cid" ) >&2 || true
-			eval "$@" # to hopefully get a useful error message
-			false
-		fi
-		echo >&2 -n .
-		sleep "$sleep"
-	done
-}
-export -f retry
-
 # arg handling
 opts="$(getopt -o 'ht:?' --long 'dry-run,help,test:' -- "$@" || { usage >&2 && false; })"
 eval set -- "$opts"

+ 1 - 1
test/tests/docker-dind/run.sh

@@ -20,7 +20,7 @@ docker_() {
 		"$@"
 }
 
-retry 'docker_ version'
+. "$dir/../../retry.sh" 'docker_ version'
 
 docker_ pull busybox
 

+ 3 - 1
test/tests/jetty-hello-web/run.sh

@@ -1,5 +1,7 @@
 #!/bin/bash
 
+[ "$DEBUG" ] && set -x
+
 set -eo pipefail
 
 dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
@@ -25,7 +27,7 @@ _request() {
 }
 
 # Make sure that Jetty is listening on port 8080
-retry --tries 40 --sleep 0.25 '[ "$(_request GET / --output /dev/null || echo $?)" = 7 ]'
+. "$dir/../../retry.sh" --tries 40 --sleep 0.25 '[ "$(_request GET / --output /dev/null || echo $?)" = 7 ]'
 
 # Check that we can request /index.jsp with no params
 [ "$(_request GET "/" | tail -1)" = "null" ]

+ 1 - 1
test/tests/mysql-basics/run.sh

@@ -33,7 +33,7 @@ mysql() {
 		"$MYSQL_DATABASE"
 }
 
-retry "echo 'SELECT 1' | mysql"
+. "$dir/../../retry.sh" "echo 'SELECT 1' | mysql"
 
 echo 'CREATE TABLE test (a INT, b INT, c VARCHAR(255))' | mysql
 [ "$(echo 'SELECT COUNT(*) FROM test' | mysql)" = 0 ]

+ 1 - 1
test/tests/mysql-initdb/run.sh

@@ -35,7 +35,7 @@ mysql() {
 		"$MYSQL_DATABASE"
 }
 
-retry --tries 20 "echo 'SELECT 1' | mysql"
+. "$dir/../../retry.sh" --tries 20 "echo 'SELECT 1' | mysql"
 
 [ "$(echo 'SELECT COUNT(*) FROM test' | mysql)" = 1 ]
 [ "$(echo 'SELECT c FROM test' | mysql)" = 'goodbye!' ]

+ 1 - 1
test/tests/postgres-basics/run.sh

@@ -24,7 +24,7 @@ psql() {
 		"$@"
 }
 
-retry --tries "$POSTGRES_TEST_TRIES" --sleep "$POSTGRES_TEST_SLEEP" "echo 'SELECT 1' | psql"
+. "$dir/../../retry.sh" --tries "$POSTGRES_TEST_TRIES" --sleep "$POSTGRES_TEST_SLEEP" "echo 'SELECT 1' | psql"
 
 echo 'CREATE TABLE test (a INT, b INT, c VARCHAR(255))' | psql
 [ "$(echo 'SELECT COUNT(*) FROM test' | psql)" = 0 ]

+ 1 - 1
test/tests/postgres-initdb/run.sh

@@ -33,7 +33,7 @@ psql() {
 		"$@"
 }
 
-retry --tries "$POSTGRES_TEST_TRIES" --sleep "$POSTGRES_TEST_SLEEP" "echo 'SELECT 1' | psql"
+. "$dir/../../retry.sh" --tries "$POSTGRES_TEST_TRIES" --sleep "$POSTGRES_TEST_SLEEP" "echo 'SELECT 1' | psql"
 
 [ "$(echo 'SELECT COUNT(*) FROM test' | psql)" = 1 ]
 [ "$(echo 'SELECT c FROM test' | psql)" = 'goodbye!' ]