From 99ff02c6a951ee6655f6c50b2428f1443ebf0d7a Mon Sep 17 00:00:00 2001 From: Will Rouesnel Date: Sun, 11 Nov 2018 15:33:27 +1100 Subject: [PATCH] Fix the errors introduced into smoke-test from the refactor. --- cmd/postgres_exporter/tests/test-smoke | 41 +++++++++++++++----------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/cmd/postgres_exporter/tests/test-smoke b/cmd/postgres_exporter/tests/test-smoke index 3cd43103..b8a886f8 100755 --- a/cmd/postgres_exporter/tests/test-smoke +++ b/cmd/postgres_exporter/tests/test-smoke @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash -x # Basic integration tests with postgres. Requires docker to work. SOURCE="${BASH_SOURCE[0]}" @@ -38,12 +38,12 @@ VERSIONS=( \ wait_for_postgres(){ local ip=$1 local port=$2 - if [ -z $ip ]; then + if [ -z "$ip" ]; then echo "No IP specified." 1>&2 exit 1 fi - if [ -z $port ]; then + if [ -z "$port" ]; then echo "No port specified." 1>&2 exit 1 fi @@ -51,26 +51,28 @@ wait_for_postgres(){ local wait_start wait_start=$(date +%s) || exit 1 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 + 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 fi sleep 1 done + echo "Postgres is online at $ip:$port" } wait_for_exporter() { local wait_start wait_start=$(date +%s) || exit 1 echo "Waiting for exporter to start..." - while ! nc -z localhost $exporter_port ; do - if [ $(( $(date +%s) - $wait_start )) -gt $TIMEOUT ]; then + while ! nc -z localhost "$exporter_port" ; do + if [ $(( $(date +%s) - wait_start )) -gt "$TIMEOUT" ]; then echo "Timed out waiting for exporter!" 1>&2 exit 1 fi sleep 1 done + echo "Exporter is online at localhost:$exporter_port" } smoketest_postgres() { @@ -89,22 +91,24 @@ smoketest_postgres() { CONTAINER_NAME=$($docker_cmd) standalone_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CONTAINER_NAME) + # shellcheck disable=SC2064 trap "docker logs $CONTAINER_NAME ; docker kill $CONTAINER_NAME ; docker rm -v $CONTAINER_NAME; exit 1" EXIT INT TERM - wait_for_postgres $standalone_ip 5432 - + wait_for_postgres "$standalone_ip" 5432 # Run the test binary. DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$standalone_ip:5432/?sslmode=disable" $test_binary || exit $? # Extract a raw metric list. - DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$standalone_ip:5432/?sslmode=disable" $postgres_exporter --log.level=debug --web.listen-address=:$exporter_port & + DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$standalone_ip:5432/?sslmode=disable" $postgres_exporter \ + --log.level=debug --web.listen-address=:$exporter_port & exporter_pid=$! + # shellcheck disable=SC2064 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. - if wget -q -O - http://localhost:$exporter_port/metrics 1> "$METRICS_DIR/.metrics.single.$version.prom" ; then - echo "Failed on postgres $version ($DOCKER_IMAGE)" 1>&2 + if ! wget -q -O - http://localhost:$exporter_port/metrics 1> "$METRICS_DIR/.metrics.single.$version.prom" ; then + echo "Failed on postgres $version (standalone $DOCKER_IMAGE)" 1>&2 kill $exporter_pid exit 1 fi @@ -117,8 +121,8 @@ smoketest_postgres() { fi kill $exporter_pid - docker kill $CONTAINER_NAME - docker rm -v $CONTAINER_NAME + docker kill "$CONTAINER_NAME" + docker rm -v "$CONTAINER_NAME" trap - EXIT INT TERM echo "#######################" @@ -127,7 +131,7 @@ smoketest_postgres() { old_pwd=$(pwd) cd docker-postgres-replication || exit 1 - if VERSION="$version" p2 -t Dockerfile.p2 -o Dockerfile ; then + if ! VERSION="$version" p2 -t Dockerfile.p2 -o Dockerfile ; then echo "Templating failed" 1>&2 exit 1 fi @@ -146,14 +150,15 @@ smoketest_postgres() { DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$master_ip:5432/?sslmode=disable" $test_binary || exit $? - DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$master_ip:5432/?sslmode=disable" $postgres_exporter --log.level=debug --web.listen-address=:$exporter_port & + DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@$master_ip:5432/?sslmode=disable" $postgres_exporter \ + --log.level=debug --web.listen-address=:$exporter_port & exporter_pid=$! # shellcheck disable=SC2064 trap "docker-compose logs; docker-compose down ; docker-compose rm -v ; kill $exporter_pid; exit 1" EXIT INT TERM wait_for_exporter - if wget -q -O - http://localhost:$exporter_port/metrics 1> "$METRICS_DIR/.metrics.replicated.$version.prom" ; then - echo "Failed on postgres $version ($DOCKER_IMAGE)" 1>&2 + if ! wget -q -O - http://localhost:$exporter_port/metrics 1> "$METRICS_DIR/.metrics.replicated.$version.prom" ; then + echo "Failed on postgres $version (replicated $DOCKER_IMAGE)" 1>&2 exit 1 fi