Skip to content

test(e2e): cover key max_budget blocks on personal, team, and team-member keys - #33895

Merged
ryan-crabbe-berri merged 2 commits into
litellm_internal_stagingfrom
litellm_e2e_key_budget_key_kinds
Jul 23, 2026
Merged

ryan-crabbe-berri merged 2 commits into
litellm_internal_stagingfrom
litellm_e2e_key_budget_key_kinds

Conversation

@ryan-crabbe-berri

@ryan-crabbe-berri ryan-crabbe-berri commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Relevant issues

Linear ticket

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • My PR passes all CI/CD checks (e.g., lint, format, unit tests)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Screenshots / Proof of Fix

All runs are against a live proxy on localhost:4000 running staging commit 366ec6f with a throwaway Postgres and Redis, the fast budget rescheduler, and real provider calls (claude-haiku-4-5 is backed by groq/llama-3.3-70b-versatile locally since that is the only live provider key on this machine). Proof was restamped after the refactor commit: the file content that ran is byte-identical to this PR's head b30603e

$ for run in 1 2 3 4 5; do python -m pytest tests/e2e/quota_management/budgets/test_budget_enforcement_e2e.py -q | tail -1; sleep 15; done
9 passed in 23.09s
9 passed in 22.41s
9 passed in 22.83s
9 passed in 22.60s
9 passed in 22.71s

Earlier, the three new sweep tests alone also ran five consecutive times at the pre-refactor commit f012661 (3 passed in 8.50s, 8.21s, 8.68s, 8.20s, 8.26s). basedpyright tests/e2e reports 0 errors and python -m coverage_registry.collector --strict passes

Type

✅ Test

Changes

Two commits. The first covers the "Key max_budget blocks" row of the budgets coverage matrix for the three unexercised key kinds: the existing bare-key case proves a key's own budget blocks; the new cases prove the same key-level cap still blocks when the key is minted to a person (user_id), a team (team_id), or a team membership (both), i.e. that surrounding entities do not shadow or absorb the key's own budget. Each puts the tiny cap (3e-6) on the key itself and every budget around it at a roomy 100.0, so the key-level check is provably the only thing that can block; after the capped key is refused with a 429 budget_exceeded, an uncapped control key minted to the same surroundings must still serve

The second commit converts the file from the E2ECase class pattern (init/run/teardown case classes driven by run_case through one parametrized test) to the plain-pytest resources fixture pattern every other budgets file already uses. That pattern existed only in this one file, dating from the founding suite PR #30869; the rewrite turns the nine cases into two spec classes, TestBudgetBlocksPerLevel (one test per budget level: bare key, team, internal user, end-user, organization, team member) and TestKeyBudgetBlocksAcrossKeyKinds (the sweep above), with cleanup via resources.defer and the repeated block-shape assert hoisted into _assert_blocked_429. Assertions and setup shapes are behavior-preserving; node ids change from test_budget_enforcement[XxxCase] to the class::method form. The now-dead E2ECase protocol and run_case driver are removed from lifecycle.py (this file was their only consumer) and the conftest docstring updated

All nine tests keep their original registry cells; the three sweep tests intentionally share quota_management.budget.key.blocks_over_limit with the bare-key test (same pattern as the team-member-key variant sharing internal_user.resets_after_window): the matrix tracks the per-key-kind split, the registry keeps one row per budget level x behavior

