1
0
mirror of https://github.com/home-assistant/supervisor.git synced 2026-07-02 19:35:42 +01:00
Files
supervisor/tests/backups/conftest.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

100 lines
3.5 KiB
Python

"""Mock test."""
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from supervisor.backups.backup import BackupLocation
from supervisor.backups.const import LOCATION_CLOUD_BACKUP, LOCATION_TYPE, BackupType
from supervisor.backups.validate import ALL_FOLDERS
from supervisor.coresys import CoreSys
from supervisor.mounts.mount import Mount
from tests.const import TEST_ADDON_SLUG
@pytest.fixture(name="backup_mock")
def fixture_backup_mock():
"""Backup class mock."""
with patch("supervisor.backups.manager.Backup") as backup_mock:
backup_instance = MagicMock()
backup_mock.return_value = backup_instance
backup_instance.store_apps = AsyncMock(return_value=None)
backup_instance.store_folders = AsyncMock(return_value=None)
backup_instance.store_homeassistant = AsyncMock(return_value=None)
backup_instance.store_apps = AsyncMock(return_value=None)
backup_instance.store_supervisor_config = AsyncMock(return_value=None)
backup_instance.restore_folders = AsyncMock(return_value=True)
backup_instance.restore_homeassistant = AsyncMock(return_value=None)
backup_instance.restore_apps = AsyncMock(return_value=(True, []))
backup_instance.restore_repositories = AsyncMock(return_value=None)
backup_instance.restore_supervisor_config = AsyncMock(return_value=(True, []))
backup_instance.remove_delta_apps = AsyncMock(return_value=True)
yield backup_mock
@pytest.fixture
def partial_backup_mock(backup_mock):
"""Partial backup mock."""
backup_instance = backup_mock.return_value
backup_instance.sys_type = BackupType.PARTIAL
backup_instance.folders = []
backup_instance.app_list = [TEST_ADDON_SLUG]
backup_instance.supervisor_version = "9999.09.9.dev9999"
backup_instance.location = None
backup_instance.all_locations = {
None: BackupLocation(path=Path("/"), protected=False, size_bytes=0)
}
backup_instance.validate_backup = AsyncMock()
return backup_mock
@pytest.fixture
def full_backup_mock(backup_mock):
"""Full backup mock."""
backup_instance = backup_mock.return_value
backup_instance.sys_type = BackupType.FULL
backup_instance.folders = ALL_FOLDERS
backup_instance.app_list = [TEST_ADDON_SLUG]
backup_instance.supervisor_version = "9999.09.9.dev9999"
backup_instance.location = None
backup_instance.all_locations = {
None: BackupLocation(path=Path("/"), protected=False, size_bytes=0)
}
backup_instance.validate_backup = AsyncMock()
return backup_mock
@pytest.fixture(name="backup_locations")
async def fixture_backup_locations(
request: pytest.FixtureRequest, coresys: CoreSys, mount_propagation, mock_is_mount
) -> list[LOCATION_TYPE]:
"""Return a list of prcoessed backup locations."""
locations: list[LOCATION_TYPE] = []
loaded = False
for location in request.param:
if location in {None, LOCATION_CLOUD_BACKUP}:
locations.append(location)
else:
if not loaded:
await coresys.mounts.load()
await coresys.mounts.create_mount(
Mount.from_dict(
coresys,
{
"name": location,
"usage": "backup",
"type": "cifs",
"server": "test.local",
"share": "test",
},
)
)
locations.append(coresys.mounts.get(location))
return locations