Skip to content

Commit

Permalink
Fix: Fixed existing tests and add new test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
nesitor committed Dec 3, 2024
1 parent 2cf7e70 commit fb379ff
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/aleph/vm/orchestrator/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ async def about_system_usage(request: web.Request):
duration_seconds=60,
),
properties=machine_properties,
gpu=get_machine_gpus(request)
gpu=get_machine_gpus(request),
)

return web.json_response(text=usage.json(exclude_none=True))
Expand Down
40 changes: 40 additions & 0 deletions tests/supervisor/test_resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from unittest import mock

from aleph.vm.resources import get_gpu_devices


def mock_is_kernel_enabled_gpu(pci_host: str) -> bool:
value = True if pci_host == "01:00.0" else False
return value


def test_get_gpu_devices():

class DevicesReturn:
stdout: str = (
'00:1f.0 "ISA bridge [0601]" "Intel Corporation [8086]" "Device [7a06]" -r11 -p00 "ASUSTeK Computer Inc. [1043]" "Device [8882]"'
'\n00:1f.4 "SMBus [0c05]" "Intel Corporation [8086]" "Raptor Lake-S PCH SMBus Controller [7a23]" -r11 -p00 "ASUSTeK Computer Inc. [1043]" "Device [8882]"'
'\n00:1f.5 "Serial bus controller [0c80]" "Intel Corporation [8086]" "Raptor Lake SPI (flash) Controller [7a24]" -r11 -p00 "ASUSTeK Computer Inc. [1043]" "Device [8882]"'
'\n01:00.0 "VGA compatible controller [0300]" "NVIDIA Corporation [10de]" "AD104GL [RTX 4000 SFF Ada Generation] [27b0]" -ra1 -p00 "NVIDIA Corporation [10de]" "AD104GL [RTX 4000 SFF Ada Generation] [16fa]"'
'\n01:00.1 "Audio device [0403]" "NVIDIA Corporation [10de]" "Device [22bc]" -ra1 -p00 "NVIDIA Corporation [10de]" "Device [16fa]"'
'\n02:00.0 "Non-Volatile memory controller [0108]" "Samsung Electronics Co Ltd [144d]" "NVMe SSD Controller PM9A1/PM9A3/980PRO [a80a]" -p02 "Samsung Electronics Co Ltd [144d]" "NVMe SSD Controller PM9A1/PM9A3/980PRO [aa0a]"'
)

with mock.patch(
"subprocess.run",
return_value=DevicesReturn(),
):
with mock.patch(
"aleph.vm.resources.is_kernel_enabled_gpu",
wraps=mock_is_kernel_enabled_gpu,
):

expected_gpu_devices = get_gpu_devices()

print(expected_gpu_devices)

assert expected_gpu_devices[0].vendor == "NVIDIA"
assert expected_gpu_devices[0].device_name == "AD104GL [RTX 4000 SFF Ada Generation]"
assert expected_gpu_devices[0].device_class == "0300"
assert expected_gpu_devices[0].pci_host == "01:00.0"
assert expected_gpu_devices[0].device_id == "10de:27b0"
17 changes: 17 additions & 0 deletions tests/supervisor/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ async def test_allocation_fails_on_invalid_item_hash(aiohttp_client):
@pytest.mark.asyncio
async def test_system_usage(aiohttp_client):
"""Test that the usage system endpoints responds. No auth needed"""

class FakeVmPool:
gpus = []

def get_available_gpus(self):
return []

app = setup_webapp()
app["vm_pool"] = FakeVmPool()
client = await aiohttp_client(app)
response: web.Response = await client.get("/about/usage/system")
assert response.status == 200
Expand All @@ -49,6 +57,13 @@ async def test_system_usage(aiohttp_client):
@pytest.mark.asyncio
async def test_system_usage_mock(aiohttp_client, mocker):
"""Test that the usage system endpoints response value. No auth needed"""

class FakeVmPool:
gpus = []

def get_available_gpus(self):
return []

mocker.patch(
"cpuinfo.cpuinfo.get_cpu_info",
{
Expand All @@ -64,7 +79,9 @@ async def test_system_usage_mock(aiohttp_client, mocker):
"psutil.cpu_count",
lambda: 200,
)

app = setup_webapp()
app["vm_pool"] = FakeVmPool()
client = await aiohttp_client(app)
response: web.Response = await client.get("/about/usage/system")
assert response.status == 200
Expand Down

0 comments on commit fb379ff

Please sign in to comment.