Files
supervisor/tests/plugins/test_plugin_manager.py
Stefan Agner 2d12920b35 Stop refreshing the update information on outdated OS versions (#6098)
* Stop refreshing the update information on outdated OS versions

Add `JobCondition.OS_SUPPORTED` to the updater job to avoid
refreshing update information when the OS version is unsupported.

This effectively freezes installations on unsupported OS versions
and blocks Supervisor updates. Once deployed, this ensures that any
Supervisor will always run on at least the minimum supported OS
version.

This requires to move the OS version check before Supervisor updater
initialization to allow the `JobCondition.OS_SUPPORTED` to work
correctly.

* Run only OS version check in setup loads

Instead of running a full system evaluation, only run the OS version
check right after the OS manager is loaded. This allows the
updater job condition to work correctly without running the full
system evaluation, which is not needed at this point.

* Prevent Core and Add-on updates on unsupported OS versions

Also prevent Home Assistant Core and Add-on updates on unsupported OS
versions. We could imply `JobCondition.SUPERVISOR_UPDATED` whenever
OS is outdated, but this would also prevent the OS update itself. So
we need this separate condition everywhere where
`JobCondition.SUPERVISOR_UPDATED` is used except for OS updates.

It should also be safe to let the add-on store update, we simply
don't allow the add-on to be installed or updated if the OS is
outdated.

* Remove unnecessary Host info update

It seems that the CPE information are already loaded in the HostInfo
object. Remove the unnecessary update call.

* Fix pytest

* Delay refreshing of update data

Delay refreshing of update data until after setup phase. This allows to
use the JobCondition.OS_SUPPORTED safely. We still have to fetch the
updater data in case OS information is outdated. This typically happens
on device wipe.

Note also that plug-ins will automatically refresh updater data in case
it is missing the latest version information.

This will reverse the order of updates when there are new plug-in and
Supervisor update information available (e.g. on first startup):
Previously the updater data got refreshed before the plug-in started,
which caused them to update first. Then the Supervisor got update in
startup phase. Now the updater data gets refreshed in startup phase,
which then causes the Supervisor to update first before the plug-ins
get updated after Supervisor restart.

* Fix pytest

* Fix updater tests

* Add new tests to verify that updater reload is skipped

* Fix pylint

* Apply suggestions from code review

Co-authored-by: Mike Degatano <michael.degatano@gmail.com>

* Add debug message when we delay version fetch

---------

Co-authored-by: Mike Degatano <michael.degatano@gmail.com>
2025-08-22 11:09:56 +02:00

72 lines
2.2 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()
await coresys.updater.reload()
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