|
1 | 1 | from aiohttp import ClientSession |
| 2 | +from aioresponses import aioresponses |
2 | 3 |
|
3 | | -from nanokvm.client import NanoKVMClient |
| 4 | +from nanokvm.client import NanoKVMApiError, NanoKVMClient |
| 5 | +from nanokvm.models import ApiResponseCode |
4 | 6 |
|
5 | 7 |
|
6 | 8 | async def test_client() -> None: |
7 | 9 | """Test the NanoKVMClient.""" |
8 | 10 | async with ClientSession() as session: |
9 | 11 | client = NanoKVMClient("http://localhost:8888/api/", session) |
10 | 12 | assert client is not None |
| 13 | + |
| 14 | + |
| 15 | +async def test_get_images_success() -> None: |
| 16 | + """Test get_images with a successful response.""" |
| 17 | + async with ClientSession() as session: |
| 18 | + client = NanoKVMClient( |
| 19 | + "http://localhost:8888/api/", session, token="test-token" |
| 20 | + ) |
| 21 | + |
| 22 | + with aioresponses() as m: |
| 23 | + m.get( |
| 24 | + "http://localhost:8888/api/storage/image", |
| 25 | + payload={ |
| 26 | + "code": 0, |
| 27 | + "msg": "success", |
| 28 | + "data": { |
| 29 | + "files": [ |
| 30 | + "/data/alpine-standard-3.23.2-x86_64.iso", |
| 31 | + "/data/cs10-js.iso" |
| 32 | + ] |
| 33 | + } |
| 34 | + } |
| 35 | + ) |
| 36 | + |
| 37 | + response = await client.get_images() |
| 38 | + |
| 39 | + assert response is not None |
| 40 | + assert len(response.files) == 2 |
| 41 | + assert "/data/alpine-standard-3.23.2-x86_64.iso" in response.files |
| 42 | + assert "/data/cs10-js.iso" in response.files |
| 43 | + |
| 44 | + |
| 45 | +async def test_get_images_empty() -> None: |
| 46 | + """Test get_images with an empty list.""" |
| 47 | + async with ClientSession() as session: |
| 48 | + client = NanoKVMClient( |
| 49 | + "http://localhost:8888/api/", session, token="test-token" |
| 50 | + ) |
| 51 | + |
| 52 | + with aioresponses() as m: |
| 53 | + m.get( |
| 54 | + "http://localhost:8888/api/storage/image", |
| 55 | + payload={ |
| 56 | + "code": 0, |
| 57 | + "msg": "success", |
| 58 | + "data": { |
| 59 | + "files": [] |
| 60 | + } |
| 61 | + } |
| 62 | + ) |
| 63 | + |
| 64 | + response = await client.get_images() |
| 65 | + |
| 66 | + assert response is not None |
| 67 | + assert len(response.files) == 0 |
| 68 | + |
| 69 | + |
| 70 | +async def test_get_images_api_error() -> None: |
| 71 | + """Test get_images with an API error response.""" |
| 72 | + async with ClientSession() as session: |
| 73 | + client = NanoKVMClient( |
| 74 | + "http://localhost:8888/api/", session, token="test-token" |
| 75 | + ) |
| 76 | + |
| 77 | + with aioresponses() as m: |
| 78 | + m.get( |
| 79 | + "http://localhost:8888/api/storage/image", |
| 80 | + payload={ |
| 81 | + "code": -1, |
| 82 | + "msg": "failed to list images", |
| 83 | + "data": None |
| 84 | + } |
| 85 | + ) |
| 86 | + |
| 87 | + try: |
| 88 | + await client.get_images() |
| 89 | + raise AssertionError("Expected NanoKVMApiError to be raised") |
| 90 | + except NanoKVMApiError as e: |
| 91 | + assert e.code == ApiResponseCode.FAILURE |
| 92 | + assert "failed to list images" in e.msg |
0 commit comments