1
0
mirror of https://github.com/home-assistant/supervisor.git synced 2025-12-24 12:29:08 +00:00

Recreate aiohttp ClientSession after DNS plug-in load (#5862)

* 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>
This commit is contained in:
Stefan Agner
2025-05-06 16:23:40 +02:00
committed by GitHub
parent 2e44e6494f
commit 85f8107b60
24 changed files with 256 additions and 102 deletions

View File

@@ -276,6 +276,7 @@ async def test_api_backup_restore_background(
backup_type: str,
options: dict[str, Any],
tmp_supervisor_data: Path,
supervisor_internet: AsyncMock,
):
"""Test background option on backup/restore APIs."""
await coresys.core.set_state(CoreState.RUNNING)
@@ -472,6 +473,7 @@ async def test_restore_immediate_errors(
api_client: TestClient,
coresys: CoreSys,
mock_partial_backup: Backup,
supervisor_internet: AsyncMock,
):
"""Test restore errors that return immediately even in background mode."""
await coresys.core.set_state(CoreState.RUNNING)
@@ -1010,6 +1012,7 @@ async def test_restore_backup_from_location(
coresys: CoreSys,
tmp_supervisor_data: Path,
local_location: str | None,
supervisor_internet: AsyncMock,
):
"""Test restoring a backup from a specific location."""
await coresys.core.set_state(CoreState.RUNNING)
@@ -1059,6 +1062,7 @@ async def test_restore_backup_from_location(
async def test_restore_backup_unencrypted_after_encrypted(
api_client: TestClient,
coresys: CoreSys,
supervisor_internet: AsyncMock,
):
"""Test restoring an unencrypted backup after an encrypted backup and vis-versa."""
enc_tar = copy(get_fixture_path("test_consolidate.tar"), coresys.config.path_backup)
@@ -1131,6 +1135,7 @@ async def test_restore_homeassistant_adds_env(
docker: DockerAPI,
backup_type: str,
postbody: dict[str, Any],
supervisor_internet: AsyncMock,
):
"""Test restoring home assistant from backup adds env to container."""
event = asyncio.Event()
@@ -1328,6 +1333,7 @@ async def test_missing_file_removes_location_from_cache(
url_path: str,
body: dict[str, Any] | None,
backup_file: str,
supervisor_internet: AsyncMock,
):
"""Test finding a missing file removes the location from cache."""
await coresys.core.set_state(CoreState.RUNNING)
@@ -1387,6 +1393,7 @@ async def test_missing_file_removes_backup_from_cache(
url_path: str,
body: dict[str, Any] | None,
backup_file: str,
supervisor_internet: AsyncMock,
):
"""Test finding a missing file removes the backup from cache if its the only one."""
await coresys.core.set_state(CoreState.RUNNING)
@@ -1412,7 +1419,9 @@ async def test_missing_file_removes_backup_from_cache(
@pytest.mark.usefixtures("tmp_supervisor_data")
async def test_immediate_list_after_missing_file_restore(
api_client: TestClient, coresys: CoreSys
api_client: TestClient,
coresys: CoreSys,
supervisor_internet: AsyncMock,
):
"""Test race with reload for missing file on restore does not error."""
await coresys.core.set_state(CoreState.RUNNING)