mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 14:27:07 +00:00
Check for errors when creating backups using supervisor (#137220)
* Check for errors when creating backups using supervisor * Improve error reporting when there's no backup reference
This commit is contained in:
parent
a41566611e
commit
58b7be7c2f
@ -354,6 +354,7 @@ class SupervisorBackupReaderWriter(BackupReaderWriter):
|
|||||||
"""Wait for a backup to complete."""
|
"""Wait for a backup to complete."""
|
||||||
backup_complete = asyncio.Event()
|
backup_complete = asyncio.Event()
|
||||||
backup_id: str | None = None
|
backup_id: str | None = None
|
||||||
|
create_errors: list[dict[str, str]] = []
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def on_job_progress(data: Mapping[str, Any]) -> None:
|
def on_job_progress(data: Mapping[str, Any]) -> None:
|
||||||
@ -361,6 +362,7 @@ class SupervisorBackupReaderWriter(BackupReaderWriter):
|
|||||||
nonlocal backup_id
|
nonlocal backup_id
|
||||||
if data.get("done") is True:
|
if data.get("done") is True:
|
||||||
backup_id = data.get("reference")
|
backup_id = data.get("reference")
|
||||||
|
create_errors.extend(data.get("errors", []))
|
||||||
backup_complete.set()
|
backup_complete.set()
|
||||||
|
|
||||||
unsub = self._async_listen_job_events(backup.job_id, on_job_progress)
|
unsub = self._async_listen_job_events(backup.job_id, on_job_progress)
|
||||||
@ -369,8 +371,11 @@ class SupervisorBackupReaderWriter(BackupReaderWriter):
|
|||||||
await backup_complete.wait()
|
await backup_complete.wait()
|
||||||
finally:
|
finally:
|
||||||
unsub()
|
unsub()
|
||||||
if not backup_id:
|
if not backup_id or create_errors:
|
||||||
raise BackupReaderWriterError("Backup failed")
|
# We should add more specific error handling here in the future
|
||||||
|
raise BackupReaderWriterError(
|
||||||
|
f"Backup failed: {create_errors or 'no backup_id'}"
|
||||||
|
)
|
||||||
|
|
||||||
async def open_backup() -> AsyncIterator[bytes]:
|
async def open_backup() -> AsyncIterator[bytes]:
|
||||||
try:
|
try:
|
||||||
|
@ -1360,11 +1360,40 @@ async def test_reader_writer_create_partial_backup_error(
|
|||||||
assert supervisor_client.backups.partial_backup.call_count == 1
|
assert supervisor_client.backups.partial_backup.call_count == 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"supervisor_event",
|
||||||
|
[
|
||||||
|
# Missing backup reference
|
||||||
|
{
|
||||||
|
"event": "job",
|
||||||
|
"data": {
|
||||||
|
"done": True,
|
||||||
|
"uuid": TEST_JOB_ID,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
# Errors
|
||||||
|
{
|
||||||
|
"event": "job",
|
||||||
|
"data": {
|
||||||
|
"done": True,
|
||||||
|
"errors": [
|
||||||
|
{
|
||||||
|
"type": "BackupMountDownError",
|
||||||
|
"message": "test_mount is down, cannot back-up to it",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"uuid": TEST_JOB_ID,
|
||||||
|
"reference": "test_slug",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
)
|
||||||
@pytest.mark.usefixtures("hassio_client", "setup_integration")
|
@pytest.mark.usefixtures("hassio_client", "setup_integration")
|
||||||
async def test_reader_writer_create_missing_reference_error(
|
async def test_reader_writer_create_missing_reference_error(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
hass_ws_client: WebSocketGenerator,
|
hass_ws_client: WebSocketGenerator,
|
||||||
supervisor_client: AsyncMock,
|
supervisor_client: AsyncMock,
|
||||||
|
supervisor_event: dict[str, Any],
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test missing reference error when generating a backup."""
|
"""Test missing reference error when generating a backup."""
|
||||||
client = await hass_ws_client(hass)
|
client = await hass_ws_client(hass)
|
||||||
@ -1395,13 +1424,7 @@ async def test_reader_writer_create_missing_reference_error(
|
|||||||
assert supervisor_client.backups.partial_backup.call_count == 1
|
assert supervisor_client.backups.partial_backup.call_count == 1
|
||||||
|
|
||||||
await client.send_json_auto_id(
|
await client.send_json_auto_id(
|
||||||
{
|
{"type": "supervisor/event", "data": supervisor_event}
|
||||||
"type": "supervisor/event",
|
|
||||||
"data": {
|
|
||||||
"event": "job",
|
|
||||||
"data": {"done": True, "uuid": TEST_JOB_ID},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user