"""Static checks of the Dockerfile and the compose file, since an image cannot be built in every environment. They pin the properties that matter: the start order, a non-root user, what is copied, and that every dependency the code imports is declared (an image installs only what is declared).""" import ast import importlib.metadata as md import re import sys import tomllib from pathlib import Path import yaml ROOT = Path(__file__).resolve().parents[3] DOCKERFILE = (ROOT / "Dockerfile").read_text() COMPOSE = yaml.safe_load((ROOT / "docker-compose.yml").read_text()) # ----------------------------------------------------------------------- compose def test_compose_has_a_database_a_migration_job_and_the_service(): assert set(COMPOSE["services"]) == {"db", "migrate", "app"} def test_the_start_order_is_database_then_migrations_then_service(): s = COMPOSE["services"] assert s["migrate"]["depends_on"]["db"]["condition"] == "service_healthy" assert s["app"]["depends_on"]["migrate"]["condition"] == "service_completed_successfully" assert "db" not in s["app"]["depends_on"] # the service reaches the database only through the job assert s["migrate"]["command"] == ["migrate"] and s["migrate"]["restart"] == "no" assert s["app"]["restart"] == "unless-stopped" def test_the_database_is_postgres_with_pgvector_and_a_healthcheck_and_a_volume(): db = COMPOSE["services"]["db"] assert db["image"].startswith("pgvector/pgvector:pg") and ":latest" not in db["image"] check = " ".join(db["healthcheck"]["test"]) assert "pg_isready" in check and "-h 127.0.0.1" in check # a TCP check: see the comment in the compose file assert any(v.startswith("dbdata:") for v in db["volumes"]) and "dbdata" in COMPOSE["volumes"] def test_migrate_and_service_use_the_same_image_and_the_same_settings(): s = COMPOSE["services"] assert s["migrate"]["image"] == s["app"]["image"] and "build" in s["migrate"] and "build" not in s["app"] assert s["migrate"]["environment"] == s["app"]["environment"] # one source of truth for the connection def test_the_password_is_required_and_the_connection_points_at_the_db_service(): env = COMPOSE["services"]["app"]["environment"] assert "@db:5432/estimator" in env["EST_DATABASE__DSN"] for value in (env["EST_DATABASE__DSN"], COMPOSE["services"]["db"]["environment"]["POSTGRES_PASSWORD"]): assert "${POSTGRES_PASSWORD:?" in value # compose refuses to start without it def test_the_service_is_hardened_and_exposes_only_its_port(): app = COMPOSE["services"]["app"] assert app["cap_drop"] == ["ALL"] and "no-new-privileges:true" in app["security_opt"] assert not app.get("privileged") and "ports" in app and app["ports"][0].endswith(":8000") assert "ports" not in COMPOSE["services"]["db"] # the database is not published to the host assert not any("docker.sock" in str(v) for v in app.get("volumes", [])) # -------------------------------------------------------------------- Dockerfile def test_dockerfile_is_multi_stage_on_a_pinned_slim_python(): froms = re.findall(r"^FROM (\S+)", DOCKERFILE, flags=re.MULTILINE) assert froms == ["python:3.12-slim", "python:3.12-slim"] def test_the_service_runs_as_a_non_root_user(): assert re.search(r"^USER app$", DOCKERFILE, flags=re.MULTILINE) assert DOCKERFILE.index("USER app") < DOCKERFILE.index("ENTRYPOINT") assert "10001" in DOCKERFILE and "nologin" in DOCKERFILE def test_entrypoint_command_and_healthcheck_are_declared(): assert 'ENTRYPOINT ["/app/docker/entrypoint.sh"]' in DOCKERFILE and 'CMD ["serve"]' in DOCKERFILE assert "HEALTHCHECK" in DOCKERFILE and "/health" in DOCKERFILE assert "chmod 0755 /app/docker/entrypoint.sh" in DOCKERFILE # does not depend on the file mode in the checkout def test_what_the_service_reads_at_run_time_is_copied_into_the_image(): # only copies from the build context (a copy --from=builder takes files from the earlier stage) context_copies = [ln for ln in DOCKERFILE.splitlines() if ln.startswith("COPY ") and "--from=" not in ln] copied = [re.match(r"COPY (?:--\S+ )*(\S+) (\S+)", ln).groups() for ln in context_copies] sources = {src for src, _ in copied} assert {"config", "prompts", "migrations", "alembic.ini", "docker/entrypoint.sh", "pyproject.toml", "src"} <= sources ignored = {line.strip() for line in (ROOT / ".dockerignore").read_text().splitlines()} for src in sources: assert (ROOT / src).exists(), src assert src not in ignored and src.split("/")[0] not in ignored, f"{src} is excluded by .dockerignore" def test_the_image_is_not_built_from_an_editable_install_and_holds_no_secrets(): assert "-e " not in DOCKERFILE and "pip install -e" not in DOCKERFILE assert not re.search(r"^\s*(ENV|ARG)\s+\S*(KEY|PASSWORD|SECRET|TOKEN)", DOCKERFILE, flags=re.MULTILINE | re.IGNORECASE) def test_env_files_and_tests_stay_out_of_the_build_context(): ignored = (ROOT / ".dockerignore").read_text().splitlines() for item in (".env", "tests", ".git", ".venv"): assert item in ignored assert "!.env.example" in ignored def test_app_directories_match_what_the_code_expects_relative_to_the_working_directory(): default = tomllib.loads((ROOT / "config" / "default.toml").read_text()) assert default["agent"]["prompts_dir"] == "prompts" # /app/prompts assert "script_location = migrations" in (ROOT / "alembic.ini").read_text() # /app/migrations assert default["api"]["host"] == "0.0.0.0" # reachable from outside the container # ----------------------------------------------------- dependencies are declared _TRANSITIVE_OK = {"starlette"} # always installed with fastapi, which is declared def _declared() -> set[str]: deps = tomllib.loads((ROOT / "pyproject.toml").read_text())["project"]["dependencies"] return {re.split(r"[\[<>=!~ ;]", d, maxsplit=1)[0].lower().replace("_", "-") for d in deps} def _imported_top_level_modules() -> set[str]: found: set[str] = set() for path in (ROOT / "src" / "estimator").rglob("*.py"): for node in ast.walk(ast.parse(path.read_text())): if isinstance(node, ast.Import): found.update(a.name.split(".")[0] for a in node.names) elif isinstance(node, ast.ImportFrom) and node.level == 0 and node.module: found.add(node.module.split(".")[0]) return found - set(sys.stdlib_module_names) - {"estimator"} def test_every_third_party_package_the_code_imports_is_a_declared_dependency(): owners = md.packages_distributions() declared = _declared() missing = {} for module in _imported_top_level_modules(): dists = {d.lower().replace("_", "-") for d in owners.get(module, [])} if not dists & declared and module not in _TRANSITIVE_OK: missing[module] = sorted(dists) or ["not installed"] assert not missing, f"imported but not declared in pyproject.toml: {missing}" def test_pgvector_is_declared_because_the_schema_needs_it(): assert "pgvector" in _declared() def test_the_migration_imports_only_declared_packages(): modules = set() for node in ast.walk(ast.parse((ROOT / "migrations" / "versions" / "0001_initial.py").read_text())): if isinstance(node, ast.ImportFrom) and node.module: modules.add(node.module.split(".")[0]) elif isinstance(node, ast.Import): modules.update(a.name.split(".")[0] for a in node.names) owners = md.packages_distributions() declared = _declared() for module in modules - set(sys.stdlib_module_names): assert {d.lower().replace("_", "-") for d in owners.get(module, [])} & declared, module