1
0
mirror of https://github.com/home-assistant/supervisor.git synced 2026-07-07 22:05:06 +01:00
Files
supervisor/tests/dbus/test_hostname.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.4 KiB
Python

"""Test hostname dbus interface."""
# pylint: disable=import-error
from dbus_fast.aio.message_bus import MessageBus
import pytest
from supervisor.dbus.hostname import Hostname
from supervisor.exceptions import DBusNotConnectedError
from tests.common import mock_dbus_services
from tests.dbus_service_mocks.hostname import Hostname as HostnameService
@pytest.fixture(name="hostname_service")
async def fixture_hostname_service(dbus_session_bus: MessageBus) -> HostnameService:
"""Mock hostname dbus service."""
return (await mock_dbus_services({"hostname": None}, dbus_session_bus))["hostname"]
async def test_dbus_hostname_info(
hostname_service: HostnameService, dbus_session_bus: MessageBus
):
"""Test hostname properties."""
hostname = Hostname()
assert hostname.hostname is None
await hostname.connect(dbus_session_bus)
assert hostname.hostname == "homeassistant-n2"
assert hostname.kernel == "5.10.33"
assert (
hostname.cpe
== "cpe:2.3:o:home-assistant:haos:6.0.dev20210504:*:development:*:*:*:odroid-n2:*"
)
assert hostname.operating_system == "Home Assistant OS 6.0.dev20210504"
hostname_service.emit_properties_changed({"StaticHostname": "test"})
await hostname_service.ping()
assert hostname.hostname == "test"
hostname_service.emit_properties_changed({}, ["StaticHostname"])
await hostname_service.ping()
await hostname_service.ping() # To process the follow-up get all properties call
assert hostname.hostname == "homeassistant-n2"
async def test_dbus_sethostname(
hostname_service: HostnameService, dbus_session_bus: MessageBus
):
"""Set hostname on backend."""
hostname_service.SetStaticHostname.calls.clear()
hostname = Hostname()
with pytest.raises(DBusNotConnectedError):
await hostname.set_static_hostname("StarWars")
await hostname.connect(dbus_session_bus)
assert hostname.hostname == "homeassistant-n2"
await hostname.set_static_hostname("StarWars")
assert hostname_service.SetStaticHostname.calls == [("StarWars", False)]
await hostname_service.ping()
assert hostname.hostname == "StarWars"
async def test_dbus_hostname_connect_error(
dbus_session_bus: MessageBus, caplog: pytest.LogCaptureFixture
):
"""Test connecting to hostname error."""
hostname = Hostname()
await hostname.connect(dbus_session_bus)
assert "No hostname support on the host" in caplog.text