From 0d7891de1a439f518b514237fcb7c1137c521004 Mon Sep 17 00:00:00 2001 From: Will Rouesnel Date: Wed, 3 Jul 2019 23:14:20 +1000 Subject: [PATCH] Fix smoke-tests to more reliably use pg_isready. --- .../tests/docker-postgres-replication/Dockerfile | 3 ++- cmd/postgres_exporter/tests/test-smoke | 15 ++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/cmd/postgres_exporter/tests/docker-postgres-replication/Dockerfile b/cmd/postgres_exporter/tests/docker-postgres-replication/Dockerfile index edea0fe49..f12569faf 100755 --- a/cmd/postgres_exporter/tests/docker-postgres-replication/Dockerfile +++ b/cmd/postgres_exporter/tests/docker-postgres-replication/Dockerfile @@ -1,7 +1,8 @@ -FROM postgres:10 +FROM postgres:11 MAINTAINER Daniel Dent (https://www.danieldent.com) ENV PG_MAX_WAL_SENDERS 8 ENV PG_WAL_KEEP_SEGMENTS 8 +RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y inetutils-ping COPY setup-replication.sh /docker-entrypoint-initdb.d/ COPY docker-entrypoint.sh /docker-entrypoint.sh RUN chmod +x /docker-entrypoint-initdb.d/setup-replication.sh /docker-entrypoint.sh diff --git a/cmd/postgres_exporter/tests/test-smoke b/cmd/postgres_exporter/tests/test-smoke index eef65c3b0..4062e235b 100755 --- a/cmd/postgres_exporter/tests/test-smoke +++ b/cmd/postgres_exporter/tests/test-smoke @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash -x # Basic integration tests with postgres. Requires docker to work. SOURCE="${BASH_SOURCE[0]}" @@ -34,8 +34,9 @@ VERSIONS=( \ ) wait_for_postgres(){ - local ip=$1 - local port=$2 + local container=$1 + local ip=$2 + local port=$3 if [ -z "$ip" ]; then echo "No IP specified." 1>&2 exit 1 @@ -49,7 +50,7 @@ wait_for_postgres(){ local wait_start wait_start=$(date +%s) || exit 1 echo "Waiting for postgres to start listening..." - while ! pg_isready --host="$ip" --port="$port" &> /dev/null; do + while ! docker exec "$container" pg_isready --host="$ip" --port="$port" &> /dev/null; do if [ $(( $(date +%s) - wait_start )) -gt "$TIMEOUT" ]; then echo "Timed out waiting for postgres to start!" 1>&2 exit 1 @@ -91,7 +92,7 @@ smoketest_postgres() { standalone_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CONTAINER_NAME) # shellcheck disable=SC2064 trap "docker logs $CONTAINER_NAME ; docker kill $CONTAINER_NAME ; docker rm -v $CONTAINER_NAME; exit 1" EXIT INT TERM - wait_for_postgres "$standalone_ip" 5432 + wait_for_postgres "$CONTAINER_NAME" "$standalone_ip" 5432 # Run the test binary. DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$standalone_ip:5432/?sslmode=disable" $test_binary || exit $? @@ -143,8 +144,8 @@ smoketest_postgres() { master_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$master_container") slave_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$slave_container") echo "Got master IP: $master_ip" - wait_for_postgres "$master_ip" 5432 - wait_for_postgres "$slave_ip" 5432 + wait_for_postgres "$master_container" "$master_ip" 5432 + wait_for_postgres "$slave_container" "$slave_ip" 5432 DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$master_ip:5432/?sslmode=disable" $test_binary || exit $?