Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /mvdbeek_pulsar-relay /tests /e2e /conftest.py
| """Fixtures for the OIDC end-to-end test suite. | |
| The suite is gated behind ``-m e2e`` and skipped if Docker isn't reachable. | |
| """ | |
| from __future__ import annotations | |
| import os | |
| import shutil | |
| import socket | |
| import subprocess | |
| import time | |
| from collections.abc import Iterator | |
| from pathlib import Path | |
| import httpx | |
| import pytest | |
| from .keycloak_bootstrap import KeycloakSetup, provision | |
| E2E_DIR = Path(__file__).parent | |
| COMPOSE_FILE = E2E_DIR / "docker-compose.yml" | |
| def _free_port() -> int: | |
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | |
| s.bind(("", 0)) | |
| port: int = s.getsockname()[1] | |
| return port | |
| def _compose_cmd() -> list[str]: | |
| """Resolve a working compose driver, preferring `docker compose`.""" | |
| docker = shutil.which("docker") | |
| if docker is not None: | |
| # `docker compose version` exits 0 when v2 is available. | |
| try: | |
| subprocess.run( | |
| [docker, "compose", "version"], | |
| check=True, | |
| capture_output=True, | |
| timeout=5, | |
| ) | |
| return [docker, "compose"] | |
| except (subprocess.CalledProcessError, subprocess.TimeoutExpired, FileNotFoundError): | |
| pass | |
| legacy = shutil.which("docker-compose") | |
| if legacy is not None: | |
| return [legacy] | |
| # pytest.skip is NoReturn, but mypy doesn't always infer that — explicit | |
| # raise keeps the function's return type honest. | |
| pytest.skip("docker / docker-compose not available") | |
| raise RuntimeError("unreachable") | |
| def _docker_running() -> bool: | |
| docker = shutil.which("docker") | |
| if docker is None: | |
| return False | |
| try: | |
| subprocess.run([docker, "info"], check=True, capture_output=True, timeout=5) | |
| return True | |
| except (subprocess.CalledProcessError, subprocess.TimeoutExpired): | |
| return False | |
| def keycloak() -> Iterator[KeycloakSetup]: | |
| """Start Keycloak via docker-compose, provision the test realm, tear down.""" | |
| if not _docker_running(): | |
| pytest.skip("Docker daemon not reachable; skipping e2e tests") | |
| compose = _compose_cmd() | |
| host_port = int(os.environ.get("KEYCLOAK_HOST_PORT", str(_free_port()))) | |
| env = {**os.environ, "KEYCLOAK_HOST_PORT": str(host_port)} | |
| subprocess.run( | |
| [*compose, "-f", str(COMPOSE_FILE), "up", "-d", "keycloak"], | |
| check=True, | |
| env=env, | |
| ) | |
| base_url = f"http://localhost:{host_port}" | |
| # Wait for Keycloak to be healthy (the realm endpoint returns 200 once ready). | |
| deadline = time.time() + 180 | |
| last_exc: Exception | None = None | |
| while time.time() < deadline: | |
| try: | |
| with httpx.Client(timeout=2.0) as c: | |
| r = c.get(f"{base_url}/realms/master") | |
| if r.status_code in (200, 302): | |
| break | |
| except Exception as exc: # network errors during startup | |
| last_exc = exc | |
| time.sleep(2) | |
| else: | |
| subprocess.run( | |
| [*compose, "-f", str(COMPOSE_FILE), "logs", "keycloak"], | |
| env=env, | |
| ) | |
| subprocess.run( | |
| [*compose, "-f", str(COMPOSE_FILE), "down", "-v"], | |
| env=env, | |
| ) | |
| pytest.fail(f"Keycloak did not become ready: {last_exc}") | |
| # We don't yet know the relay's port; tests that need to register a | |
| # specific redirect_uri call ``provision_with_relay`` (below) instead. | |
| yield KeycloakSetup(base_url=base_url) | |
| subprocess.run( | |
| [*compose, "-f", str(COMPOSE_FILE), "down", "-v"], | |
| env=env, | |
| ) | |
| def relay_against_keycloak(keycloak: KeycloakSetup, tmp_path: Path) -> Iterator[dict]: | |
| """Boot a pulsar-relay subprocess wired up to Keycloak. | |
| Returns a dict with ``base_url`` and the provisioned ``KeycloakSetup``. | |
| """ | |
| relay_port = _free_port() | |
| base_url = f"http://localhost:{relay_port}" | |
| callback = f"{base_url}/auth/oidc/keycloak/callback" | |
| # Provision realm with the relay's callback URL registered. | |
| setup = provision(redirect_uris=[callback], setup=KeycloakSetup(base_url=keycloak.base_url)) | |
| env = { | |
| **os.environ, | |
| "PULSAR_JWT_SECRET_KEY": "e2e-test-jwt-secret-very-long-string-1234567890", | |
| "PULSAR_BOOTSTRAP_ADMIN_USERNAME": "admin", | |
| "PULSAR_BOOTSTRAP_ADMIN_PASSWORD": "adminpw1234", | |
| "PULSAR_BOOTSTRAP_ADMIN_EMAIL": "admin@example.com", | |
| "PULSAR_OIDC__ENABLED": "true", | |
| "PULSAR_OIDC__BASE_URL": base_url, | |
| "PULSAR_OIDC__PROVIDERS__KEYCLOAK__DISPLAY_NAME": "Keycloak", | |
| "PULSAR_OIDC__PROVIDERS__KEYCLOAK__DISCOVERY_URL": setup.discovery_url, | |
| "PULSAR_OIDC__PROVIDERS__KEYCLOAK__CLIENT_ID": setup.client_id, | |
| "PULSAR_OIDC__PROVIDERS__KEYCLOAK__CLIENT_SECRET": setup.client_secret, | |
| "PULSAR_OIDC__PROVIDERS__KEYCLOAK__CLAIM_USERNAME": "preferred_username", | |
| } | |
| proc = subprocess.Popen( | |
| [ | |
| "uvicorn", | |
| "pulsar_relay.main:app", | |
| "--host", | |
| "127.0.0.1", | |
| "--port", | |
| str(relay_port), | |
| "--log-level", | |
| "warning", | |
| ], | |
| env=env, | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.PIPE, | |
| ) | |
| try: | |
| deadline = time.time() + 30 | |
| while time.time() < deadline: | |
| try: | |
| with httpx.Client(timeout=1.0) as c: | |
| if c.get(f"{base_url}/health").status_code == 200: | |
| break | |
| except Exception: | |
| pass | |
| time.sleep(0.3) | |
| else: | |
| stdout, stderr = proc.communicate(timeout=2) | |
| pytest.fail(f"Relay subprocess did not start.\nstdout={stdout!r}\nstderr={stderr!r}") | |
| yield {"base_url": base_url, "keycloak": setup} | |
| finally: | |
| proc.terminate() | |
| try: | |
| proc.wait(timeout=5) | |
| except subprocess.TimeoutExpired: | |
| proc.kill() | |
| proc.wait() | |
Xet Storage Details
- Size:
- 5.97 kB
- Xet hash:
- 9f75c64caf44e102a69f34bbe72bc60eabac31e1dd5d6b76c2c378823f1d230f
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.