|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import asyncio |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +import orjson |
| 10 | +import pytest |
| 11 | + |
| 12 | +from aiperf.common.models.model_autodetect import ( |
| 13 | + autodetect_model_names_from_v1_models, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class _FakeRecord: |
| 18 | + def __init__(self, *, status: int, body_text: str) -> None: |
| 19 | + self.status = status |
| 20 | + resp = type("_Resp", (), {"text": body_text})() |
| 21 | + self.responses = [resp] |
| 22 | + |
| 23 | + |
| 24 | +class _FakeClient: |
| 25 | + def __init__(self, *, status: int, body_text: str) -> None: |
| 26 | + self._status = status |
| 27 | + self._body_text = body_text |
| 28 | + self.urls: list[str] = [] |
| 29 | + self.headers: list[dict[str, str]] = [] |
| 30 | + self.closed = False |
| 31 | + |
| 32 | + async def get_request( |
| 33 | + self, url: str, headers: dict[str, str], **_: Any |
| 34 | + ) -> _FakeRecord: |
| 35 | + self.urls.append(url) |
| 36 | + self.headers.append(headers) |
| 37 | + return _FakeRecord(status=self._status, body_text=self._body_text) |
| 38 | + |
| 39 | + async def close(self) -> None: |
| 40 | + self.closed = True |
| 41 | + |
| 42 | + |
| 43 | +def _install_fake_aiohttp( |
| 44 | + monkeypatch: pytest.MonkeyPatch, *, status: int, body_text: str |
| 45 | +) -> _FakeClient: |
| 46 | + fake = _FakeClient(status=status, body_text=body_text) |
| 47 | + |
| 48 | + def _factory(*_: Any, **__: Any) -> _FakeClient: |
| 49 | + return fake |
| 50 | + |
| 51 | + monkeypatch.setattr( |
| 52 | + "aiperf.common.models.model_autodetect.AioHttpClient", |
| 53 | + _factory, |
| 54 | + ) |
| 55 | + return fake |
| 56 | + |
| 57 | + |
| 58 | +def test_autodetect_picks_first_id_from_data( |
| 59 | + monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture |
| 60 | +) -> None: |
| 61 | + import logging |
| 62 | + |
| 63 | + caplog.set_level(logging.WARNING, logger="aiperf.common.models.model_autodetect") |
| 64 | + body_text = orjson.dumps( |
| 65 | + {"data": [{"id": "model-a"}, {"id": "model-b"}]} |
| 66 | + ).decode("utf-8") |
| 67 | + fake = _install_fake_aiohttp( |
| 68 | + monkeypatch, status=200, body_text=body_text |
| 69 | + ) |
| 70 | + |
| 71 | + result = asyncio.run( |
| 72 | + autodetect_model_names_from_v1_models( |
| 73 | + urls=["http://localhost:8000"], |
| 74 | + headers={"Authorization": "Bearer token"}, |
| 75 | + timeout_s=1.0, |
| 76 | + ) |
| 77 | + ) |
| 78 | + |
| 79 | + assert result == ["model-a"] |
| 80 | + assert fake.urls == ["http://localhost:8000/v1/models"] |
| 81 | + assert fake.headers[0]["Authorization"] == "Bearer token" |
| 82 | + assert fake.closed is True |
| 83 | + assert "2 models returned" in caplog.text |
| 84 | + assert "pass --model" in caplog.text |
| 85 | + assert "first listed model 'model-a'" in caplog.text |
| 86 | + |
| 87 | + |
| 88 | +def test_autodetect_single_model_logs_info_not_warning( |
| 89 | + monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture |
| 90 | +) -> None: |
| 91 | + import logging |
| 92 | + |
| 93 | + caplog.set_level(logging.INFO, logger="aiperf.common.models.model_autodetect") |
| 94 | + body_text = orjson.dumps({"data": [{"id": "only-one"}]}).decode("utf-8") |
| 95 | + _install_fake_aiohttp(monkeypatch, status=200, body_text=body_text) |
| 96 | + |
| 97 | + asyncio.run( |
| 98 | + autodetect_model_names_from_v1_models( |
| 99 | + urls=["http://localhost:8000"], |
| 100 | + headers={}, |
| 101 | + timeout_s=1.0, |
| 102 | + ) |
| 103 | + ) |
| 104 | + |
| 105 | + assert "Auto-detected model 'only-one'" in caplog.text |
| 106 | + assert "pass --model" not in caplog.text |
| 107 | + |
| 108 | + |
| 109 | +def test_autodetect_raises_on_non_200(monkeypatch: pytest.MonkeyPatch) -> None: |
| 110 | + fake_body_text = "oops" |
| 111 | + _install_fake_aiohttp( |
| 112 | + monkeypatch, status=404, body_text=fake_body_text |
| 113 | + ) |
| 114 | + |
| 115 | + with pytest.raises(ValueError, match="Failed to auto-detect models"): |
| 116 | + asyncio.run( |
| 117 | + autodetect_model_names_from_v1_models( |
| 118 | + urls=["http://localhost:8000"], |
| 119 | + headers={}, |
| 120 | + timeout_s=1.0, |
| 121 | + ) |
| 122 | + ) |
0 commit comments