Skip to content

Commit

Permalink
Fix pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
agners committed Oct 9, 2024
1 parent b66e7f7 commit 86c97a3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
29 changes: 24 additions & 5 deletions tests/test_supervisor.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Test supervisor object."""

from datetime import datetime
from datetime import datetime, timedelta
import errno
from unittest.mock import AsyncMock, Mock, PropertyMock, patch

from aiohttp import ClientTimeout
from aiohttp.client_exceptions import ClientError
from awesomeversion import AwesomeVersion
import pytest
from time_machine import travel

from supervisor.const import UpdateChannel
from supervisor.coresys import CoreSys
Expand All @@ -18,9 +19,11 @@
SupervisorUpdateError,
)
from supervisor.host.apparmor import AppArmorControl
from supervisor.jobs.decorator import Job
from supervisor.resolution.const import ContextType, IssueType
from supervisor.resolution.data import Issue
from supervisor.supervisor import Supervisor
from tests.utils.test_decorator import get_job_decorator


@pytest.fixture(name="websession", scope="function")
Expand Down Expand Up @@ -58,21 +61,37 @@ async def test_connectivity_check(
assert supervisor_unthrottled.connectivity is connectivity


@pytest.mark.parametrize("side_effect,call_count", [(ClientError(), 3), (None, 1)])
@pytest.mark.parametrize(
"side_effect,call_interval,throttled",
[
(None, timedelta(minutes=5), True),
(None, timedelta(minutes=15), False),
(ClientError(), timedelta(seconds=20), True),
(ClientError(), timedelta(seconds=40), False),
],
)
async def test_connectivity_check_throttling(
coresys: CoreSys,
websession: AsyncMock,
side_effect: Exception | None,
call_count: int,
call_interval: timedelta,
throttled: bool,
):
"""Test connectivity check throttled when checks succeed."""
coresys.supervisor.connectivity = None
websession.head.side_effect = side_effect

for _ in range(3):
job_decorator = get_job_decorator(Supervisor.check_connectivity)
job_decorator._last_call.clear()

Check warning on line 85 in tests/test_supervisor.py

View workflow job for this annotation

GitHub Actions / Check pylint

W0212: Access to a protected member _last_call of a client class (protected-access)

with (
travel(datetime.now(), tick=False) as traveller,
):
await coresys.supervisor.check_connectivity()
traveller.shift(call_interval)
await coresys.supervisor.check_connectivity()

assert websession.head.call_count == call_count
assert websession.head.call_count == (1 if throttled else 2)


async def test_update_failed(coresys: CoreSys, capture_exception: Mock):
Expand Down
15 changes: 15 additions & 0 deletions tests/utils/test_decorator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Helper for decorators."""

from supervisor.jobs.decorator import Job


def get_job_decorator(func):
"""Get Job object of decorated function."""
# Access the closure of the wrapper function
closure = func.__closure__
if closure:
for cell in closure:
obj = cell.cell_contents
if isinstance(obj, Job):
return obj
return None

0 comments on commit 86c97a3

Please sign in to comment.