From 05ecfa3470f6809497f4c6f35f4849c0e84b0dd2 Mon Sep 17 00:00:00 2001 From: Yuneng Jiang Date: Mon, 30 Mar 2026 18:32:04 -0700 Subject: [PATCH] [Fix] Use correct Redis env vars and fix Azure AD token test mocking - test_caching_router: Use REDIS_HOST/PORT/PASSWORD/SSL instead of non-existent REDIS_HOST_2 variants - test_router_init_azure_service_principal: Use monkeypatch.setenv instead of patching the os module in only one file, so both common_utils._resolve_env_var and get_azure_ad_token_provider see the mocked credentials. Also clear AZURE_OPENAI_API_KEY to prevent it from short-circuiting the token provider path. --- tests/local_testing/test_caching_ssl.py | 8 ++++---- tests/local_testing/test_router_client_init.py | 18 ++++++------------ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/tests/local_testing/test_caching_ssl.py b/tests/local_testing/test_caching_ssl.py index 523976f123..f94b702473 100644 --- a/tests/local_testing/test_caching_ssl.py +++ b/tests/local_testing/test_caching_ssl.py @@ -69,10 +69,10 @@ def test_caching_router(): ] litellm.cache = Cache( type="redis", - host="os.environ/REDIS_HOST_2", - port="os.environ/REDIS_PORT_2", - password="os.environ/REDIS_PASSWORD_2", - ssl="os.environ/REDIS_SSL_2", + host="os.environ/REDIS_HOST", + port="os.environ/REDIS_PORT", + password="os.environ/REDIS_PASSWORD", + ssl="os.environ/REDIS_SSL", ) router = Router( model_list=model_list, diff --git a/tests/local_testing/test_router_client_init.py b/tests/local_testing/test_router_client_init.py index f2541601c3..f53ca1ced8 100644 --- a/tests/local_testing/test_router_client_init.py +++ b/tests/local_testing/test_router_client_init.py @@ -71,9 +71,7 @@ def test_router_init_with_neither_api_key_nor_azure_service_principal_with_secre @patch("azure.identity.get_bearer_token_provider") @patch("azure.identity.ClientSecretCredential") -@patch("litellm.secret_managers.get_azure_ad_token_provider.os") def test_router_init_azure_service_principal_with_secret_with_environment_variables( - mocked_os_lib: MagicMock, mocked_credential: MagicMock, mocked_get_bearer_token_provider: MagicMock, monkeypatch, @@ -86,21 +84,17 @@ def test_router_init_azure_service_principal_with_secret_with_environment_variab and environment variables. """ monkeypatch.delenv("AZURE_API_KEY", raising=False) + monkeypatch.delenv("AZURE_OPENAI_API_KEY", raising=False) litellm.enable_azure_ad_token_refresh = True # mock the token provider function mocked_func_generating_token = MagicMock(return_value="test_token") mocked_get_bearer_token_provider.return_value = mocked_func_generating_token - # mock the environment variables with mocked credentials - environment_variables_expected_to_use = { - "AZURE_CLIENT_ID": "test_client_id", - "AZURE_CLIENT_SECRET": "test_client_secret", - "AZURE_TENANT_ID": "test_tenant_id", - } - mocked_environ = PropertyMock(return_value=environment_variables_expected_to_use) - # Because of the way mock attributes are stored you can’t directly attach a PropertyMock to a mock object. - # https://docs.python.org/3.11/library/unittest.mock.html#unittest.mock.PropertyMock - type(mocked_os_lib).environ = mocked_environ + # set environment variables with mocked credentials using monkeypatch + # so both common_utils._resolve_env_var and get_azure_ad_token_provider see them + monkeypatch.setenv("AZURE_CLIENT_ID", "test_client_id") + monkeypatch.setenv("AZURE_CLIENT_SECRET", "test_client_secret") + monkeypatch.setenv("AZURE_TENANT_ID", "test_tenant_id") # define the model list model_list = [