Files
supervisor/tests/dbus/agent/test_agent.py
Stefan Agner 8ab396d77c Improve and extend error handling on D-Bus connect (#5245)
* Improve and extend error handling on D-Bus connect

Avoid initializing the Supervisor board since it does not support the
Properties interface (see https://github.com/home-assistant/os-agent/issues/206).

This prevents the following somewhat confusing warning:
  No OS-Agent support on the host. Some Host functions have been disabled.

The OS Agent is actually installed on the host, it is just a single
object which caused issues. No functionalty was actually lost, as the
Supervisor board object has no features currently, and all other
interfaces got properly initialized still (thanks to gather()).

Print warnings more fine graned so it is clear which object exactly
causes an issue. Also print a log message on the lowest layer when an
error occures on calling D-Bus. This allows to easier track the actual
D-Bus error source.

Fixes: #5241

* Fix tests

* Use local variable

* Avoid stack trace when board support fails to load

* Fix tests

* Use override mechanism to disable Properties support

Instead of disable loading of Supervised entirly override initialization
to prevent loading the Properties interface.

* Revert "Fix tests"

This reverts commit 1e3c491ace.
2024-08-20 17:55:53 +02:00

86 lines
2.3 KiB
Python

"""Test OSAgent dbus interface."""
# pylint: disable=import-error
from dbus_fast.aio.message_bus import MessageBus
import pytest
from supervisor.dbus.agent import OSAgent
from tests.common import mock_dbus_services
from tests.dbus_service_mocks.base import DBusServiceMock
from tests.dbus_service_mocks.os_agent import OSAgent as OSAgentService
@pytest.fixture(name="os_agent_service")
async def fixture_os_agent_service(
os_agent_services: dict[str, DBusServiceMock],
) -> OSAgentService:
"""Mock OS Agent dbus service."""
yield os_agent_services["os_agent"]
async def test_dbus_osagent(
os_agent_service: OSAgentService, dbus_session_bus: MessageBus
):
"""Test OS Agent properties."""
os_agent = OSAgent()
assert os_agent.version is None
assert os_agent.diagnostics is None
await os_agent.connect(dbus_session_bus)
assert os_agent.version == "1.1.0"
assert os_agent.diagnostics
os_agent_service.emit_properties_changed({"Diagnostics": False})
await os_agent_service.ping()
assert os_agent.diagnostics is False
os_agent_service.emit_properties_changed({}, ["Diagnostics"])
await os_agent_service.ping()
await os_agent_service.ping()
assert os_agent.diagnostics is True
@pytest.mark.parametrize(
"skip_service,error",
[
("os_agent", "No OS-Agent support on the host"),
(
"agent_apparmor",
"Can't load OS Agent dbus interface io.hass.os /io/hass/os/AppArmor",
),
(
"agent_datadisk",
"Can't load OS Agent dbus interface io.hass.os /io/hass/os/DataDisk",
),
],
)
async def test_dbus_osagent_connect_error(
skip_service: str,
error: str,
dbus_session_bus: MessageBus,
caplog: pytest.LogCaptureFixture,
):
"""Test OS Agent errors during connect."""
os_agent_services = {
"os_agent": None,
"agent_apparmor": None,
"agent_cgroup": None,
"agent_datadisk": None,
"agent_system": None,
"agent_boards": None,
"agent_boards_yellow": None,
}
os_agent_services.pop(skip_service)
await mock_dbus_services(
os_agent_services,
dbus_session_bus,
)
os_agent = OSAgent()
await os_agent.connect(dbus_session_bus)
assert error in caplog.text