mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-12 08:42:18 +00:00
test(proxy): run the #44517 regression test through the startup UI rewrite
Move the startup loop that rewrites the UI files into _apply_server_root_path_to_ui so the test can run it on a temp UI directory across restarts, and fold the test into test_proxy_server.py
This commit is contained in:
parent
aeb333273d
commit
2ac43122bd
3 changed files with 59 additions and 61 deletions
|
|
@ -2379,6 +2379,44 @@ try:
|
|||
for part in content.split(ui_config_path)
|
||||
)
|
||||
|
||||
def _apply_server_root_path_to_ui(ui_dir: str, server_root_path: str) -> None:
|
||||
for root, dirs, files in os.walk(ui_dir):
|
||||
for filename in files:
|
||||
file_path = os.path.join(root, filename)
|
||||
# Skip binary files and files that don't need path replacement
|
||||
if filename.endswith(
|
||||
(
|
||||
".png",
|
||||
".jpg",
|
||||
".jpeg",
|
||||
".gif",
|
||||
".ico",
|
||||
".woff",
|
||||
".woff2",
|
||||
".ttf",
|
||||
".eot",
|
||||
)
|
||||
):
|
||||
continue
|
||||
try:
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
|
||||
# Replace the asset prefix with the server root path
|
||||
modified_content = content.replace(
|
||||
f"{litellm_asset_prefix}",
|
||||
f"{server_root_path}",
|
||||
)
|
||||
|
||||
# Replace the /.well-known/litellm-ui-config with the server root path
|
||||
modified_content = _rewrite_ui_config_path(modified_content, server_root_path)
|
||||
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
f.write(modified_content)
|
||||
except (UnicodeDecodeError, PermissionError, OSError):
|
||||
# Skip binary files or files we can't write to
|
||||
continue
|
||||
|
||||
# Use a writable runtime UI directory whenever possible.
|
||||
# This prevents mutating the packaged UI directory (e.g. site-packages or the repo checkout)
|
||||
# and ensures extensionless routes like /ui/login work via <route>/index.html.
|
||||
|
|
@ -2464,43 +2502,7 @@ try:
|
|||
ui_path,
|
||||
)
|
||||
else:
|
||||
# Iterate through files in the UI directory
|
||||
for root, dirs, files in os.walk(ui_path):
|
||||
for filename in files:
|
||||
file_path = os.path.join(root, filename)
|
||||
# Skip binary files and files that don't need path replacement
|
||||
if filename.endswith(
|
||||
(
|
||||
".png",
|
||||
".jpg",
|
||||
".jpeg",
|
||||
".gif",
|
||||
".ico",
|
||||
".woff",
|
||||
".woff2",
|
||||
".ttf",
|
||||
".eot",
|
||||
)
|
||||
):
|
||||
continue
|
||||
try:
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
|
||||
# Replace the asset prefix with the server root path
|
||||
modified_content = content.replace(
|
||||
f"{litellm_asset_prefix}",
|
||||
f"{server_root_path}",
|
||||
)
|
||||
|
||||
# Replace the /.well-known/litellm-ui-config with the server root path
|
||||
modified_content = _rewrite_ui_config_path(modified_content, server_root_path)
|
||||
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
f.write(modified_content)
|
||||
except (UnicodeDecodeError, PermissionError, OSError):
|
||||
# Skip binary files or files we can't write to
|
||||
continue
|
||||
_apply_server_root_path_to_ui(ui_path, server_root_path)
|
||||
|
||||
# # Mount the _next directory at the root level
|
||||
app.mount(
|
||||
|
|
|
|||
|
|
@ -1,24 +0,0 @@
|
|||
"""SERVER_ROOT_PATH rewrite of the UI's config request (#44517)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from litellm.proxy.proxy_server import _rewrite_ui_config_path
|
||||
|
||||
# The two shapes the config path takes in the packaged UI bundle
|
||||
PACKAGED_UI_CHUNK = (
|
||||
'fetch(`${g}/litellm/.well-known/litellm-ui-config`);const u={url:"/litellm/.well-known/litellm-ui-config"}'
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("server_root_path", ["/foo", "/litellm", "/foo/litellm"])
|
||||
def test_restarts_keep_a_single_root_path_prefix(server_root_path: str) -> None:
|
||||
rewritten = PACKAGED_UI_CHUNK
|
||||
for _ in range(3): # the proxy rewrites the same UI files on every start
|
||||
rewritten = _rewrite_ui_config_path(rewritten, server_root_path)
|
||||
|
||||
assert rewritten == (
|
||||
f"fetch(`${{g}}{server_root_path}/.well-known/litellm-ui-config`);"
|
||||
f'const u={{url:"{server_root_path}/.well-known/litellm-ui-config"}}'
|
||||
)
|
||||
|
|
@ -3020,6 +3020,26 @@ def test_non_root_ui_path_logic(monkeypatch, tmp_path, ui_exists, ui_has_content
|
|||
assert mock_logger.info.call_count == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize("server_root_path", ["/foo", "/litellm", "/foo/litellm"])
|
||||
def test_server_root_path_ui_rewrite_is_stable_across_restarts(tmp_path, server_root_path):
|
||||
from litellm.proxy.proxy_server import _apply_server_root_path_to_ui
|
||||
|
||||
chunk = tmp_path / "_next" / "static" / "chunks" / "app.js"
|
||||
chunk.parent.mkdir(parents=True)
|
||||
chunk.write_text(
|
||||
'fetch(`${g}/litellm/.well-known/litellm-ui-config`);const u={url:"/litellm/.well-known/litellm-ui-config"}',
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
for _ in range(3):
|
||||
_apply_server_root_path_to_ui(str(tmp_path), server_root_path)
|
||||
|
||||
assert chunk.read_text(encoding="utf-8") == (
|
||||
f"fetch(`${{g}}{server_root_path}/.well-known/litellm-ui-config`);"
|
||||
f'const u={{url:"{server_root_path}/.well-known/litellm-ui-config"}}'
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_config_callbacks_with_all_types(client_no_auth):
|
||||
"""
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue