Files
supervisor/tests/dbus/agent/test_swap.py
Jan Čermák 0a684bdb12 Add API for swap configuration (#5770)
* Add API for swap configuration

Add HTTP API for swap size and swappiness to /os/config/swap. Individual
options can be set in JSON and are calling the DBus API added in OS
Agent 1.7.x, available since OS 15.0. Check for presence of OS of the
required version and return 404 if the criteria are not met.

* Fix type hints and reboot_required logic

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Fix formatting after adding suggestions from GH

* Address @mdegat01 review comments

- Improve swap options validation
- Add swap to the 'all' property of dbus agent
- Use APINotFound with reason instead of HTTPNotFound
- Reorder API routes

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2025-03-27 17:53:46 +01:00

50 lines
1.4 KiB
Python

"""Test Swap configuration interface."""
from dbus_fast.aio.message_bus import MessageBus
import pytest
from supervisor.dbus.agent import OSAgent
from tests.dbus_service_mocks.agent_swap import Swap as SwapService
from tests.dbus_service_mocks.base import DBusServiceMock
@pytest.fixture(name="swap_service", autouse=True)
async def fixture_swap_service(
os_agent_services: dict[str, DBusServiceMock],
) -> SwapService:
"""Mock System dbus service."""
yield os_agent_services["agent_swap"]
async def test_dbus_osagent_swap_size(
swap_service: SwapService, dbus_session_bus: MessageBus
):
"""Test DBus API for swap size."""
os_agent = OSAgent()
assert os_agent.swap.swap_size is None
await os_agent.swap.connect(dbus_session_bus)
assert os_agent.swap.swap_size == "1M"
swap_service.emit_properties_changed({"SwapSize": "2M"})
await swap_service.ping()
assert os_agent.swap.swap_size == "2M"
async def test_dbus_osagent_swappiness(
swap_service: SwapService, dbus_session_bus: MessageBus
):
"""Test DBus API for swappiness."""
os_agent = OSAgent()
assert os_agent.swap.swappiness is None
await os_agent.swap.connect(dbus_session_bus)
assert os_agent.swap.swappiness == 1
swap_service.emit_properties_changed({"Swappiness": 10})
await swap_service.ping()
assert os_agent.swap.swappiness == 10