فهرست منبع

Add a test for new postgres initdb logic

Tianon Gravi 10 سال پیش
والد
کامیت
15f3541b6d
4فایلهای تغییر یافته به همراه54 افزوده شده و 1 حذف شده
  1. 1 0
      test/config.sh
  2. 1 1
      test/tests/postgres-basics/run.sh
  3. 4 0
      test/tests/postgres-initdb/initdb.sql
  4. 48 0
      test/tests/postgres-initdb/run.sh

+ 1 - 0
test/config.sh

@@ -77,6 +77,7 @@ declare -A imageTests=(
 	'
 	[postgres]='
 		postgres-basics
+		postgres-initdb
 	'
 	[python]='
 		python-hy

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

@@ -47,5 +47,5 @@ EOSQL
 [ "$(echo 'SELECT COUNT(*) FROM test' | psql)" = 2 ]
 echo 'DELETE FROM test WHERE a = 1' | psql
 [ "$(echo 'SELECT COUNT(*) FROM test' | psql)" = 1 ]
-[ "$(echo 'SELECT c FROM test' | psql)" = "goodbye!" ]
+[ "$(echo 'SELECT c FROM test' | psql)" = 'goodbye!' ]
 echo 'DROP TABLE test' | psql

+ 4 - 0
test/tests/postgres-initdb/initdb.sql

@@ -0,0 +1,4 @@
+CREATE TABLE test (a INT, b INT, c VARCHAR(255));
+INSERT INTO test VALUES (1, 2, 'hello');
+INSERT INTO test VALUES (2, 3, 'goodbye!');
+DELETE FROM test WHERE a = 1;

+ 48 - 0
test/tests/postgres-initdb/run.sh

@@ -0,0 +1,48 @@
+#!/bin/bash
+set -e
+
+image="$1"
+testDir="$(readlink -f "$(dirname "$BASH_SOURCE")")"
+
+export POSTGRES_USER='my cool postgres user'
+export POSTGRES_PASSWORD='my cool postgres password'
+export POSTGRES_DB='my cool postgres database'
+
+cname="postgres-container-$RANDOM-$RANDOM"
+cid="$(
+	docker run -d \
+		-e POSTGRES_USER \
+		-e POSTGRES_PASSWORD \
+		-e POSTGRES_DB \
+		--name "$cname" \
+		-v "$testDir/initdb.sql:/docker-entrypoint-initdb.d/test.sql":ro \
+		"$image"
+)"
+trap "docker rm -f $cid > /dev/null" EXIT
+
+psql() {
+	docker run --rm -i \
+		--link "$cname":postgres \
+		--entrypoint psql \
+		-e PGPASSWORD="$POSTGRES_PASSWORD" \
+		"$image" \
+		--host postgres \
+		--username "$POSTGRES_USER" \
+		--dbname "$POSTGRES_DB" \
+		--quiet --no-align --tuples-only \
+		"$@"
+}
+
+tries=10
+while ! echo 'SELECT 1' | psql &> /dev/null; do
+	(( tries-- ))
+	if [ $tries -le 0 ]; then
+		echo >&2 'postgres failed to accept connetions in a reasonable amount of time!'
+		echo 'SELECT 1' | psql # to hopefully get a useful error message
+		false
+	fi
+	sleep 2
+done
+
+[ "$(echo 'SELECT COUNT(*) FROM test' | psql)" = 1 ]
+[ "$(echo 'SELECT c FROM test' | psql)" = 'goodbye!' ]