Skip to content

Commit ca882cd

Browse files
committed
images: add get_images method
1 parent 6ded1c5 commit ca882cd

2 files changed

Lines changed: 92 additions & 1 deletion

File tree

nanokvm/client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
GetHardwareRsp,
2929
GetHdmiStateRsp,
3030
GetHidModeRsp,
31+
GetImagesRsp,
3132
GetInfoRsp,
3233
GetMdnsStateRsp,
3334
GetMemoryLimitRsp,
@@ -397,6 +398,14 @@ async def get_tailscale_status(self) -> GetTailscaleStatusRsp:
397398
response_model=GetTailscaleStatusRsp,
398399
)
399400

401+
async def get_images(self) -> GetImagesRsp:
402+
"""Get the list of available image files."""
403+
return await self._api_request_json(
404+
hdrs.METH_GET,
405+
"/storage/image",
406+
response_model=GetImagesRsp,
407+
)
408+
400409
async def get_mounted_image(self) -> GetMountedImageRsp:
401410
"""Get the currently mounted image file."""
402411
return await self._api_request_json(

tests/test_client.py

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,92 @@
11
from aiohttp import ClientSession
2+
from aioresponses import aioresponses
23

3-
from nanokvm.client import NanoKVMClient
4+
from nanokvm.client import NanoKVMApiError, NanoKVMClient
5+
from nanokvm.models import ApiResponseCode
46

57

68
async def test_client() -> None:
79
"""Test the NanoKVMClient."""
810
async with ClientSession() as session:
911
client = NanoKVMClient("http://localhost:8888/api/", session)
1012
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

Comments
 (0)