diff --git a/supervisor/api/__init__.py b/supervisor/api/__init__.py index 88baaeb36..cf56a354f 100644 --- a/supervisor/api/__init__.py +++ b/supervisor/api/__init__.py @@ -401,7 +401,7 @@ class RestAPI(CoreSysAttributes): async def get_supervisor_logs(*args, **kwargs): try: - return await self._api_host.advanced_logs( + return await self._api_host.advanced_logs_handler( *args, identifier="hassio_supervisor", **kwargs ) except Exception as err: # pylint: disable=broad-exception-caught diff --git a/supervisor/api/host.py b/supervisor/api/host.py index ee7b395e5..73a6c7464 100644 --- a/supervisor/api/host.py +++ b/supervisor/api/host.py @@ -1,4 +1,5 @@ """Init file for Supervisor host RESTful API.""" + import asyncio from contextlib import suppress import logging @@ -163,8 +164,7 @@ class APIHost(CoreSysAttributes): raise APIError() from err return possible_offset - @api_process_raw(CONTENT_TYPE_TEXT, error_type=CONTENT_TYPE_TEXT) - async def advanced_logs( + async def advanced_logs_handler( self, request: web.Request, identifier: str | None = None, follow: bool = False ) -> web.StreamResponse: """Return systemd-journald logs.""" @@ -218,3 +218,10 @@ class APIHost(CoreSysAttributes): "Connection reset when trying to fetch data from systemd-journald." ) from ex return response + + @api_process_raw(CONTENT_TYPE_TEXT, error_type=CONTENT_TYPE_TEXT) + async def advanced_logs( + self, request: web.Request, identifier: str | None = None, follow: bool = False + ) -> web.StreamResponse: + """Return systemd-journald logs. Wrapped as standard API handler.""" + return await self.advanced_logs_handler(request, identifier, follow) diff --git a/tests/api/test_supervisor.py b/tests/api/test_supervisor.py index 3573d4cff..86f69067f 100644 --- a/tests/api/test_supervisor.py +++ b/tests/api/test_supervisor.py @@ -6,7 +6,7 @@ from aiohttp.test_utils import TestClient import pytest from supervisor.coresys import CoreSys -from supervisor.exceptions import StoreGitError, StoreNotFound +from supervisor.exceptions import HassioError, StoreGitError, StoreNotFound from supervisor.store.repository import Repository from tests.api import common_test_api_advanced_logs @@ -160,7 +160,7 @@ async def test_api_supervisor_fallback( api_client: TestClient, journald_logs: MagicMock, docker_logs: MagicMock ): """Check that supervisor logs read from container logs if reading from journald gateway fails badly.""" - journald_logs.side_effect = OSError("Something bad happened!") + journald_logs.side_effect = HassioError("Something bad happened!") with patch("supervisor.api._LOGGER.exception") as logger: resp = await api_client.get("/supervisor/logs") @@ -176,6 +176,19 @@ async def test_api_supervisor_fallback( b"\x1b[36m22-10-11 14:04:23 DEBUG (MainThread) [supervisor.utils.dbus] D-Bus call - org.freedesktop.DBus.Properties.call_get_all on /io/hass/os/AppArmor\x1b[0m", ] + journald_logs.reset_mock() + + # also check generic Python error + journald_logs.side_effect = OSError("Something bad happened!") + + with patch("supervisor.api._LOGGER.exception") as logger: + resp = await api_client.get("/supervisor/logs") + logger.assert_called_once_with( + "Failed to get supervisor logs using advanced_logs API" + ) + assert resp.status == 200 + assert resp.content_type == "text/plain" + async def test_api_supervisor_reload(api_client: TestClient): """Test supervisor reload."""