QA runbook

  • TestBudgetBlocksPerLevel::test_bare_key_blocks_over_its_own_budget mints a bare key capped at 3e-6, drives /chat/completions until refused, asserts 429 budget_exceeded; key deleted on teardown
  • TestBudgetBlocksPerLevel::test_team_budget_blocks_every_team_key creates a team capped at 3e-6 with two uncapped keys, drives one to the block, asserts the sibling's first call is also a 429 budget_exceeded; teardown deletes keys then team
  • TestBudgetBlocksPerLevel::test_user_budget_enforced_across_all_their_keys creates a user capped at 3e-6 with two personal keys and a team-member key on an uncapped team, drives the first personal key to the block, asserts the other two keys are refused as well; teardown deletes keys, team, user
  • TestBudgetBlocksPerLevel::test_end_user_budget_blocks_attributed_calls creates a customer capped at 3e-6 and drives calls attributed via user= until refused as a budget block; teardown deletes key and customer
  • TestBudgetBlocksPerLevel::test_org_budget_blocks_keys_under_it creates a capped org with an uncapped team and key under it, drives to the block, asserts the refusal names Organization=<id>; teardown deletes key, team, org
  • TestBudgetBlocksPerLevel::test_member_budget_blocks_without_touching_teammates creates a roomy team, caps one member at 3e-6 via max_budget_in_team, drives that member's key to the block, asserts an uncapped teammate's key still serves; teardown deletes keys, users, team
  • TestKeyBudgetBlocksAcrossKeyKinds::test_personal_key_blocks_over_its_own_budget creates a user with max_budget 100, mints a personal key capped at 3e-6 plus an uncapped control key, drives the capped key to a 429 budget_exceeded, asserts the control key still serves; teardown deletes keys then user
  • TestKeyBudgetBlocksAcrossKeyKinds::test_team_key_blocks_over_its_own_budget does the same on a team with max_budget 100
  • TestKeyBudgetBlocksAcrossKeyKinds::test_team_member_key_blocks_over_its_own_budget does the same on a team-member key with team, user, and membership all at 100
  • python -m coverage_registry.collector --strict passes with the sweep tests sharing quota_management.budget.key.blocks_over_limit

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

@greptile-apps

greptile-apps Bot commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds three new e2e tests covering max_budget enforcement on personal, team, and team-member keys, and refactors the entire test_budget_enforcement_e2e.py file from the bespoke E2ECase/run_case class-driven pattern to the plain-pytest resources fixture pattern that every other budget suite already uses.

  • New sweep tests (TestKeyBudgetBlocksAcrossKeyKinds): each test places TINY_CAP (3e-6) on the key itself while keeping every surrounding budget roomy (100.0), then asserts the key is refused with 429 budget_exceeded and that an uncapped control key minted to the same surroundings keeps serving — proving the key-level cap is the only blocker regardless of mint shape.
  • Refactor (TestBudgetBlocksPerLevel): the six existing level tests are rewritten as class methods using resources.defer for LIFO cleanup; behavior, assertions, and coverage registry cells are preserved exactly.
  • Dead code removal from lifecycle.py: E2ECase Protocol and run_case() are removed as this file was their only consumer.

Confidence Score: 5/5

Safe to merge — changes are confined to the e2e test directory, touch no production code, and preserve all existing test assertions and coverage registry cells while adding well-isolated new cases.

The refactor is behavior-preserving: teardown order is correctly LIFO throughout, lambda captures are not in loops so there is no late-binding hazard, and the new sweep tests correctly keep surroundings roomy (100.0) so only the key-level cap (3e-6) can trip — the control-key assertion then confirms nothing else in the hierarchy was the blocker. Dead-code removal of E2ECase and run_case is clean with no remaining consumers.

No files require special attention.

Important Files Changed

Filename Overview
tests/e2e/quota_management/budgets/test_budget_enforcement_e2e.py Rewrites from E2ECase/run_case to plain-pytest class pattern; adds three new sweep tests isolating the key-level max_budget across personal, team, and team-member mint shapes
tests/e2e/lifecycle.py Removes the now-unused E2ECase Protocol and run_case() driver; ResourceManager and related protocols are unchanged
tests/e2e/conftest.py Docstring-only update to reflect the ResourceManager lifecycle; no functional change

Reviews (2): Last reviewed commit: "refactor(e2e): convert budget enforcemen..." | Re-trigger Greptile

@codecov

codecov Bot commented Jul 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_e2e_key_budget_key_kinds (b30603e) with litellm_internal_staging (ac1b4b3)

Open in CodSpeed

…ure pattern

The E2ECase class pattern existed only in this file; every other suite uses
plain pytest tests with the resources fixture. Rewrites the nine cases as two
spec classes and removes the now-dead E2ECase protocol and run_case driver
from lifecycle.py
@ryan-crabbe-berri

Copy link
Copy Markdown
Contributor Author

@greptileai re review

@ryan-crabbe-berri
ryan-crabbe-berri merged commit 9f6b3d2 into litellm_internal_staging Jul 23, 2026
88 checks passed
@ryan-crabbe-berri
ryan-crabbe-berri deleted the litellm_e2e_key_budget_key_kinds branch July 23, 2026 00:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants