|
| 1 | +"""Unit tests for utility function to check Llama Stack version.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from semver import Version |
| 5 | + |
| 6 | +from llama_stack_client.types import VersionInfo |
| 7 | + |
| 8 | +from utils.llama_stack_version import ( |
| 9 | + check_llama_stack_version, |
| 10 | + InvalidLlamaStackVersionException, |
| 11 | +) |
| 12 | + |
| 13 | +from constants import ( |
| 14 | + MINIMAL_SUPPORTED_LLAMA_STACK_VERSION, |
| 15 | + MAXIMAL_SUPPORTED_LLAMA_STACK_VERSION, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.asyncio |
| 20 | +async def test_check_llama_stack_version_minimal_supported_version(mocker): |
| 21 | + """Test the check_llama_stack_version function.""" |
| 22 | + |
| 23 | + # mock the Llama Stack client |
| 24 | + mock_client = mocker.AsyncMock() |
| 25 | + mock_client.inspect.version.return_value = VersionInfo( |
| 26 | + version=MINIMAL_SUPPORTED_LLAMA_STACK_VERSION |
| 27 | + ) |
| 28 | + |
| 29 | + # test if the version is checked |
| 30 | + await check_llama_stack_version(mock_client) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.asyncio |
| 34 | +async def test_check_llama_stack_version_maximal_supported_version(mocker): |
| 35 | + """Test the check_llama_stack_version function.""" |
| 36 | + |
| 37 | + # mock the Llama Stack client |
| 38 | + mock_client = mocker.AsyncMock() |
| 39 | + mock_client.inspect.version.return_value = VersionInfo( |
| 40 | + version=MAXIMAL_SUPPORTED_LLAMA_STACK_VERSION |
| 41 | + ) |
| 42 | + |
| 43 | + # test if the version is checked |
| 44 | + await check_llama_stack_version(mock_client) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.asyncio |
| 48 | +async def test_check_llama_stack_version_too_small_version(mocker): |
| 49 | + """Test the check_llama_stack_version function.""" |
| 50 | + |
| 51 | + # mock the Llama Stack client |
| 52 | + mock_client = mocker.AsyncMock() |
| 53 | + |
| 54 | + # that is surely out of range |
| 55 | + mock_client.inspect.version.return_value = VersionInfo(version="0.0.0") |
| 56 | + |
| 57 | + expected_exception_msg = ( |
| 58 | + f"Llama Stack version >= {MINIMAL_SUPPORTED_LLAMA_STACK_VERSION} " |
| 59 | + + "is required, but 0.0.0 is used" |
| 60 | + ) |
| 61 | + # test if the version is checked |
| 62 | + with pytest.raises(InvalidLlamaStackVersionException, match=expected_exception_msg): |
| 63 | + await check_llama_stack_version(mock_client) |
| 64 | + |
| 65 | + |
| 66 | +async def _check_version_must_fail(mock_client, bigger_version): |
| 67 | + mock_client.inspect.version.return_value = VersionInfo(version=str(bigger_version)) |
| 68 | + |
| 69 | + expected_exception_msg = ( |
| 70 | + f"Llama Stack version <= {MAXIMAL_SUPPORTED_LLAMA_STACK_VERSION} is required, " |
| 71 | + + f"but {bigger_version} is used" |
| 72 | + ) |
| 73 | + # test if the version is checked |
| 74 | + with pytest.raises(InvalidLlamaStackVersionException, match=expected_exception_msg): |
| 75 | + await check_llama_stack_version(mock_client) |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.asyncio |
| 79 | +async def test_check_llama_stack_version_too_big_version(mocker, subtests): |
| 80 | + """Test the check_llama_stack_version function.""" |
| 81 | + |
| 82 | + # mock the Llama Stack client |
| 83 | + mock_client = mocker.AsyncMock() |
| 84 | + |
| 85 | + max_version = Version.parse(MAXIMAL_SUPPORTED_LLAMA_STACK_VERSION) |
| 86 | + |
| 87 | + with subtests.test(msg="Increased patch number"): |
| 88 | + bigger_version = max_version.bump_patch() |
| 89 | + await _check_version_must_fail(mock_client, bigger_version) |
| 90 | + |
| 91 | + with subtests.test(msg="Increased minor number"): |
| 92 | + bigger_version = max_version.bump_minor() |
| 93 | + await _check_version_must_fail(mock_client, bigger_version) |
| 94 | + |
| 95 | + with subtests.test(msg="Increased major number"): |
| 96 | + bigger_version = max_version.bump_major() |
| 97 | + await _check_version_must_fail(mock_client, bigger_version) |
| 98 | + |
| 99 | + with subtests.test(msg="Increased all numbers"): |
| 100 | + bigger_version = max_version.bump_major().bump_minor().bump_patch() |
| 101 | + await _check_version_must_fail(mock_client, bigger_version) |
0 commit comments