litellm/tests/test_litellm/integrations/test_agentops.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

113 lines
3.9 KiB
Python

import os
from unittest.mock import MagicMock, patch
import pytest
from litellm.integrations.agentops.agentops import AgentOps, AgentOpsConfig
@pytest.fixture
def mock_auth_response():
return {"token": "test_jwt_token", "project_id": "test_project_id"}
@pytest.fixture
def agentops_config():
return AgentOpsConfig(
endpoint="https://otlp.agentops.cloud/v1/traces",
api_key="test_api_key",
service_name="test_service",
deployment_environment="test_env",
auth_endpoint="https://api.agentops.ai/v3/auth/token",
)
def test_agentops_config_from_env():
"""Test that AgentOpsConfig correctly reads from environment variables"""
with patch.dict(
os.environ,
{
"AGENTOPS_API_KEY": "test_key",
"AGENTOPS_SERVICE_NAME": "test_service",
"AGENTOPS_ENVIRONMENT": "test_env",
},
):
config = AgentOpsConfig.from_env()
assert config.api_key == "test_key"
assert config.service_name == "test_service"
assert config.deployment_environment == "test_env"
assert config.endpoint == "https://otlp.agentops.cloud/v1/traces"
assert config.auth_endpoint == "https://api.agentops.ai/v3/auth/token"
def test_agentops_config_defaults():
"""Test that AgentOpsConfig uses correct default values"""
config = AgentOpsConfig()
assert config.service_name is None
assert config.deployment_environment is None
assert config.api_key is None
assert config.endpoint == "https://otlp.agentops.cloud/v1/traces"
assert config.auth_endpoint == "https://api.agentops.ai/v3/auth/token"
@patch("litellm.integrations.agentops.agentops.AgentOps._fetch_auth_token")
def test_fetch_auth_token_success(mock_fetch_auth_token, mock_auth_response):
"""Test successful JWT token fetch"""
mock_fetch_auth_token.return_value = mock_auth_response
config = AgentOpsConfig(api_key="test_key")
agentops = AgentOps(config=config)
mock_fetch_auth_token.assert_called_once_with(
"test_key", "https://api.agentops.ai/v3/auth/token"
)
assert agentops.resource_attributes.get("project.id") == mock_auth_response.get(
"project_id"
)
@patch("litellm.integrations.agentops.agentops.AgentOps._fetch_auth_token")
def test_fetch_auth_token_failure(mock_fetch_auth_token):
"""Test failed JWT token fetch"""
mock_fetch_auth_token.side_effect = Exception(
"Failed to fetch auth token: Unauthorized"
)
config = AgentOpsConfig(api_key="test_key")
agentops = AgentOps(config=config)
mock_fetch_auth_token.assert_called_once()
assert "project.id" not in agentops.resource_attributes
@patch("litellm.integrations.agentops.agentops.AgentOps._fetch_auth_token")
def test_agentops_initialization(
mock_fetch_auth_token, agentops_config, mock_auth_response
):
"""Test AgentOps initialization with config"""
mock_fetch_auth_token.return_value = mock_auth_response
agentops = AgentOps(config=agentops_config)
assert agentops.resource_attributes["service.name"] == "test_service"
assert agentops.resource_attributes["deployment.environment"] == "test_env"
assert agentops.resource_attributes["telemetry.sdk.name"] == "agentops"
assert agentops.resource_attributes["project.id"] == "test_project_id"
def test_agentops_initialization_no_auth():
"""Test AgentOps initialization without authentication"""
test_config = AgentOpsConfig(
endpoint="https://otlp.agentops.cloud/v1/traces",
api_key=None, # No API key
service_name="test_service",
deployment_environment="test_env",
)
agentops = AgentOps(config=test_config)
assert agentops.resource_attributes["service.name"] == "test_service"
assert agentops.resource_attributes["deployment.environment"] == "test_env"
assert agentops.resource_attributes["telemetry.sdk.name"] == "agentops"
assert "project.id" not in agentops.resource_attributes