Skip to content

Commit

Permalink
List discovery only includes healthy addons (#4451)
Browse files Browse the repository at this point in the history
Co-authored-by: Stefan Agner <[email protected]>
  • Loading branch information
mdegat01 and agners authored Jul 22, 2023
1 parent 1691f0e commit e60af93
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 16 deletions.
25 changes: 13 additions & 12 deletions supervisor/api/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ATTR_SERVICES,
ATTR_UUID,
REQUEST_FROM,
AddonState,
)
from ..coresys import CoreSysAttributes
from ..discovery.validate import valid_discovery_service
Expand Down Expand Up @@ -41,19 +42,19 @@ def _extract_message(self, request):
@api_process
@require_home_assistant
async def list(self, request):
"""Show register services."""

"""Show registered and available services."""
# Get available discovery
discovery = []
for message in self.sys_discovery.list_messages:
discovery.append(
{
ATTR_ADDON: message.addon,
ATTR_SERVICE: message.service,
ATTR_UUID: message.uuid,
ATTR_CONFIG: message.config,
}
)
discovery = [
{
ATTR_ADDON: message.addon,
ATTR_SERVICE: message.service,
ATTR_UUID: message.uuid,
ATTR_CONFIG: message.config,
}
for message in self.sys_discovery.list_messages
if (addon := self.sys_addons.get(message.addon, local_only=True))
and addon.state == AddonState.STARTED
]

# Get available services/add-ons
services = {}
Expand Down
54 changes: 50 additions & 4 deletions tests/api/test_discovery.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
"""Test discovery API."""

import logging
from unittest.mock import MagicMock, patch
from unittest.mock import ANY, MagicMock, patch
from uuid import uuid4

from aiohttp.test_utils import TestClient
import pytest

from supervisor.addons.addon import Addon
from supervisor.discovery import Discovery
from supervisor.const import AddonState
from supervisor.coresys import CoreSys
from supervisor.discovery import Discovery, Message

from tests.common import load_json_fixture


@pytest.mark.parametrize("api_client", ["local_ssh"], indirect=True)
async def test_discovery_forbidden(
async def test_api_discovery_forbidden(
api_client: TestClient, caplog: pytest.LogCaptureFixture, install_addon_ssh
):
"""Test addon sending discovery message for an unregistered service."""
Expand All @@ -32,7 +36,7 @@ async def test_discovery_forbidden(


@pytest.mark.parametrize("api_client", ["local_ssh"], indirect=True)
async def test_discovery_unknown_service(
async def test_api_discovery_unknown_service(
api_client: TestClient, caplog: pytest.LogCaptureFixture, install_addon_ssh: Addon
):
"""Test addon sending discovery message for an unkown service."""
Expand All @@ -51,3 +55,45 @@ async def test_discovery_unknown_service(
result = await resp.json()
assert result["data"]["uuid"] == message.uuid
assert "Please report this to the maintainer of the add-on" in caplog.text


@pytest.mark.parametrize(
"skip_state", [AddonState.ERROR, AddonState.STOPPED, AddonState.STARTUP]
)
async def test_api_list_discovery(
api_client: TestClient,
coresys: CoreSys,
install_addon_ssh: Addon,
skip_state: AddonState,
):
"""Test listing discovery messages only returns ones for healthy services."""
with patch(
"supervisor.utils.common.read_json_or_yaml_file",
return_value=load_json_fixture("discovery.json"),
), patch("supervisor.utils.common.Path.is_file", return_value=True):
coresys.discovery.read_data()

await coresys.discovery.load()
assert coresys.discovery.list_messages == [
Message(addon="core_mosquitto", service="mqtt", config=ANY, uuid=ANY),
Message(addon="local_ssh", service="adguard", config=ANY, uuid=ANY),
]

install_addon_ssh.state = AddonState.STARTED
resp = await api_client.get("/discovery")
assert resp.status == 200
result = await resp.json()
assert result["data"]["discovery"] == [
{
"addon": "local_ssh",
"service": "adguard",
"config": ANY,
"uuid": ANY,
}
]

install_addon_ssh.state = skip_state
resp = await api_client.get("/discovery")
assert resp.status == 200
result = await resp.json()
assert result["data"]["discovery"] == []
26 changes: 26 additions & 0 deletions tests/fixtures/discovery.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"discovery": [
{
"addon": "core_mosquitto",
"service": "mqtt",
"config": {
"host": "core-mosquitto",
"port": 1883,
"ssl": false,
"protocol": "3.1.1",
"username": "homeassistant",
"password": "password"
},
"uuid": "0c83a27125fe4421b58ea28eef5834c5"
},
{
"addon": "local_ssh",
"service": "adguard",
"config": {
"host": "127.0.0.1",
"port": 45158
},
"uuid": "0658be435a2948ec8b4d706d6708c56e"
}
]
}

0 comments on commit e60af93

Please sign in to comment.