diff --git a/tests/test_litellm/proxy/db/test_routing_prisma_wrapper.py b/tests/test_litellm/proxy/db/test_routing_prisma_wrapper.py index 8c3a2b9e2d7..a8a6d252afb 100644 --- a/tests/test_litellm/proxy/db/test_routing_prisma_wrapper.py +++ b/tests/test_litellm/proxy/db/test_routing_prisma_wrapper.py @@ -18,6 +18,52 @@ sys.path.insert(0, os.path.abspath("../../../..")) # stub the prisma SDK do so per-test via monkeypatch, which is properly scoped. +_MANAGED_DB_ENV_VARS = ( + "IAM_TOKEN_DB_AUTH", + "DATABASE_URL", + "DATABASE_URL_READ_REPLICA", + "DATABASE_HOST", + "DATABASE_PORT", + "DATABASE_USER", + "DATABASE_USERNAME", + "DATABASE_NAME", + "DATABASE_SCHEMA", + "DATABASE_PASSWORD", + "DATABASE_HOST_READ_REPLICA", + "DATABASE_PORT_READ_REPLICA", + "DATABASE_USER_READ_REPLICA", + "DATABASE_USERNAME_READ_REPLICA", + "DATABASE_NAME_READ_REPLICA", + "DATABASE_SCHEMA_READ_REPLICA", + "DATABASE_PASSWORD_READ_REPLICA", +) + + +@pytest.fixture(autouse=True) +def _scrub_db_env(): + """Snapshot and restore DB env vars across every test in this module. + + `PrismaWrapper.get_rds_iam_token` (and a few sibling code paths) write + `DATABASE_URL` directly into `os.environ`, which `monkeypatch` cannot + undo when the variable started out unset. Without this fixture the + synthesized writer URL leaks into later tests in the same xdist worker; + e.g. `tests/test_litellm/proxy/common_utils/test_key_rotation_e2e.py:: + TestDeprecatedKeyLookupDbE2E::test_deprecated_key_grace_period_cache_hit_path` + stops skipping (`os.getenv("DATABASE_URL")` is now truthy) and tries to + open a Prisma connection against the bogus host, failing with + `httpx.ConnectError`. Mirrors the fixture in `test_db_url_settings.py`. + """ + saved = {var: os.environ.get(var) for var in _MANAGED_DB_ENV_VARS} + try: + yield + finally: + for var, value in saved.items(): + if value is None: + os.environ.pop(var, None) + else: + os.environ[var] = value + + def _make_wrappers(): from litellm.proxy.db.prisma_client import PrismaWrapper