-
Notifications
You must be signed in to change notification settings - Fork 667
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
329 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
"""Constants for OS.""" | ||
|
||
FILESYSTEM_LABEL_DATA_DISK = "hassos-data" | ||
FILESYSTEM_LABEL_DISABLED_DATA_DISK = "hassos-data-dis" | ||
FILESYSTEM_LABEL_OLD_DATA_DISK = "hassos-data-old" | ||
PARTITION_NAME_EXTERNAL_DATA_DISK = "hassos-data-external" | ||
PARTITION_NAME_OLD_EXTERNAL_DATA_DISK = "hassos-data-external-old" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""Helpers to check for a disabled data disk.""" | ||
|
||
from pathlib import Path | ||
|
||
from ...const import CoreState | ||
from ...coresys import CoreSys | ||
from ...dbus.udisks2.block import UDisks2Block | ||
from ...dbus.udisks2.data import DeviceSpecification | ||
from ...os.const import FILESYSTEM_LABEL_DISABLED_DATA_DISK | ||
from ..const import ContextType, IssueType, SuggestionType | ||
from .base import CheckBase | ||
|
||
|
||
def setup(coresys: CoreSys) -> CheckBase: | ||
"""Check setup function.""" | ||
return CheckDisabledDataDisk(coresys) | ||
|
||
|
||
class CheckDisabledDataDisk(CheckBase): | ||
"""CheckDisabledDataDisk class for check.""" | ||
|
||
async def run_check(self) -> None: | ||
"""Run check if not affected by issue.""" | ||
for block_device in self.sys_dbus.udisks2.block_devices: | ||
if self._is_disabled_data_disk(block_device): | ||
self.sys_resolution.create_issue( | ||
IssueType.DISABLED_DATA_DISK, | ||
ContextType.SYSTEM, | ||
reference=block_device.device.as_posix(), | ||
suggestions=[ | ||
SuggestionType.RENAME_DATA_DISK, | ||
SuggestionType.ADOPT_DATA_DISK, | ||
], | ||
) | ||
|
||
async def approve_check(self, reference: str | None = None) -> bool: | ||
"""Approve check if it is affected by issue.""" | ||
resolved = await self.sys_dbus.udisks2.resolve_device( | ||
DeviceSpecification(path=Path(reference)) | ||
) | ||
return resolved and self._is_disabled_data_disk(resolved[0]) | ||
|
||
def _is_disabled_data_disk(self, block_device: UDisks2Block) -> bool: | ||
"""Return true if filesystem block device has name indicating it was disabled by OS.""" | ||
return ( | ||
block_device.filesystem | ||
and block_device.id_label == FILESYSTEM_LABEL_DISABLED_DATA_DISK | ||
) | ||
|
||
@property | ||
def issue(self) -> IssueType: | ||
"""Return a IssueType enum.""" | ||
return IssueType.DISABLED_DATA_DISK | ||
|
||
@property | ||
def context(self) -> ContextType: | ||
"""Return a ContextType enum.""" | ||
return ContextType.SYSTEM | ||
|
||
@property | ||
def states(self) -> list[CoreState]: | ||
"""Return a list of valid states when this check can run.""" | ||
return [CoreState.RUNNING, CoreState.SETUP] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
"""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.""" | ||
yield 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) | ||
coresys.core.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) | ||
coresys.core.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: | ||
coresys.core.state = state | ||
await disabled_data_disk() | ||
check.assert_called_once() | ||
check.reset_mock() | ||
|
||
for state in should_not_run: | ||
coresys.core.state = state | ||
await disabled_data_disk() | ||
check.assert_not_called() | ||
check.reset_mock() |
Oops, something went wrong.