litellm/tests/proxy_unit_tests/test_proxy_config_unit_test.py
Krish Dholakia 27e18358ab
fix(pattern_match_deployments.py): default to user input if unable to… (#6632)
* fix(pattern_match_deployments.py): default to user input if unable to map based on wildcards

* test: fix test

* test: reset test name

* test: update conftest to reload proxy server module between tests

* ci(config.yml): move langfuse out of local_testing

reduce ci/cd time

* ci(config.yml): cleanup langfuse ci/cd tests

* fix: update test to not use global proxy_server app module

* ci: move caching to a separate test pipeline

speed up ci pipeline

* test: update conftest to check if proxy_server attr exists before reloading

* build(conftest.py): don't block on inability to reload proxy_server

* ci(config.yml): update caching unit test filter to work on 'cache' keyword as well

* fix(encrypt_decrypt_utils.py): use function to get salt key

* test: mark flaky test

* test: handle anthropic overloaded errors

* refactor: create separate ci/cd pipeline for proxy unit tests

make ci/cd faster

* ci(config.yml): add litellm_proxy_unit_testing to build_and_test jobs

* ci(config.yml): generate prisma binaries for proxy unit tests

* test: readd vertex_key.json

* ci(config.yml): remove `-s` from proxy_unit_test cmd

speed up test

* ci: remove any 'debug' logging flag

speed up ci pipeline

* test: fix test

* test(test_braintrust.py): rerun

* test: add delay for braintrust test
2024-11-08 00:55:57 +05:30

117 lines
3.4 KiB
Python

import os
import sys
import traceback
from unittest import mock
import pytest
from dotenv import load_dotenv
import litellm.proxy
import litellm.proxy.proxy_server
load_dotenv()
import io
import os
# this file is to test litellm/proxy
sys.path.insert(
0, os.path.abspath("../..")
) # Adds the parent directory to the system path
import asyncio
import logging
from litellm.proxy.proxy_server import ProxyConfig
@pytest.mark.asyncio
async def test_basic_reading_configs_from_files():
"""
Test that the config is read correctly from the files in the example_config_yaml folder
"""
proxy_config_instance = ProxyConfig()
current_path = os.path.dirname(os.path.abspath(__file__))
example_config_yaml_path = os.path.join(current_path, "example_config_yaml")
# get all the files from example_config_yaml
files = os.listdir(example_config_yaml_path)
print(files)
for file in files:
config_path = os.path.join(example_config_yaml_path, file)
config = await proxy_config_instance.get_config(config_file_path=config_path)
print(config)
@pytest.mark.asyncio
async def test_read_config_from_bad_file_path():
"""
Raise an exception if the file path is not valid
"""
proxy_config_instance = ProxyConfig()
config_path = "non-existent-file.yaml"
with pytest.raises(Exception):
config = await proxy_config_instance.get_config(config_file_path=config_path)
@pytest.mark.asyncio
async def test_read_config_file_with_os_environ_vars():
"""
Ensures os.environ variables are read correctly from config.yaml
Following vars are set as os.environ variables in the config.yaml file
- DEFAULT_USER_ROLE
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AZURE_GPT_4O
- FIREWORKS
"""
_env_vars_for_testing = {
"DEFAULT_USER_ROLE": "admin",
"AWS_ACCESS_KEY_ID": "1234567890",
"AWS_SECRET_ACCESS_KEY": "1234567890",
"AZURE_GPT_4O": "1234567890",
"FIREWORKS": "1234567890",
}
_old_env_vars = {}
for key, value in _env_vars_for_testing.items():
if key in os.environ:
_old_env_vars[key] = os.environ.get(key)
os.environ[key] = value
# Read config
proxy_config_instance = ProxyConfig()
current_path = os.path.dirname(os.path.abspath(__file__))
config_path = os.path.join(
current_path, "example_config_yaml", "config_with_env_vars.yaml"
)
config = await proxy_config_instance.get_config(config_file_path=config_path)
print(config)
# Add assertions
assert (
config["litellm_settings"]["default_internal_user_params"]["user_role"]
== "admin"
)
assert (
config["litellm_settings"]["s3_callback_params"]["s3_aws_access_key_id"]
== "1234567890"
)
assert (
config["litellm_settings"]["s3_callback_params"]["s3_aws_secret_access_key"]
== "1234567890"
)
for model in config["model_list"]:
if "azure" in model["litellm_params"]["model"]:
assert model["litellm_params"]["api_key"] == "1234567890"
elif "fireworks" in model["litellm_params"]["model"]:
assert model["litellm_params"]["api_key"] == "1234567890"
# cleanup
for key, value in _env_vars_for_testing.items():
if key in _old_env_vars:
os.environ[key] = _old_env_vars[key]
else:
del os.environ[key]