#!/bin/bash

set -e

cd $(dirname "$0");

PG_VERSION=$1
TESTCMD=$2

docker build -t pgmemento-test--$PG_VERSION - < ./Dockerfile-$PG_VERSION;
CONTAINER=$(docker run -d -e POSTGRES_PASSWORD=password -v $(pwd)/../..:/home/pgmemento pgmemento-test--$PG_VERSION);

echo "Waiting for 10 seconds to let the database init before we roll";
sleep 10;
echo ""
echo ""
echo ""
echo "== Launching the database ======================================================================================"
echo "================================================================================================================"
echo "================================================================================================================"
echo "================================================================================================================"
echo "================================================================================================================"
echo "================================================================================================================"
docker logs $CONTAINER
echo "================================================================================================================"
echo "================================================================================================================"
echo "================================================================================================================"
echo "================================================================================================================"
echo "================================================================================================================"
echo "== Running tests ==============================================================================================="
echo ""
echo "$TESTCMD"
echo ""
trap "[ ! \"$(docker ps -a | grep $CONTAINER)\" ] && docker logs $CONTAINER" EXIT

eval "$TESTCMD"
docker stop $CONTAINER
