From 4b500ef873fe1744da0ddfaa6ffa3ec6f41cc749 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Tue, 17 Nov 2020 18:46:02 +0100 Subject: [PATCH] JobManager should not touch our exceptions (#2261) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * JobManager should not touch our exceptions * Add tests * temporary allow su update all time * Update supervisor/jobs/decorator.py Co-authored-by: Joakim Sørensen Co-authored-by: Joakim Sørensen --- supervisor/jobs/decorator.py | 11 ++++-- supervisor/misc/tasks.py | 2 +- supervisor/updater.py | 2 +- tests/{job => jobs}/test_job_decorator.py | 45 +++++++++++++++++++++++ tests/{job => jobs}/test_job_manager.py | 0 5 files changed, 54 insertions(+), 6 deletions(-) rename tests/{job => jobs}/test_job_decorator.py (76%) rename tests/{job => jobs}/test_job_manager.py (100%) diff --git a/supervisor/jobs/decorator.py b/supervisor/jobs/decorator.py index 7909e40a7..0dc41d127 100644 --- a/supervisor/jobs/decorator.py +++ b/supervisor/jobs/decorator.py @@ -3,6 +3,8 @@ from enum import Enum import logging from typing import List, Optional +import sentry_sdk + from ..const import CoreState from ..coresys import CoreSys from ..exceptions import HassioError, JobException @@ -57,16 +59,17 @@ class Job: return False try: - result = await self._method(*args, **kwargs) + return await self._method(*args, **kwargs) except HassioError as err: - _LOGGER.error(err) + raise err + except Exception as err: + _LOGGER.exception("Unhandled exception: %s", err) + sentry_sdk.capture_exception(err) raise JobException() from err finally: if self.cleanup: self._coresys.jobs.remove_job(job) - return result - return wrapper def _check_conditions(self): diff --git a/supervisor/misc/tasks.py b/supervisor/misc/tasks.py index 8bf4a2120..20249dfb8 100644 --- a/supervisor/misc/tasks.py +++ b/supervisor/misc/tasks.py @@ -144,7 +144,7 @@ class Tasks(CoreSysAttributes): except AddonsError: _LOGGER.error("Can't auto update Add-on %s", addon.slug) - @Job(conditions=[JobCondition.HEALTHY, JobCondition.FREE_SPACE]) + @Job(conditions=[JobCondition.FREE_SPACE]) async def _update_supervisor(self): """Check and run update of Supervisor Supervisor.""" if not self.sys_supervisor.need_update: diff --git a/supervisor/updater.py b/supervisor/updater.py index 3a6295378..61790d125 100644 --- a/supervisor/updater.py +++ b/supervisor/updater.py @@ -159,7 +159,7 @@ class Updater(JsonConfig, CoreSysAttributes): self._data[ATTR_CHANNEL] = value @AsyncThrottle(timedelta(seconds=30)) - @Job(conditions=[JobCondition.HEALTHY]) + @Job(conditions=[JobCondition.INTERNET]) async def fetch_data(self): """Fetch current versions from Github. diff --git a/tests/job/test_job_decorator.py b/tests/jobs/test_job_decorator.py similarity index 76% rename from tests/job/test_job_decorator.py rename to tests/jobs/test_job_decorator.py index b1b60c34f..152ff39cf 100644 --- a/tests/job/test_job_decorator.py +++ b/tests/jobs/test_job_decorator.py @@ -2,8 +2,11 @@ # pylint: disable=protected-access,import-error from unittest.mock import patch +import pytest + from supervisor.const import CoreState from supervisor.coresys import CoreSys +from supervisor.exceptions import HassioError, JobException from supervisor.jobs.decorator import Job, JobCondition from supervisor.resolution.const import UnhealthyReason @@ -127,3 +130,45 @@ async def test_internet_connectivity_with_core_state(coresys: CoreSys): coresys.core.state = CoreState.STOPPING assert await test.execute() + + +async def test_exception(coresys: CoreSys): + """Test the healty decorator.""" + + class TestClass: + """Test class.""" + + def __init__(self, coresys: CoreSys): + """Initialize the test class.""" + self.coresys = coresys + + @Job(conditions=[JobCondition.HEALTHY]) + async def execute(self): + """Execute the class method.""" + raise HassioError() + + test = TestClass(coresys) + + with pytest.raises(HassioError): + assert await test.execute() + + +async def test_exception_not_handle(coresys: CoreSys): + """Test the healty decorator.""" + + class TestClass: + """Test class.""" + + def __init__(self, coresys: CoreSys): + """Initialize the test class.""" + self.coresys = coresys + + @Job(conditions=[JobCondition.HEALTHY]) + async def execute(self): + """Execute the class method.""" + raise Exception() + + test = TestClass(coresys) + + with pytest.raises(JobException): + assert await test.execute() diff --git a/tests/job/test_job_manager.py b/tests/jobs/test_job_manager.py similarity index 100% rename from tests/job/test_job_manager.py rename to tests/jobs/test_job_manager.py