mirror of
https://github.com/home-assistant/supervisor.git
synced 2026-07-05 12:55:01 +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.
101 lines
3.3 KiB
Python
101 lines
3.3 KiB
Python
"""Test check for disabled data disk."""
|
|
|
|
# pylint: disable=import-error
|
|
from dataclasses import replace
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
from supervisor.const import CoreState
|
|
from supervisor.coresys import CoreSys
|
|
from supervisor.resolution.checks.disabled_data_disk import CheckDisabledDataDisk
|
|
from supervisor.resolution.const import ContextType, IssueType, SuggestionType
|
|
from supervisor.resolution.data import Issue, Suggestion
|
|
|
|
from tests.dbus_service_mocks.base import DBusServiceMock
|
|
from tests.dbus_service_mocks.udisks2_block import Block as BlockService
|
|
|
|
|
|
@pytest.fixture(name="sda1_block_service")
|
|
async def fixture_sda1_block_service(
|
|
udisks2_services: dict[str, DBusServiceMock | dict[str, DBusServiceMock]],
|
|
) -> BlockService:
|
|
"""Return sda1 block service."""
|
|
return udisks2_services["udisks2_block"][
|
|
"/org/freedesktop/UDisks2/block_devices/sda1"
|
|
]
|
|
|
|
|
|
async def test_base(coresys: CoreSys):
|
|
"""Test check basics."""
|
|
disabled_data_disk = CheckDisabledDataDisk(coresys)
|
|
assert disabled_data_disk.slug == "disabled_data_disk"
|
|
assert disabled_data_disk.enabled
|
|
|
|
|
|
async def test_check(coresys: CoreSys, sda1_block_service: BlockService):
|
|
"""Test check."""
|
|
disabled_data_disk = CheckDisabledDataDisk(coresys)
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
|
|
await disabled_data_disk.run_check()
|
|
|
|
assert len(coresys.resolution.issues) == 0
|
|
assert len(coresys.resolution.suggestions) == 0
|
|
|
|
sda1_block_service.emit_properties_changed({"IdLabel": "hassos-data-dis"})
|
|
await sda1_block_service.ping()
|
|
|
|
await disabled_data_disk.run_check()
|
|
|
|
assert coresys.resolution.issues == [
|
|
Issue(IssueType.DISABLED_DATA_DISK, ContextType.SYSTEM, reference="/dev/sda1")
|
|
]
|
|
assert coresys.resolution.suggestions == [
|
|
Suggestion(
|
|
SuggestionType.RENAME_DATA_DISK, ContextType.SYSTEM, reference="/dev/sda1"
|
|
),
|
|
Suggestion(
|
|
SuggestionType.ADOPT_DATA_DISK, ContextType.SYSTEM, reference="/dev/sda1"
|
|
),
|
|
]
|
|
|
|
|
|
async def test_approve(coresys: CoreSys, sda1_block_service: BlockService):
|
|
"""Test approve."""
|
|
disabled_data_disk = CheckDisabledDataDisk(coresys)
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
|
|
assert not await disabled_data_disk.approve_check(reference="/dev/sda1")
|
|
|
|
sda1_block_service.fixture = replace(
|
|
sda1_block_service.fixture, IdLabel="hassos-data-dis"
|
|
)
|
|
|
|
assert await disabled_data_disk.approve_check(reference="/dev/sda1")
|
|
|
|
|
|
async def test_did_run(coresys: CoreSys):
|
|
"""Test that the check ran as expected."""
|
|
disabled_data_disk = CheckDisabledDataDisk(coresys)
|
|
should_run = disabled_data_disk.states
|
|
should_not_run = [state for state in CoreState if state not in should_run]
|
|
assert len(should_run) != 0
|
|
assert len(should_not_run) != 0
|
|
|
|
with patch(
|
|
"supervisor.resolution.checks.disabled_data_disk.CheckDisabledDataDisk.run_check",
|
|
return_value=None,
|
|
) as check:
|
|
for state in should_run:
|
|
await coresys.core.set_state(state)
|
|
await disabled_data_disk()
|
|
check.assert_called_once()
|
|
check.reset_mock()
|
|
|
|
for state in should_not_run:
|
|
await coresys.core.set_state(state)
|
|
await disabled_data_disk()
|
|
check.assert_not_called()
|
|
check.reset_mock()
|