mirror of
https://github.com/home-assistant/core.git
synced 2026-12-04 12:50:45 -04:00
Use the location Supervisor created the backup in for the hassio backup agent (#193358)
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
26f448b89d
commit
d3d407ca6f
@@ -323,11 +323,9 @@ class SupervisorBackupReaderWriter(BackupReaderWriter):
|
||||
# supervisor handle as much as possible.
|
||||
# Therefore, we split the locations into two lists: encrypted and decrypted.
|
||||
# The backup will be created in the first location in the list sent to
|
||||
# supervisor, and if that location is not available, the backup will
|
||||
# fail.
|
||||
# To make it less likely that the backup fails, we prefer to create the
|
||||
# backup in the local storage location if included in the list of
|
||||
# locations.
|
||||
# supervisor, or in the next one if that location is not usable.
|
||||
# We prefer to create the backup in the local storage location if
|
||||
# included in the list of locations.
|
||||
# Hence, we send the list of locations to supervisor in this priority order:
|
||||
# 1. The list which has local storage
|
||||
# 2. The longest list of locations
|
||||
@@ -501,9 +499,16 @@ class SupervisorBackupReaderWriter(BackupReaderWriter):
|
||||
f"Error getting backup details: {err}"
|
||||
) from err
|
||||
|
||||
created_location = next(
|
||||
(loc for loc in locations if loc in details.location_attributes), None
|
||||
)
|
||||
if created_location is None:
|
||||
raise BackupReaderWriterError(
|
||||
f"Backup {backup_id} was not created in any of {locations}"
|
||||
)
|
||||
return WrittenBackup(
|
||||
addon_errors=addon_errors,
|
||||
backup=_backup_details_to_agent_backup(details, locations[0]),
|
||||
backup=_backup_details_to_agent_backup(details, created_location),
|
||||
folder_errors=folder_errors,
|
||||
open_stream=open_backup,
|
||||
release_stream=remove_backup,
|
||||
|
||||
@@ -3088,3 +3088,133 @@ async def test_config_load_config_info(
|
||||
|
||||
await client.send_json_auto_id({"type": "backup/config/info"})
|
||||
assert await client.receive_json() == snapshot
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("hassio_client")
|
||||
@pytest.mark.parametrize(
|
||||
("created_locations", "expected_uploads", "expected_event"),
|
||||
[
|
||||
pytest.param(
|
||||
["share1", "share2"],
|
||||
["share0"],
|
||||
{
|
||||
"manager_state": "create_backup",
|
||||
"reason": None,
|
||||
"stage": None,
|
||||
"state": "completed",
|
||||
},
|
||||
id="created_in_later_locations",
|
||||
),
|
||||
pytest.param(
|
||||
[],
|
||||
[],
|
||||
{
|
||||
"manager_state": "create_backup",
|
||||
"reason": "upload_failed",
|
||||
"stage": None,
|
||||
"state": "failed",
|
||||
},
|
||||
id="not_created_in_any_location",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_reader_writer_create_location_fallback(
|
||||
hass: HomeAssistant,
|
||||
hass_supervisor_ws_client: WebSocketGenerator,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
supervisor_client: AsyncMock,
|
||||
created_locations: list[str],
|
||||
expected_uploads: list[str],
|
||||
expected_event: dict[str, Any],
|
||||
) -> None:
|
||||
"""Test generating a backup which Supervisor did not create in the first location."""
|
||||
client = await hass_supervisor_ws_client()
|
||||
freezer.move_to("2025-01-30 13:42:12.345678")
|
||||
mounts = MountsInfo(
|
||||
default_backup_mount=None,
|
||||
mounts=[
|
||||
supervisor_mounts.CIFSMountResponse(
|
||||
share=f"share{i}",
|
||||
name=f"share{i}",
|
||||
read_only=False,
|
||||
state=supervisor_mounts.MountState.ACTIVE,
|
||||
user_path=PurePath(f"share{i}"),
|
||||
usage=supervisor_mounts.MountUsage.BACKUP,
|
||||
server=f"share{i}",
|
||||
type=supervisor_mounts.MountType.CIFS,
|
||||
)
|
||||
for i in range(3)
|
||||
],
|
||||
)
|
||||
supervisor_client.backups.partial_backup.return_value.job_id = UUID(TEST_JOB_ID)
|
||||
supervisor_client.backups.backup_info.return_value = replace(
|
||||
TEST_BACKUP_DETAILS,
|
||||
extra=DEFAULT_BACKUP_OPTIONS.extra,
|
||||
location_attributes={
|
||||
location: supervisor_backups.BackupLocationAttributes(
|
||||
protected=False, size_bytes=1048576
|
||||
)
|
||||
for location in created_locations
|
||||
},
|
||||
)
|
||||
supervisor_client.jobs.get_job.return_value = TEST_JOB_NOT_DONE
|
||||
supervisor_client.mounts.info.return_value = mounts
|
||||
assert await async_setup_component(hass, BACKUP_DOMAIN, {BACKUP_DOMAIN: {}})
|
||||
|
||||
await client.send_json_auto_id({"type": "backup/subscribe_events"})
|
||||
response = await client.receive_json()
|
||||
assert response["event"] == {"manager_state": "idle"}
|
||||
response = await client.receive_json()
|
||||
assert response["success"]
|
||||
|
||||
await client.send_json_auto_id(
|
||||
{
|
||||
"type": "backup/generate",
|
||||
"agent_ids": ["hassio.share0", "hassio.share1", "hassio.share2"],
|
||||
"name": "Test",
|
||||
}
|
||||
)
|
||||
response = await client.receive_json()
|
||||
assert response["event"] == {
|
||||
"manager_state": "create_backup",
|
||||
"reason": None,
|
||||
"stage": None,
|
||||
"state": "in_progress",
|
||||
}
|
||||
|
||||
response = await client.receive_json()
|
||||
assert response["success"]
|
||||
assert response["result"] == {"backup_job_id": TEST_JOB_ID}
|
||||
|
||||
supervisor_client.backups.partial_backup.assert_called_once_with(
|
||||
replace(DEFAULT_BACKUP_OPTIONS, location=["share0", "share1", "share2"])
|
||||
)
|
||||
|
||||
await client.send_json_auto_id(
|
||||
{
|
||||
"type": "supervisor/event",
|
||||
"data": {
|
||||
"event": "job",
|
||||
"data": {"done": True, "uuid": TEST_JOB_ID, "reference": "test_slug"},
|
||||
},
|
||||
}
|
||||
)
|
||||
response = await client.receive_json()
|
||||
assert response["success"]
|
||||
|
||||
response = await client.receive_json()
|
||||
while "uploaded_bytes" in response["event"] or response["event"]["stage"] in (
|
||||
"upload_to_agents",
|
||||
"cleaning_up",
|
||||
):
|
||||
response = await client.receive_json()
|
||||
assert response["event"] == expected_event
|
||||
|
||||
assert [
|
||||
next(iter(call.args[1].location))
|
||||
for call in supervisor_client.backups.upload_backup.mock_calls
|
||||
] == expected_uploads
|
||||
supervisor_client.backups.remove_backup.assert_not_called()
|
||||
|
||||
response = await client.receive_json()
|
||||
assert response["event"] == {"manager_state": "idle"}
|
||||
|
||||
Reference in New Issue
Block a user