diff --git a/.gitea/workflows/cd.yaml b/.gitea/workflows/cd.yaml index 2ddc5d60..189b442d 100644 --- a/.gitea/workflows/cd.yaml +++ b/.gitea/workflows/cd.yaml @@ -124,31 +124,15 @@ jobs: exit $PYTEST_EXIT # ── 整合測試 B5 (2026-04-10) ────────────────────────────────────────── - # 起臨時 pgvector PostgreSQL → 建 schema → 跑 test_b5_core_flows.py → 清理 + # pytest-runner service 在 compose 網路內跑,hostname=postgres-test 直連 + # schema 由 postgres initdb.d 自動初始化,不依賴 host /opt/api-venv - name: Integration Tests (B5 — 真實 DB) run: | cd apps/api - docker compose -f docker-compose.test.yml up -d --wait - # schema 初始化 - docker compose -f docker-compose.test.yml exec -T postgres-test \ - psql -U awoooi -d awoooi_test \ - -f /dev/stdin < tests/integration/setup_test_schema.sql - # compose 網路名 (目錄名=api → api_default) - COMPOSE_NET=$(docker network ls --format '{{.Name}}' | grep '^api_default$' || \ - docker network ls --format '{{.Name}}' | grep 'default' | head -1) - echo "compose network: $COMPOSE_NET" - # 在同一 compose 網路的臨時 python container 內跑 pytest - # 掛載原始碼 + venv,hostname=postgres-test 直連 5432 - docker run --rm \ - --network "$COMPOSE_NET" \ - -v /opt/api-venv:/opt/api-venv \ - -v "$(pwd):/workspace" \ - -w /workspace \ - -e TEST_DATABASE_URL="postgresql+asyncpg://awoooi:awoooi_test_2026@postgres-test:5432/awoooi_test?ssl=disable" \ - python:3.11-slim \ - /opt/api-venv/bin/pytest tests/integration/test_b5_core_flows.py -v --tb=short + docker compose -f docker-compose.test.yml --profile test up \ + --abort-on-container-exit --exit-code-from pytest-runner TEST_EXIT=$? - docker compose -f docker-compose.test.yml down -v 2>/dev/null || true + docker compose -f docker-compose.test.yml --profile test down -v 2>/dev/null || true exit $TEST_EXIT - name: Login to Harbor diff --git a/apps/api/docker-compose.test.yml b/apps/api/docker-compose.test.yml index 9ecbdd63..8fb13f65 100644 --- a/apps/api/docker-compose.test.yml +++ b/apps/api/docker-compose.test.yml @@ -17,6 +17,9 @@ services: POSTGRES_PASSWORD: awoooi_test_2026 ports: - "15432:5432" + # 啟動時自動執行 setup_test_schema.sql (initdb.d 機制) + volumes: + - ./tests/integration/setup_test_schema.sql:/docker-entrypoint-initdb.d/01-schema.sql:ro healthcheck: test: ["CMD-SHELL", "pg_isready -U awoooi -d awoooi_test"] interval: 5s @@ -34,3 +37,24 @@ services: interval: 5s timeout: 3s retries: 5 + + # 2026-04-10 Claude Sonnet 4.6 Asia/Taipei: 整合測試 runner + # 在 compose 網路內跑 pytest,hostname=postgres-test 直連,不依賴 host venv + pytest-runner: + image: python:3.11-slim + working_dir: /workspace + volumes: + - .:/workspace + environment: + TEST_DATABASE_URL: "postgresql+asyncpg://awoooi:awoooi_test_2026@postgres-test:5432/awoooi_test?ssl=disable" + depends_on: + postgres-test: + condition: service_healthy + redis-test: + condition: service_healthy + command: > + sh -c "pip install -q uv && + uv pip install -q --system -e '.[dev]' && + pytest tests/integration/test_b5_core_flows.py -v --tb=short" + profiles: + - test # 只在明確指定 --profile test 時才啟動