Supervisor updated before addon repositories (#3795)

This commit is contained in:
Mike Degatano 2022-08-17 01:28:06 -04:00 committed by GitHub
parent e6dfe83d62
commit 5fc9484f73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 5 deletions

View File

@ -59,7 +59,7 @@ class Tasks(CoreSysAttributes):
self.sys_scheduler.register_task(self._update_observer, RUN_UPDATE_OBSERVER)
# Reload
self.sys_scheduler.register_task(self.sys_store.reload, RUN_RELOAD_ADDONS)
self.sys_scheduler.register_task(self._reload_store, RUN_RELOAD_ADDONS)
self.sys_scheduler.register_task(self.sys_updater.reload, RUN_RELOAD_UPDATER)
self.sys_scheduler.register_task(self.sys_backups.reload, RUN_RELOAD_BACKUPS)
self.sys_scheduler.register_task(self.sys_host.reload, RUN_RELOAD_HOST)
@ -316,3 +316,8 @@ class Tasks(CoreSysAttributes):
await self.sys_host.network.check_connectivity()
finally:
self._cache["connectivity"] = 0
@Job(conditions=[JobCondition.SUPERVISOR_UPDATED])
async def _reload_store(self) -> None:
"""Reload store and check for addon updates."""
await self.sys_store.reload()

View File

@ -80,6 +80,7 @@ class StoreManager(CoreSysAttributes, FileConfiguration):
self._data[ATTR_REPOSITORIES], add_with_errors=True
)
@Job(conditions=[JobCondition.SUPERVISOR_UPDATED], on_condition=StoreJobError)
async def reload(self) -> None:
"""Update add-ons from repository and reload list."""
tasks = [repository.update() for repository in self.all]
@ -90,7 +91,10 @@ class StoreManager(CoreSysAttributes, FileConfiguration):
await self.load()
self._read_addons()
@Job(conditions=[JobCondition.INTERNET_SYSTEM])
@Job(
conditions=[JobCondition.INTERNET_SYSTEM, JobCondition.SUPERVISOR_UPDATED],
on_condition=StoreJobError,
)
async def add_repository(
self, url: str, *, persist: bool = True, add_with_errors: bool = False
):
@ -194,7 +198,6 @@ class StoreManager(CoreSysAttributes, FileConfiguration):
await self.data.update()
self._read_addons()
@Job(conditions=[JobCondition.INTERNET_SYSTEM])
async def update_repositories(
self,
list_repositories: list[str],

View File

@ -1,6 +1,6 @@
"""Test add custom repository."""
import json
from unittest.mock import patch
from unittest.mock import PropertyMock, patch
import pytest
@ -10,6 +10,7 @@ from supervisor.exceptions import (
StoreError,
StoreGitCloneError,
StoreGitError,
StoreJobError,
StoreNotFound,
)
from supervisor.resolution.const import SuggestionType
@ -287,3 +288,22 @@ async def test_add_with_update_repositories(
assert repository.source in coresys.store.repository_urls
assert "http://example.com" in coresys.store.repository_urls
@pytest.mark.parametrize("use_update", [True, False])
async def test_add_repository_fails_if_out_of_date(
coresys: CoreSys, store_manager: StoreManager, use_update: bool
):
"""Test adding a repository fails when supervisor not updated."""
with patch.object(
type(coresys.supervisor), "need_update", new=PropertyMock(return_value=True)
), pytest.raises(StoreJobError):
if use_update:
await store_manager.update_repositories(
coresys.store.repository_urls + ["http://example.com"],
add_with_errors=True,
)
else:
await store_manager.add_repository(
"http://example.com", add_with_errors=True
)

View File

@ -1,9 +1,12 @@
"""Test store manager."""
from unittest.mock import patch
from unittest.mock import PropertyMock, patch
import pytest
from supervisor.bootstrap import migrate_system_env
from supervisor.const import ATTR_ADDONS_CUSTOM_LIST
from supervisor.coresys import CoreSys
from supervisor.exceptions import StoreJobError
from supervisor.store import StoreManager
from supervisor.store.repository import Repository
@ -100,3 +103,11 @@ async def test_load_from_core_config(coresys: CoreSys):
coresys.config.save_data.assert_called_once()
coresys.store.save_data.assert_called_once()
async def test_reload_fails_if_out_of_date(coresys: CoreSys):
"""Test reload fails when supervisor not updated."""
with patch.object(
type(coresys.supervisor), "need_update", new=PropertyMock(return_value=True)
), pytest.raises(StoreJobError):
await coresys.store.reload()