Make host log endpoints cli friendly (#3974)

* Make host log endpoints cli friendly

* Remove blank identifiers
This commit is contained in:
Mike Degatano 2022-10-27 02:48:15 -04:00 committed by GitHub
parent 9affa5316c
commit 1df0a5db2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 14 deletions

View File

@ -16,6 +16,7 @@ ATTR_APPARMOR_VERSION = "apparmor_version"
ATTR_AGENT_VERSION = "agent_version"
ATTR_AVAILABLE_UPDATES = "available_updates"
ATTR_BOOT_TIMESTAMP = "boot_timestamp"
ATTR_BOOTS = "boots"
ATTR_BROADCAST_LLMNR = "broadcast_llmnr"
ATTR_BROADCAST_MDNS = "broadcast_mdns"
ATTR_DATA_DISK = "data_disk"
@ -23,6 +24,7 @@ ATTR_DEVICE = "device"
ATTR_DT_SYNCHRONIZED = "dt_synchronized"
ATTR_DT_UTC = "dt_utc"
ATTR_FALLBACK = "fallback"
ATTR_IDENTIFIERS = "identifiers"
ATTR_LLMNR = "llmnr"
ATTR_LLMNR_HOSTNAME = "llmnr_hostname"
ATTR_MDNS = "mdns"

View File

@ -33,10 +33,12 @@ from .const import (
ATTR_AGENT_VERSION,
ATTR_APPARMOR_VERSION,
ATTR_BOOT_TIMESTAMP,
ATTR_BOOTS,
ATTR_BROADCAST_LLMNR,
ATTR_BROADCAST_MDNS,
ATTR_DT_SYNCHRONIZED,
ATTR_DT_UTC,
ATTR_IDENTIFIERS,
ATTR_LLMNR_HOSTNAME,
ATTR_STARTUP_TIME,
ATTR_USE_NTP,
@ -130,13 +132,16 @@ class APIHost(CoreSysAttributes):
"""Return a list of boot IDs."""
boot_ids = await self.sys_host.logs.get_boot_ids()
return {
str(1 + i - len(boot_ids)): boot_id for i, boot_id in enumerate(boot_ids)
ATTR_BOOTS: {
str(1 + i - len(boot_ids)): boot_id
for i, boot_id in enumerate(boot_ids)
}
}
@api_process
async def list_identifiers(self, _: web.Request):
"""Return a list of syslog identifiers."""
return await self.sys_host.logs.get_identifiers()
return {ATTR_IDENTIFIERS: await self.sys_host.logs.get_identifiers()}
async def _get_boot_id(self, possible_offset: str) -> str:
"""Convert offset into boot ID if required."""

View File

@ -41,7 +41,6 @@ class LogsControl(CoreSysAttributes):
self.coresys: CoreSys = coresys
self._profiles: set[str] = set()
self._boot_ids: list[str] = []
self._identifiers: list[str] = []
self._default_identifiers: list[str] = []
@property
@ -54,11 +53,6 @@ class LogsControl(CoreSysAttributes):
"""Get boot IDs from oldest to newest."""
return self._boot_ids
@property
def identifiers(self) -> list[str]:
"""Get syslog identifiers."""
return self._identifiers
@property
def default_identifiers(self) -> list[str]:
"""Get default syslog identifiers."""
@ -120,7 +114,7 @@ class LogsControl(CoreSysAttributes):
path=f"/fields/{PARAM_SYSLOG_IDENTIFIER}",
timeout=ClientTimeout(total=20),
) as resp:
return (await resp.text()).split("\n")
return [i for i in (await resp.text()).split("\n") if i]
except (ClientError, TimeoutError) as err:
raise HostLogError(
"Could not get a list of syslog identifiers from systemd-journal-gatewayd",

View File

@ -128,14 +128,16 @@ async def test_api_boot_ids_info(api_client: TestClient, journald_logs: MagicMoc
"""Test getting boot IDs."""
resp = await api_client.get("/host/logs/boots")
result = await resp.json()
assert result["data"] == {"0": "ccc", "-1": "bbb", "-2": "aaa"}
assert result["data"] == {"boots": {"0": "ccc", "-1": "bbb", "-2": "aaa"}}
async def test_api_identifiers_info(api_client: TestClient, journald_logs: MagicMock):
"""Test getting syslog identifiers."""
resp = await api_client.get("/host/logs/identifiers")
result = await resp.json()
assert result["data"] == ["hassio_supervisor", "hassos-config", "kernel"]
assert result["data"] == {
"identifiers": ["hassio_supervisor", "hassos-config", "kernel"]
}
async def test_advanced_logs(

View File

@ -47,4 +47,4 @@ ghcr.io/home-assistant/odroid-n2-homeassistant:2022.10.0.dev20220919/homeassista
ghcr.io/home-assistant/aarch64-hassio-observer:2022.09.dev1101/hassio_observer
ghcr.io/home-assistant/aarch64-addon-example:1.2.0/addon_94cfad5a_example
ghcr.io/home-assistant/aarch64-hassio-audio:2022.05.0/hassio_audio
addon_local_ssh
addon_local_ssh

View File

@ -22,7 +22,6 @@ async def test_load(coresys: CoreSys):
await coresys.host.logs.load()
assert coresys.host.logs.boot_ids == []
assert coresys.host.logs.identifiers == []
# File is quite large so just check it loaded
for identifier in ["kernel", "os-agent", "systemd"]:
@ -79,6 +78,7 @@ async def test_identifiers(coresys: CoreSys, journald_gateway: MagicMock):
)
# Mock is large so just look for a few different types of identifiers
identifiers = await coresys.host.logs.get_identifiers()
for identifier in [
"addon_local_ssh",
"hassio_dns",
@ -86,4 +86,6 @@ async def test_identifiers(coresys: CoreSys, journald_gateway: MagicMock):
"kernel",
"os-agent",
]:
assert identifier in await coresys.host.logs.get_identifiers()
assert identifier in identifiers
assert "" not in identifiers