Skip to content

Commit 2297eb5

Browse files
committed
Tests passing for all postgres versions.
1 parent be1a76b commit 2297eb5

File tree

2 files changed

+27
-44
lines changed

2 files changed

+27
-44
lines changed

tests/docker-postgres-replication/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM postgres:9.1
1+
FROM postgres:9.6
22
MAINTAINER Daniel Dent (https://www.danieldent.com)
33
ENV PG_MAX_WAL_SENDERS 8
44
ENV PG_WAL_KEEP_SEGMENTS 8

tests/test-smoke

+26-43
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
1212
# Read the absolute path to the exporter
1313
postgres_exporter=$(readlink -f $1)
1414
test_binary=$(readlink -f $2)
15-
POSTGRES_PASSWORD=postgres
15+
export POSTGRES_PASSWORD=postgres
1616
exporter_port=9187
1717

1818
cd $DIR
@@ -26,53 +26,35 @@ VERSIONS=( \
2626
9.6 \
2727
)
2828

29-
wait_for_replicated_postgres() {
30-
local CONTAINER_NAME=$1
31-
if [ -z $CONTAINER_NAME ]; then
32-
echo "No container name specified." 1>&2
29+
wait_for_postgres(){
30+
local ip=$1
31+
local port=$2
32+
if [ -z $ip ]; then
33+
echo "No IP specified." 1>&2
3334
exit 1
3435
fi
35-
local WAIT_START=$(date +%s)
36-
echo "Waiting for postgres to initialize..."
37-
while ! docker exec $CONTAINER_NAME bash -c "test -e /tmp/.postgres_init_complete" ; do
38-
if [ $(( $(date +%s) - $WAIT_START )) -gt $TIMEOUT ]; then
39-
echo "Timed out waiting for postgres!" 1>&2
40-
exit 1
41-
fi
42-
sleep 1
43-
done
44-
echo "Waiting for postgres to start..."
45-
while ! docker exec $CONTAINER_NAME bash -c "psql -U postgres -c \"select 'running'\" > /dev/null 2>&1 " ; do
46-
if [ $(( $(date +%s) - $WAIT_START )) -gt $TIMEOUT ]; then
47-
echo "Timed out waiting for postgres!" 1>&2
48-
exit 1
49-
fi
50-
sleep 1
51-
done
52-
}
53-
54-
wait_for_postgres() {
55-
local CONTAINER_NAME=$1
56-
if [ -z $CONTAINER_NAME ]; then
57-
echo "No container name specified." 1>&2
36+
37+
if [ -z $port ]; then
38+
echo "No port specified." 1>&2
5839
exit 1
5940
fi
60-
local WAIT_START=$(date +%s)
61-
echo "Waiting for postgres to start..."
62-
while ! docker exec $CONTAINER_NAME bash -c "psql -U postgres -c \"select 'running'\" > /dev/null 2>&1 " ; do
63-
if [ $(( $(date +%s) - $WAIT_START )) -gt $TIMEOUT ]; then
64-
echo "Timed out waiting for postgres!" 1>&2
65-
exit 1
41+
42+
local wait_start=$(date +%s)
43+
echo "Waiting for postgres to start listening..."
44+
while ! pg_isready --host=$ip --port=$port &> /dev/null; do
45+
if [ $(( $(date +%s) - $wait_start )) -gt $TIMEOUT ]; then
46+
echo "Timed out waiting for postgres to start!" 1>&2
47+
exit 1
6648
fi
6749
sleep 1
68-
done
50+
done
6951
}
7052

7153
wait_for_exporter() {
72-
local DAEMON_WAIT_START=$(date +%s)
54+
local wait_start=$(date +%s)
7355
echo "Waiting for exporter to start..."
7456
while ! nc -z localhost $exporter_port ; do
75-
if [ $(( $(date +%s) - $DAEMON_WAIT_START )) -gt $TIMEOUT ]; then
57+
if [ $(( $(date +%s) - $wait_start )) -gt $TIMEOUT ]; then
7658
echo "Timed out waiting for exporter!" 1>&2
7759
exit 1
7860
fi
@@ -91,12 +73,12 @@ smoketest_postgres() {
9173
echo "#######################"
9274
echo "Standalone Postgres $version"
9375
echo "#######################"
94-
local docker_cmd='docker run -d -e POSTGRES_PASSWORD=$POSTGRES_PASSWORD -p 127.0.0.1:55432:5432 $CUR_IMAGE'
76+
local docker_cmd="docker run -d -e POSTGRES_PASSWORD=$POSTGRES_PASSWORD -p 127.0.0.1:55432:5432 $CUR_IMAGE"
9577
echo "Docker Cmd: $docker_cmd"
9678

97-
CONTAINER_NAME=$(eval $docker_cmd)
79+
CONTAINER_NAME=$($docker_cmd)
9880
trap "docker logs $CONTAINER_NAME ; docker kill $CONTAINER_NAME ; docker rm -v $CONTAINER_NAME; exit 1" EXIT INT TERM
99-
wait_for_postgres $CONTAINER_NAME
81+
wait_for_postgres localhost 55432
10082

10183
DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@localhost:55432/?sslmode=disable" $test_binary --log.level=debug || exit $?
10284
# exporter_pid=$!
@@ -112,7 +94,7 @@ smoketest_postgres() {
11294
#
11395
# kill $exporter_pid
11496
docker kill $CONTAINER_NAME
115-
docker rm $CONTAINER_NAME
97+
docker rm -v $CONTAINER_NAME
11698
trap - EXIT INT TERM
11799

118100
echo "#######################"
@@ -134,9 +116,10 @@ smoketest_postgres() {
134116
master_container=$(docker-compose ps -q pg-master)
135117
slave_container=$(docker-compose ps -q pg-slave)
136118
master_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $master_container)
119+
slave_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $slave_container)
137120
echo "Got master IP: $master_ip"
138-
wait_for_replicated_postgres $master_container
139-
wait_for_replicated_postgres $slave_container
121+
wait_for_postgres $master_ip 5432
122+
wait_for_postgres $slave_ip 5432
140123

141124
DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$master_ip:5432/?sslmode=disable" $test_binary --log.level=debug || exit $?
142125
# exporter_pid=$!

0 commit comments

Comments
 (0)