mirror of
https://github.com/home-assistant/core.git
synced 2025-08-02 12:15:08 +02:00
also raise BackupAgentUnreachableError during upload when nas unavailable
This commit is contained in:
@@ -178,6 +178,8 @@ class SynologyDSMBackupAgent(BackupAgent):
|
|||||||
)
|
)
|
||||||
except SynologyDSMAPIErrorException as err:
|
except SynologyDSMAPIErrorException as err:
|
||||||
raise BackupAgentError("Failed to upload backup") from err
|
raise BackupAgentError("Failed to upload backup") from err
|
||||||
|
except SynologyDSMRequestException as err:
|
||||||
|
raise BackupAgentUnreachableError from err
|
||||||
|
|
||||||
# upload backup_meta.json file when backup.tar was successful uploaded
|
# upload backup_meta.json file when backup.tar was successful uploaded
|
||||||
try:
|
try:
|
||||||
@@ -188,6 +190,8 @@ class SynologyDSMBackupAgent(BackupAgent):
|
|||||||
)
|
)
|
||||||
except SynologyDSMAPIErrorException as err:
|
except SynologyDSMAPIErrorException as err:
|
||||||
raise BackupAgentError("Failed to upload backup") from err
|
raise BackupAgentError("Failed to upload backup") from err
|
||||||
|
except SynologyDSMRequestException as err:
|
||||||
|
raise BackupAgentUnreachableError from err
|
||||||
|
|
||||||
async def async_delete_backup(
|
async def async_delete_backup(
|
||||||
self,
|
self,
|
||||||
|
@@ -444,27 +444,38 @@ async def test_agents_get_backup_not_existing(
|
|||||||
assert response["result"] == {"agent_errors": {}, "backup": None}
|
assert response["result"] == {"agent_errors": {}, "backup": None}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("error", "expected_aget_errors"),
|
||||||
|
[
|
||||||
|
(
|
||||||
|
SynologyDSMAPIErrorException("api", "500", "error"),
|
||||||
|
{"synology_dsm.mocked_syno_dsm_entry": "Failed to list backups"},
|
||||||
|
),
|
||||||
|
(
|
||||||
|
SynologyDSMRequestException(ClientError("error")),
|
||||||
|
{"synology_dsm.mocked_syno_dsm_entry": "The backup agent is unreachable."},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
async def test_agents_get_backup_error(
|
async def test_agents_get_backup_error(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
hass_ws_client: WebSocketGenerator,
|
hass_ws_client: WebSocketGenerator,
|
||||||
setup_dsm_with_filestation: MagicMock,
|
setup_dsm_with_filestation: MagicMock,
|
||||||
|
error: SynologyDSMException,
|
||||||
|
expected_aget_errors: dict[str, str],
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test agent error while get backup."""
|
"""Test agent error while get backup."""
|
||||||
client = await hass_ws_client(hass)
|
client = await hass_ws_client(hass)
|
||||||
backup_id = "ef34ab12"
|
backup_id = "ef34ab12"
|
||||||
|
|
||||||
setup_dsm_with_filestation.file.get_files.side_effect = (
|
setup_dsm_with_filestation.file.get_files.side_effect = error
|
||||||
SynologyDSMAPIErrorException("api", "500", "error")
|
|
||||||
)
|
|
||||||
|
|
||||||
await client.send_json_auto_id({"type": "backup/details", "backup_id": backup_id})
|
await client.send_json_auto_id({"type": "backup/details", "backup_id": backup_id})
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
|
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
assert response["result"] == {
|
assert response["result"] == {
|
||||||
"agent_errors": {
|
"agent_errors": expected_aget_errors,
|
||||||
"synology_dsm.mocked_syno_dsm_entry": "Failed to list backups"
|
|
||||||
},
|
|
||||||
"backup": None,
|
"backup": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -574,11 +585,26 @@ async def test_agents_upload(
|
|||||||
assert mock.call_args_list[1].kwargs["path"] == "/ha_backup/my_backup_path"
|
assert mock.call_args_list[1].kwargs["path"] == "/ha_backup/my_backup_path"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("error", "expected_error"),
|
||||||
|
[
|
||||||
|
(
|
||||||
|
SynologyDSMAPIErrorException("api", "500", "error"),
|
||||||
|
"Failed to upload backup",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
SynologyDSMRequestException(ClientError("error")),
|
||||||
|
"The backup agent is unreachable.",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
async def test_agents_upload_error(
|
async def test_agents_upload_error(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
hass_client: ClientSessionGenerator,
|
hass_client: ClientSessionGenerator,
|
||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
setup_dsm_with_filestation: MagicMock,
|
setup_dsm_with_filestation: MagicMock,
|
||||||
|
error: SynologyDSMException,
|
||||||
|
expected_error: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test agent error while uploading backup."""
|
"""Test agent error while uploading backup."""
|
||||||
client = await hass_client()
|
client = await hass_client()
|
||||||
@@ -611,9 +637,7 @@ async def test_agents_upload_error(
|
|||||||
):
|
):
|
||||||
mocked_open.return_value.read = Mock(side_effect=[b"test", b""])
|
mocked_open.return_value.read = Mock(side_effect=[b"test", b""])
|
||||||
fetch_backup.return_value = test_backup
|
fetch_backup.return_value = test_backup
|
||||||
setup_dsm_with_filestation.file.upload_file.side_effect = (
|
setup_dsm_with_filestation.file.upload_file.side_effect = error
|
||||||
SynologyDSMAPIErrorException("api", "500", "error")
|
|
||||||
)
|
|
||||||
resp = await client.post(
|
resp = await client.post(
|
||||||
"/api/backup/upload?agent_id=synology_dsm.mocked_syno_dsm_entry",
|
"/api/backup/upload?agent_id=synology_dsm.mocked_syno_dsm_entry",
|
||||||
data={"file": StringIO("test")},
|
data={"file": StringIO("test")},
|
||||||
@@ -621,7 +645,7 @@ async def test_agents_upload_error(
|
|||||||
|
|
||||||
assert resp.status == 201
|
assert resp.status == 201
|
||||||
assert f"Uploading backup {backup_id}" in caplog.text
|
assert f"Uploading backup {backup_id}" in caplog.text
|
||||||
assert "Failed to upload backup" in caplog.text
|
assert expected_error in caplog.text
|
||||||
mock: AsyncMock = setup_dsm_with_filestation.file.upload_file
|
mock: AsyncMock = setup_dsm_with_filestation.file.upload_file
|
||||||
assert len(mock.mock_calls) == 1
|
assert len(mock.mock_calls) == 1
|
||||||
assert mock.call_args_list[0].kwargs["filename"] == f"{base_filename}.tar"
|
assert mock.call_args_list[0].kwargs["filename"] == f"{base_filename}.tar"
|
||||||
@@ -652,7 +676,7 @@ async def test_agents_upload_error(
|
|||||||
|
|
||||||
assert resp.status == 201
|
assert resp.status == 201
|
||||||
assert f"Uploading backup {backup_id}" in caplog.text
|
assert f"Uploading backup {backup_id}" in caplog.text
|
||||||
assert "Failed to upload backup" in caplog.text
|
assert expected_error in caplog.text
|
||||||
mock: AsyncMock = setup_dsm_with_filestation.file.upload_file
|
mock: AsyncMock = setup_dsm_with_filestation.file.upload_file
|
||||||
assert len(mock.mock_calls) == 3
|
assert len(mock.mock_calls) == 3
|
||||||
assert mock.call_args_list[1].kwargs["filename"] == f"{base_filename}.tar"
|
assert mock.call_args_list[1].kwargs["filename"] == f"{base_filename}.tar"
|
||||||
|
Reference in New Issue
Block a user