-
Notifications
You must be signed in to change notification settings - Fork 667
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retry run if image missing and handle fixup
- Loading branch information
Showing
11 changed files
with
183 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
"""Helper to fix missing image for addon.""" | ||
|
||
import logging | ||
|
||
from ...coresys import CoreSys | ||
from ..const import ContextType, IssueType, SuggestionType | ||
from .base import FixupBase | ||
|
||
_LOGGER: logging.Logger = logging.getLogger(__name__) | ||
|
||
|
||
def setup(coresys: CoreSys) -> FixupBase: | ||
"""Check setup function.""" | ||
return FixupAddonExecuteRepair(coresys) | ||
|
||
|
||
class FixupAddonExecuteRepair(FixupBase): | ||
"""Storage class for fixup.""" | ||
|
||
async def process_fixup(self, reference: str | None = None) -> None: | ||
"""Pull the addons image.""" | ||
addon = self.sys_addons.get(reference, local_only=True) | ||
if not addon: | ||
_LOGGER.info( | ||
"Cannot repair addon %s as it is not installed, dismissing suggestion", | ||
reference, | ||
) | ||
return | ||
|
||
if await addon.instance.exists(): | ||
_LOGGER.info( | ||
"Addon %s does not need repair, dismissing suggestion", reference | ||
) | ||
return | ||
|
||
_LOGGER.info("Installing image for addon %s") | ||
await addon.instance.install(addon.version) | ||
|
||
@property | ||
def suggestion(self) -> SuggestionType: | ||
"""Return a SuggestionType enum.""" | ||
return SuggestionType.EXECUTE_REPAIR | ||
|
||
@property | ||
def context(self) -> ContextType: | ||
"""Return a ContextType enum.""" | ||
return ContextType.ADDON | ||
|
||
@property | ||
def issues(self) -> list[IssueType]: | ||
"""Return a IssueType enum list.""" | ||
return [IssueType.MISSING_IMAGE] | ||
|
||
@property | ||
def auto(self) -> bool: | ||
"""Return if a fixup can be apply as auto fix.""" | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"""Test fixup core execute repair.""" | ||
|
||
from unittest.mock import MagicMock, patch | ||
|
||
from docker.errors import NotFound | ||
|
||
from supervisor.addons.addon import Addon | ||
from supervisor.coresys import CoreSys | ||
from supervisor.docker.addon import DockerAddon | ||
from supervisor.docker.interface import DockerInterface | ||
from supervisor.docker.manager import DockerAPI | ||
from supervisor.resolution.const import ContextType, IssueType, SuggestionType | ||
from supervisor.resolution.fixups.addon_execute_repair import FixupAddonExecuteRepair | ||
|
||
|
||
async def test_fixup(docker: DockerAPI, coresys: CoreSys, install_addon_ssh: Addon): | ||
"""Test fixup rebuilds addon's container.""" | ||
docker.images.get.side_effect = NotFound("missing") | ||
install_addon_ssh.data["image"] = "test_image" | ||
|
||
addon_execute_repair = FixupAddonExecuteRepair(coresys) | ||
assert addon_execute_repair.auto is True | ||
|
||
coresys.resolution.create_issue( | ||
IssueType.MISSING_IMAGE, | ||
ContextType.ADDON, | ||
reference="local_ssh", | ||
suggestions=[SuggestionType.EXECUTE_REPAIR], | ||
) | ||
with patch.object(DockerInterface, "install") as install: | ||
await addon_execute_repair() | ||
install.assert_called_once() | ||
|
||
assert not coresys.resolution.issues | ||
assert not coresys.resolution.suggestions | ||
|
||
|
||
async def test_fixup_no_addon(coresys: CoreSys): | ||
"""Test fixup dismisses if addon is missing.""" | ||
addon_execute_repair = FixupAddonExecuteRepair(coresys) | ||
assert addon_execute_repair.auto is True | ||
|
||
coresys.resolution.create_issue( | ||
IssueType.MISSING_IMAGE, | ||
ContextType.ADDON, | ||
reference="local_ssh", | ||
suggestions=[SuggestionType.EXECUTE_REPAIR], | ||
) | ||
|
||
with patch.object(DockerAddon, "install") as install: | ||
await addon_execute_repair() | ||
install.assert_not_called() | ||
|
||
|
||
async def test_fixup_image_exists( | ||
docker: DockerAPI, coresys: CoreSys, install_addon_ssh: Addon | ||
): | ||
"""Test fixup dismisses if image exists.""" | ||
docker.images.get.return_value = MagicMock() | ||
|
||
addon_execute_repair = FixupAddonExecuteRepair(coresys) | ||
assert addon_execute_repair.auto is True | ||
|
||
coresys.resolution.create_issue( | ||
IssueType.MISSING_IMAGE, | ||
ContextType.ADDON, | ||
reference="local_ssh", | ||
suggestions=[SuggestionType.EXECUTE_REPAIR], | ||
) | ||
|
||
with patch.object(DockerAddon, "install") as install: | ||
await addon_execute_repair() | ||
install.assert_not_called() |