mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-26 01:12:21 +00:00
* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * ci: rename fork-flag to unit-flag now that it applies on every event * test: move tests/test_litellm root and small trees into tests/unit Pure renames, no content changes. Follow-up commits in this PR fix references, merge the three files that already existed in tests/unit, keep live-provider tests in tests/test_litellm and wire CI. * test: carry tests/test_litellm conftest isolation into tests/unit Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS, proxy-URL and keychain env, and session-end client cleanup now reset for unit tests too. The environment isolation owns its MonkeyPatch so a test's own monkeypatch is undone before the model-cost teardown runs. * test: merge, split and prune the moved root and small-tree tests Merge batches/test_batch_utils.py and the chat_completions and messages dispatch tests into the files that already existed in tests/unit. Keep the live Gemini interactions tests, the async image-fetch format test and the OpenAI embedding scorer test in tests/test_litellm since they need real network or keys. Put test_router.py under tests/unit/test_router so the existing package no longer shadows it. Delete eight tests the audit found superseded by stronger ones kept in this move. * ci: run the moved root and small-tree tests under their legacy flags Add the misc and responses-caching-types flags to unit_selection.sh and CircleCI, extend enterprise-routing and mcp-integration, and point the legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest and change classifier at the new paths. * test: make the new tests/unit directories packages tests/unit/test_package_layout.py requires every directory to carry an __init__.py, and without one the moved and retained test_litellm_responses_bridge.py modules collide on import. * test: scope the unit socket block to tests/unit in shared sessions The GHA shards collect the legacy test-path and the unit selection in one pytest session. The unit conftest's loopback-only block leaked into legacy modules that reach the network at import. The legacy conftest now lifts the restriction at collect and setup time, and the unit conftest re-applies it when collecting its own modules. * test: give the shard-script tests their own GITHUB_OUTPUT They only passed where the runner set it. The CircleCI unit job's env allowlist drops it, so the script's redirect failed there. * test: point the router and module-deletion checks at tests/unit router_code_coverage and code_qa_check_tests only searched tests/test_litellm, so the moved router tests no longer counted. The two silent-experiment tests the audit deleted were the only direct callers of those methods; they are replaced with tests that assert the forwarded shadow request and the recursion guard. --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
190 lines
7.3 KiB
Python
190 lines
7.3 KiB
Python
"""Tests for the env-var extraction used by tests/documentation_tests/test_env_keys.py.
|
|
|
|
That script is the CI gate that fails when a user-facing environment variable read
|
|
under litellm/ is mentioned nowhere on the docs site. It only sees a key if one of its
|
|
patterns matches the call, so a call shape the patterns miss silently bypasses the gate.
|
|
Each supported shape is asserted here, along with the shapes that must not be treated as
|
|
env var reads, so narrowing a pattern makes a test fail instead of quietly reopening the
|
|
hole. The docs side is asserted too, since a key documented on a provider page rather
|
|
than in the central reference table still counts as documented.
|
|
"""
|
|
|
|
import importlib.util
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
_REPO_ROOT = Path(__file__).resolve().parents[2]
|
|
_MODULE_PATH = _REPO_ROOT / "tests" / "documentation_tests" / "test_env_keys.py"
|
|
_spec = importlib.util.spec_from_file_location("documentation_test_env_keys", _MODULE_PATH)
|
|
assert _spec is not None and _spec.loader is not None
|
|
gate = importlib.util.module_from_spec(_spec)
|
|
sys.modules[_spec.name] = gate
|
|
_spec.loader.exec_module(gate)
|
|
|
|
|
|
def test_bare_get_secret_bool_is_captured() -> None:
|
|
assert gate.extract_env_keys('flag = get_secret_bool("QSTASH_FLUSH_ON_BOOT")') == {"QSTASH_FLUSH_ON_BOOT"}
|
|
|
|
|
|
def test_get_secret_bool_with_default_is_captured() -> None:
|
|
assert gate.extract_env_keys('if get_secret_bool("QSTASH_FLUSH_ON_BOOT", False) is not True:') == {
|
|
"QSTASH_FLUSH_ON_BOOT"
|
|
}
|
|
|
|
|
|
def test_get_secret_bool_with_keyword_default_is_captured() -> None:
|
|
assert gate.extract_env_keys('get_secret_bool("QSTASH_FLUSH_ON_BOOT", default_value=False)') == {
|
|
"QSTASH_FLUSH_ON_BOOT"
|
|
}
|
|
|
|
|
|
def test_litellm_prefixed_get_secret_bool_is_captured() -> None:
|
|
assert gate.extract_env_keys('litellm.get_secret_bool("QSTASH_FLUSH_ON_BOOT")') == {"QSTASH_FLUSH_ON_BOOT"}
|
|
|
|
|
|
def test_bare_get_secret_is_captured() -> None:
|
|
assert gate.extract_env_keys('key = get_secret("QSTASH_ALPHA")') == {"QSTASH_ALPHA"}
|
|
|
|
|
|
def test_bare_get_secret_with_default_is_captured() -> None:
|
|
assert gate.extract_env_keys('key = get_secret("QSTASH_ALPHA", "fallback")') == {"QSTASH_ALPHA"}
|
|
|
|
|
|
def test_bare_get_secret_str_is_captured() -> None:
|
|
assert gate.extract_env_keys('key = get_secret_str("QSTASH_BRAVO")') == {"QSTASH_BRAVO"}
|
|
|
|
|
|
def test_bare_get_secret_str_with_keyword_default_is_captured() -> None:
|
|
assert gate.extract_env_keys('get_secret_str("QSTASH_BRAVO", default_value=None)') == {"QSTASH_BRAVO"}
|
|
|
|
|
|
def test_get_secret_reached_through_the_utils_module_is_captured() -> None:
|
|
assert gate.extract_env_keys('litellm.utils.get_secret("QSTASH_ALPHA")') == {"QSTASH_ALPHA"}
|
|
|
|
|
|
def test_get_secret_on_an_unrelated_utils_attribute_is_not_an_env_read() -> None:
|
|
source = "\n".join(
|
|
(
|
|
'vault.utils.get_secret("QSTASH_ALPHA")',
|
|
'self.utils.get_secret_str("QSTASH_BRAVO")',
|
|
)
|
|
)
|
|
assert gate.extract_env_keys(source) == frozenset()
|
|
|
|
|
|
def test_previously_supported_call_shapes_are_still_captured() -> None:
|
|
source = "\n".join(
|
|
(
|
|
'os.getenv("QSTASH_ALPHA")',
|
|
'os.getenv("QSTASH_BRAVO", "fallback")',
|
|
'litellm.get_secret("QSTASH_CHARLIE")',
|
|
'litellm.get_secret_str("QSTASH_DELTA", default_value=None)',
|
|
)
|
|
)
|
|
assert gate.extract_env_keys(source) == {"QSTASH_ALPHA", "QSTASH_BRAVO", "QSTASH_CHARLIE", "QSTASH_DELTA"}
|
|
|
|
|
|
def test_get_secret_calls_on_unrelated_objects_are_not_env_reads() -> None:
|
|
source = "\n".join(
|
|
(
|
|
'vault_client.get_secret("QSTASH_ALPHA")',
|
|
'self.get_secret_str("QSTASH_BRAVO")',
|
|
'provider.get_secret_bool("QSTASH_CHARLIE")',
|
|
)
|
|
)
|
|
assert gate.extract_env_keys(source) == frozenset()
|
|
|
|
|
|
def test_similarly_named_helpers_are_not_env_reads() -> None:
|
|
assert gate.extract_env_keys('get_secret_bundle("QSTASH_ALPHA")') == frozenset()
|
|
|
|
|
|
def test_non_literal_arguments_are_not_env_reads() -> None:
|
|
assert gate.extract_env_keys("get_secret_bool(flag_name)") == frozenset()
|
|
|
|
|
|
def test_excluded_keys_are_filtered_for_every_call_shape() -> None:
|
|
source = "\n".join(
|
|
(
|
|
'os.getenv("TERM_PROGRAM")',
|
|
'get_secret_bool("LITELLM_RUST")',
|
|
'litellm.get_secret_str("MAVVRIK_FOCUS_FREQUENCY")',
|
|
)
|
|
)
|
|
assert gate.extract_env_keys(source) == frozenset()
|
|
|
|
|
|
def test_a_key_mentioned_outside_the_reference_table_counts_as_documented() -> None:
|
|
docs = "\n".join(
|
|
(
|
|
"# Qstash",
|
|
"",
|
|
"Set `QSTASH_ALPHA` to your endpoint before calling the provider.",
|
|
"",
|
|
"```bash",
|
|
'export QSTASH_BRAVO="sk-..."',
|
|
"```",
|
|
)
|
|
)
|
|
documented = gate.extract_documented_keys(docs)
|
|
assert "QSTASH_ALPHA" in documented
|
|
assert "QSTASH_BRAVO" in documented
|
|
|
|
|
|
def test_a_name_glued_to_surrounding_text_is_not_a_mention() -> None:
|
|
documented = gate.extract_documented_keys("the useQSTASH_ALPHA helper reads it")
|
|
assert "QSTASH_ALPHA" not in documented
|
|
|
|
|
|
def test_a_longer_name_does_not_document_the_key_it_ends_with() -> None:
|
|
documented = gate.extract_documented_keys("Set AZURE_QSTASH_ALPHA in your environment")
|
|
assert "AZURE_QSTASH_ALPHA" in documented
|
|
assert "QSTASH_ALPHA" not in documented
|
|
|
|
|
|
def test_lowercase_mentions_are_not_treated_as_env_var_names() -> None:
|
|
assert gate.extract_documented_keys("pass qstash_alpha as a config key") == frozenset()
|
|
|
|
|
|
def test_documented_keys_are_collected_from_every_page_of_the_docs_site(tmp_path: Path) -> None:
|
|
(tmp_path / "providers").mkdir()
|
|
(tmp_path / "providers" / "qstash.md").write_text("Set `QSTASH_ALPHA` to your endpoint.\n", encoding="utf-8")
|
|
(tmp_path / "providers" / "qstash_batches.mdx").write_text("| QSTASH_BRAVO | second key |\n", encoding="utf-8")
|
|
documented = gate.collect_documented_keys(str(tmp_path))
|
|
assert "QSTASH_ALPHA" in documented
|
|
assert "QSTASH_BRAVO" in documented
|
|
assert "QSTASH_CHARLIE" not in documented
|
|
|
|
|
|
def _write_tree(tmp_path: Path, source: str, docs_page: str) -> tuple[str, str]:
|
|
source_dir = tmp_path / "litellm"
|
|
docs_dir = tmp_path / "docs" / "providers"
|
|
source_dir.mkdir()
|
|
docs_dir.mkdir(parents=True)
|
|
(source_dir / "qstash.py").write_text(source, encoding="utf-8")
|
|
(docs_dir / "qstash.md").write_text(docs_page, encoding="utf-8")
|
|
return str(source_dir), str(tmp_path / "docs")
|
|
|
|
|
|
def test_a_key_documented_only_on_a_provider_page_satisfies_the_gate(tmp_path: Path) -> None:
|
|
source_dir, docs_dir = _write_tree(
|
|
tmp_path,
|
|
'api_key = get_secret_str("QSTASH_ALPHA")\n',
|
|
"# Qstash\n\nSet `QSTASH_ALPHA` to your API key.\n",
|
|
)
|
|
assert gate.undocumented_env_keys(source_dir, docs_dir) == frozenset()
|
|
|
|
|
|
def test_a_key_documented_on_no_page_at_all_fails_the_gate(tmp_path: Path) -> None:
|
|
source_dir, docs_dir = _write_tree(
|
|
tmp_path,
|
|
'api_key = get_secret_str("QSTASH_ALPHA")\n',
|
|
"# Qstash\n\nThis provider needs an API key.\n",
|
|
)
|
|
assert gate.undocumented_env_keys(source_dir, docs_dir) == {"QSTASH_ALPHA"}
|
|
|
|
|
|
def test_only_documentation_pages_are_scanned_for_mentions(tmp_path: Path) -> None:
|
|
(tmp_path / "notes.txt").write_text("QSTASH_ALPHA\n", encoding="utf-8")
|
|
(tmp_path / "example.py").write_text('get_secret("QSTASH_BRAVO")\n', encoding="utf-8")
|
|
assert gate.collect_documented_keys(str(tmp_path)) == frozenset()
|