81 lines
1.9 KiB
Bash
Executable File
81 lines
1.9 KiB
Bash
Executable File
#!/bin/sh
|
|
set -eu
|
|
|
|
ROOT=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
|
|
cd "$ROOT"
|
|
|
|
if [ ! -f "$ROOT/.env" ]; then
|
|
echo "Missing $ROOT/.env. Copy .env.example to .env first." >&2
|
|
exit 1
|
|
fi
|
|
|
|
set -a
|
|
# shellcheck source=/dev/null
|
|
. "$ROOT/.env"
|
|
set +a
|
|
|
|
if [ "${DATABASE_MODE:-container}" != container ]; then
|
|
echo "scripts/test.sh requires the isolated local DATABASE_MODE=container setup." >&2
|
|
exit 1
|
|
fi
|
|
|
|
: "${POSTGRES_USER:?Set POSTGRES_USER in .env}"
|
|
: "${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env}"
|
|
|
|
run_id="$(date -u +%Y%m%d%H%M%S)-$$"
|
|
image="who-need-help:test-$run_id"
|
|
container="who-need-help-test-$run_id"
|
|
|
|
image_tag_is_referenced() {
|
|
expected_image=$1
|
|
|
|
for container_id in $(docker ps -aq --filter "ancestor=$expected_image"); do
|
|
observed_image=$(docker inspect --format '{{.Config.Image}}' "$container_id")
|
|
if [ "$observed_image" = "$expected_image" ]; then
|
|
return 0
|
|
fi
|
|
done
|
|
|
|
return 1
|
|
}
|
|
|
|
cleanup() {
|
|
status=$?
|
|
cleanup_status=0
|
|
trap - EXIT HUP INT TERM
|
|
|
|
if docker inspect "$container" >/dev/null 2>&1; then
|
|
docker rm -f "$container" >/dev/null 2>&1 || cleanup_status=$?
|
|
fi
|
|
|
|
if docker image inspect "$image" >/dev/null 2>&1; then
|
|
if image_tag_is_referenced "$image"; then
|
|
echo "Refusing to remove referenced test image: $image" >&2
|
|
cleanup_status=1
|
|
else
|
|
docker image rm "$image" >/dev/null || cleanup_status=$?
|
|
fi
|
|
fi
|
|
|
|
if [ "$status" -eq 0 ] && [ "$cleanup_status" -ne 0 ]; then
|
|
status=$cleanup_status
|
|
fi
|
|
|
|
exit "$status"
|
|
}
|
|
trap cleanup EXIT HUP INT TERM
|
|
|
|
docker compose -p who_need_help up -d --wait db
|
|
docker build --target test --tag "$image" .
|
|
|
|
docker run --rm \
|
|
--name "$container" \
|
|
--network who_need_help_internal \
|
|
--env MIX_ENV=test \
|
|
--env DB_HOST=db \
|
|
--env "DB_USER=$POSTGRES_USER" \
|
|
--env "DB_PASSWORD=$POSTGRES_PASSWORD" \
|
|
--env TEST_POOL_SIZE="${TEST_POOL_SIZE:-10}" \
|
|
"$image" \
|
|
mix test
|