Browse Source

Merge pull request #1045 from appropriate/retry-function

Retry script for tests
Tianon Gravi 10 years ago
parent
commit
3ea2df395e

+ 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

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

@@ -20,18 +20,7 @@ docker_() {
 		"$@"
 }
 
-tries=10
-while ! docker_ version &> /dev/null; do
-	(( tries-- ))
-	if [ $tries -le 0 ]; then
-		echo >&2 'docker daemon failed to accept connections in a reasonable amount of time!'
-		( set -x && docker logs "$cid" ) >&2 || true
-		docker_ version # to hopefully get a useful error message
-		false
-	fi
-	echo >&2 -n .
-	sleep 2
-done
+. "$dir/../../retry.sh" 'docker_ version'
 
 docker_ pull busybox
 

+ 3 - 16
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,22 +27,7 @@ _request() {
 }
 
 # Make sure that Jetty is listening on port 8080
-attempts=40
-tried="$attempts"
-duration=0.25
-while [ "$tried" -ge 0 -a "$(_request GET / --output /dev/null || echo $?)" = 7 ]; do
-	(( tried-- ))
-
-	if [ "$tried" -le 0 ]; then
-		echo >&2 "Unable to connect to Jetty. Aborting."
-		( set -x && docker logs "$cid" ) >&2 || true
-		false
-	fi
-
-	echo >&2 -n .
-
-	sleep "$duration"
-done
+. "$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 - 12
test/tests/mysql-basics/run.sh

@@ -33,18 +33,7 @@ mysql() {
 		"$MYSQL_DATABASE"
 }
 
-tries=20
-while ! echo 'SELECT 1' | mysql &> /dev/null; do
-	(( tries-- ))
-	if [ $tries -le 0 ]; then
-		echo >&2 'mysqld failed to accept connections in a reasonable amount of time!'
-		( set -x && docker logs "$cid" ) >&2 || true
-		echo 'SELECT 1' | mysql # to hopefully get a useful error message
-		false
-	fi
-	echo >&2 -n .
-	sleep 2
-done
+. "$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 - 12
test/tests/mysql-initdb/run.sh

@@ -35,18 +35,7 @@ mysql() {
 		"$MYSQL_DATABASE"
 }
 
-tries=20
-while ! echo 'SELECT 1' | mysql &> /dev/null; do
-	(( tries-- ))
-	if [ $tries -le 0 ]; then
-		echo >&2 'mysqld failed to accept connections in a reasonable amount of time!'
-		( set -x && docker logs "$cid" ) >&2 || true
-		echo 'SELECT 1' | mysql # to hopefully get a useful error message
-		false
-	fi
-	echo >&2 -n .
-	sleep 2
-done
+. "$dir/../../retry.sh" --tries 20 "echo 'SELECT 1' | mysql"
 
 [ "$(echo 'SELECT COUNT(*) FROM test' | mysql)" = 1 ]
 [ "$(echo 'SELECT c FROM test' | mysql)" = 'goodbye!' ]

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

@@ -24,21 +24,7 @@ psql() {
 		"$@"
 }
 
-: ${POSTGRES_TEST_TRIES:=10}
-: ${POSTGRES_TEST_SLEEP:=2}
-
-tries="$POSTGRES_TEST_TRIES"
-while ! echo 'SELECT 1' | psql &> /dev/null; do
-	(( tries-- ))
-	if [ $tries -le 0 ]; then
-		echo >&2 'postgres failed to accept connections in a reasonable amount of time!'
-		( set -x && docker logs "$cid" ) >&2 || true
-		echo 'SELECT 1' | psql # to hopefully get a useful error message
-		false
-	fi
-	echo >&2 -n .
-	sleep "$POSTGRES_TEST_SLEEP"
-done
+. "$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 - 15
test/tests/postgres-initdb/run.sh

@@ -33,21 +33,7 @@ psql() {
 		"$@"
 }
 
-: ${POSTGRES_TEST_TRIES:=10}
-: ${POSTGRES_TEST_SLEEP:=2}
-
-tries="$POSTGRES_TEST_TRIES"
-while ! echo 'SELECT 1' | psql &> /dev/null; do
-	(( tries-- ))
-	if [ $tries -le 0 ]; then
-		echo >&2 'postgres failed to accept connections in a reasonable amount of time!'
-		( set -x && docker logs "$cid" ) >&2 || true
-		echo 'SELECT 1' | psql # to hopefully get a useful error message
-		false
-	fi
-	echo >&2 -n .
-	sleep "${POSTGRES_TEST_SLEEP}"
-done
+. "$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!' ]