mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-26 01:12:21 +00:00
A test's JUnit report says whether it passed, never what it did or where a failing test died. This records that from the harness, so nothing about it is hand-written and it cannot drift from what the test actually ran
`@step("create team with a budget")` from the new tests/e2e/e2e_metadata.py goes on harness helpers, never on tests, and appends its label to the running test's step log in call order. The label is recorded before the wrapped call, so a helper that raises still leaves its own label last: a failing test's last step is where it died. Every public harness method that performs an action now carries one, 355 across the client modules, lifecycle, idp, the logging readers, migrations and the claude_code driver
Only the outermost step records, tracked per thread. Harness layers call each other (ResourceManager.key goes through ProxyClient.generate_key, a domain client wraps the shared ProxyClient), so every layer carries a label and the story still reads at the level the test called in at, one beat per action. A step above @contextmanager holds the guard through __enter__ and __exit__, so a context's cleanup never lands behind the step a test died on, and a bare generator function is refused at import because its body interleaves with its caller's. Consecutive duplicates collapse and the log caps at 50, so a poll loop is one beat rather than fifty. The wrapper is a frame, so the eight cleanup and retry warnings raised directly inside decorated helpers use stacklevel=2 + STEP_FRAMES to keep reporting at their caller
The log is emptied first thing in pytest_runtest_setup and attached from the existing pytest_runtest_makereport wrapper after setup and again after call, so a test that errors in a fixture keeps the steps recorded before the crash. Teardown does not attach: finalizer steps are cleanup. Each attach drops the item's earlier step entries, so the second attach and a --reruns 1 retry replace the story rather than doubling it
Steps ride out as repeated <property name="step"> entries behind the fixed package/covers/source prefix, which stays byte-identical. The project-releaser emitter already regroups them into the results JSON's steps array. test_junit_report.py runs real pytest with --junitxml against this conftest, in-process and under -n 2, and pins the passing, failing, setup-error, rerun and wide-scope-fixture cases on the parsed XML
144 lines
5.7 KiB
Python
144 lines
5.7 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Generator
|
|
from contextlib import contextmanager
|
|
from dataclasses import dataclass
|
|
from typing import Final, LiteralString
|
|
from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit
|
|
from uuid import uuid4
|
|
|
|
import psycopg
|
|
from e2e_metadata import step
|
|
from psycopg import sql
|
|
from pydantic import TypeAdapter
|
|
|
|
Scalar = str | int | bool | None
|
|
ROWS: Final = TypeAdapter(tuple[tuple[Scalar, ...], ...])
|
|
GATE_KEY: Final = 39178002
|
|
PRISMA_LOCK: Final = 72707369
|
|
COORDINATOR_LOCK: Final = int.from_bytes(b"llm_mig2", "big")
|
|
|
|
|
|
def connect_url(url: str, name: str) -> str:
|
|
return urlunsplit(urlsplit(url)._replace(path=f"/{name}", query=""))
|
|
|
|
|
|
def prisma_url(url: str, schema: str) -> str:
|
|
parsed: Final = urlsplit(url)
|
|
query: Final = tuple((key, value) for key, value in parse_qsl(parsed.query) if key != "schema")
|
|
return urlunsplit(parsed._replace(query=urlencode((*query, ("schema", schema)))))
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class Database:
|
|
name: str
|
|
url: str
|
|
container_url: str
|
|
schema: str = "public"
|
|
|
|
@contextmanager
|
|
def connection(self) -> Generator[psycopg.Connection[tuple[object, ...]]]:
|
|
with psycopg.connect(self.url, autocommit=True, connect_timeout=5) as connection:
|
|
connection.execute(sql.SQL("SET search_path TO {}").format(sql.Identifier(self.schema)))
|
|
connection.execute("SET statement_timeout = '15s'")
|
|
yield connection
|
|
|
|
@step("run a SQL statement")
|
|
def execute(self, statement: LiteralString | sql.Composed, params: tuple[Scalar, ...] = ()) -> None:
|
|
with self.connection() as connection:
|
|
connection.execute(statement, params or None)
|
|
|
|
@step("query the database")
|
|
def query(
|
|
self, statement: LiteralString | sql.Composed, params: tuple[Scalar, ...] = ()
|
|
) -> tuple[tuple[Scalar, ...], ...]:
|
|
with self.connection() as connection:
|
|
return ROWS.validate_python(connection.execute(statement, params or None).fetchall())
|
|
|
|
@step("check whether a table exists")
|
|
def exists(self, name: str) -> bool:
|
|
return self.query("SELECT to_regclass(%s) IS NOT NULL", (name,)) == ((True,),)
|
|
|
|
@step("read the _prisma_migrations history")
|
|
def history(self) -> tuple[tuple[Scalar, ...], ...]:
|
|
if not self.exists("_prisma_migrations"):
|
|
return ()
|
|
return self.query(
|
|
"SELECT id, migration_name, checksum, started_at::text, finished_at::text, rolled_back_at::text, "
|
|
"applied_steps_count, logs FROM _prisma_migrations ORDER BY id"
|
|
)
|
|
|
|
@step("list backends waiting on an advisory lock")
|
|
def blocked(self, key: int = GATE_KEY) -> tuple[tuple[Scalar, ...], ...]:
|
|
return self.query(
|
|
"SELECT pid FROM pg_locks WHERE locktype = 'advisory' AND NOT granted "
|
|
"AND database = (SELECT oid FROM pg_database WHERE datname = current_database()) "
|
|
"AND classid = %s AND objid = %s ORDER BY pid",
|
|
(key >> 32, key & 0xFFFFFFFF),
|
|
)
|
|
|
|
@step("hold an advisory lock")
|
|
@contextmanager
|
|
def lock(self, key: int = GATE_KEY) -> Generator[None]:
|
|
with self.connection() as connection:
|
|
connection.execute("SELECT pg_advisory_lock(%s)", (key,))
|
|
try:
|
|
yield
|
|
finally:
|
|
connection.execute("SELECT pg_advisory_unlock(%s)", (key,))
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class Databases:
|
|
admin_url: str
|
|
container_admin_url: str
|
|
|
|
@step("create a test database")
|
|
@contextmanager
|
|
def create(self, template: Database | None = None, schema: str = "public") -> Generator[Database]:
|
|
name: Final = f"litellm_migration_test_{uuid4().hex[:20]}"
|
|
database: Final = Database(
|
|
name, connect_url(self.admin_url, name), connect_url(self.container_admin_url, name), schema
|
|
)
|
|
with psycopg.connect(self.admin_url, autocommit=True, connect_timeout=5) as connection:
|
|
connection.execute(
|
|
sql.SQL("CREATE DATABASE {} TEMPLATE {}").format(
|
|
sql.Identifier(name), sql.Identifier(template.name if template else "template0")
|
|
)
|
|
)
|
|
try:
|
|
yield database
|
|
finally:
|
|
with psycopg.connect(self.admin_url, autocommit=True, connect_timeout=5) as connection:
|
|
connection.execute(sql.SQL("DROP DATABASE {} WITH (FORCE)").format(sql.Identifier(name)))
|
|
|
|
|
|
@step("create a read-only database role")
|
|
@contextmanager
|
|
def restricted_user(database: Database) -> Generator[Database]:
|
|
role: Final = f"migration_reader_{uuid4().hex[:16]}"
|
|
password: Final = "migration-test-password"
|
|
with database.connection() as connection:
|
|
connection.execute(
|
|
sql.SQL("CREATE ROLE {} LOGIN PASSWORD {}").format(sql.Identifier(role), sql.Literal(password))
|
|
)
|
|
try:
|
|
database.execute(
|
|
sql.SQL("GRANT USAGE ON SCHEMA {} TO {}").format(sql.Identifier(database.schema), sql.Identifier(role))
|
|
)
|
|
database.execute(
|
|
sql.SQL("GRANT SELECT ON ALL TABLES IN SCHEMA {} TO {}").format(
|
|
sql.Identifier(database.schema), sql.Identifier(role)
|
|
)
|
|
)
|
|
local: Final = urlsplit(database.url)
|
|
remote: Final = urlsplit(database.container_url)
|
|
yield Database(
|
|
database.name,
|
|
urlunsplit(local._replace(netloc=f"{role}:{password}@{local.hostname}:{local.port}")),
|
|
urlunsplit(remote._replace(netloc=f"{role}:{password}@{remote.hostname}:{remote.port}")),
|
|
database.schema,
|
|
)
|
|
finally:
|
|
database.execute(sql.SQL("DROP OWNED BY {}").format(sql.Identifier(role)))
|
|
database.execute(sql.SQL("DROP ROLE {}").format(sql.Identifier(role)))
|