Fix recorder system health when the db_url is lacking a hostname (#72612)

This commit is contained in:
J. Nick Koston 2022-05-27 07:32:26 -10:00 committed by GitHub
parent a733b92389
commit 040e120101
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 3 deletions

View File

@ -2,8 +2,7 @@
from __future__ import annotations
from typing import Any
from yarl import URL
from urllib.parse import urlparse
from homeassistant.components import system_health
from homeassistant.components.recorder.core import Recorder
@ -60,7 +59,7 @@ async def system_health_info(hass: HomeAssistant) -> dict[str, Any]:
instance = get_instance(hass)
run_history = instance.run_history
database_name = URL(instance.db_url).path.lstrip("/")
database_name = urlparse(instance.db_url).path.lstrip("/")
db_engine_info = _async_get_db_engine_info(instance)
db_stats: dict[str, Any] = {}

View File

@ -53,6 +53,37 @@ async def test_recorder_system_health_alternate_dbms(hass, recorder_mock, dialec
}
@pytest.mark.parametrize(
"dialect_name", [SupportedDialect.MYSQL, SupportedDialect.POSTGRESQL]
)
async def test_recorder_system_health_db_url_missing_host(
hass, recorder_mock, dialect_name
):
"""Test recorder system health with a db_url without a hostname."""
assert await async_setup_component(hass, "system_health", {})
await async_wait_recording_done(hass)
instance = get_instance(hass)
with patch(
"homeassistant.components.recorder.core.Recorder.dialect_name", dialect_name
), patch.object(
instance,
"db_url",
"postgresql://homeassistant:blabla@/home_assistant?host=/config/socket",
), patch(
"sqlalchemy.orm.session.Session.execute",
return_value=Mock(first=Mock(return_value=("1048576",))),
):
info = await get_system_health_info(hass, "recorder")
assert info == {
"current_recorder_run": instance.run_history.current.start,
"oldest_recorder_run": instance.run_history.first.start,
"estimated_db_size": "1.00 MiB",
"database_engine": dialect_name.value,
"database_version": ANY,
}
async def test_recorder_system_health_crashed_recorder_runs_table(
hass: HomeAssistant, async_setup_recorder_instance: SetupRecorderInstanceT
):