mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-12-26 21:47:15 +00:00
Add enhanced logging REST endpoints using systemd-journal-gatewayd (#3291)
* Add enhanced logging REST endpoints using systemd-journal-gatewayd
Add /host/logs/entries and /host/logs/{identifier}/entries to expose log
entries from systemd-journald running on the host. Use
systemd-journal-gatewayd which exposes the logs to the Supervisor via
Unix socket.
Current two query string parameters are allowed: "boot" and "follow".
The first will only return logs since last boot. The second will keep
the HTTP request open and send new log entries as they get added to the
systemd-journal.
* Allow Range header
Forward the Range header to systemd-journal-gatewayd. This allows to
select only a certain amount of log data. The Range header is a standard
header to select only partial amount of data. However, the "entries="
prefix is custom for systemd-journal-gatewayd, denoting that the numbers
following represent log entries (as opposed to bytes or other metrics).
* Avoid connecting if systemd-journal-gatewayd is not available
* Use path for all options
* Add pytests
* Address pylint issues
* Boot ID offsets and slug to identifier
* Fix tests
* API refactor from feedback
* fix tests and add identifiers
* stop isort and pylint fighting
* fix tests
* Update default log identifiers
* Only modify /host/logs endpoints
* Fix bad import
* Load log caches asynchronously at startup
* Allow task to complete in fixture
* Boot IDs and identifiers loaded on demand
* Add suggested identifiers
* Fix tests around boot ids
Co-authored-by: Mike Degatano <michael.degatano@gmail.com>
This commit is contained in:
@@ -7,6 +7,7 @@ from unittest.mock import AsyncMock, MagicMock, PropertyMock, patch
|
||||
from uuid import uuid4
|
||||
|
||||
from aiohttp import web
|
||||
from aiohttp.test_utils import TestClient
|
||||
from awesomeversion import AwesomeVersion
|
||||
from dbus_fast.aio.message_bus import MessageBus
|
||||
from dbus_fast.aio.proxy_object import ProxyInterface, ProxyObject
|
||||
@@ -50,12 +51,19 @@ from supervisor.dbus.systemd import Systemd
|
||||
from supervisor.dbus.timedate import TimeDate
|
||||
from supervisor.docker.manager import DockerAPI
|
||||
from supervisor.docker.monitor import DockerMonitor
|
||||
from supervisor.host.logs import LogsControl
|
||||
from supervisor.store.addon import AddonStore
|
||||
from supervisor.store.repository import Repository
|
||||
from supervisor.utils.dbus import DBUS_INTERFACE_PROPERTIES, DBus
|
||||
from supervisor.utils.dt import utcnow
|
||||
|
||||
from .common import exists_fixture, get_dbus_name, load_fixture, load_json_fixture
|
||||
from .common import (
|
||||
exists_fixture,
|
||||
get_dbus_name,
|
||||
load_binary_fixture,
|
||||
load_fixture,
|
||||
load_json_fixture,
|
||||
)
|
||||
from .const import TEST_ADDON_SLUG
|
||||
|
||||
# pylint: disable=redefined-outer-name, protected-access
|
||||
@@ -358,6 +366,18 @@ async def coresys(
|
||||
await coresys_obj.websession.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def journald_gateway() -> MagicMock:
|
||||
"""Mock logs control."""
|
||||
with patch("supervisor.host.logs.Path.is_socket", return_value=True), patch(
|
||||
"supervisor.host.logs.ClientSession.get"
|
||||
) as get:
|
||||
get.return_value.__aenter__.return_value.text = AsyncMock(
|
||||
return_value=load_fixture("logs_host.txt")
|
||||
)
|
||||
yield get
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sys_machine():
|
||||
"""Mock sys_machine."""
|
||||
@@ -376,7 +396,7 @@ def sys_supervisor():
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def api_client(aiohttp_client, coresys: CoreSys):
|
||||
async def api_client(aiohttp_client, coresys: CoreSys) -> TestClient:
|
||||
"""Fixture for RestAPI client."""
|
||||
|
||||
@web.middleware
|
||||
@@ -388,7 +408,9 @@ async def api_client(aiohttp_client, coresys: CoreSys):
|
||||
api = RestAPI(coresys)
|
||||
api.webapp = web.Application(middlewares=[_security_middleware])
|
||||
api.start = AsyncMock()
|
||||
await api.load()
|
||||
with patch("supervisor.docker.supervisor.os") as os:
|
||||
os.environ = {"SUPERVISOR_NAME": "hassio_supervisor"}
|
||||
await api.load()
|
||||
yield await aiohttp_client(api.webapp)
|
||||
|
||||
|
||||
@@ -528,3 +550,34 @@ async def backups(
|
||||
coresys.backups._backups[backup.slug] = backup
|
||||
|
||||
yield coresys.backups.list_backups
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def journald_logs(coresys: CoreSys) -> MagicMock:
|
||||
"""Mock journald logs and make it available."""
|
||||
with patch.object(
|
||||
LogsControl, "available", new=PropertyMock(return_value=True)
|
||||
), patch.object(
|
||||
LogsControl, "get_boot_ids", return_value=["aaa", "bbb", "ccc"]
|
||||
), patch.object(
|
||||
LogsControl,
|
||||
"get_identifiers",
|
||||
return_value=["hassio_supervisor", "hassos-config", "kernel"],
|
||||
), patch.object(
|
||||
LogsControl, "journald_logs", new=MagicMock()
|
||||
) as logs:
|
||||
await coresys.host.logs.load()
|
||||
yield logs
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def docker_logs(docker: DockerAPI) -> MagicMock:
|
||||
"""Mock log output for a container from docker."""
|
||||
container_mock = MagicMock()
|
||||
container_mock.logs.return_value = load_binary_fixture("logs_docker_container.txt")
|
||||
docker.containers.get.return_value = container_mock
|
||||
|
||||
with patch("supervisor.docker.supervisor.os") as os:
|
||||
os.environ = {"SUPERVISOR_NAME": "hassio_supervisor"}
|
||||
|
||||
yield container_mock.logs
|
||||
|
||||
Reference in New Issue
Block a user