mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-06-15 06:26:29 +00:00

* Recreate aiohttp ClientSession after DNS plug-in load Create a temporary ClientSession early in case we need to load version information from the internet. This doesn't use the final DNS setup and hence might fail to load in certain situations since we don't have the fallback mechanims in place yet. But if the DNS container image is present, we'll continue the setup and load the DNS plug-in. We then can recreate the ClientSession such that it uses the DNS plug-in. This works around an issue with aiodns, which today doesn't reload `resolv.conf` automatically when it changes. This lead to Supervisor using the initial `resolv.conf` as created by Docker. It meant that we did not use the DNS plug-in (and its fallback capabilities) in Supervisor. Also it meant that changes to the DNS setup at runtime did not propagate to the aiohttp ClientSession (as observed in #5332). * Mock aiohttp.ClientSession for all tests Currently in several places pytest actually uses the aiohttp ClientSession and reaches out to the internet. This is not ideal for unit tests and should be avoided. This creates several new fixtures to aid this effort: The `websession` fixture simply returns a mocked aiohttp.ClientSession, which can be used whenever a function is tested which needs the global websession. A separate new fixture to mock the connectivity check named `supervisor_internet` since this is often used through the Job decorator which require INTERNET_SYSTEM. And the `mock_update_data` uses the already existing update json test data from the fixture directory instead of loading the data from the internet. * Log ClientSession nameserver information When recreating the aiohttp ClientSession, log information what nameservers exactly are going to be used. * Refuse ClientSession initialization when API is available Previous attempts to reinitialize the ClientSession have shown use of the ClientSession after it was closed due to API requets being handled in parallel to the reinitialization (see #5851). Make sure this is not possible by refusing to reinitialize the ClientSession when the API is available. * Fix pytests Also sure we don't create aiohttp ClientSession objects unnecessarily. * Apply suggestions from code review Co-authored-by: Jan Čermák <sairon@users.noreply.github.com> --------- Co-authored-by: Jan Čermák <sairon@users.noreply.github.com>
71 lines
2.1 KiB
Python
71 lines
2.1 KiB
Python
"""Test plugin manager."""
|
|
|
|
from unittest.mock import AsyncMock, PropertyMock, patch
|
|
|
|
from awesomeversion import AwesomeVersion
|
|
import pytest
|
|
|
|
from supervisor.coresys import CoreSys
|
|
from supervisor.docker.interface import DockerInterface
|
|
from supervisor.plugins.base import PluginBase
|
|
from supervisor.supervisor import Supervisor
|
|
|
|
from tests.common import MockResponse
|
|
|
|
|
|
def mock_awaitable_bool(value: bool):
|
|
"""Return a mock of an awaitable bool."""
|
|
|
|
async def _mock_bool(*args, **kwargs) -> bool:
|
|
return value
|
|
|
|
return _mock_bool
|
|
|
|
|
|
async def test_repair(coresys: CoreSys):
|
|
"""Test repair."""
|
|
with patch.object(DockerInterface, "install") as install:
|
|
# If instance exists, repair does nothing
|
|
with patch.object(DockerInterface, "exists", new=mock_awaitable_bool(True)):
|
|
await coresys.plugins.repair()
|
|
|
|
install.assert_not_called()
|
|
|
|
# If not, repair installs the image
|
|
with patch.object(DockerInterface, "exists", new=mock_awaitable_bool(False)):
|
|
await coresys.plugins.repair()
|
|
|
|
assert install.call_count == len(coresys.plugins.all_plugins)
|
|
|
|
|
|
@pytest.mark.usefixtures("no_job_throttle")
|
|
async def test_load(
|
|
coresys: CoreSys, mock_update_data: MockResponse, supervisor_internet: AsyncMock
|
|
):
|
|
"""Test plugin manager load."""
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
await coresys.updater.load()
|
|
|
|
need_update = PropertyMock(return_value=True)
|
|
with (
|
|
patch.object(DockerInterface, "attach") as attach,
|
|
patch.object(DockerInterface, "update") as update,
|
|
patch.object(Supervisor, "need_update", new=need_update),
|
|
patch.object(PluginBase, "need_update", new=PropertyMock(return_value=True)),
|
|
patch.object(
|
|
PluginBase,
|
|
"version",
|
|
new=PropertyMock(return_value=AwesomeVersion("1970-01-01")),
|
|
),
|
|
):
|
|
await coresys.plugins.load()
|
|
|
|
assert attach.call_count == 5
|
|
update.assert_not_called()
|
|
|
|
need_update.return_value = False
|
|
await coresys.plugins.load()
|
|
|
|
assert attach.call_count == 10
|
|
assert update.call_count == 5
|