litellm/tests/unit/containers/test_container_regional_api_base.py
yuneng-jiang f6882246d4
test: move tests/test_litellm root and small trees into tests/unit (#43186)
* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* ci: rename fork-flag to unit-flag now that it applies on every event

* test: move tests/test_litellm root and small trees into tests/unit

Pure renames, no content changes. Follow-up commits in this PR fix
references, merge the three files that already existed in tests/unit,
keep live-provider tests in tests/test_litellm and wire CI.

* test: carry tests/test_litellm conftest isolation into tests/unit

Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS,
proxy-URL and keychain env, and session-end client cleanup now reset for
unit tests too. The environment isolation owns its MonkeyPatch so a test's
own monkeypatch is undone before the model-cost teardown runs.

* test: merge, split and prune the moved root and small-tree tests

Merge batches/test_batch_utils.py and the chat_completions and messages
dispatch tests into the files that already existed in tests/unit. Keep
the live Gemini interactions tests, the async image-fetch format test and
the OpenAI embedding scorer test in tests/test_litellm since they need
real network or keys. Put test_router.py under tests/unit/test_router so
the existing package no longer shadows it. Delete eight tests the audit
found superseded by stronger ones kept in this move.

* ci: run the moved root and small-tree tests under their legacy flags

Add the misc and responses-caching-types flags to unit_selection.sh and
CircleCI, extend enterprise-routing and mcp-integration, and point the
legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest
and change classifier at the new paths.

* test: make the new tests/unit directories packages

tests/unit/test_package_layout.py requires every directory to carry an
__init__.py, and without one the moved and retained
test_litellm_responses_bridge.py modules collide on import.

* test: scope the unit socket block to tests/unit in shared sessions

The GHA shards collect the legacy test-path and the unit selection in one
pytest session. The unit conftest's loopback-only block leaked into legacy
modules that reach the network at import. The legacy conftest now lifts the
restriction at collect and setup time, and the unit conftest re-applies it
when collecting its own modules.

* test: give the shard-script tests their own GITHUB_OUTPUT

They only passed where the runner set it. The CircleCI unit job's env
allowlist drops it, so the script's redirect failed there.

* test: point the router and module-deletion checks at tests/unit

router_code_coverage and code_qa_check_tests only searched tests/test_litellm,
so the moved router tests no longer counted. The two silent-experiment tests
the audit deleted were the only direct callers of those methods; they are
replaced with tests that assert the forwarded shadow request and the
recursion guard.

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-09-25 11:30:43 -07:00

166 lines
5.7 KiB
Python

"""
Tests for OpenAI Containers API regional api_base support.
Validates that litellm.create_container and litellm.upload_container_file
correctly use regional endpoints like https://us.api.openai.com/v1 for
US Data Residency instead of defaulting to https://api.openai.com/v1.
"""
import os
from unittest.mock import MagicMock, patch
import httpx
import pytest
import litellm
class TestContainerRegionalApiBase:
"""Test suite for container API regional api_base support."""
def setup_method(self):
"""Set up test fixtures."""
os.environ["OPENAI_API_KEY"] = "sk-test123"
def teardown_method(self):
"""Clean up after tests."""
if "OPENAI_API_KEY" in os.environ:
del os.environ["OPENAI_API_KEY"]
if "OPENAI_BASE_URL" in os.environ:
del os.environ["OPENAI_BASE_URL"]
if "OPENAI_API_BASE" in os.environ:
del os.environ["OPENAI_API_BASE"]
litellm.api_base = None
@patch("litellm.llms.custom_httpx.http_handler.HTTPHandler.post")
def test_create_container_uses_regional_api_base(self, mock_post):
"""
Test that litellm.create_container uses the regional api_base when provided.
This validates the fix for US Data Residency support where requests should
go to https://us.api.openai.com/v1 instead of https://api.openai.com/v1.
"""
mock_response = MagicMock(spec=httpx.Response)
mock_response.status_code = 200
mock_response.json.return_value = {
"id": "cntr_123456",
"object": "container",
"created_at": 1747857508,
"status": "running",
"expires_after": {"anchor": "last_active_at", "minutes": 20},
"last_active_at": 1747857508,
"name": "Test Container",
}
mock_post.return_value = mock_response
litellm.create_container(
name="Test Container",
custom_llm_provider="openai",
api_base="https://us.api.openai.com/v1",
)
mock_post.assert_called_once()
call_args = mock_post.call_args
called_url = call_args[1]["url"]
assert (
"us.api.openai.com" in called_url
), f"Expected US regional URL, got: {called_url}"
assert called_url == "https://us.api.openai.com/v1/containers"
@patch("litellm.llms.custom_httpx.http_handler.HTTPHandler.post")
def test_create_container_uses_env_var_openai_base_url(self, mock_post):
"""
Test that litellm.create_container uses OPENAI_BASE_URL env var.
"""
os.environ["OPENAI_BASE_URL"] = "https://us.api.openai.com/v1"
mock_response = MagicMock(spec=httpx.Response)
mock_response.status_code = 200
mock_response.json.return_value = {
"id": "cntr_123456",
"object": "container",
"created_at": 1747857508,
"status": "running",
"expires_after": {"anchor": "last_active_at", "minutes": 20},
"last_active_at": 1747857508,
"name": "Test Container",
}
mock_post.return_value = mock_response
litellm.create_container(
name="Test Container",
custom_llm_provider="openai",
)
mock_post.assert_called_once()
call_args = mock_post.call_args
called_url = call_args[1]["url"]
assert (
"us.api.openai.com" in called_url
), f"Expected US regional URL, got: {called_url}"
@patch("litellm.llms.custom_httpx.http_handler.HTTPHandler.post")
def test_create_container_defaults_to_standard_openai(self, mock_post):
"""
Test that litellm.create_container defaults to standard OpenAI URL
when no regional api_base is configured.
"""
mock_response = MagicMock(spec=httpx.Response)
mock_response.status_code = 200
mock_response.json.return_value = {
"id": "cntr_123456",
"object": "container",
"created_at": 1747857508,
"status": "running",
"expires_after": {"anchor": "last_active_at", "minutes": 20},
"last_active_at": 1747857508,
"name": "Test Container",
}
mock_post.return_value = mock_response
litellm.create_container(
name="Test Container",
custom_llm_provider="openai",
)
mock_post.assert_called_once()
call_args = mock_post.call_args
called_url = call_args[1]["url"]
assert called_url == "https://api.openai.com/v1/containers"
@patch("litellm.llms.custom_httpx.http_handler.HTTPHandler.post")
def test_upload_container_file_uses_regional_api_base(self, mock_post):
"""
Test that litellm.upload_container_file uses the regional api_base when provided.
"""
mock_response = MagicMock(spec=httpx.Response)
mock_response.status_code = 200
mock_response.json.return_value = {
"id": "file_123456",
"object": "container.file",
"created_at": 1747857508,
"container_id": "cntr_123456",
"path": "/mnt/user/data.csv",
"source": "user",
}
mock_post.return_value = mock_response
litellm.upload_container_file(
container_id="cntr_123456",
file=("data.csv", b"col1,col2\n1,2", "text/csv"),
custom_llm_provider="openai",
api_base="https://us.api.openai.com/v1",
)
mock_post.assert_called_once()
call_args = mock_post.call_args
called_url = call_args[1]["url"]
assert (
"us.api.openai.com" in called_url
), f"Expected US regional URL, got: {called_url}"
assert "cntr_123456/files" in called_url