mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-07 17:26:32 +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>
64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
"""Testing handling with CoreState."""
|
|
|
|
from datetime import timedelta
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from aiohttp.hdrs import USER_AGENT
|
|
import pytest
|
|
|
|
from supervisor.const import CoreState
|
|
from supervisor.coresys import CoreSys
|
|
from supervisor.dbus.timedate import TimeDate
|
|
from supervisor.utils.dt import utcnow
|
|
|
|
|
|
async def test_timezone(coresys: CoreSys):
|
|
"""Test write corestate to /run/supervisor."""
|
|
# pylint: disable=protected-access
|
|
coresys.host.sys_dbus._timedate = TimeDate()
|
|
# pylint: enable=protected-access
|
|
|
|
assert coresys.timezone == "UTC"
|
|
assert coresys.config.timezone is None
|
|
|
|
await coresys.dbus.timedate.connect(coresys.dbus.bus)
|
|
assert coresys.timezone == "Etc/UTC"
|
|
|
|
await coresys.config.set_timezone("Europe/Zurich")
|
|
assert coresys.timezone == "Europe/Zurich"
|
|
|
|
|
|
async def test_now(coresys: CoreSys):
|
|
"""Test datetime now with local time."""
|
|
await coresys.config.set_timezone("Europe/Zurich")
|
|
|
|
zurich = coresys.now()
|
|
utc = utcnow()
|
|
|
|
assert zurich != utc
|
|
assert zurich - utc <= timedelta(hours=2)
|
|
|
|
|
|
@pytest.mark.no_mock_init_websession
|
|
async def test_custom_user_agent(coresys: CoreSys):
|
|
"""Test custom useragent."""
|
|
with patch(
|
|
"supervisor.coresys.aiohttp.ClientSession", return_value=MagicMock()
|
|
) as mock_session:
|
|
await coresys.init_websession()
|
|
assert (
|
|
"HomeAssistantSupervisor/9999.09.9.dev9999"
|
|
in mock_session.call_args_list[0][1]["headers"][USER_AGENT]
|
|
)
|
|
|
|
|
|
@pytest.mark.no_mock_init_websession
|
|
async def test_no_init_when_api_running(coresys: CoreSys):
|
|
"""Test ClientSession reinitialization is refused when API is running."""
|
|
with patch("supervisor.coresys.aiohttp.ClientSession"):
|
|
await coresys.init_websession()
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
# Reinitialize websession should not be possible while running
|
|
with pytest.raises(RuntimeError):
|
|
await coresys.init_websession()
|