fix(ci): B5 整合測試改用 compose pytest-runner service
Some checks failed
CD Pipeline / build-and-deploy (push) Failing after 2m27s

問題: runner 是 Docker-in-Docker,-v /opt/api-venv 掛到 host 路徑不存在

修正:
- docker-compose.test.yml: 新增 pytest-runner service (python:3.11-slim)
  在 compose 網路內跑,hostname=postgres-test 直連,自裝 deps
- postgres-test: initdb.d 自動執行 setup_test_schema.sql,省去手動 psql
- cd.yaml: 改用 --profile test + --exit-code-from pytest-runner

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
OG T
2026-04-10 12:58:44 +08:00
parent 3ebfca62a2
commit c8b5c994d4
2 changed files with 29 additions and 21 deletions

View File

@@ -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
# 掛載原始碼 + venvhostname=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

View File

@@ -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 網路內跑 pytesthostname=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 時才啟動