2016-05-28 07:03:38 +00:00
#!/bin/bash
# Basic integration tests with postgres. Requires docker to work.
2016-11-16 13:58:25 +00:00
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
2017-04-13 15:21:31 +00:00
METRICS_DIR=$(pwd)
2016-11-16 15:00:42 +00:00
# Read the absolute path to the exporter
postgres_exporter=$(readlink -f $1)
2016-11-17 11:14:13 +00:00
test_binary=$(readlink -f $2)
2016-11-17 15:47:24 +00:00
export POSTGRES_PASSWORD=postgres
2016-11-16 15:00:42 +00:00
exporter_port=9187
2016-11-16 13:58:25 +00:00
cd $DIR
2016-05-28 07:03:38 +00:00
VERSIONS=( \
9.1 \
9.2 \
9.3 \
9.4 \
9.5 \
2016-11-16 13:58:25 +00:00
9.6 \
2016-05-28 07:03:38 +00:00
)
2016-11-17 15:47:24 +00:00
wait_for_postgres(){
local ip=$1
local port=$2
if [ -z $ip ]; then
echo "No IP specified." 1>&2
2016-11-17 15:26:33 +00:00
exit 1
fi
2016-11-17 15:47:24 +00:00
if [ -z $port ]; then
echo "No port specified." 1>&2
2016-11-17 11:14:13 +00:00
exit 1
fi
2016-11-17 15:47:24 +00:00
local wait_start=$(date +%s)
echo "Waiting for postgres to start listening..."
while ! 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
2016-05-28 07:03:38 +00:00
fi
sleep 1
2016-11-17 15:47:24 +00:00
done
2016-11-17 11:14:13 +00:00
}
2016-05-28 07:03:38 +00:00
2016-11-17 11:14:13 +00:00
wait_for_exporter() {
2016-11-17 15:47:24 +00:00
local wait_start=$(date +%s)
2016-11-17 11:14:13 +00:00
echo "Waiting for exporter to start..."
2016-11-16 15:00:42 +00:00
while ! nc -z localhost $exporter_port ; do
2016-11-17 15:47:24 +00:00
if [ $(( $(date +%s) - $wait_start )) -gt $TIMEOUT ]; then
2016-05-28 07:03:38 +00:00
echo "Timed out waiting for exporter!" 1>&2
exit 1
fi
sleep 1
done
2016-11-17 11:14:13 +00:00
}
2016-05-28 07:03:38 +00:00
2016-11-17 11:14:13 +00:00
smoketest_postgres() {
local version=$1
local CONTAINER_NAME=postgres_exporter-test-smoke
local TIMEOUT=30
local IMAGE_NAME=postgres
local CUR_IMAGE=$IMAGE_NAME:$version
2016-11-17 15:26:33 +00:00
echo "#######################"
echo "Standalone Postgres $version"
echo "#######################"
2016-11-17 15:47:24 +00:00
local docker_cmd="docker run -d -e POSTGRES_PASSWORD=$POSTGRES_PASSWORD -p 127.0.0.1:55432:5432 $CUR_IMAGE"
2016-11-17 15:26:33 +00:00
echo "Docker Cmd: $docker_cmd"
2016-11-17 15:47:24 +00:00
CONTAINER_NAME=$($docker_cmd)
2016-11-17 15:26:33 +00:00
trap "docker logs $CONTAINER_NAME ; docker kill $CONTAINER_NAME ; docker rm -v $CONTAINER_NAME; exit 1" EXIT INT TERM
2016-11-17 15:47:24 +00:00
wait_for_postgres localhost 55432
2016-05-28 07:03:38 +00:00
2017-04-13 15:21:31 +00:00
# Run the test binary.
2016-11-17 15:26:33 +00:00
DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@localhost:55432/?sslmode=disable" $test_binary --log.level=debug || exit $?
2017-02-24 13:19:07 +00:00
2017-04-13 15:21:31 +00:00
# Extract a raw metric list.
DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@localhost:55432/?sslmode=disable" $postgres_exporter --log.level=debug --web.listen-address=:$exporter_port &
exporter_pid=$!
trap "docker logs $CONTAINER_NAME ; docker kill $CONTAINER_NAME ; docker rm -v $CONTAINER_NAME; kill $exporter_pid; exit 1" EXIT INT TERM
wait_for_exporter
# Dump the metrics to a file.
wget -q -O - http://localhost:$exporter_port/metrics 1> $METRICS_DIR/.metrics.single.$version.prom
if [ "$?" != "0" ]; then
echo "Failed on postgres $version ($DOCKER_IMAGE)" 1>&2
kill $exporter_pid
exit 1
fi
kill $exporter_pid
2016-05-28 07:03:38 +00:00
docker kill $CONTAINER_NAME
2016-11-17 15:47:24 +00:00
docker rm -v $CONTAINER_NAME
2016-11-16 13:58:25 +00:00
trap - EXIT INT TERM
2016-11-17 15:26:33 +00:00
echo "#######################"
echo "Replicated Postgres $version"
echo "#######################"
2016-11-16 13:58:25 +00:00
old_pwd=$(pwd)
cd docker-postgres-replication
VERSION=$version p2 -t Dockerfile.p2 -o Dockerfile
if [ "$?" != "0" ]; then
echo "Templating failed" 1>&2
exit 1
fi
2016-11-16 15:00:42 +00:00
trap "docker-compose logs; docker-compose down ; docker-compose rm -v; exit 1" EXIT INT TERM
2016-11-17 15:26:33 +00:00
local compose_cmd="POSTGRES_PASSWORD=$POSTGRES_PASSWORD docker-compose up -d --force-recreate --build"
echo "Compose Cmd: $compose_cmd"
eval $compose_cmd
2016-11-16 13:58:25 +00:00
master_container=$(docker-compose ps -q pg-master)
slave_container=$(docker-compose ps -q pg-slave)
master_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $master_container)
2016-11-17 15:47:24 +00:00
slave_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $slave_container)
2016-11-17 15:26:33 +00:00
echo "Got master IP: $master_ip"
2016-11-17 15:47:24 +00:00
wait_for_postgres $master_ip 5432
wait_for_postgres $slave_ip 5432
2016-11-16 13:58:25 +00:00
2016-11-17 15:26:33 +00:00
DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$master_ip:5432/?sslmode=disable" $test_binary --log.level=debug || exit $?
2016-11-16 13:58:25 +00:00
2017-04-13 15:21:31 +00:00
DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$master_ip:5432/?sslmode=disable" $postgres_exporter --log.level=debug --web.listen-address=:$exporter_port &
exporter_pid=$!
trap "docker-compose logs; docker-compose down ; docker-compose rm -v ; kill $exporter_pid; exit 1" EXIT INT TERM
wait_for_exporter
wget -q -O - http://localhost:$exporter_port/metrics 1> $METRICS_DIR/.metrics.replicated.$version.prom
if [ "$?" != "0" ]; then
echo "Failed on postgres $version ($DOCKER_IMAGE)" 1>&2
exit 1
fi
kill $exporter_pid
2016-11-16 13:58:25 +00:00
docker-compose down
docker-compose rm -v
trap - EXIT INT TERM
cd $old_pwd
2016-05-28 07:03:38 +00:00
}
# Start pulling the docker images in advance
for version in ${VERSIONS[@]}; do
docker pull postgres:$version > /dev/null &
done
for version in ${VERSIONS[@]}; do
echo "Testing postgres version $version"
smoketest_postgres $version
done