Skip to content

Commit 8fe4fb5

Browse files
fix precommit unit test failures
Signed-off-by: Brandon Pelfrey <bpelfrey@nvidia.com>
1 parent 9534605 commit 8fe4fb5

File tree

6 files changed

+81
-30
lines changed

6 files changed

+81
-30
lines changed

tests/unit/common/config/test_user_config.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ def make_endpoint(
3939
**kwargs,
4040
) -> EndpointConfig:
4141
"""Create an EndpointConfig with sensible defaults for testing."""
42+
# Convert url= to urls= for backward compatibility
43+
if "url" in kwargs:
44+
kwargs["urls"] = [kwargs.pop("url")]
4245
return EndpointConfig(
4346
model_names=model_names or ["test-model"],
4447
type=endpoint_type,
@@ -97,7 +100,7 @@ def test_to_json_string(self):
97100
type=EndpointType.CHAT,
98101
custom_endpoint="custom_endpoint",
99102
streaming=True,
100-
url="http://custom-url",
103+
urls=["http://custom-url"],
101104
api_key="test_api_key",
102105
timeout_seconds=10,
103106
),

tests/unit/server_metrics/test_server_metrics_manager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def user_config_with_endpoint() -> UserConfig:
2121
endpoint=EndpointConfig(
2222
model_names=["test-model"],
2323
type=EndpointType.CHAT,
24-
url="http://localhost:8000/v1/chat",
24+
urls=["http://localhost:8000/v1/chat"],
2525
),
2626
)
2727

@@ -33,7 +33,7 @@ def user_config_with_server_metrics_urls() -> UserConfig:
3333
endpoint=EndpointConfig(
3434
model_names=["test-model"],
3535
type=EndpointType.CHAT,
36-
url="http://localhost:8000/v1/chat",
36+
urls=["http://localhost:8000/v1/chat"],
3737
),
3838
server_metrics=[
3939
"http://custom-endpoint:9400/metrics",
@@ -423,7 +423,7 @@ async def test_configure_when_server_metrics_disabled(
423423
endpoint=EndpointConfig(
424424
model_names=["test-model"],
425425
type=EndpointType.CHAT,
426-
url="http://localhost:8000/v1/chat",
426+
urls=["http://localhost:8000/v1/chat"],
427427
),
428428
no_server_metrics=True, # Disable server metrics
429429
)

tests/unit/timing/test_timing_manager.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424

2525
@pytest.fixture
2626
def user_config() -> UserConfig:
27+
mock_endpoint = MagicMock()
28+
mock_endpoint.urls = ["http://localhost:8000"]
29+
mock_endpoint.url_selection_strategy = "round_robin"
2730
return UserConfig.model_construct(
28-
endpoint=MagicMock(), _timing_mode=TimingMode.REQUEST_RATE
31+
endpoint=mock_endpoint, _timing_mode=TimingMode.REQUEST_RATE
2932
)
3033

3134

@@ -62,6 +65,14 @@ def mock_metadata() -> DatasetMetadata:
6265
)
6366

6467

