build: add docker-compose test stack with ephemeral postgres

This commit is contained in:
agatha 2026-03-15 15:39:11 -04:00
parent 6da6dcd33a
commit c215b8e45f
5 changed files with 42 additions and 10 deletions

View File

@ -10,6 +10,7 @@ RUN --mount=type=cache,target=/root/.cache/uv \
COPY src/ src/ COPY src/ src/
COPY alembic/ alembic/ COPY alembic/ alembic/
COPY alembic.ini . COPY alembic.ini .
COPY README.md .
RUN --mount=type=cache,target=/root/.cache/uv \ RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen --no-dev uv sync --frozen --no-dev

View File

@ -2,6 +2,7 @@ FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim
WORKDIR /app WORKDIR /app
ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy
ENV PATH="/app/.venv/bin:$PATH"
COPY pyproject.toml uv.lock ./ COPY pyproject.toml uv.lock ./
RUN --mount=type=cache,target=/root/.cache/uv \ RUN --mount=type=cache,target=/root/.cache/uv \
@ -11,6 +12,7 @@ COPY src/ src/
COPY tests/ tests/ COPY tests/ tests/
COPY alembic/ alembic/ COPY alembic/ alembic/
COPY alembic.ini . COPY alembic.ini .
COPY README.md .
RUN --mount=type=cache,target=/root/.cache/uv \ RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen uv sync --frozen

26
docker-compose.test.yml Normal file
View File

@ -0,0 +1,26 @@
services:
postgres:
tmpfs: /var/lib/postgresql/data
ports: []
volumes: []
redis:
ports: []
test:
build:
context: .
dockerfile: Dockerfile.test
command: >
sh -c "alembic upgrade head &&
pytest tests/ -x -v --tb=short --timeout=30"
environment:
DB_URL: "postgresql+asyncpg://proxypool:proxypool@postgres:5432/proxypool"
REDIS_URL: "redis://redis:6379/1"
SECRET_KEY: "test-secret-key"
LOG_LEVEL: "WARNING"
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy

View File

@ -40,8 +40,6 @@ services:
POSTGRES_PASSWORD: proxypool POSTGRES_PASSWORD: proxypool
ports: ports:
- "5432:5432" - "5432:5432"
volumes:
- pg_data:/var/lib/postgresql/data
healthcheck: healthcheck:
test: ["CMD-SHELL", "pg_isready -U proxypool"] test: ["CMD-SHELL", "pg_isready -U proxypool"]
interval: 2s interval: 2s
@ -57,6 +55,3 @@ services:
interval: 2s interval: 2s
timeout: 5s timeout: 5s
retries: 10 retries: 10
volumes:
pg_data:

View File

@ -17,11 +17,19 @@ def event_loop():
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
def test_settings() -> Settings: def test_settings() -> Settings:
import os
return Settings( return Settings(
secret_key="test-secret", secret_key=os.environ.get("SECRET_KEY", "test-secret"),
log_level="DEBUG", log_level=os.environ.get("LOG_LEVEL", "DEBUG"),
db=DatabaseSettings(url="postgresql+asyncpg://proxypool:proxypool@localhost:5432/proxypool"), db=DatabaseSettings(
redis=RedisSettings(url="redis://localhost:6379"), url=os.environ.get(
"DB_URL",
"postgresql+asyncpg://proxypool:proxypool@localhost:5432/proxypool",
),
),
redis=RedisSettings(
url=os.environ.get("REDIS_URL", "redis://localhost:6379/1"),
),
) )