Skip to content

Commit 99ff02c

Browse files
committed
Fix the errors introduced into smoke-test from the refactor.
1 parent ae8bea6 commit 99ff02c

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

cmd/postgres_exporter/tests/test-smoke

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/bash -x
22
# Basic integration tests with postgres. Requires docker to work.
33

44
SOURCE="${BASH_SOURCE[0]}"
@@ -38,39 +38,41 @@ VERSIONS=( \
3838
wait_for_postgres(){
3939
local ip=$1
4040
local port=$2
41-
if [ -z $ip ]; then
41+
if [ -z "$ip" ]; then
4242
echo "No IP specified." 1>&2
4343
exit 1
4444
fi
4545

46-
if [ -z $port ]; then
46+
if [ -z "$port" ]; then
4747
echo "No port specified." 1>&2
4848
exit 1
4949
fi
5050

5151
local wait_start
5252
wait_start=$(date +%s) || exit 1
5353
echo "Waiting for postgres to start listening..."
54-
while ! pg_isready --host=$ip --port=$port &> /dev/null; do
55-
if [ $(( $(date +%s) - $wait_start )) -gt $TIMEOUT ]; then
54+
while ! pg_isready --host="$ip" --port="$port" &> /dev/null; do
55+
if [ $(( $(date +%s) - wait_start )) -gt "$TIMEOUT" ]; then
5656
echo "Timed out waiting for postgres to start!" 1>&2
5757
exit 1
5858
fi
5959
sleep 1
6060
done
61+
echo "Postgres is online at $ip:$port"
6162
}
6263

6364
wait_for_exporter() {
6465
local wait_start
6566
wait_start=$(date +%s) || exit 1
6667
echo "Waiting for exporter to start..."
67-
while ! nc -z localhost $exporter_port ; do
68-
if [ $(( $(date +%s) - $wait_start )) -gt $TIMEOUT ]; then
68+
while ! nc -z localhost "$exporter_port" ; do
69+
if [ $(( $(date +%s) - wait_start )) -gt "$TIMEOUT" ]; then
6970
echo "Timed out waiting for exporter!" 1>&2
7071
exit 1
7172
fi
7273
sleep 1
7374
done
75+
echo "Exporter is online at localhost:$exporter_port"
7476
}
7577

7678
smoketest_postgres() {
@@ -89,22 +91,24 @@ smoketest_postgres() {
8991

9092
CONTAINER_NAME=$($docker_cmd)
9193
standalone_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CONTAINER_NAME)
94+
# shellcheck disable=SC2064
9295
trap "docker logs $CONTAINER_NAME ; docker kill $CONTAINER_NAME ; docker rm -v $CONTAINER_NAME; exit 1" EXIT INT TERM
93-
wait_for_postgres $standalone_ip 5432
94-
96+
wait_for_postgres "$standalone_ip" 5432
9597

9698
# Run the test binary.
9799
DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$standalone_ip:5432/?sslmode=disable" $test_binary || exit $?
98100

99101
# Extract a raw metric list.
100-
DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$standalone_ip:5432/?sslmode=disable" $postgres_exporter --log.level=debug --web.listen-address=:$exporter_port &
102+
DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$standalone_ip:5432/?sslmode=disable" $postgres_exporter \
103+
--log.level=debug --web.listen-address=:$exporter_port &
101104
exporter_pid=$!
105+
# shellcheck disable=SC2064
102106
trap "docker logs $CONTAINER_NAME ; docker kill $CONTAINER_NAME ; docker rm -v $CONTAINER_NAME; kill $exporter_pid; exit 1" EXIT INT TERM
103107
wait_for_exporter
104108

105109
# Dump the metrics to a file.
106-
if wget -q -O - http://localhost:$exporter_port/metrics 1> "$METRICS_DIR/.metrics.single.$version.prom" ; then
107-
echo "Failed on postgres $version ($DOCKER_IMAGE)" 1>&2
110+
if ! wget -q -O - http://localhost:$exporter_port/metrics 1> "$METRICS_DIR/.metrics.single.$version.prom" ; then
111+
echo "Failed on postgres $version (standalone $DOCKER_IMAGE)" 1>&2
108112
kill $exporter_pid
109113
exit 1
110114
fi
@@ -117,8 +121,8 @@ smoketest_postgres() {
117121
fi
118122

119123
kill $exporter_pid
120-
docker kill $CONTAINER_NAME
121-
docker rm -v $CONTAINER_NAME
124+
docker kill "$CONTAINER_NAME"
125+
docker rm -v "$CONTAINER_NAME"
122126
trap - EXIT INT TERM
123127

124128
echo "#######################"
@@ -127,7 +131,7 @@ smoketest_postgres() {
127131
old_pwd=$(pwd)
128132
cd docker-postgres-replication || exit 1
129133

130-
if VERSION="$version" p2 -t Dockerfile.p2 -o Dockerfile ; then
134+
if ! VERSION="$version" p2 -t Dockerfile.p2 -o Dockerfile ; then
131135
echo "Templating failed" 1>&2
132136
exit 1
133137
fi
@@ -146,14 +150,15 @@ smoketest_postgres() {
146150

147151
DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$master_ip:5432/?sslmode=disable" $test_binary || exit $?
148152

149-
DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$master_ip:5432/?sslmode=disable" $postgres_exporter --log.level=debug --web.listen-address=:$exporter_port &
153+
DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$master_ip:5432/?sslmode=disable" $postgres_exporter \
154+
--log.level=debug --web.listen-address=:$exporter_port &
150155
exporter_pid=$!
151156
# shellcheck disable=SC2064
152157
trap "docker-compose logs; docker-compose down ; docker-compose rm -v ; kill $exporter_pid; exit 1" EXIT INT TERM
153158
wait_for_exporter
154159

155-
if wget -q -O - http://localhost:$exporter_port/metrics 1> "$METRICS_DIR/.metrics.replicated.$version.prom" ; then
156-
echo "Failed on postgres $version ($DOCKER_IMAGE)" 1>&2
160+
if ! wget -q -O - http://localhost:$exporter_port/metrics 1> "$METRICS_DIR/.metrics.replicated.$version.prom" ; then
161+
echo "Failed on postgres $version (replicated $DOCKER_IMAGE)" 1>&2
157162
exit 1
158163
fi
159164

0 commit comments

Comments
 (0)