Skip to content

Commit 8cca2a5

Browse files
committed
Add docker based integration test suite.
This simply pulls and runs postgres against a number of versions and checks that we can successfully connect to it.
1 parent 86caf39 commit 8cca2a5

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ go:
77
script:
88
- make all
99
- make docker
10+
- make test-integration
1011
after_success:
1112
- if [ "$TRAVIS_BRANCH" == "master" ]; then docker login -e $DOCKER_EMAIL -u $DOCKER_USER
1213
-p $DOCKER_PASS ; docker push wrouesnel/postgres_exporter ; fi

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ vet:
2121
test:
2222
go test -v .
2323

24+
test-integration:
25+
tests/test-smoke
26+
2427
# Do a self-contained docker build - we pull the official upstream container,
2528
# then template out a dockerfile which builds the real image.
2629
docker-build: postgres_exporter

postgres_exporter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"strconv"
2020
)
2121

22-
var Version string = "0.0.0-dev"
22+
var Version string = "0.0.1"
2323

2424
var (
2525
listenAddress = flag.String(

tests/test-smoke

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)