1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-05-31 01:16:01 +02:00

Chore: rework docker-postgres.sh to be cross-platform (#1037)

I found the previous docker-postgres.sh script did not
work for me on macOS. this commit abstracts a pair
of commands behind functions which can be customized
by platform.

Co-authored-by: Ivar Conradi Østhus <ivarconr@gmail.com>
This commit is contained in:
David Alpert 2021-10-23 02:12:34 -05:00 committed by GitHub
parent 95fe1bc91c
commit dc9c55234e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 12 deletions

View File

@ -39,6 +39,7 @@
"prepare": "yarn run build",
"test": "NODE_ENV=test PORT=4243 jest",
"test:docker": "./scripts/docker-postgres.sh",
"test:docker:cleanup":"docker rm -f unleash-postgres",
"test:watch": "yarn test --watch",
"test:coverage": "NODE_ENV=test PORT=4243 jest --coverage --forceExit",
"test:coverage:jest": "NODE_ENV=test PORT=4243 jest --silent --ci --json --coverage --testLocationInResults --outputFile=\"report.json\" --forceExit",

View File

@ -1,31 +1,75 @@
#!/bin/bash
set -e
export POSTGRES_PASSWORD="uleash"
echo "starting postgres in docker "
function getDockerDBPort() {
case "$OSTYPE" in
darwin*) echo `docker ps| grep unleash-post| awk '{print $(NF-1)}'| awk -F "->" '{print $1}'| awk -F \: '{print $4}'`;;
# solaris*) echo "SOLARIS" ;;
# linux*) echo "LINUX" ;;
# bsd*) echo "BSD" ;;
# msys*) echo "WINDOWS" ;;
# cygwin*) echo "ALSO WINDOWS" ;;
*) echo `docker ps| grep unleash-post| awk '{print $(NF-1)}'| awk -F "->" '{print $1}'| awk -F \: '{print $2}'`;;
esac
}
HASH=`docker run -P --name unleash-postgres -e POSTGRES_PASSWORD=$POSTGRES_PASSWORD -d postgres:10`
export PGPORT=`docker ps| grep unleash-post| awk '{print $(NF-1)}'| awk -F "->" '{print $1}'| awk -F \: '{print $2}'`
if docker ps | grep unleash-postgres > /dev/null; then
echo "unleash-postgress is already running"
else
echo "starting postgres in docker "
HASH=`docker run -P --name unleash-postgres -e POSTGRES_PASSWORD=$POSTGRES_PASSWORD -d postgres:10`
fi
export PGPORT=$(getDockerDBPort)
if [ -z "$PGPORT" ]; then
echo "could not find PGPORT! is postgres running?"
exit 1
fi
echo "PGPORT: $PGPORT"
echo ""
# ----------- Wait for postgres to start -----------
echo "Waiting for postgres to start..."
function isPostgresAvailable() {
echo "polling $1:$2"
case "$OSTYPE" in
darwin*) nc -z $1 $2;;
# solaris*) echo "SOLARIS" ;;
# linux*) echo "LINUX" ;;
# bsd*) echo "BSD" ;;
# msys*) echo "WINDOWS" ;;
# cygwin*) echo "ALSO WINDOWS" ;;
*) netcat -z $1 $2;;
esac
}
if [ -z "$DOCKER_HOST" ]
then
export database_host="127.0.0.1"
else
export database_host=$(echo $DOCKER_HOST |awk -F \/ '{print $NF}'| awk -F \: '{print $1}')
fi
for i in `seq 1 120`;
do
echo -n "."
sleep 1
netcat -z $database_host $PGPORT && echo "postgres is up and running in docker in $i seconds!" && break
done
export TEST_DATABASE_URL=postgres://postgres:$POSTGRES_PASSWORD@$database_host:$PGPORT/postgres
yarn
for i in `seq 1 120`;
do
# echo -n "."
sleep 1
isPostgresAvailable $database_host $PGPORT && echo "postgres is up and running in docker in $i seconds!" && break
done
echo "running migrations..."
DATABASE_URL=$TEST_DATABASE_URL npm run db-migrate -- up
echo "running tests..."
yarn test
echo "cleanup..."
docker stop $HASH
docker rm $HASH
echo "done"