test: add PostgreSQL migration integration coverage
CI / verify (pull_request) Successful in 19s
CI / infrastructure-smoke (pull_request) Successful in 21s
CI / migration-smoke (pull_request) Failing after 16s

This commit is contained in:
ekklesia
2026-07-21 09:37:15 -05:00
parent b38f85f438
commit 831a48187c
16 changed files with 1690 additions and 96 deletions
+112
View File
@@ -0,0 +1,112 @@
#!/usr/bin/env bash
COMPOSE_JOB_NETWORK_RUNNING_IN_CONTAINER=false
COMPOSE_JOB_NETWORK_CURRENT_CONTAINER=""
COMPOSE_JOB_NETWORK_NAME=""
COMPOSE_JOB_NETWORK_CONNECTED=false
compose_job_network_initialize() {
COMPOSE_JOB_NETWORK_RUNNING_IN_CONTAINER=false
COMPOSE_JOB_NETWORK_CURRENT_CONTAINER=""
COMPOSE_JOB_NETWORK_NAME=""
COMPOSE_JOB_NETWORK_CONNECTED=false
if [[ ! -f /.dockerenv ]]; then
return 0
fi
if [[ -z "${HOSTNAME:-}" ]]; then
return 1
fi
COMPOSE_JOB_NETWORK_RUNNING_IN_CONTAINER=true
COMPOSE_JOB_NETWORK_CURRENT_CONTAINER="$HOSTNAME"
}
compose_job_network_connect() {
if [[ "$COMPOSE_JOB_NETWORK_RUNNING_IN_CONTAINER" != "true" ]]; then
return 1
fi
if (($# == 0)); then
return 1
fi
local service="$1"
shift
if [[ -z "$service" || $# -eq 0 ]]; then
return 1
fi
local service_container_output
if ! service_container_output="$("$@" ps -q "$service" 2>/dev/null)"; then
return 1
fi
local service_containers=()
while IFS= read -r container_id; do
if [[ -n "$container_id" ]]; then
service_containers+=("$container_id")
fi
done <<<"$service_container_output"
if ((${#service_containers[@]} != 1)); then
return 1
fi
local service_network_output
if ! service_network_output="$(
docker inspect \
--format '{{range $networkName, $_ := .NetworkSettings.Networks}}{{println $networkName}}{{end}}' \
"${service_containers[0]}" 2>/dev/null
)"; then
return 1
fi
local service_networks=()
while IFS= read -r network_name; do
if [[ -n "$network_name" ]]; then
service_networks+=("$network_name")
fi
done <<<"$service_network_output"
if ((${#service_networks[@]} != 1)); then
return 1
fi
COMPOSE_JOB_NETWORK_NAME="${service_networks[0]}"
local current_network_output
if ! current_network_output="$(
docker inspect \
--format '{{range $networkName, $_ := .NetworkSettings.Networks}}{{println $networkName}}{{end}}' \
"$COMPOSE_JOB_NETWORK_CURRENT_CONTAINER" 2>/dev/null
)"; then
return 1
fi
while IFS= read -r network_name; do
if [[ "$network_name" == "$COMPOSE_JOB_NETWORK_NAME" ]]; then
COMPOSE_JOB_NETWORK_CONNECTED=true
return 0
fi
done <<<"$current_network_output"
COMPOSE_JOB_NETWORK_CONNECTED=true
if ! docker network connect \
"$COMPOSE_JOB_NETWORK_NAME" \
"$COMPOSE_JOB_NETWORK_CURRENT_CONTAINER" >/dev/null 2>&1; then
docker network disconnect \
"$COMPOSE_JOB_NETWORK_NAME" \
"$COMPOSE_JOB_NETWORK_CURRENT_CONTAINER" >/dev/null 2>&1 || true
COMPOSE_JOB_NETWORK_CONNECTED=false
return 1
fi
}
compose_job_network_disconnect() {
if [[ "$COMPOSE_JOB_NETWORK_CONNECTED" != "true" ]]; then
return 0
fi
if ! docker network disconnect \
"$COMPOSE_JOB_NETWORK_NAME" \
"$COMPOSE_JOB_NETWORK_CURRENT_CONTAINER" >/dev/null 2>&1; then
return 1
fi
COMPOSE_JOB_NETWORK_CONNECTED=false
}
+13 -58
View File
@@ -5,6 +5,9 @@ set -Eeuo pipefail
repository_root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$repository_root"
# shellcheck source=scripts/lib/compose-job-network.sh
source "$repository_root/scripts/lib/compose-job-network.sh"
command -v docker >/dev/null || {
echo "Docker is required for the infrastructure smoke test." >&2
exit 1
@@ -50,17 +53,9 @@ compose=(
--env-file .env.example
--file infrastructure/compose.yaml
)
running_in_container=false
current_container=""
compose_network=""
connected_to_compose_network=false
if [[ -f /.dockerenv ]]; then
running_in_container=true
if [[ -z "${HOSTNAME:-}" ]]; then
echo "HOSTNAME is required when the smoke test runs inside a Docker container." >&2
exit 1
fi
current_container="$HOSTNAME"
if ! compose_job_network_initialize; then
echo "The current Docker job container could not be identified." >&2
exit 1
fi
cleanup() {
@@ -70,15 +65,10 @@ cleanup() {
if ((exit_status != 0)); then
"${compose[@]}" logs --no-color >&2 || true
fi
if [[ "$connected_to_compose_network" == "true" ]]; then
if ! cleanup_output="$(
docker network disconnect "$compose_network" "$current_container" 2>&1
)"; then
echo "Infrastructure smoke network cleanup failed:" >&2
echo "$cleanup_output" >&2
if ((exit_status == 0)); then
exit_status=1
fi
if ! compose_job_network_disconnect; then
echo "Infrastructure smoke network cleanup failed." >&2
if ((exit_status == 0)); then
exit_status=1
fi
fi
if ! cleanup_output="$(
@@ -133,46 +123,11 @@ if "${compose[@]}" exec -T \
exit 1
fi
if [[ "$running_in_container" == "true" ]]; then
object_storage_container="$("${compose[@]}" ps -q object-storage)"
if [[ -z "$object_storage_container" ]]; then
echo "Compose did not return the object-storage container ID." >&2
if [[ "$COMPOSE_JOB_NETWORK_RUNNING_IN_CONTAINER" == "true" ]]; then
if ! compose_job_network_connect object-storage "${compose[@]}"; then
echo "The infrastructure smoke job could not join the isolated storage network." >&2
exit 1
fi
network_output="$(
docker inspect \
--format '{{range $networkName, $_ := .NetworkSettings.Networks}}{{println $networkName}}{{end}}' \
"$object_storage_container"
)"
object_storage_networks=()
while IFS= read -r network_name; do
if [[ -n "$network_name" ]]; then
object_storage_networks+=("$network_name")
fi
done <<<"$network_output"
if ((${#object_storage_networks[@]} != 1)); then
echo "Expected object-storage to use exactly one Compose network." >&2
exit 1
fi
compose_network="${object_storage_networks[0]}"
current_network_output="$(
docker inspect \
--format '{{range $networkName, $_ := .NetworkSettings.Networks}}{{println $networkName}}{{end}}' \
"$current_container"
)"
already_connected=false
while IFS= read -r network_name; do
if [[ "$network_name" == "$compose_network" ]]; then
already_connected=true
break
fi
done <<<"$current_network_output"
if [[ "$already_connected" != "true" ]]; then
docker network connect "$compose_network" "$current_container"
connected_to_compose_network=true
fi
s3_endpoint="http://object-storage:8333"
else
s3_binding="$("${compose[@]}" port object-storage 8333)"
+100
View File
@@ -0,0 +1,100 @@
#!/usr/bin/env bash
set -Eeuo pipefail
repository_root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$repository_root"
# shellcheck source=scripts/lib/compose-job-network.sh
source "$repository_root/scripts/lib/compose-job-network.sh"
command -v docker >/dev/null || {
echo "Docker is required for the PostgreSQL migration integration suite." >&2
exit 1
}
command -v pnpm >/dev/null || {
echo "pnpm is required for the PostgreSQL migration integration suite." >&2
exit 1
}
if ! docker compose version >/dev/null 2>&1; then
echo "Docker Compose is required for the PostgreSQL migration integration suite." >&2
exit 1
fi
if ! compose_job_network_initialize; then
echo "The current Docker job container could not be identified." >&2
exit 1
fi
raw_project="ekklesia-migration-${GITHUB_RUN_ID:-local}-${GITHUB_RUN_ATTEMPT:-0}-${GITHUB_JOB:-job}-$$"
project="${raw_project//[^a-zA-Z0-9_-]/-}"
project="${project,,}"
export POSTGRES_DB="ekklesia_migration_test"
export POSTGRES_USER="ekklesia_migration_test"
export POSTGRES_PASSWORD="temporary-$project-${RANDOM}-${RANDOM}"
export POSTGRES_PORT="0"
compose=(
docker compose
--project-name "$project"
--env-file .env.example
--file infrastructure/compose.yaml
)
cleanup() {
local exit_status=$?
trap - EXIT INT TERM
if ! compose_job_network_disconnect; then
echo "PostgreSQL migration test network cleanup failed." >&2
if ((exit_status == 0)); then
exit_status=1
fi
fi
if ! "${compose[@]}" down --volumes --remove-orphans --timeout 10 >/dev/null 2>&1; then
echo "PostgreSQL migration test infrastructure cleanup failed." >&2
if ((exit_status == 0)); then
exit_status=1
fi
fi
exit "$exit_status"
}
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
if ! "${compose[@]}" config --quiet >/dev/null 2>&1; then
echo "PostgreSQL migration test infrastructure configuration is invalid." >&2
exit 1
fi
if ! "${compose[@]}" up --detach --wait --wait-timeout 120 postgres >/dev/null 2>&1; then
echo "Disposable PostgreSQL did not become ready." >&2
exit 1
fi
if [[ "$COMPOSE_JOB_NETWORK_RUNNING_IN_CONTAINER" == "true" ]]; then
if ! compose_job_network_connect postgres "${compose[@]}"; then
echo "The migration test job could not join the isolated PostgreSQL network." >&2
exit 1
fi
database_host="postgres"
database_port="5432"
else
if ! postgres_binding="$("${compose[@]}" port postgres 5432 2>/dev/null)"; then
echo "The disposable PostgreSQL loopback port could not be determined." >&2
exit 1
fi
if [[ ! "$postgres_binding" =~ ^127\.0\.0\.1:([0-9]+)$ ]]; then
echo "The disposable PostgreSQL port is not bound safely to loopback." >&2
exit 1
fi
database_host="127.0.0.1"
database_port="${BASH_REMATCH[1]}"
if ((database_port < 1 || database_port > 65535)); then
echo "The disposable PostgreSQL loopback port is invalid." >&2
exit 1
fi
fi
EKKLESIA_MIGRATION_TEST_DATABASE_URL="postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@$database_host:$database_port/$POSTGRES_DB" \
pnpm exec vitest run --config vitest.migrations.integration.config.ts