From 551cc3f3f937dad21ce0390103f56e8c5853fd5f Mon Sep 17 00:00:00 2001 From: Olivier Le Thanh Duong Date: Mon, 29 Apr 2024 17:42:46 +0200 Subject: [PATCH] add unit test for system usage --- src/aleph/vm/models.py | 6 +----- src/aleph/vm/orchestrator/resources.py | 4 ++-- src/aleph/vm/pool.py | 5 +++-- tests/supervisor/test_views.py | 13 +++++++++++++ 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/aleph/vm/models.py b/src/aleph/vm/models.py index 9d38eea97..82390cb08 100644 --- a/src/aleph/vm/models.py +++ b/src/aleph/vm/models.py @@ -85,11 +85,7 @@ class VmExecution: @property def is_running(self) -> bool: - return ( - bool(self.times.starting_at and not self.times.stopping_at) - if not self.persistent - else self.systemd_manager.is_service_active(self.controller_service) - ) + return bool(self.times.starting_at and not self.times.stopping_at) @property def is_stopping(self) -> bool: diff --git a/src/aleph/vm/orchestrator/resources.py b/src/aleph/vm/orchestrator/resources.py index a40c6ff13..b58faaacc 100644 --- a/src/aleph/vm/orchestrator/resources.py +++ b/src/aleph/vm/orchestrator/resources.py @@ -87,8 +87,8 @@ def get_machine_properties() -> MachineProperties: cpu_info = cpuinfo.get_cpu_info() # Slow return MachineProperties( cpu=CpuProperties( - architecture=cpu_info["raw_arch_string"], - vendor=cpu_info["vendor_id"], + architecture=cpu_info.get("raw_arch_string", cpu_info.get("arch_string_raw")), + vendor=cpu_info["vendor_id_raw"], ), ) diff --git a/src/aleph/vm/pool.py b/src/aleph/vm/pool.py index 3e5c5f3ec..b68de6873 100644 --- a/src/aleph/vm/pool.py +++ b/src/aleph/vm/pool.py @@ -239,8 +239,9 @@ async def load_persistent_executions(self): persistent=saved_execution.persistent, ) - if execution.is_running: - # TODO: Improve the way that we re-create running execution + if await self.systemd_manager.is_service_active( + execution.controller_service + ): # TODO: Improve the way that we re-create running execution await execution.prepare() if self.network: vm_type = VmType.from_message_content(execution.message) diff --git a/tests/supervisor/test_views.py b/tests/supervisor/test_views.py index 49a6fa91e..73bcfec45 100644 --- a/tests/supervisor/test_views.py +++ b/tests/supervisor/test_views.py @@ -24,3 +24,16 @@ async def test_allocation_fails_on_invalid_item_hash(aiohttp_client): "type": "value_error.unknownhash", }, ] + + +@pytest.mark.asyncio +async def test_system_usage(aiohttp_client): + """Test that the allocation endpoint fails when an invalid item_hash is provided.""" + client = await aiohttp_client(app) + settings.ALLOCATION_TOKEN_HASH = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" # = "test" + response: web.Response = await client.get("/about/usage/system") + assert response.status == 200 + # check if it is valid json + resp = await response.json() + assert "cpu" in resp + assert resp["cpu"]["count"] > 0