LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
UN-3638 [TEST] Generic rig seams for downstream e2e overlays + login provider (#2220)
Chandrasekharan M64dc89c813a4
diff --git a/tests/e2e/auth/test_login.py b/tests/e2e/auth/test_login.pyindex d9f41a7eb..68beb5d95 100644--- a/tests/e2e/auth/test_login.py+++ b/tests/e2e/auth/test_login.py@@ -6,12 +6,24 @@ under test. Other e2e tests should take the shared ``authed_session`` fixture.from __future__ import annotations+import os+import pytestimport requests+from tests.e2e.conftest import LOGIN_PROVIDER_ENVfrom tests.rig.runtime import PlatformEndpoints-pytestmark = [pytest.mark.e2e, pytest.mark.critical]+pytestmark = [+ pytest.mark.e2e,+ pytest.mark.critical,+ # A deployment that swapped the login provider has no form login to assert+ # against; its own suite covers the flow it actually ships.+ pytest.mark.skipif(+ bool(os.environ.get(LOGIN_PROVIDER_ENV, "").strip()),+ reason=f"{LOGIN_PROVIDER_ENV} set: form login is not the flow under test",+ ),+]@pytest.mark.critical_path("auth-login")diff --git a/tests/e2e/conftest.py b/tests/e2e/conftest.pyindex 7ebfb13cd..e7ef7e114 100644--- a/tests/e2e/conftest.py+++ b/tests/e2e/conftest.py@@ -8,9 +8,11 @@ a clear message rather than spuriously failing.from __future__ import annotations+import importlibimport osimport timeimport uuid+from collections.abc import Callablefrom dataclasses import dataclassimport pytest@@ -21,6 +23,11 @@ from tests.rig.runtime import (PlatformEndpoints,)+# A downstream repo (e.g. cloud with an Auth0 login) can supply its own login by+# pointing this at a "module.func" that takes PlatformEndpoints and returns a+# logged-in requests.Session. Unset → the OSS form login below.+LOGIN_PROVIDER_ENV = "UNSTRACT_E2E_LOGIN_PROVIDER"+# Mirrors unstract.core.data_models.ExecutionStatus.terminal_statuses(); kept a# local literal because the e2e venv carries only pytest/requests/minio, and# these are the JSON status strings the HTTP API returns anyway.@@ -64,16 +71,40 @@ def platform() -> PlatformEndpoints:return PlatformEndpoints.from_env()+def _load_login_provider() -> Callable[[PlatformEndpoints], requests.Session]:+ """Resolve the login callable from LOGIN_PROVIDER_ENV, else the OSS one."""+ dotted = os.environ.get(LOGIN_PROVIDER_ENV, "").strip()+ if not dotted:+ return _oss_form_login+ module_name, _, func_name = dotted.rpartition(".")+ if not module_name or not func_name:+ raise ValueError(+ f"{LOGIN_PROVIDER_ENV} must be a 'module.func' path, got {dotted!r}"+ )+ provider = getattr(importlib.import_module(module_name), func_name)+ if not callable(provider):+ raise ValueError(+ f"{LOGIN_PROVIDER_ENV}={dotted!r} does not resolve to a callable"+ )+ return provider++@pytest.fixture(scope="session")def authed_session(platform: PlatformEndpoints) -> requests.Session:"""A logged-in session with the active organization set.- Standardizes the OSS mock-login flow so tests don't re-implement it: form- POST to /api/v1/login (302 + sessionid), then the org handshake- (GET /organization seeds the csrftoken cookie, POST /organization/{id}/set- with X-CSRFToken) so org-scoped endpoints are reachable. Session-scoped:- logged in once, reused across tests. Tests that exercise login itself- should build their own session instead.+ Session-scoped: logged in once, reused across tests. Tests that exercise+ login itself should build their own session instead. The login flow is+ pluggable via LOGIN_PROVIDER_ENV so a downstream repo can swap in its own+ (e.g. Auth0) while every non-auth test reuses this one seam unchanged.+ """+ return _load_login_provider()(platform)+++def _oss_form_login(platform: PlatformEndpoints) -> requests.Session:+ """The OSS mock-login flow: form POST to /api/v1/login (302 + sessionid),+ then the org handshake (GET /organization seeds the csrftoken cookie, POST+ /organization/{id}/set with X-CSRFToken) so org-scoped endpoints work."""base = platform.backend_url.rstrip("/")session = CsrfSession()diff --git a/tests/rig/runtime.py b/tests/rig/runtime.pyindex aaabb1df9..5fbb5ce3e 100644--- a/tests/rig/runtime.py+++ b/tests/rig/runtime.py@@ -24,6 +24,7 @@ import shutilimport subprocessimport timefrom dataclasses import dataclass, field+from pathlib import Pathfrom typing import ClassVar, Protocolfrom tests.rig.groups import REPO_ROOT@@ -33,6 +34,52 @@ log = logging.getLogger(__name__)COMPOSE_OVERLAY = REPO_ROOT / "tests" / "compose" / "docker-compose.test.yaml"BASE_COMPOSE = REPO_ROOT / "docker" / "docker-compose.yaml"+# Extra compose overlays a downstream repo can layer on (e.g. a cloud repo+# adding an auth mock or an LLM proxy it copied into this tree). Same contract+# as UNSTRACT_RIG_EXTRA_MANIFESTS: os.pathsep-separated, relative to REPO_ROOT.+EXTRA_COMPOSE_ENV = "UNSTRACT_RIG_EXTRA_COMPOSE"+++def _extra_compose_files() -> list[Path]:+ raw = os.environ.get(EXTRA_COMPOSE_ENV, "").strip()+ if not raw:+ return []+ paths: list[Path] = []+ for entry in filter(None, (e.strip() for e in raw.split(os.pathsep))):+ p = Path(entry)+ p = p if p.is_absolute() else REPO_ROOT / p+ if not p.is_file():+ raise ValueError(+ f"{EXTRA_COMPOSE_ENV}: {entry!r} is not a file (resolved to {p})"+ )+ paths.append(p)+ return paths+++def _compose_file_args() -> list[str]:+ """The ``-f`` args for compose: base, the test overlay, then any extras."""+ args = ["-f", str(BASE_COMPOSE)]+ if COMPOSE_OVERLAY.exists():+ args += ["-f", str(COMPOSE_OVERLAY)]+ for extra in _extra_compose_files():+ args += ["-f", str(extra)]+ return args+++def _drop_missing_files(args: list[str]) -> list[str]:+ """Keep only the ``-f <path>`` pairs still on disk.++ A path compose can't read makes it refuse the whole command, so a file that+ vanished mid-run would block teardown entirely rather than cost one overlay.+ """+ kept: list[str] = []+ for flag, path in zip(args[::2], args[1::2], strict=True):+ if Path(path).is_file():+ kept += [flag, path]+ else:+ log.warning("compose file %s vanished; excluding it from teardown", path)+ return kept+# Shared by the workers and the tests, so the exact completion is assertable.LLM_MOCK_RESPONSE_ENV = "UNSTRACT_LLM_MOCK_RESPONSE"DEFAULT_LLM_MOCK_RESPONSE = "MOCK_LLM_OK"@@ -130,14 +177,18 @@ class ComposeRuntime:def __init__(self, *, project_name: str = "unstract-test") -> None:self.project_name = project_name+ # Captured at up() so down() tears down the exact same project even if+ # UNSTRACT_RIG_EXTRA_COMPOSE changes or an overlay is removed mid-run.+ self._compose_files: list[str] | None = Nonedef up(self) -> PlatformEndpoints:if shutil.which("docker") is None:raise RuntimeError("ComposeRuntime requires the `docker` CLI on PATH")- files = ["-f", str(BASE_COMPOSE)]- if COMPOSE_OVERLAY.exists():- files += ["-f", str(COMPOSE_OVERLAY)]- _run(["docker", "compose", "-p", self.project_name, *files, "up", "-d", "--wait"])+ files = _compose_file_args()+ _run(+ ["docker", "compose", "-p", self.project_name, *files, "up", "-d", "--wait"]+ )+ self._compose_files = self._snapshot_config(files) or filesendpoints = PlatformEndpoints.from_env()_wait_ready(endpoints)return endpoints@@ -145,23 +196,87 @@ class ComposeRuntime:def down(self) -> None:if shutil.which("docker") is None:return- files = ["-f", str(BASE_COMPOSE)]- if COMPOSE_OVERLAY.exists():- files += ["-f", str(COMPOSE_OVERLAY)]- self._dump_logs(files)+ files = self._compose_files+ if files is None:+ # down() without a prior up() (e.g. pre-run cleanup): best-effort,+ # never let a since-removed overlay abort teardown.+ try:+ files = _compose_file_args()+ except ValueError:+ files = ["-f", str(BASE_COMPOSE)]+ # No-op on the snapshot, which is a single file we wrote ourselves; this+ # covers the raw-path cases (snapshot unavailable, or no prior up()).+ readable = _drop_missing_files(files)+ self._dump_logs(readable)_run(["docker","compose","-p",self.project_name,- *files,+ *readable,"down","-v","--remove-orphans",],check=False,)+ if len(readable) != len(files):+ # `down -v` only removes volumes it can still see declared, so a+ # dropped file takes its volumes out of scope. The project label is+ # on them regardless of which file declared them.+ self._remove_project_volumes()++ def _remove_project_volumes(self) -> None:+ try:+ listed = subprocess.run( # noqa: S603+ [+ "docker",+ "volume",+ "ls",+ "-q",+ "--filter",+ f"label=com.docker.compose.project={self.project_name}",+ ],+ capture_output=True,+ text=True,+ check=False,+ )+ names = listed.stdout.split()+ if names:+ subprocess.run( # noqa: S603+ ["docker", "volume", "rm", "-f", *names],+ capture_output=True,+ check=False,+ )+ except OSError as exc:+ log.warning("Could not remove leftover project volumes: %s", exc)++ def _snapshot_config(self, files: list[str]) -> list[str] | None:+ """Freeze the merged compose config so teardown never rereads the sources.++ The overlay files can move or be cleaned up between up() and down();+ a resolved snapshot keeps every service, network and volume the project+ owns visible to `down -v`, not just the ones still on disk. Returns None+ when the snapshot can't be written, leaving the caller on raw paths.+ """+ target = REPO_ROOT / "reports" / f"compose-config-{self.project_name}.yaml"+ try:+ completed = subprocess.run( # noqa: S603+ ["docker", "compose", "-p", self.project_name, *files, "config"],+ capture_output=True,+ text=True,+ check=False,+ )+ if completed.returncode != 0 or not completed.stdout.strip():+ log.warning("compose config snapshot failed: %s", completed.stderr)+ return None+ target.parent.mkdir(parents=True, exist_ok=True)+ target.write_text(completed.stdout)+ except OSError as exc:+ log.warning("Could not write compose config snapshot: %s", exc)+ return None+ return ["-f", str(target)]def _dump_logs(self, files: list[str]) -> None:"""Capture service logs while the containers still exist.diff --git a/tests/rig/tests/test_runtime.py b/tests/rig/tests/test_runtime.pyindex 5570e77d9..f41e2c955 100644--- a/tests/rig/tests/test_runtime.py+++ b/tests/rig/tests/test_runtime.py@@ -7,6 +7,9 @@ need a real Docker daemon to exercise and live outside this unit-rig group.from __future__ import annotations+import subprocess+from pathlib import Path+import pytestfrom tests.rig.runtime import InfraEndpoints, PlatformEndpoints@@ -62,3 +65,135 @@ def test_platform_endpoints_from_env_uses_defaults(monkeypatch) -> None:assert endpoints.backend_url == "http://localhost:8000"assert endpoints.runner_url == "http://localhost:5002"assert endpoints.admin_user == "unstract"+++# ── Injection seams: extra compose overlays + pluggable login provider ───────++from tests.e2e.conftest import _load_login_provider, _oss_form_login # noqa: E402+from tests.rig.runtime import EXTRA_COMPOSE_ENV, _extra_compose_files # noqa: E402+++def test_extra_compose_files_empty_when_unset(monkeypatch) -> None:+ monkeypatch.delenv(EXTRA_COMPOSE_ENV, raising=False)+ assert _extra_compose_files() == []+++def test_extra_compose_files_reads_existing_paths(monkeypatch, tmp_path) -> None:+ a, b = tmp_path / "a.yaml", tmp_path / "b.yaml"+ a.write_text("services: {}")+ b.write_text("services: {}")+ import os as _os++ monkeypatch.setenv(EXTRA_COMPOSE_ENV, _os.pathsep.join([str(a), str(b)]))+ assert _extra_compose_files() == [a, b]+++def test_extra_compose_files_rejects_missing(monkeypatch, tmp_path) -> None:+ monkeypatch.setenv(EXTRA_COMPOSE_ENV, str(tmp_path / "nope.yaml"))+ with pytest.raises(ValueError, match=EXTRA_COMPOSE_ENV):+ _extra_compose_files()+++_OVERLAY_YAML = "services:\n mock:\n image: mock:latest\n"+++def _up_then_delete_overlay(monkeypatch, tmp_path, *, snapshot_ok: bool):+ """up() with an extra overlay, then delete it — the mid-run removal case.++ Returns (runtime_module, docker_commands, subprocess_calls).+ """+ from tests.rig import runtime as rt++ extra = tmp_path / "extra.yaml"+ extra.write_text(_OVERLAY_YAML)+ monkeypatch.setenv(EXTRA_COMPOSE_ENV, str(extra))+ monkeypatch.setattr(rt.shutil, "which", lambda _: "/usr/bin/docker")+ monkeypatch.setattr(rt, "_wait_ready", lambda *_a, **_k: None)++ commands: list[list[str]] = []+ monkeypatch.setattr(rt, "_run", lambda cmd, **_k: commands.append(cmd))++ # Stands in for `docker compose config` (and, after down, `docker volume`).+ calls: list[list[str]] = []++ def fake_run(cmd, *_a, **_k):+ calls.append(cmd)+ return subprocess.CompletedProcess(+ cmd,+ 0 if snapshot_ok else 1,+ stdout=_OVERLAY_YAML if snapshot_ok else "",+ stderr="" if snapshot_ok else "boom",+ )++ monkeypatch.setattr(rt.subprocess, "run", fake_run)++ compose = rt.ComposeRuntime()+ compose.up()+ assert str(extra) in commands[0]++ extra.unlink()+ monkeypatch.setattr(compose, "_dump_logs", lambda _files: None)+ compose.down()+ return rt, extra, commands, calls+++def test_down_uses_config_snapshot_after_overlay_removed(monkeypatch, tmp_path) -> None:+ """The snapshot keeps the removed overlay's resources in scope for down -v."""+ _rt, _extra, commands, _calls = _up_then_delete_overlay(+ monkeypatch, tmp_path, snapshot_ok=True+ )++ down_cmd = commands[-1]+ assert down_cmd[-3:] == ["down", "-v", "--remove-orphans"]+ snapshot = Path(down_cmd[down_cmd.index("-f") + 1])+ assert snapshot.is_file()+ assert "mock:latest" in snapshot.read_text()+++def test_down_sweeps_volumes_when_snapshot_failed(monkeypatch, tmp_path) -> None:+ """Without a snapshot the file is dropped, so volumes go by project label."""+ rt, extra, commands, calls = _up_then_delete_overlay(+ monkeypatch, tmp_path, snapshot_ok=False+ )++ down_cmd = commands[-1]+ assert down_cmd[-3:] == ["down", "-v", "--remove-orphans"]+ assert str(extra) not in down_cmd+ assert str(rt.BASE_COMPOSE) in down_cmd+ # Teardown can no longer see what the overlay declared, so it falls back to+ # the label every compose-created volume carries.+ assert any(+ c[:3] == ["docker", "volume", "ls"]+ and any("com.docker.compose.project=" in part for part in c)+ for c in calls+ ), calls+++def test_login_provider_defaults_to_oss(monkeypatch) -> None:+ monkeypatch.delenv("UNSTRACT_E2E_LOGIN_PROVIDER", raising=False)+ assert _load_login_provider() is _oss_form_login+++def test_login_provider_resolves_dotted_path(monkeypatch) -> None:+ monkeypatch.setenv("UNSTRACT_E2E_LOGIN_PROVIDER", "json.loads")+ import json++ assert _load_login_provider() is json.loads+++def test_login_provider_rejects_bare_name(monkeypatch) -> None:+ monkeypatch.setenv("UNSTRACT_E2E_LOGIN_PROVIDER", "loads")+ with pytest.raises(ValueError, match=r"module\.func"):+ _load_login_provider()+++def test_login_provider_rejects_trailing_dot(monkeypatch) -> None:+ monkeypatch.setenv("UNSTRACT_E2E_LOGIN_PROVIDER", "json.")+ with pytest.raises(ValueError, match=r"module\.func"):+ _load_login_provider()+++def test_login_provider_rejects_non_callable(monkeypatch) -> None:+ monkeypatch.setenv("UNSTRACT_E2E_LOGIN_PROVIDER", "json.__name__")+ with pytest.raises(ValueError, match="callable"):+ _load_login_provider()