11import asyncio
2+ import os
23import tempfile
34from copy import deepcopy
45from pathlib import Path
1112from pytest_mock import MockerFixture
1213
1314from aleph .vm .conf import settings
14- from aleph .vm .orchestrator .machine import get_hardware_info
1515from aleph .vm .orchestrator .supervisor import setup_webapp
1616from aleph .vm .pool import VmPool
1717from aleph .vm .sevclient import SevClient
@@ -45,6 +45,7 @@ async def test_allocation_fails_on_invalid_item_hash(aiohttp_client):
4545@pytest .mark .asyncio
4646async def test_system_usage (aiohttp_client , mocker , mock_app_with_pool ):
4747 """Test that the usage system endpoints responds. No auth needed"""
48+ mocker .patch ("aleph.vm.orchestrator.resources.get_hardware_info" , return_value = MOCK_SYSTEM_INFO )
4849
4950 client = await aiohttp_client (await mock_app_with_pool )
5051 response : web .Response = await client .get ("/about/usage/system" )
@@ -55,7 +56,7 @@ async def test_system_usage(aiohttp_client, mocker, mock_app_with_pool):
5556 assert resp ["cpu" ]["count" ] > 0
5657
5758
58- FAKE_SYSTEM_INFO = {
59+ MOCK_SYSTEM_INFO = {
5960 "cpu" : {
6061 "id" : "cpu" ,
6162 "class" : "processor" ,
@@ -74,6 +75,7 @@ async def test_system_usage(aiohttp_client, mocker, mock_app_with_pool):
7475 "width" : 64 ,
7576 "configuration" : {"cores" : "8" , "enabledcores" : "8" , "microcode" : "167776681" , "threads" : "1" },
7677 "capabilities" : {
78+ "x86-64" : "64bits extensions (x86-64)" ,
7779 "fpu" : "mathematical co-processor" ,
7880 "fpu_exception" : "FPU exceptions reporting" ,
7981 "wp" : True ,
@@ -209,7 +211,7 @@ async def test_system_usage(aiohttp_client, mocker, mock_app_with_pool):
209211async def test_system_usage_mock (aiohttp_client , mocker , mock_app_with_pool ):
210212 """Test that the usage system endpoints response value. No auth needed"""
211213
212- mocker .patch ("aleph.vm.orchestrator.machine .get_hardware_info" , FAKE_SYSTEM_INFO )
214+ mocker .patch ("aleph.vm.orchestrator.resources .get_hardware_info" , return_value = MOCK_SYSTEM_INFO )
213215 mocker .patch (
214216 "psutil.getloadavg" ,
215217 lambda : [1 , 2 , 3 ],
@@ -233,7 +235,10 @@ async def test_system_usage_mock(aiohttp_client, mocker, mock_app_with_pool):
233235@pytest .mark .asyncio
234236async def test_system_capability_mock (aiohttp_client , mocker ):
235237 """Test that the capability system endpoints response value. No auth needed"""
236- mocker .patch ("aleph.vm.orchestrator.machine.get_hardware_info" , FAKE_SYSTEM_INFO )
238+ mocker .patch ("aleph.vm.orchestrator.resources.get_hardware_info" , return_value = MOCK_SYSTEM_INFO )
239+ mocker .patch ("aleph.vm.orchestrator.resources.check_amd_sev_supported" , return_value = True )
240+ mocker .patch ("aleph.vm.orchestrator.resources.check_amd_sev_es_supported" , return_value = True )
241+ mocker .patch ("aleph.vm.orchestrator.resources.check_amd_sev_snp_supported" , return_value = False )
237242 mocker .patch (
238243 "psutil.getloadavg" ,
239244 lambda : [1 , 2 , 3 ],
@@ -252,14 +257,34 @@ async def test_system_capability_mock(aiohttp_client, mocker):
252257 "cpu" : {
253258 "architecture" : "x86_64" ,
254259 "vendor" : "AuthenticAMD" ,
260+ "features" : ["sev" , "sev_es" ],
255261 "model" : "AMD EPYC 7763 64-Core Processor" ,
256- "frequency" : " 2000000000" ,
257- "count" : " 200" ,
262+ "frequency" : 2000000000 ,
263+ "count" : 200 ,
258264 },
259- "memory" : {"size" : " 17179869184" , "units" : "bytes" , "type" : "" , "clock" : None , "clock_units" : "" },
265+ "memory" : {"size" : 17179869184 , "units" : "bytes" , "type" : "" , "clock" : None , "clock_units" : None },
260266 }
261267
262268
269+ @pytest .mark .asyncio
270+ async def test_system_capability_real (aiohttp_client , mocker ):
271+ """Test that the capability system endpoints response value
272+ with real system value, no mock so we don't know the definive value but want ot see that it works"""
273+ if os .environ .get ("GITHUB_JOB" ):
274+ pytest .xfail ("Test fail inside GITHUB CI because of invalid lshw return inside worker" )
275+
276+ app = setup_webapp ()
277+ client = await aiohttp_client (app )
278+ response : web .Response = await client .get ("/about/capability" )
279+ assert response .status == 200
280+ # check if it is valid json
281+ resp = await response .json ()
282+ assert resp .get ("cpu" ), resp
283+ assert resp ["cpu" ].get ("architecture" )
284+ assert resp .get ("memory" )
285+ assert resp ["memory" ].get ("size" )
286+
287+
263288@pytest .mark .asyncio
264289async def test_allocation_invalid_auth_token (aiohttp_client ):
265290 """Test that the allocation endpoint fails when an invalid auth token is provided."""
@@ -456,6 +481,7 @@ def mock_is_kernel_enabled_gpu(pci_host: str) -> bool:
456481async def test_system_usage_gpu_ressources (aiohttp_client , mocker , mock_app_with_pool ):
457482 """Test gpu are properly listed"""
458483 client = await aiohttp_client (await mock_app_with_pool )
484+ mocker .patch ("aleph.vm.orchestrator.resources.get_hardware_info" , return_value = MOCK_SYSTEM_INFO )
459485
460486 response : web .Response = await client .get ("/about/usage/system" )
461487 assert response .status == 200
0 commit comments