From 0e17462589a741c03d8a5ad608676cc69c8c8169 Mon Sep 17 00:00:00 2001 From: yuneng-jiang Date: Wed, 5 Nov 2025 11:00:04 -0800 Subject: [PATCH] Adding test --- tests/proxy_unit_tests/test_proxy_server.py | 72 +++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/tests/proxy_unit_tests/test_proxy_server.py b/tests/proxy_unit_tests/test_proxy_server.py index 1f634d7b9a5..1f4bf806c16 100644 --- a/tests/proxy_unit_tests/test_proxy_server.py +++ b/tests/proxy_unit_tests/test_proxy_server.py @@ -2299,3 +2299,75 @@ def test_get_timeout_from_request(): } timeout = LiteLLMProxyRequestSetup._get_timeout_from_request(headers) assert timeout == 90.5 + + +@pytest.mark.parametrize( + "ui_exists, ui_has_content", + [ + (True, True), # UI path exists and has content + (True, False), # UI path exists but is empty + (False, False), # UI path doesn't exist + ], +) +def test_non_root_ui_path_logic(monkeypatch, tmp_path, ui_exists, ui_has_content): + """ + Test the non-root Docker UI path detection logic. + + Tests that when LITELLM_NON_ROOT is set to "true": + - If UI path exists and has content, it should be used + - If UI path doesn't exist or is empty, proper error logging occurs + """ + import tempfile + import shutil + from unittest.mock import MagicMock + + # Create a temporary directory to act as /tmp/litellm_ui + test_ui_path = tmp_path / "litellm_ui" + + if ui_exists: + test_ui_path.mkdir(parents=True, exist_ok=True) + if ui_has_content: + # Create some dummy files to simulate built UI + (test_ui_path / "index.html").write_text("") + (test_ui_path / "app.js").write_text("console.log('test');") + + # Mock the environment variable and os.path operations + monkeypatch.setenv("LITELLM_NON_ROOT", "true") + + # Create a mock logger to capture log messages + mock_logger = MagicMock() + + # We need to reimport or reload the relevant code section + # Since this is module-level code, we'll test the logic directly + ui_path = None + non_root_ui_path = str(test_ui_path) + + # Simulate the logic from proxy_server.py lines 909-920 + if os.getenv("LITELLM_NON_ROOT", "").lower() == "true": + if os.path.exists(non_root_ui_path) and os.listdir(non_root_ui_path): + mock_logger.info(f"Using pre-built UI for non-root Docker: {non_root_ui_path}") + mock_logger.info(f"UI files found: {len(os.listdir(non_root_ui_path))} items") + ui_path = non_root_ui_path + else: + mock_logger.error(f"UI not found at {non_root_ui_path}. UI will not be available.") + mock_logger.error(f"Path exists: {os.path.exists(non_root_ui_path)}, Has content: {os.path.exists(non_root_ui_path) and bool(os.listdir(non_root_ui_path))}") + + # Verify behavior based on test parameters + if ui_exists and ui_has_content: + # UI should be found and used + assert ui_path == non_root_ui_path + assert mock_logger.info.call_count == 2 + mock_logger.info.assert_any_call(f"Using pre-built UI for non-root Docker: {non_root_ui_path}") + # Verify the second info call mentions the number of items + info_calls = [call[0][0] for call in mock_logger.info.call_args_list] + assert any("UI files found:" in call and "items" in call for call in info_calls) + assert mock_logger.error.call_count == 0 + else: + # UI should not be found, error should be logged + assert ui_path is None + assert mock_logger.error.call_count == 2 + mock_logger.error.assert_any_call(f"UI not found at {non_root_ui_path}. UI will not be available.") + # Verify the second error call has path existence info + error_calls = [call[0][0] for call in mock_logger.error.call_args_list] + assert any("Path exists:" in call for call in error_calls) + assert mock_logger.info.call_count == 0