From 53ad0efbbbadb8401db46e87f95837946fda311c Mon Sep 17 00:00:00 2001 From: Will Rouesnel Date: Thu, 17 Nov 2016 02:00:42 +1100 Subject: [PATCH] Add docker-compose and replication environment tests to the smoke-tests. Use docker-compose and p2 to setup a replicated environment for each version of PostgreSQL we want to support. Still missing: key-specific tests to ensure each version doesn't lose metrics. --- Makefile | 4 +-- tests/docker-postgres-replication/Dockerfile | 7 +++++ tests/test-smoke | 30 +++++++++++--------- 3 files changed, 26 insertions(+), 15 deletions(-) create mode 100755 tests/docker-postgres-replication/Dockerfile diff --git a/Makefile b/Makefile index a55e4349..204b40fd 100644 --- a/Makefile +++ b/Makefile @@ -19,8 +19,8 @@ vet: test: go test -v . -test-integration: - tests/test-smoke +test-integration: postgres_exporter + tests/test-smoke ./postgres_exporter # Do a self-contained docker build - we pull the official upstream container # and do a self-contained build. diff --git a/tests/docker-postgres-replication/Dockerfile b/tests/docker-postgres-replication/Dockerfile new file mode 100755 index 00000000..e17409a8 --- /dev/null +++ b/tests/docker-postgres-replication/Dockerfile @@ -0,0 +1,7 @@ +FROM postgres:9.6 +MAINTAINER Daniel Dent (https://www.danieldent.com) +ENV PG_MAX_WAL_SENDERS 8 +ENV PG_WAL_KEEP_SEGMENTS 8 +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/tests/test-smoke b/tests/test-smoke index 6619f2ef..8fa00b28 100755 --- a/tests/test-smoke +++ b/tests/test-smoke @@ -9,6 +9,11 @@ while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symli done DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" +# Read the absolute path to the exporter +postgres_exporter=$(readlink -f $1) +exporter_port=9187 +$exporter_port=password + cd $DIR VERSIONS=( \ @@ -29,9 +34,9 @@ smoketest_postgres() { local CUR_IMAGE=$IMAGE_NAME:$version echo "Test standalone cluster..." - docker run -d --name=$CONTAINER_NAME -e POSTGRES_PASSWORD=password -p 127.0.0.1:55432:5432 $CUR_IMAGE + CONTAINER_NAME=$(docker run -d -e POSTGRES_PASSWORD=$POSTGRES_PASSWORD -p 127.0.0.1:55432:5432 $CUR_IMAGE) - trap "docker logs $CONTAINER_NAME ; docker kill $CONTAINER_NAME ; docker rm $CONTAINER_NAME" EXIT INT TERM + trap "docker logs $CONTAINER_NAME ; docker kill $CONTAINER_NAME ; docker rm $CONTAINER_NAME; exit 1" EXIT INT TERM local WAIT_START=$(date +%s) while ! docker exec $CONTAINER_NAME bash -c "psql -U postgres -c \"select 'running'\" > /dev/null 2>&1 " ; do @@ -43,11 +48,11 @@ smoketest_postgres() { sleep 1 done - DATA_SOURCE_NAME="postgresql://postgres:password@localhost:55432/?sslmode=disable" ./postgres_exporter & + DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@localhost:55432/?sslmode=disable" $postgres_exporter & exporter_pid=$! - trap "docker logs $CONTAINER_NAME ; docker kill $CONTAINER_NAME ; docker rm $CONTAINER_NAME ; kill $exporter_pid" EXIT INT TERM + trap "docker logs $CONTAINER_NAME ; docker kill $CONTAINER_NAME ; docker rm $CONTAINER_NAME ; kill $exporter_pid; exit 1" EXIT INT TERM local DAEMON_WAIT_START=$(date +%s) - while ! nc -z localhost 9113 ; do + while ! nc -z localhost $exporter_port ; do echo "Waiting for exporter to start..." if [ $(( $(date +%s) - $WAIT_START )) -gt $TIMEOUT ]; then echo "Timed out waiting for exporter!" 1>&2 @@ -56,7 +61,7 @@ smoketest_postgres() { sleep 1 done - wget -q -O - http://localhost:9113/metrics 1> /dev/null + wget -q -O - http://localhost:$exporter_port/metrics 1> /dev/null if [ "$?" != "0" ]; then echo "Failed on postgres $version ($DOCKER_IMAGE)" 1>&2 kill $exporter_pid @@ -69,7 +74,6 @@ smoketest_postgres() { trap - EXIT INT TERM echo "Test replicated cluster..." - postgres_exporter=$(readlink -f ./postgres_exporter) old_pwd=$(pwd) cd docker-postgres-replication @@ -78,8 +82,8 @@ smoketest_postgres() { echo "Templating failed" 1>&2 exit 1 fi - trap "docker-compose logs; docker-compose down ; docker-compose rm -v" EXIT INT TERM - docker-compose up -d --force-recreate --build + trap "docker-compose logs; docker-compose down ; docker-compose rm -v; exit 1" EXIT INT TERM + POSTGRES_PASSWORD=$POSTGRES_PASSWORD docker-compose up -d --force-recreate --build master_container=$(docker-compose ps -q pg-master) slave_container=$(docker-compose ps -q pg-slave) @@ -105,11 +109,11 @@ smoketest_postgres() { sleep 1 done - DATA_SOURCE_NAME="postgresql://postgres:password@$master_ip:5432/?sslmode=disable" $postgres_exporter & + DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$master_ip:5432/?sslmode=disable" $postgres_exporter & exporter_pid=$! - trap "docker-compose logs; docker-compose down ; docker-compose rm -v ; kill $exporter_pid" EXIT INT TERM + trap "docker-compose logs; docker-compose down ; docker-compose rm -v ; kill $exporter_pid; exit 1" EXIT INT TERM local DAEMON_WAIT_START=$(date +%s) - while ! nc -z localhost 9113 ; do + while ! nc -z localhost $exporter_port ; do echo "Waiting for exporter to start..." if [ $(( $(date +%s) - $WAIT_START )) -gt $TIMEOUT ]; then echo "Timed out waiting for exporter!" 1>&2 @@ -118,7 +122,7 @@ smoketest_postgres() { sleep 1 done - wget -q -O - http://localhost:9113/metrics 1> /dev/null + wget -q -O - http://localhost:$exporter_port/metrics 1> /dev/null if [ "$?" != "0" ]; then echo "Failed on postgres $version ($DOCKER_IMAGE)" 1>&2 exit 1