68+
def _create_mock_endpoint() -> MagicMock:
69+
"""Create a mock endpoint with required URL attributes."""
70+
mock_endpoint = MagicMock()
71+
mock_endpoint.urls = ["http://localhost:8000"]
72+
mock_endpoint.url_selection_strategy = "round_robin"
73+
return mock_endpoint
74+
75+
6576
class TestTimingManagerDatasetConfiguration:
6677
@pytest.mark.parametrize(
6778
"timing_mode", [TimingMode.FIXED_SCHEDULE, TimingMode.REQUEST_RATE]
@@ -70,7 +81,9 @@ class TestTimingManagerDatasetConfiguration:
7081
async def test_profile_configure_waits_for_dataset_notification(
7182
self, create_manager, mock_metadata, timing_mode
7283
) -> None:
73-
cfg = UserConfig.model_construct(endpoint=MagicMock(), _timing_mode=timing_mode)
84+
cfg = UserConfig.model_construct(
85+
endpoint=_create_mock_endpoint(), _timing_mode=timing_mode
86+
)
7487
mgr = create_manager(cfg)
7588
mock_engine = MagicMock()
7689
mock_engine.initialize = lambda *a, **kw: asyncio.sleep(0)
@@ -105,7 +118,7 @@ async def test_profile_configure_waits_for_dataset_notification(
105118
@pytest.mark.asyncio
106119
async def test_dataset_configuration_timeout(self, create_manager) -> None:
107120
cfg = UserConfig.model_construct(
108-
endpoint=MagicMock(), _timing_mode=TimingMode.FIXED_SCHEDULE
121+
endpoint=_create_mock_endpoint(), _timing_mode=TimingMode.FIXED_SCHEDULE
109122
)
110123
mgr = create_manager(cfg)
111124
with (
@@ -123,7 +136,7 @@ async def test_dataset_notification_before_configure(
123136
self, create_manager, mock_metadata
124137
) -> None:
125138
cfg = UserConfig.model_construct(
126-
endpoint=MagicMock(), _timing_mode=TimingMode.FIXED_SCHEDULE
139+
endpoint=_create_mock_endpoint(), _timing_mode=TimingMode.FIXED_SCHEDULE
127140
)
128141
mgr = create_manager(cfg)
129142
await mgr._on_dataset_configured_notification(

tests/unit/transports/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def create_model_endpoint_info(
5454
),
5555
endpoint=EndpointInfo(
5656
type=EndpointType.CHAT,
57-
base_url=base_url,
57+
base_urls=[base_url],
5858
custom_endpoint=custom_endpoint,
5959
streaming=streaming,
6060
api_key=api_key,
@@ -114,7 +114,7 @@ def user_config() -> UserConfig:
114114
return UserConfig(
115115
endpoint=EndpointConfig(
116116
type=EndpointType.CHAT,
117-
url="http://localhost:8000",
117+
urls=["http://localhost:8000"],
118118
timeout_seconds=600,
119119
model_names=["gpt-4"],
120120
api_key="test-api-key",

tests/unit/transports/test_aiohttp_transport.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,14 @@ def test_get_transport_headers(self, transport, streaming, expected_accept):
153153
],
154154
ids=["http-prefix", "no-scheme", "https-prefix"],
155155
)
156-
def test_get_url(
157-
self, model_endpoint_non_streaming, base_url, custom_endpoint, expected_url
158-
):
156+
def test_get_url(self, base_url, custom_endpoint, expected_url):
159157
"""Test get_url with various base URLs and endpoints."""
160-
model_endpoint_non_streaming.endpoint.base_url = base_url
161-
model_endpoint_non_streaming.endpoint.custom_endpoint = custom_endpoint
158+
model_endpoint = create_model_endpoint_info(
159+
base_url=base_url, custom_endpoint=custom_endpoint
160+
)
162161

163-
transport = AioHttpTransport(model_endpoint=model_endpoint_non_streaming)
164-
request_info = create_request_info(model_endpoint_non_streaming)
162+
transport = AioHttpTransport(model_endpoint=model_endpoint)
163+
request_info = create_request_info(model_endpoint)
165164
url = transport.get_url(request_info)
166165
assert url == expected_url
167166

@@ -458,7 +457,7 @@ async def test_full_request_flow_non_streaming(self):
458457
"headers": transport.aiohttp_client.post_request.call_args[0][2],
459458
}
460459

461-
assert "https://api.example.com/v1/chat/completions" in args["url"]
460+
assert args["url"].startswith("https://api.example.com/v1/chat/completions")
462461
assert "api-version=2024-10-01" in args["url"]
463462
assert "Hello" in args["json_str"]
464463
assert args["headers"]["Authorization"] == "Bearer test-key"

tests/unit/transports/test_base_transport.py

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
AIPERF_USER_AGENT = f"aiperf/{importlib_metadata.version('aiperf')}"
2525

2626

27+
def _ensure_scheme(url: str) -> str:
28+
"""Ensure URL has a scheme prefix."""
29+
if not url.startswith(("http://", "https://")):
30+
return f"http://{url}"
31+
return url
32+
33+
2734
class FakeTransport(BaseTransport):
2835
"""Concrete implementation of BaseTransport for testing."""
2936

@@ -36,7 +43,9 @@ def metadata(cls) -> TransportMetadata:
3643

3744
def get_url(self, request_info: RequestInfo) -> str:
3845
endpoint_info = request_info.model_endpoint.endpoint
39-
base_url = endpoint_info.base_url or ""
46+
base_url = (
47+
_ensure_scheme(endpoint_info.base_url) if endpoint_info.base_url else ""
48+
)
4049
if endpoint_info.custom_endpoint:
4150
return f"{base_url}{endpoint_info.custom_endpoint}"
4251
return base_url
@@ -60,7 +69,7 @@ def model_endpoint(self):
6069
),
6170
endpoint=EndpointInfo(
6271
type=EndpointType.CHAT,
63-
base_url="http://localhost:8000",
72+
base_urls=["http://localhost:8000"],
6473
custom_endpoint="/v1/chat/completions",
6574
),
6675
)
@@ -200,11 +209,20 @@ def test_build_url_with_endpoint_params(self, transport, request_info):
200209
assert "timeout=30" in url
201210
assert url.startswith("http://localhost:8000/v1/chat/completions?")
202211

203-
def test_build_url_preserves_existing_params(self, transport, model_endpoint):
212+
def test_build_url_preserves_existing_params(self, transport):
204213
"""Test that existing URL params are preserved."""
205-
model_endpoint.endpoint.base_url = (
206-
"http://localhost:8000/v1/chat/completions?existing=param"
214+
model_endpoint = ModelEndpointInfo(
215+
models=ModelListInfo(
216+
models=[ModelInfo(name="test-model")],
217+
model_selection_strategy=ModelSelectionStrategy.ROUND_ROBIN,
218+
),
219+
endpoint=EndpointInfo(
220+
type=EndpointType.CHAT,
221+
base_urls=["http://localhost:8000/v1/chat/completions?existing=param"],
222+
custom_endpoint=None,
223+
),
207224
)
225+
transport = FakeTransport(model_endpoint=model_endpoint)
208226
request_info = RequestInfo(
209227
model_endpoint=model_endpoint,
210228
turns=[],
@@ -222,13 +240,20 @@ def test_build_url_preserves_existing_params(self, transport, model_endpoint):
222240
assert "existing=param" in url
223241
assert "new=value" in url
224242

225-
def test_build_url_endpoint_params_override_existing(
226-
self, transport, model_endpoint
227-
):
243+
def test_build_url_endpoint_params_override_existing(self, transport):
228244
"""Test that endpoint params override existing URL params."""
229-
model_endpoint.endpoint.base_url = (
230-
"http://localhost:8000/v1/chat/completions?key=original"
245+
model_endpoint = ModelEndpointInfo(
246+
models=ModelListInfo(
247+
models=[ModelInfo(name="test-model")],
248+
model_selection_strategy=ModelSelectionStrategy.ROUND_ROBIN,
249+
),
250+
endpoint=EndpointInfo(
251+
type=EndpointType.CHAT,
252+
base_urls=["http://localhost:8000/v1/chat/completions?key=original"],
253+
custom_endpoint=None,
254+
),
231255
)
256+
transport = FakeTransport(model_endpoint=model_endpoint)
232257
request_info = RequestInfo(
233258
model_endpoint=model_endpoint,
234259
turns=[],
@@ -267,9 +292,20 @@ def test_build_url_no_params_preserves_clean_url(self, transport, request_info):
267292
assert "?" not in url
268293
assert url == "http://localhost:8000/v1/chat/completions"
269294

270-
def test_build_url_complex_query_string(self, transport, model_endpoint):
295+
def test_build_url_complex_query_string(self, transport):
271296
"""Test complex query string handling."""
272-
model_endpoint.endpoint.base_url = "http://localhost:8000/api?a=1&b=2&c=3"
297+
model_endpoint = ModelEndpointInfo(
298+
models=ModelListInfo(
299+
models=[ModelInfo(name="test-model")],
300+
model_selection_strategy=ModelSelectionStrategy.ROUND_ROBIN,
301+
),
302+
endpoint=EndpointInfo(
303+
type=EndpointType.CHAT,
304+
base_urls=["http://localhost:8000/api?a=1&b=2&c=3"],
305+
custom_endpoint=None,
306+
),
307+
)
308+
transport = FakeTransport(model_endpoint=model_endpoint)
273309
request_info = RequestInfo(
274310
model_endpoint=model_endpoint,
275311
turns=[],

0 commit comments

Comments
 (0)