|
| 1 | +#!/bin/bash |
| 2 | +# Basic integration tests with postgres. Requires docker to work. |
| 3 | + |
| 4 | +VERSIONS=( \ |
| 5 | + 9.1 \ |
| 6 | + 9.2 \ |
| 7 | + 9.3 \ |
| 8 | + 9.4 \ |
| 9 | + 9.5 \ |
| 10 | +) |
| 11 | + |
| 12 | +smoketest_postgres() { |
| 13 | + local version=$1 |
| 14 | + local CONTAINER_NAME=postgres_exporter-test-smoke |
| 15 | + local TIMEOUT=30 |
| 16 | + local IMAGE_NAME=postgres |
| 17 | + |
| 18 | + local CUR_IMAGE=$IMAGE_NAME:$version |
| 19 | + |
| 20 | + docker run -d --name=$CONTAINER_NAME -e POSTGRES_PASSWORD=password -p 127.0.0.1:55432:5432 $CUR_IMAGE |
| 21 | + |
| 22 | + local WAIT_START=$(date +%s) |
| 23 | + while ! docker exec $CONTAINER_NAME bash -c "psql -U postgres -c \"select 'running'\" > /dev/null 2>&1 " ; do |
| 24 | + echo "Waiting for postgres to start..." |
| 25 | + if [ $(( $(date +%s) - $WAIT_START )) -gt $TIMEOUT ]; then |
| 26 | + echo "Timed out waiting for postgres!" 1>&2 |
| 27 | + docker logs $CONTAINER_NAME |
| 28 | + docker kill $CONTAINER_NAME |
| 29 | + docker rm $CONTAINER_NAME |
| 30 | + exit 1 |
| 31 | + fi |
| 32 | + sleep 1 |
| 33 | + done |
| 34 | + |
| 35 | + DATA_SOURCE_NAME="postgresql://postgres:password@localhost:55432/?sslmode=disable" ./postgres_exporter & |
| 36 | + exporter_pid=$! |
| 37 | + local DAEMON_WAIT_START=$(date +%s) |
| 38 | + while ! nc -z localhost 9113 ; do |
| 39 | + echo "Waiting for exporter to start..." |
| 40 | + if [ $(( $(date +%s) - $WAIT_START )) -gt $TIMEOUT ]; then |
| 41 | + echo "Timed out waiting for exporter!" 1>&2 |
| 42 | + docker logs $CONTAINER_NAME |
| 43 | + docker kill $CONTAINER_NAME |
| 44 | + docker rm $CONTAINER_NAME |
| 45 | + exit 1 |
| 46 | + fi |
| 47 | + sleep 1 |
| 48 | + done |
| 49 | + |
| 50 | + wget -q -O - http://localhost:9113/metrics 1> /dev/null |
| 51 | + if [ "$?" != "0" ]; then |
| 52 | + echo "Failed on postgres $version ($DOCKER_IMAGE)" 1>&2 |
| 53 | + kill $exporter_pid |
| 54 | + docker logs $CONTAINER_NAME |
| 55 | + docker kill $CONTAINER_NAME |
| 56 | + docker rm $CONTAINER_NAME |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + |
| 60 | + kill $exporter_pid |
| 61 | + docker kill $CONTAINER_NAME |
| 62 | + docker rm $CONTAINER_NAME |
| 63 | +} |
| 64 | + |
| 65 | +# Start pulling the docker images in advance |
| 66 | +for version in ${VERSIONS[@]}; do |
| 67 | + docker pull postgres:$version > /dev/null & |
| 68 | +done |
| 69 | + |
| 70 | +for version in ${VERSIONS[@]}; do |
| 71 | + echo "Testing postgres version $version" |
| 72 | + smoketest_postgres $version |
| 73 | +done |
0 commit comments