#!/usr/bin/env bash set -euo pipefail repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "${repo_root}" project_name="${MINEROPS_CREDENTIAL_TRANSITION_PROJECT:-minerops-credential-transition-$$}" compose=(docker compose --project-name "${project_name}" --env-file .env.dev -f compose.dev.yaml) cleanup() { status=$? if (( status != 0 )); then "${compose[@]}" logs --no-color --tail 160 postgres || true fi "${compose[@]}" down --volumes --remove-orphans >/dev/null 2>&1 || true exit "${status}" } trap cleanup EXIT [[ -f .env.dev ]] || { echo "Missing .env.dev" >&2; exit 1; } for source_file in \ backend/tests/test_agent_credential_transition_postgres.py \ agents/windows/Upgrade-MinerOpsAgentCredential.ps1; do permissions="$(stat -c '%a' "${source_file}")" (( (10#${permissions} % 10) & 4 )) || { echo "${source_file} is not readable by the non-root test container" >&2 exit 1 } done "${compose[@]}" up --detach postgres for attempt in $(seq 1 60); do "${compose[@]}" exec -T postgres sh -ec \ 'pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB"' >/dev/null 2>&1 && break (( attempt < 60 )) || { echo "PostgreSQL did not become ready" >&2; exit 1; } sleep 1 done "${compose[@]}" build backend expected_head="$("${compose[@]}" run --rm --no-deps backend alembic heads 2>/dev/null | awk 'NR == 1 { print $1 }')" [[ "${expected_head}" == "0020_existing_agent_transition" ]] echo "Testing the additive 0019 -> 0020 transition-binding migration..." "${compose[@]}" run --rm --no-deps backend alembic upgrade 0019_agent_legacy_auth_retire "${compose[@]}" run --rm --no-deps backend alembic upgrade head echo "Testing production-shaped transactional credential transition..." "${compose[@]}" run --rm --no-deps \ --volume "${repo_root}/backend/tests:/app/tests:ro" \ --env MINEROPS_CREDENTIAL_TRANSITION_DISPOSABLE=1 \ --env MINEROPS_AGENT_ID=plexus-windows-agent \ --env MINEROPS_AGENT_SHARED_SECRET=credential-transition-legacy-secret \ backend python -m unittest -v \ tests.test_agent_credential_transition_postgres echo "Testing downgrade refusal after transition history exists..." if output="$("${compose[@]}" run --rm --no-deps backend \ alembic downgrade 0019_agent_legacy_auth_retire 2>&1)"; then echo "0020 downgrade unexpectedly discarded transition history" >&2 exit 1 fi [[ "${output}" == *"existing-Agent credential transition history exists"* ]] echo "Existing-Agent credential transition checks passed."