Allow check to cleanup issues (#2302)

This commit is contained in:
Pascal Vizeli 2020-11-26 22:38:33 +01:00 committed by GitHub
parent fda1b523ba
commit d9e20307de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 8 deletions

View File

@ -1,11 +1,12 @@
"""Baseclass for system checks.""" """Baseclass for system checks."""
from abc import ABC, abstractmethod, abstractproperty from abc import ABC, abstractmethod, abstractproperty
import logging import logging
from typing import List from typing import List, Optional
from ...const import CoreState from ...const import CoreState
from ...coresys import CoreSys, CoreSysAttributes from ...coresys import CoreSys, CoreSysAttributes
from ..const import ContextType, IssueType from ..const import ContextType, IssueType
from ..data import Issue
_LOGGER: logging.Logger = logging.getLogger(__name__) _LOGGER: logging.Logger = logging.getLogger(__name__)
@ -22,18 +23,33 @@ class CheckBase(ABC, CoreSysAttributes):
if self.sys_core.state not in self.states: if self.sys_core.state not in self.states:
return return
# Don't need run if issue exists # Check if system is affected by the issue
affected: Optional[Issue] = None
for issue in self.sys_resolution.issues: for issue in self.sys_resolution.issues:
if issue.type != self.issue or issue.context != self.context: if issue.type != self.issue or issue.context != self.context:
continue continue
return affected = issue
break
# System is not affected
if affected is None:
_LOGGER.debug("Run check for %s/%s", self.issue, self.context) _LOGGER.debug("Run check for %s/%s", self.issue, self.context)
await self.run_check() await self.run_check()
return
# Check if issue still exists
if await self.approve_check():
return
self.sys_resolution.dismiss_issue(affected)
@abstractmethod @abstractmethod
async def run_check(self): async def run_check(self) -> None:
"""Run check.""" """Run check if not affected by issue."""
@abstractmethod
async def approve_check(self) -> bool:
"""Approve check if it is affected by issue."""
@property @property
@abstractproperty @abstractproperty

View File

@ -19,8 +19,8 @@ _LOGGER: logging.Logger = logging.getLogger(__name__)
class CheckFreeSpace(CheckBase): class CheckFreeSpace(CheckBase):
"""Storage class for check.""" """Storage class for check."""
async def run_check(self): async def run_check(self) -> None:
"""Run check.""" """Run check if not affected by issue."""
if self.sys_host.info.free_space > MINIMUM_FREE_SPACE_THRESHOLD: if self.sys_host.info.free_space > MINIMUM_FREE_SPACE_THRESHOLD:
if len(self.sys_snapshots.list_snapshots) == 0: if len(self.sys_snapshots.list_snapshots) == 0:
# No snapshots, let's suggest the user to create one! # No snapshots, let's suggest the user to create one!
@ -46,6 +46,12 @@ class CheckFreeSpace(CheckBase):
IssueType.FREE_SPACE, ContextType.SYSTEM, suggestions=suggestions IssueType.FREE_SPACE, ContextType.SYSTEM, suggestions=suggestions
) )
async def approve_check(self) -> bool:
"""Approve check if it is affected by issue."""
if self.sys_host.info.free_space > MINIMUM_FREE_SPACE_THRESHOLD:
return False
return True
@property @property
def issue(self) -> IssueType: def issue(self) -> IssueType:
"""Return a IssueType enum.""" """Return a IssueType enum."""

View File

@ -37,3 +37,18 @@ async def test_if_check_make_issue(coresys: CoreSys):
await coresys.resolution.check.check_system() await coresys.resolution.check.check_system()
assert coresys.resolution.issues[-1].type == IssueType.FREE_SPACE assert coresys.resolution.issues[-1].type == IssueType.FREE_SPACE
async def test_if_check_cleanup_issue(coresys: CoreSys):
"""Test check for setup."""
coresys.core.state = CoreState.RUNNING
with patch("shutil.disk_usage", return_value=(1, 1, 1)):
await coresys.resolution.check.check_system()
assert coresys.resolution.issues[-1].type == IssueType.FREE_SPACE
with patch("shutil.disk_usage", return_value=(42, 42, 2 * (1024.0 ** 3))):
await coresys.resolution.check.check_system()
assert len(coresys.resolution.issues) == 0

View File

@ -26,6 +26,18 @@ async def test_check(coresys: CoreSys):
assert coresys.resolution.issues[-1].type == IssueType.FREE_SPACE assert coresys.resolution.issues[-1].type == IssueType.FREE_SPACE
async def test_approve(coresys: CoreSys):
"""Test check."""
free_space = CheckFreeSpace(coresys)
coresys.core.state = CoreState.RUNNING
with patch("shutil.disk_usage", return_value=(1, 1, 1)):
assert await free_space.approve_check()
with patch("shutil.disk_usage", return_value=(42, 42, 2 * (1024.0 ** 3))):
assert not await free_space.approve_check()
async def test_did_run(coresys: CoreSys): async def test_did_run(coresys: CoreSys):
"""Test that the check ran as expected.""" """Test that the check ran as expected."""
free_space = CheckFreeSpace(coresys) free_space = CheckFreeSpace(coresys)