Files
supervisor/tests/dbus/test_timedate.py
Stefan Agner 9088810b49 Improve D-Bus error handling for NetworkManager (#4720)
* Improve D-Bus error handling for NetworkManager

There are quite some errors captured which are related by seemingly a
suddenly missing NetworkManager. Errors appear as:
23-11-21 17:42:50 ERROR (MainThread) [supervisor.dbus.network] Error while processing /org/freedesktop/NetworkManager/Devices/10: Remote peer disconnected
...
23-11-21 17:42:50 ERROR (MainThread) [supervisor.dbus.network] Error while processing /org/freedesktop/NetworkManager/Devices/35: The name is not activatable

Both errors seem to already happen at introspection time, however
the current code doesn't converts these errors to Supervisor issues.
This PR uses the already existing `DBus.from_dbus_error()`.

Furthermore this adds a new Exception `DBusNoReplyError` for the
`ErrorType.NO_REPLY` (or `org.freedesktop.DBus.Error.NoReply` in
D-Bus terms, which is the type of the first of the two issues above).

And finally it separates the `ErrorType.SERVICE_UNKNOWN` (or
`org.freedesktop.DBus.Error.ServiceUnknown` in D-Bus terms, which is
the second of the above issue) from `DBusInterfaceError` into a new
`DBusServiceUnkownError`.

This allows to handle errors more specifically.

To avoid too much churn, all instances where `DBusInterfaceError`
got handled, we are now also handling `DBusServiceUnkownError`.

The `DBusNoReplyError` and `DBusServiceUnkownError` appear when
the NetworkManager service stops or crashes. Instead of retrying
every interface we know, just give up if one of these issues appear.
This should significantly lower error messages users are seeing
and Sentry events.

* Remove unnecessary statement

* Fix pytests

* Make sure error strings are compared correctly

* Fix typo/remove unnecessary pylint exception

* Fix DBusError typing

* Add pytest for from_dbus_error

* Revert "Make sure error strings are compared correctly"

This reverts commit 10dc2e4c3887532921414b4291fe3987186db408.

* Add test cases

---------

Co-authored-by: Mike Degatano <michael.degatano@gmail.com>
2023-11-27 23:32:11 +01:00

93 lines
2.8 KiB
Python

"""Test TimeDate dbus interface."""
# pylint: disable=import-error
from datetime import datetime, timezone
from dbus_fast.aio.message_bus import MessageBus
import pytest
from supervisor.dbus.timedate import TimeDate
from supervisor.exceptions import DBusNotConnectedError
from tests.common import mock_dbus_services
from tests.dbus_service_mocks.timedate import TimeDate as TimeDateService
@pytest.fixture(name="timedate_service")
async def fixture_timedate_service(dbus_session_bus: MessageBus) -> TimeDateService:
"""Mock timedate dbus service."""
yield (await mock_dbus_services({"timedate": None}, dbus_session_bus))["timedate"]
async def test_timedate_info(
timedate_service: TimeDateService, dbus_session_bus: MessageBus
):
"""Test timedate properties."""
timedate = TimeDate()
assert timedate.dt_utc is None
assert timedate.ntp is None
await timedate.connect(dbus_session_bus)
assert timedate.dt_utc == datetime(
2021, 5, 19, 8, 36, 54, 405718, tzinfo=timezone.utc
)
assert timedate.ntp is True
assert timedate.dt_utc.isoformat() == "2021-05-19T08:36:54.405718+00:00"
timedate_service.emit_properties_changed({"NTP": False})
await timedate_service.ping()
assert timedate.ntp is False
timedate_service.emit_properties_changed({}, ["NTP"])
await timedate_service.ping()
await timedate_service.ping() # To process the follow-up get all properties call
assert timedate.ntp is True
async def test_dbus_settime(
timedate_service: TimeDateService, dbus_session_bus: MessageBus
):
"""Set timestamp on backend."""
timedate_service.SetTime.calls.clear()
timedate = TimeDate()
test_dt = datetime(2021, 5, 19, 8, 36, 54, 405718, tzinfo=timezone.utc)
with pytest.raises(DBusNotConnectedError):
await timedate.set_time(test_dt)
await timedate.connect(dbus_session_bus)
assert await timedate.set_time(test_dt) is None
assert timedate_service.SetTime.calls == [(1621413414405718, False, False)]
async def test_dbus_setntp(
timedate_service: TimeDateService, dbus_session_bus: MessageBus
):
"""Disable NTP on backend."""
timedate_service.SetNTP.calls.clear()
timedate = TimeDate()
with pytest.raises(DBusNotConnectedError):
await timedate.set_ntp(False)
await timedate.connect(dbus_session_bus)
assert timedate.ntp is True
assert await timedate.set_ntp(False) is None
assert timedate_service.SetNTP.calls == [(False, False)]
await timedate_service.ping()
assert timedate.ntp is False
async def test_dbus_timedate_connect_error(
dbus_session_bus: MessageBus, caplog: pytest.LogCaptureFixture
):
"""Test connecting to timedate error."""
timedate = TimeDate()
await timedate.connect(dbus_session_bus)
assert "No timedate support on the host" in caplog.text