litellm/tests/llm_translation/test_containers_api.py
yuneng-jiang 6a0d03914c
test: drop the cwd-relative sys.path.insert calls from the test suite (#37802)
* test: drop the cwd-relative sys.path.insert calls from the test suite

TQ003 stands at 1,077 across 1,058 files, and 1,015 of them are the same shape:
sys.path.insert(0, os.path.abspath("../..")) and its deeper siblings. The
argument resolves against the working directory rather than the file, so from
the repo root, where every job runs pytest, it inserts the directory two levels
above the checkout. It has never pointed at litellm. The package is installed
into the environment anyway, which is what actually makes the import work, and
what the rule's message has said all along.

Removing them leaves 1,634 imports of sys and os with no remaining reference,
and those go too, except where another test module imports the name back out of
the file. The rest of TQ003 is 62 call sites that resolve against __file__ or a
variable, which are a different question and are left alone.

Collection is identical either way: 45,871 tests and the same 51 pre-existing
collection errors before and after, and ruff reports no new undefined name.

* test: drop the duplicate imports the sys.path sweep exposed to F811

* test(pre-call-utils): restore the os import the new bedrock tests need
2026-08-22 09:25:58 -07:00

110 lines
3.4 KiB
Python

"""
E2E Test for Container Files API.
Tests the container files endpoints using LiteLLM SDK methods.
"""
import os
import time
import pytest
from litellm.containers import (
create_container,
delete_container,
)
from litellm.containers.endpoint_factory import (
list_container_files,
retrieve_container_file,
retrieve_container_file_content,
delete_container_file,
)
@pytest.mark.skipif(not os.getenv("OPENAI_API_KEY"), reason="OPENAI_API_KEY not set")
def test_container_files_api():
"""
Test container files API: list, retrieve, delete.
Flow:
1. Create a container
2. List files (should be empty)
3. Try retrieve file (should error - no files)
4. Try delete file (should error - no files)
5. Cleanup: delete container
"""
api_key = os.getenv("OPENAI_API_KEY")
# 1. Create container
print("\n1. Creating container...")
container = create_container(
name=f"test-files-api-{int(time.time())}",
custom_llm_provider="openai",
api_key=api_key,
expires_after={"anchor": "last_active_at", "minutes": 5},
)
print(f" Created: {container.id}")
try:
# 2. List files
print("2. Listing container files...")
files = list_container_files(
container_id=container.id,
custom_llm_provider="openai",
api_key=api_key,
)
assert files.object == "list"
assert isinstance(files.data, list)
assert len(files.data) == 0 # New container has no files
print(f" Files found: {len(files.data)}")
# 3. Try retrieve non-existent file metadata (should raise error)
print("3. Testing retrieve_container_file (expect error)...")
with pytest.raises(Exception, match=r"(?i)not found|invalid"):
retrieve_container_file(
container_id=container.id,
file_id="cfile_nonexistent",
custom_llm_provider="openai",
api_key=api_key,
)
# 3b. Try retrieve non-existent file content (should raise error)
print("3b. Testing retrieve_container_file_content (expect error)...")
try:
retrieve_container_file_content(
container_id=container.id,
file_id="cfile_nonexistent",
custom_llm_provider="openai",
api_key=api_key,
)
pytest.fail("Should have raised error for non-existent file content")
except Exception as e:
print(f" Got expected error ✓")
# 4. Try delete non-existent file (should raise error)
print("4. Testing delete_container_file (expect error)...")
try:
delete_container_file(
container_id=container.id,
file_id="cfile_nonexistent",
custom_llm_provider="openai",
api_key=api_key,
)
pytest.fail("Should have raised error for non-existent file")
except Exception as e:
# Delete returns 400 for non-existent files
print(f" Got expected error ✓")
finally:
# 5. Cleanup
print("5. Deleting container...")
result = delete_container(
container_id=container.id,
custom_llm_provider="openai",
api_key=api_key,
)
assert result.deleted is True
print(f" Deleted ✓")
print("\nAll container files API tests passed! ✓")