mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-11-15 14:00:27 +00:00
* Add endpoint for complete logs of the latest container startup Add endpoint that returns complete logs of the latest startup of container, which can be used for downloading Core logs in the frontend. Realtime filtering header is used for the Journal API and StartedAt parameter from the Docker API is used as the reference point. This means that any other Range header is ignored for this parameter, yet the "lines" query argument can be used to limit the number of lines. By default "infinite" number of lines is returned. Closes #6147 * Implement fallback for latest logs for OS older than 16.0 Implement fallback which uses the internal CONTAINER_LOG_EPOCH metadata added to logs created by the Docker logger. Still prefer the time-based method, as it has lower overhead and using public APIs. * Address review comments * Only use CONTAINER_LOG_EPOCH for latest logs As pointed out in the review comments, we might not be able to get the StartedAt for add-ons that are not running. Thus we need to use the only reliable mechanism available now, which is the container log epoch. * Remove dead code for 'Range: realtime' header handling
96 lines
3.0 KiB
Python
96 lines
3.0 KiB
Python
"""Test for API calls."""
|
|
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
from aiohttp.test_utils import TestClient
|
|
|
|
from supervisor.coresys import CoreSys
|
|
from supervisor.host.const import LogFormat
|
|
|
|
DEFAULT_LOG_RANGE = "entries=:-99:100"
|
|
DEFAULT_LOG_RANGE_FOLLOW = "entries=:-99:18446744073709551615"
|
|
|
|
|
|
async def common_test_api_advanced_logs(
|
|
path_prefix: str,
|
|
syslog_identifier: str,
|
|
api_client: TestClient,
|
|
journald_logs: MagicMock,
|
|
coresys: CoreSys,
|
|
os_available: None,
|
|
):
|
|
"""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_FOLLOW,
|
|
accept=LogFormat.JOURNAL,
|
|
)
|
|
|
|
journald_logs.reset_mock()
|
|
|
|
mock_response = MagicMock()
|
|
mock_response.text = AsyncMock(
|
|
return_value='{"CONTAINER_LOG_EPOCH": "12345"}\n{"CONTAINER_LOG_EPOCH": "12345"}\n'
|
|
)
|
|
journald_logs.return_value.__aenter__.return_value = mock_response
|
|
|
|
resp = await api_client.get(f"{path_prefix}/logs/latest")
|
|
assert resp.status == 200
|
|
|
|
assert journald_logs.call_count == 2
|
|
|
|
# Check the first call for getting epoch
|
|
epoch_call = journald_logs.call_args_list[0]
|
|
assert epoch_call[1]["params"] == {"CONTAINER_NAME": syslog_identifier}
|
|
assert epoch_call[1]["range_header"] == "entries=:-1:2"
|
|
|
|
# Check the second call for getting logs with the epoch
|
|
logs_call = journald_logs.call_args_list[1]
|
|
assert logs_call[1]["params"]["SYSLOG_IDENTIFIER"] == syslog_identifier
|
|
assert logs_call[1]["params"]["CONTAINER_LOG_EPOCH"] == "12345"
|
|
assert logs_call[1]["range_header"] == "entries=0:18446744073709551615"
|
|
|
|
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_FOLLOW,
|
|
accept=LogFormat.JOURNAL,
|
|
)
|