-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
test(e2e): guard destructive spend-log truncate behind an explicit opt-in #33751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yassin-berriai
merged 1 commit into
litellm_internal_staging
from
litellm_lit4555_e2e_spend_truncate_guard
Jul 20, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| """Shared, destructive DB helpers for the e2e harness. | ||
|
|
||
| Kept at the top level next to e2e_config and lifecycle so every suite imports it | ||
| by name (`from e2e_db import ...`); no suite reaches into another's directory by | ||
| mutating sys.path. | ||
|
|
||
| reset_spend_logs truncates LiteLLM_SpendLogs and cannot be undone, so the | ||
| session-finish cleanup routes through run_spend_log_cleanup, which fires the | ||
| truncate only on an explicit operator opt-in. "An e2e test ran" is necessary but | ||
| never sufficient: a DATABASE_URL pointing at a shared or staging instance must | ||
| not be wiped by a routine local run that merely exercised a test. | ||
| """ | ||
|
|
||
| import os | ||
| from collections.abc import Callable | ||
|
|
||
| RESET_OPT_IN_ENV = "E2E_RESET_SPEND_LOGS" | ||
|
|
||
|
|
||
| def run_spend_log_cleanup( | ||
| *, opt_in: str | None, e2e_test_ran: bool, truncate: Callable[[], None] | ||
| ) -> bool: | ||
| """Invoke `truncate` iff the destructive spend-log reset is both opted into | ||
| and warranted, returning whether the truncate was attempted. | ||
|
|
||
| The truncate fires only when the opt-in value is exactly "1" AND an e2e test | ||
| body actually ran. Any other opt-in value (unset, "0", "true", "") leaves the | ||
| DB untouched, so the destructive path is never armed by the env var's mere | ||
| presence or by a test run on its own. Best-effort: a truncate failure is | ||
| swallowed so cleanup never fails the session, so the returned bool reports | ||
| that the reset was attempted, not that the DB call succeeded. | ||
| """ | ||
| if opt_in != "1" or not e2e_test_ran: | ||
| return False | ||
| try: | ||
| truncate() | ||
| except Exception as exc: # noqa: BLE001 - cleanup is best-effort | ||
| print(f"spend-log cleanup best-effort failed: {exc}") | ||
| return True | ||
|
|
||
|
|
||
| def reset_spend_logs() -> None: | ||
| """Truncate LiteLLM_SpendLogs for a clean slate. No proxy endpoint deletes | ||
| spend logs (/global/spend/reset keeps them), so go to the DB directly. Uses | ||
| DATABASE_URL (default: the local docker postgres on its mapped host port; the | ||
| in-container `@db` host isn't resolvable from the host, so default to | ||
| localhost). | ||
| """ | ||
| import psycopg | ||
|
|
||
| url = os.environ.get( | ||
| "DATABASE_URL", | ||
| "postgresql://llmproxy:dbpassword9090@localhost:5432/litellm", | ||
| ) | ||
| with psycopg.connect(url) as conn: | ||
| _ = conn.execute('TRUNCATE TABLE "LiteLLM_SpendLogs"') | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.