Skip to content

Commit

Permalink
Refactor testing of advanced logs endpoints to a common method
Browse files Browse the repository at this point in the history
  • Loading branch information
sairon committed Mar 25, 2024
1 parent acd471e commit c2f40c5
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 296 deletions.
65 changes: 65 additions & 0 deletions tests/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
"""Test for API calls."""
from unittest.mock import MagicMock

from aiohttp.test_utils import TestClient

from supervisor.host.const import LogFormat

DEFAULT_LOG_RANGE = "entries=:-100:"


async def common_test_api_advanced_logs(
path_prefix: str,
syslog_identifier: str,
api_client: TestClient,
journald_logs: MagicMock,
):
"""Template for tests of endpoints using advanced logs."""
resp = await api_client.get(f"{path_prefix}/logs")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": syslog_identifier},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

resp = await api_client.get(f"{path_prefix}/logs/follow")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": syslog_identifier, "follow": ""},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

resp = await api_client.get(f"{path_prefix}/logs/boots/0")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": syslog_identifier, "_BOOT_ID": "ccc"},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

resp = await api_client.get(f"{path_prefix}/logs/boots/0/follow")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={
"SYSLOG_IDENTIFIER": syslog_identifier,
"_BOOT_ID": "ccc",
"follow": "",
},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)
54 changes: 3 additions & 51 deletions tests/api/test_addons.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
from supervisor.docker.addon import DockerAddon
from supervisor.docker.const import ContainerState
from supervisor.docker.monitor import DockerContainerStateEvent
from supervisor.host.const import LogFormat
from supervisor.store.repository import Repository

from ..const import TEST_ADDON_SLUG

DEFAULT_LOG_RANGE = "entries=:-100:"
from . import common_test_api_advanced_logs


def _create_test_event(name: str, state: ContainerState) -> DockerContainerStateEvent:
Expand Down Expand Up @@ -73,54 +71,8 @@ async def test_api_addon_logs(
api_client: TestClient, journald_logs: MagicMock, install_addon_ssh: Addon
):
"""Test addon logs."""
resp = await api_client.get("/addons/local_ssh/logs")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": "addon_local_ssh"},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

resp = await api_client.get("/addons/local_ssh/logs/follow")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": "addon_local_ssh", "follow": ""},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

resp = await api_client.get("/addons/local_ssh/logs/boots/0")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": "addon_local_ssh", "_BOOT_ID": "ccc"},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

resp = await api_client.get("/addons/local_ssh/logs/boots/0/follow")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={
"SYSLOG_IDENTIFIER": "addon_local_ssh",
"_BOOT_ID": "ccc",
"follow": "",
},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
await common_test_api_advanced_logs(
"/addons/local_ssh", "addon_local_ssh", api_client, journald_logs
)


Expand Down
50 changes: 3 additions & 47 deletions tests/api/test_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,11 @@

from aiohttp.test_utils import TestClient

from supervisor.host.const import LogFormat

DEFAULT_LOG_RANGE = "entries=:-100:"
from tests.api import common_test_api_advanced_logs


async def test_api_audio_logs(api_client: TestClient, journald_logs: MagicMock):
"""Test audio logs."""
resp = await api_client.get("/audio/logs")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": "hassio_audio"},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

resp = await api_client.get("/audio/logs/follow")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": "hassio_audio", "follow": ""},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

resp = await api_client.get("/audio/logs/boots/0")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": "hassio_audio", "_BOOT_ID": "ccc"},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

resp = await api_client.get("/audio/logs/boots/0/follow")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": "hassio_audio", "_BOOT_ID": "ccc", "follow": ""},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
await common_test_api_advanced_logs(
"/audio", "hassio_audio", api_client, journald_logs
)
50 changes: 2 additions & 48 deletions tests/api/test_dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@

from supervisor.coresys import CoreSys
from supervisor.dbus.resolved import Resolved
from supervisor.host.const import LogFormat

from tests.api import common_test_api_advanced_logs
from tests.dbus_service_mocks.base import DBusServiceMock
from tests.dbus_service_mocks.resolved import Resolved as ResolvedService

DEFAULT_LOG_RANGE = "entries=:-100:"


async def test_llmnr_mdns_info(
api_client: TestClient,
Expand Down Expand Up @@ -69,48 +67,4 @@ async def test_options(api_client: TestClient, coresys: CoreSys):

async def test_api_dns_logs(api_client: TestClient, journald_logs: MagicMock):
"""Test dns logs."""
resp = await api_client.get("/dns/logs")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": "hassio_dns"},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

resp = await api_client.get("/dns/logs/follow")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": "hassio_dns", "follow": ""},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

resp = await api_client.get("/dns/logs/boots/0")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": "hassio_dns", "_BOOT_ID": "ccc"},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

resp = await api_client.get("/dns/logs/boots/0/follow")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": "hassio_dns", "_BOOT_ID": "ccc", "follow": ""},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)
await common_test_api_advanced_logs("/dns", "hassio_dns", api_client, journald_logs)
59 changes: 6 additions & 53 deletions tests/api/test_homeassistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,21 @@

from supervisor.coresys import CoreSys
from supervisor.homeassistant.module import HomeAssistant
from supervisor.host.const import LogFormat

from tests.api import common_test_api_advanced_logs
from tests.common import load_json_fixture

DEFAULT_LOG_RANGE = "entries=:-100:"


@pytest.mark.parametrize("legacy_route", [True, False])
async def test_api_core_logs(
api_client: TestClient, journald_logs: MagicMock, legacy_route: bool
):
"""Test core logs."""
resp = await api_client.get(f"/{'homeassistant' if legacy_route else 'core'}/logs")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": "homeassistant"},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

resp = await api_client.get(
f"/{'homeassistant' if legacy_route else 'core'}/logs/follow"
)
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": "homeassistant", "follow": ""},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

resp = await api_client.get(
f"/{'homeassistant' if legacy_route else 'core'}/logs/boots/0"
)
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": "homeassistant", "_BOOT_ID": "ccc"},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

resp = await api_client.get(
f"/{'homeassistant' if legacy_route else 'core'}/logs/boots/0/follow"
)
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": "homeassistant", "_BOOT_ID": "ccc", "follow": ""},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
await common_test_api_advanced_logs(
f"/{'homeassistant' if legacy_route else 'core'}",
"homeassistant",
api_client,
journald_logs,
)


Expand Down
54 changes: 3 additions & 51 deletions tests/api/test_multicast.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,11 @@

from aiohttp.test_utils import TestClient

from supervisor.host.const import LogFormat

DEFAULT_LOG_RANGE = "entries=:-100:"
from tests.api import common_test_api_advanced_logs


async def test_api_multicast_logs(api_client: TestClient, journald_logs: MagicMock):
"""Test multicast logs."""
resp = await api_client.get("/multicast/logs")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": "hassio_multicast"},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

resp = await api_client.get("/multicast/logs/follow")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": "hassio_multicast", "follow": ""},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

resp = await api_client.get("/multicast/logs/boots/0")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": "hassio_multicast", "_BOOT_ID": "ccc"},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

resp = await api_client.get("/multicast/logs/boots/0/follow")
assert resp.status == 200
assert resp.content_type == "text/plain"

journald_logs.assert_called_once_with(
params={
"SYSLOG_IDENTIFIER": "hassio_multicast",
"_BOOT_ID": "ccc",
"follow": "",
},
range_header=DEFAULT_LOG_RANGE,
accept=LogFormat.JOURNAL,
await common_test_api_advanced_logs(
"/multicast", "hassio_multicast", api_client, journald_logs
)
Loading

0 comments on commit c2f40c5

Please sign in to comment.