1
0
mirror of https://github.com/home-assistant/supervisor.git synced 2026-07-04 12:25:02 +01:00
Files
supervisor/tests/host/test_manager.py
T
Stefan Agner ed91b18c4b tests: enable flake8-pytest-style (PT) ruff rules (#6857)
* tests: enable flake8-pytest-style (PT) ruff rules

Enable the `PT` ruff rule set and fix the resulting violations across the
test suite:

- PT006: pass parametrize argument names as tuples instead of a single
  comma-separated string.
- PT022: switch fixtures that have no teardown from `yield` to `return`
  so the lack of cleanup is obvious at a glance.
- PT011: add `match=` to broad `pytest.raises(ValueError)` blocks so the
  expected error is anchored to a specific message.
- PT012: hoist setup (patches, branching) out of `pytest.raises()`
  blocks so only the call that is expected to raise remains inside.
- PT013: replace `from pytest import X` with `import pytest` and access
  attributes via the module.
- PT015: replace `try/except` + `assert False` patterns with
  `pytest.raises(...)`.
- PT017: replace `assert` on exceptions inside `except` blocks with
  `pytest.raises(...) as exc_info` and assert on `exc_info.value`.

No behavioral changes to the tests; the full suite still passes.

* tests: address review feedback on PT ruff rule enablement

- Fix fixture return-type annotations after switching `yield` to `return`
  in tests/conftest.py: drop the `Generator[...]`/`AsyncGenerator[...]`
  wrapper for `dns_manager_service`, `supervisor_internet`, `websession`,
  and `mock_update_data` so the annotation matches what the fixture
  actually returns.
- Correct the return-type annotation of `fixture_ip6config_service` from
  `IP4ConfigService` to `IP6ConfigService`.
- Fix recurring "excepiton" typo in tests/utils/test_exception_helper.py.

* tests: verify backup cleanup on permission error

After `test_new_backup_permission_error` raises `BackupPermissionError`,
assert that no tarfile was left behind and `tmp_path` is empty. The
previous version only checked that the exception was raised, which
missed any regression where a partial tarfile would survive the failed
create.

* tests: rename DNS_GOOD_V6 to DNS_V6_UNSUPPORTED

The constant was named "good" but its tests assert that the URLs are
rejected by the DNS validator. The IPv6 URLs are well-formed but
currently rejected because IPv6 doesn't work with the Docker network
(see `dns_url` in supervisor/validate.py). Rename the constant and the
related test to make the intent obvious.
2026-05-20 22:17:54 +02:00

74 lines
2.5 KiB
Python

"""Test host manager."""
from unittest.mock import patch
from awesomeversion import AwesomeVersion
import pytest
from supervisor.coresys import CoreSys
from supervisor.dbus.const import MulticastProtocolEnabled
from tests.dbus_service_mocks.base import DBusServiceMock
from tests.dbus_service_mocks.rauc import Rauc as RaucService
from tests.dbus_service_mocks.systemd import Systemd as SystemdService
@pytest.fixture(name="systemd_service")
async def fixture_systemd_service(
all_dbus_services: dict[str, DBusServiceMock | dict[str, DBusServiceMock]],
) -> SystemdService:
"""Return systemd service mock."""
return all_dbus_services["systemd"]
async def test_load(coresys: CoreSys, systemd_service: SystemdService):
"""Test manager load."""
systemd_service.ListUnits.calls.clear()
with patch.object(coresys.host.sound, "update") as sound_update:
await coresys.host.load()
assert coresys.dbus.hostname.hostname == "homeassistant-n2"
assert coresys.dbus.systemd.boot_timestamp == 1632236713344227
assert coresys.dbus.timedate.timezone == "Etc/UTC"
assert coresys.dbus.agent.diagnostics is True
assert coresys.dbus.network.connectivity_enabled is True
assert coresys.dbus.resolved.multicast_dns == MulticastProtocolEnabled.RESOLVE
assert coresys.dbus.agent.apparmor.version == "2.13.2"
assert len(coresys.host.logs.default_identifiers) > 0
assert coresys.dbus.udisks2.version == AwesomeVersion("2.9.2")
sound_update.assert_called_once()
assert systemd_service.ListUnits.calls == [()]
async def test_reload(coresys: CoreSys, systemd_service: SystemdService):
"""Test manager reload and ensure it does not unnecessarily recreate dbus objects."""
await coresys.host.load()
systemd_service.ListUnits.calls.clear()
with (
patch("supervisor.utils.dbus.DBus.connect") as connect,
patch.object(coresys.host.sound, "update") as sound_update,
):
await coresys.host.reload()
connect.assert_not_called()
sound_update.assert_called_once()
assert systemd_service.ListUnits.calls == [()]
async def test_reload_os(
coresys: CoreSys, all_dbus_services: dict[str, DBusServiceMock], os_available
):
"""Test manager reload while on OS also reloads OS info cache."""
rauc_service: RaucService = all_dbus_services["rauc"]
rauc_service.GetSlotStatus.calls.clear()
await coresys.host.load()
await coresys.host.reload()
assert rauc_service.GetSlotStatus.calls == [()]