mirror of
https://github.com/home-assistant/supervisor.git
synced 2026-07-10 23:28:09 +01:00
ed91b18c4b
* 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.
260 lines
7.9 KiB
Python
260 lines
7.9 KiB
Python
"""Test hostname dbus interface."""
|
|
|
|
# pylint: disable=import-error
|
|
from dbus_fast import DBusError, Variant
|
|
from dbus_fast.aio.message_bus import MessageBus
|
|
import pytest
|
|
|
|
from supervisor.dbus.const import StartUnitMode, StopUnitMode, UnitActiveState
|
|
from supervisor.dbus.systemd import Systemd
|
|
from supervisor.exceptions import DBusNotConnectedError, DBusSystemdNoSuchUnit
|
|
|
|
from tests.common import mock_dbus_services
|
|
from tests.dbus_service_mocks.systemd import Systemd as SystemdService
|
|
|
|
|
|
@pytest.fixture(name="systemd_service", autouse=True)
|
|
async def fixture_systemd_service(dbus_session_bus: MessageBus) -> SystemdService:
|
|
"""Mock systemd dbus service."""
|
|
return (await mock_dbus_services({"systemd": None}, dbus_session_bus))["systemd"]
|
|
|
|
|
|
async def test_dbus_systemd_info(dbus_session_bus: MessageBus):
|
|
"""Test systemd properties."""
|
|
systemd = Systemd()
|
|
|
|
assert systemd.boot_timestamp is None
|
|
assert systemd.startup_time is None
|
|
|
|
await systemd.connect(dbus_session_bus)
|
|
|
|
assert systemd.boot_timestamp == 1632236713344227
|
|
assert systemd.startup_time == 45.304696
|
|
|
|
|
|
async def test_subscribe_on_connect(
|
|
systemd_service: SystemdService, dbus_session_bus: MessageBus
|
|
):
|
|
"""Test that Subscribe is called on connect to enable signal emission."""
|
|
systemd_service.Subscribe.calls.clear()
|
|
systemd = Systemd()
|
|
|
|
await systemd.connect(dbus_session_bus)
|
|
|
|
assert systemd_service.Subscribe.calls == [()]
|
|
|
|
|
|
async def test_reboot(systemd_service: SystemdService, dbus_session_bus: MessageBus):
|
|
"""Test reboot."""
|
|
systemd_service.Reboot.calls.clear()
|
|
systemd = Systemd()
|
|
|
|
with pytest.raises(DBusNotConnectedError):
|
|
await systemd.reboot()
|
|
|
|
await systemd.connect(dbus_session_bus)
|
|
|
|
assert await systemd.reboot() is None
|
|
assert systemd_service.Reboot.calls == [()]
|
|
|
|
|
|
async def test_power_off(systemd_service: SystemdService, dbus_session_bus: MessageBus):
|
|
"""Test power off."""
|
|
systemd_service.PowerOff.calls.clear()
|
|
systemd = Systemd()
|
|
|
|
with pytest.raises(DBusNotConnectedError):
|
|
await systemd.power_off()
|
|
|
|
await systemd.connect(dbus_session_bus)
|
|
|
|
assert await systemd.power_off() is None
|
|
assert systemd_service.PowerOff.calls == [()]
|
|
|
|
|
|
async def test_start_unit(
|
|
systemd_service: SystemdService, dbus_session_bus: MessageBus
|
|
):
|
|
"""Test start unit."""
|
|
systemd_service.StartUnit.calls.clear()
|
|
systemd = Systemd()
|
|
|
|
with pytest.raises(DBusNotConnectedError):
|
|
await systemd.start_unit("test_unit", StartUnitMode.REPLACE)
|
|
|
|
await systemd.connect(dbus_session_bus)
|
|
|
|
assert (
|
|
await systemd.start_unit("test_unit", StartUnitMode.REPLACE)
|
|
== "/org/freedesktop/systemd1/job/7623"
|
|
)
|
|
assert systemd_service.StartUnit.calls == [("test_unit", "replace")]
|
|
|
|
|
|
async def test_stop_unit(systemd_service: SystemdService, dbus_session_bus: MessageBus):
|
|
"""Test stop unit."""
|
|
systemd_service.StopUnit.calls.clear()
|
|
systemd = Systemd()
|
|
|
|
with pytest.raises(DBusNotConnectedError):
|
|
await systemd.stop_unit("test_unit", StopUnitMode.REPLACE)
|
|
|
|
await systemd.connect(dbus_session_bus)
|
|
|
|
assert (
|
|
await systemd.stop_unit("test_unit", StopUnitMode.REPLACE)
|
|
== "/org/freedesktop/systemd1/job/7623"
|
|
)
|
|
assert systemd_service.StopUnit.calls == [("test_unit", "replace")]
|
|
|
|
|
|
async def test_restart_unit(
|
|
systemd_service: SystemdService, dbus_session_bus: MessageBus
|
|
):
|
|
"""Test restart unit."""
|
|
systemd_service.RestartUnit.calls.clear()
|
|
systemd = Systemd()
|
|
|
|
with pytest.raises(DBusNotConnectedError):
|
|
await systemd.restart_unit("test_unit", StartUnitMode.REPLACE)
|
|
|
|
await systemd.connect(dbus_session_bus)
|
|
|
|
assert (
|
|
await systemd.restart_unit("test_unit", StartUnitMode.REPLACE)
|
|
== "/org/freedesktop/systemd1/job/7623"
|
|
)
|
|
assert systemd_service.RestartUnit.calls == [("test_unit", "replace")]
|
|
|
|
|
|
async def test_reload_unit(
|
|
systemd_service: SystemdService, dbus_session_bus: MessageBus
|
|
):
|
|
"""Test reload unit."""
|
|
systemd_service.ReloadOrRestartUnit.calls.clear()
|
|
systemd = Systemd()
|
|
|
|
with pytest.raises(DBusNotConnectedError):
|
|
await systemd.reload_unit("test_unit", StartUnitMode.REPLACE)
|
|
|
|
await systemd.connect(dbus_session_bus)
|
|
|
|
assert (
|
|
await systemd.reload_unit("test_unit", StartUnitMode.REPLACE)
|
|
== "/org/freedesktop/systemd1/job/7623"
|
|
)
|
|
assert systemd_service.ReloadOrRestartUnit.calls == [("test_unit", "replace")]
|
|
|
|
|
|
async def test_list_units(dbus_session_bus: MessageBus):
|
|
"""Test list units."""
|
|
systemd = Systemd()
|
|
|
|
with pytest.raises(DBusNotConnectedError):
|
|
await systemd.list_units()
|
|
|
|
await systemd.connect(dbus_session_bus)
|
|
|
|
units = await systemd.list_units()
|
|
assert len(units) == 4
|
|
assert units[1][0] == "firewalld.service"
|
|
assert units[1][2] == "not-found"
|
|
assert units[3][0] == "zram-swap.service"
|
|
assert units[3][2] == "loaded"
|
|
|
|
|
|
async def test_start_transient_unit(
|
|
systemd_service: SystemdService, dbus_session_bus: MessageBus
|
|
):
|
|
"""Test start transient unit."""
|
|
systemd_service.StartTransientUnit.calls.clear()
|
|
systemd = Systemd()
|
|
|
|
with pytest.raises(DBusNotConnectedError):
|
|
await systemd.start_transient_unit(
|
|
"tmp-test.mount",
|
|
StartUnitMode.FAIL,
|
|
[],
|
|
)
|
|
|
|
await systemd.connect(dbus_session_bus)
|
|
|
|
assert (
|
|
await systemd.start_transient_unit(
|
|
"tmp-test.mount",
|
|
StartUnitMode.FAIL,
|
|
[
|
|
("Description", Variant("s", "Test")),
|
|
("What", Variant("s", "//homeassistant/config")),
|
|
("Type", Variant("s", "cifs")),
|
|
("Options", Variant("s", "username=homeassistant,password=password")),
|
|
],
|
|
)
|
|
== "/org/freedesktop/systemd1/job/7623"
|
|
)
|
|
assert systemd_service.StartTransientUnit.calls == [
|
|
(
|
|
"tmp-test.mount",
|
|
"fail",
|
|
[
|
|
("Description", Variant("s", "Test")),
|
|
("What", Variant("s", "//homeassistant/config")),
|
|
("Type", Variant("s", "cifs")),
|
|
("Options", Variant("s", "username=homeassistant,password=password")),
|
|
],
|
|
[],
|
|
)
|
|
]
|
|
|
|
|
|
async def test_reset_failed_unit(
|
|
systemd_service: SystemdService, dbus_session_bus: MessageBus
|
|
):
|
|
"""Test resetting a failed unit."""
|
|
systemd_service.ResetFailedUnit.calls.clear()
|
|
systemd = Systemd()
|
|
|
|
with pytest.raises(DBusNotConnectedError):
|
|
await systemd.reset_failed_unit("tmp-test.mount")
|
|
|
|
await systemd.connect(dbus_session_bus)
|
|
|
|
assert await systemd.reset_failed_unit("tmp-test.mount") is None
|
|
assert systemd_service.ResetFailedUnit.calls == [("tmp-test.mount",)]
|
|
|
|
|
|
async def test_get_unit(systemd_service: SystemdService, dbus_session_bus: MessageBus):
|
|
"""Test getting job ID for unit."""
|
|
await mock_dbus_services(
|
|
{"systemd_unit": "/org/freedesktop/systemd1/unit/tmp_2dyellow_2emount"},
|
|
dbus_session_bus,
|
|
)
|
|
systemd_service.GetUnit.calls.clear()
|
|
systemd = Systemd()
|
|
|
|
with pytest.raises(DBusNotConnectedError):
|
|
await systemd.get_unit("tmp-test.mount")
|
|
|
|
await systemd.connect(dbus_session_bus)
|
|
|
|
unit = await systemd.get_unit("tmp-test.mount")
|
|
assert unit.bus_name == "org.freedesktop.systemd1"
|
|
assert unit.object_path == "/org/freedesktop/systemd1/unit/tmp_2dyellow_2emount"
|
|
assert await unit.get_active_state() == UnitActiveState.ACTIVE
|
|
assert systemd_service.GetUnit.calls == [("tmp-test.mount",)]
|
|
|
|
|
|
async def test_get_unit_not_found(
|
|
systemd_service: SystemdService, dbus_session_bus: MessageBus
|
|
):
|
|
"""Test error for non-existent unit name."""
|
|
systemd_service.response_get_unit = DBusError(
|
|
"org.freedesktop.systemd1.NoSuchUnit", "error"
|
|
)
|
|
|
|
systemd = Systemd()
|
|
await systemd.connect(dbus_session_bus)
|
|
|
|
with pytest.raises(DBusSystemdNoSuchUnit):
|
|
await systemd.get_unit("error.mount")
|