mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 19:24:27 +00:00
fix(proxy_server.py): get master key from environment, if not set in … (#9617)
* fix(proxy_server.py): get master key from environment, if not set in general settings or general settings not set at all * test: mark flaky test * test(test_proxy_server.py): mock prisma client * ci: add new github workflow for testing just the mock tests * fix: fix linting error * ci(conftest.py): add conftest.py to isolate proxy tests * build(pyproject.toml): add respx to dev dependencies * build(pyproject.toml): add prisma to dev dependencies * test: fix mock prompt management tests to use a mock anthropic key * ci(test-litellm.yml): parallelize mock testing make it run faster * build(pyproject.toml): add hypercorn as dev dep * build(pyproject.toml): separate proxy vs. core dev dependencies make it easier for non-proxy contributors to run tests locally - e.g. no need to install hypercorn * ci(test-litellm.yml): pin python version * test(test_rerank.py): move test - cannot be mocked, requires aws credentials for e2e testing * ci: add thank you message to ci * test: add mock env var to test * test: add autouse to tests * test: test mock env vars for e2e tests
This commit is contained in:
parent
9a44e77046
commit
8a5b0b6218
14 changed files with 479 additions and 284 deletions
|
@ -9,6 +9,7 @@ from unittest.mock import AsyncMock, MagicMock, mock_open, patch
|
|||
import click
|
||||
import httpx
|
||||
import pytest
|
||||
import yaml
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
@ -74,3 +75,92 @@ async def test_initialize_scheduled_jobs_credentials(monkeypatch):
|
|||
call[0] for call in mock_proxy_config.get_credentials.mock_calls
|
||||
]
|
||||
assert len(mock_scheduler_calls) > 0
|
||||
|
||||
|
||||
# Mock Prisma
|
||||
class MockPrisma:
|
||||
def __init__(self, database_url=None, proxy_logging_obj=None, http_client=None):
|
||||
self.database_url = database_url
|
||||
self.proxy_logging_obj = proxy_logging_obj
|
||||
self.http_client = http_client
|
||||
|
||||
async def connect(self):
|
||||
pass
|
||||
|
||||
async def disconnect(self):
|
||||
pass
|
||||
|
||||
|
||||
mock_prisma = MockPrisma()
|
||||
|
||||
|
||||
@patch(
|
||||
"litellm.proxy.proxy_server.ProxyStartupEvent._setup_prisma_client",
|
||||
return_value=mock_prisma,
|
||||
)
|
||||
@pytest.mark.asyncio
|
||||
async def test_aaaproxy_startup_master_key(mock_prisma, monkeypatch, tmp_path):
|
||||
"""
|
||||
Test that master_key is correctly loaded from either config.yaml or environment variables
|
||||
"""
|
||||
import yaml
|
||||
from fastapi import FastAPI
|
||||
|
||||
# Import happens here - this is when the module probably reads the config path
|
||||
from litellm.proxy.proxy_server import proxy_startup_event
|
||||
|
||||
# Mock the Prisma import
|
||||
monkeypatch.setattr("litellm.proxy.proxy_server.PrismaClient", MockPrisma)
|
||||
|
||||
# Create test app
|
||||
app = FastAPI()
|
||||
|
||||
# Test Case 1: Master key from config.yaml
|
||||
test_master_key = "sk-12345"
|
||||
test_config = {"general_settings": {"master_key": test_master_key}}
|
||||
|
||||
# Create a temporary config file
|
||||
config_path = tmp_path / "config.yaml"
|
||||
with open(config_path, "w") as f:
|
||||
yaml.dump(test_config, f)
|
||||
|
||||
print(f"SET ENV VARIABLE - CONFIG_FILE_PATH, str(config_path): {str(config_path)}")
|
||||
# Second setting of CONFIG_FILE_PATH to a different value
|
||||
monkeypatch.setenv("CONFIG_FILE_PATH", str(config_path))
|
||||
print(f"config_path: {config_path}")
|
||||
print(f"os.getenv('CONFIG_FILE_PATH'): {os.getenv('CONFIG_FILE_PATH')}")
|
||||
async with proxy_startup_event(app):
|
||||
from litellm.proxy.proxy_server import master_key
|
||||
|
||||
assert master_key == test_master_key
|
||||
|
||||
# Test Case 2: Master key from environment variable
|
||||
test_env_master_key = "sk-67890"
|
||||
|
||||
# Create empty config
|
||||
empty_config = {"general_settings": {}}
|
||||
with open(config_path, "w") as f:
|
||||
yaml.dump(empty_config, f)
|
||||
|
||||
monkeypatch.setenv("LITELLM_MASTER_KEY", test_env_master_key)
|
||||
print("test_env_master_key: {}".format(test_env_master_key))
|
||||
async with proxy_startup_event(app):
|
||||
from litellm.proxy.proxy_server import master_key
|
||||
|
||||
assert master_key == test_env_master_key
|
||||
|
||||
# Test Case 3: Master key with os.environ prefix
|
||||
test_resolved_key = "sk-resolved-key"
|
||||
test_config_with_prefix = {
|
||||
"general_settings": {"master_key": "os.environ/CUSTOM_MASTER_KEY"}
|
||||
}
|
||||
|
||||
# Create config with os.environ prefix
|
||||
with open(config_path, "w") as f:
|
||||
yaml.dump(test_config_with_prefix, f)
|
||||
|
||||
monkeypatch.setenv("CUSTOM_MASTER_KEY", test_resolved_key)
|
||||
async with proxy_startup_event(app):
|
||||
from litellm.proxy.proxy_server import master_key
|
||||
|
||||
assert master_key == test_resolved_key
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue