mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 14:57:09 +00:00
Don't blow up when a backup doesn't exist on Synology DSM (#136913)
* don't raise while delte not existing backup * only raise when error ne 408
This commit is contained in:
parent
42cab208d0
commit
f9cc3361e3
@ -161,15 +161,20 @@ class SynologyDSMBackupAgent(BackupAgent):
|
|||||||
|
|
||||||
:param backup_id: The ID of the backup that was returned in async_list_backups.
|
:param backup_id: The ID of the backup that was returned in async_list_backups.
|
||||||
"""
|
"""
|
||||||
try:
|
for filename in (f"{backup_id}.tar", f"{backup_id}_meta.json"):
|
||||||
await self._file_station.delete_file(
|
try:
|
||||||
path=self.path, filename=f"{backup_id}.tar"
|
await self._file_station.delete_file(path=self.path, filename=filename)
|
||||||
)
|
except SynologyDSMAPIErrorException as err:
|
||||||
await self._file_station.delete_file(
|
err_args: dict = err.args[0]
|
||||||
path=self.path, filename=f"{backup_id}_meta.json"
|
if int(err_args.get("code", 0)) != 900 or (
|
||||||
)
|
(err_details := err_args.get("details")) is not None
|
||||||
except SynologyDSMAPIErrorException as err:
|
and isinstance(err_details, list)
|
||||||
raise BackupAgentError("Failed to delete the backup") from err
|
and isinstance(err_details[0], dict)
|
||||||
|
and int(err_details[0].get("code", 0))
|
||||||
|
!= 408 # No such file or directory
|
||||||
|
):
|
||||||
|
LOGGER.error("Failed to delete backup: %s", err)
|
||||||
|
raise BackupAgentError("Failed to delete backup") from err
|
||||||
|
|
||||||
async def async_list_backups(self, **kwargs: Any) -> list[AgentBackup]:
|
async def async_list_backups(self, **kwargs: Any) -> list[AgentBackup]:
|
||||||
"""List backups."""
|
"""List backups."""
|
||||||
|
@ -673,7 +673,11 @@ async def test_agents_delete_not_existing(
|
|||||||
backup_id = "ef34ab12"
|
backup_id = "ef34ab12"
|
||||||
|
|
||||||
setup_dsm_with_filestation.file.delete_file = AsyncMock(
|
setup_dsm_with_filestation.file.delete_file = AsyncMock(
|
||||||
side_effect=SynologyDSMAPIErrorException("api", "404", "not found")
|
side_effect=SynologyDSMAPIErrorException(
|
||||||
|
"api",
|
||||||
|
"900",
|
||||||
|
[{"code": 408, "path": f"/ha_backup/my_backup_path/{backup_id}.tar"}],
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
await client.send_json_auto_id(
|
await client.send_json_auto_id(
|
||||||
@ -685,26 +689,40 @@ async def test_agents_delete_not_existing(
|
|||||||
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": {
|
|
||||||
"synology_dsm.mocked_syno_dsm_entry": "Failed to delete the backup"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("error", "expected_log"),
|
||||||
|
[
|
||||||
|
(
|
||||||
|
SynologyDSMAPIErrorException("api", "100", "Unknown error"),
|
||||||
|
"{'api': 'api', 'code': '100', 'reason': 'Unknown', 'details': 'Unknown error'}",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
SynologyDSMAPIErrorException("api", "900", [{"code": 407}]),
|
||||||
|
"{'api': 'api', 'code': '900', 'reason': 'Unknown', 'details': [{'code': 407}]",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
SynologyDSMAPIErrorException("api", "900", [{"code": 417}]),
|
||||||
|
"{'api': 'api', 'code': '900', 'reason': 'Unknown', 'details': [{'code': 417}]",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
async def test_agents_delete_error(
|
async def test_agents_delete_error(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
hass_ws_client: WebSocketGenerator,
|
hass_ws_client: WebSocketGenerator,
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
setup_dsm_with_filestation: MagicMock,
|
setup_dsm_with_filestation: MagicMock,
|
||||||
|
error: SynologyDSMAPIErrorException,
|
||||||
|
expected_log: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test error while delete backup."""
|
"""Test error while delete backup."""
|
||||||
client = await hass_ws_client(hass)
|
client = await hass_ws_client(hass)
|
||||||
|
|
||||||
# error while delete
|
# error while delete
|
||||||
backup_id = "abcd12ef"
|
backup_id = "abcd12ef"
|
||||||
setup_dsm_with_filestation.file.delete_file.side_effect = (
|
setup_dsm_with_filestation.file.delete_file.side_effect = error
|
||||||
SynologyDSMAPIErrorException("api", "404", "not found")
|
|
||||||
)
|
|
||||||
await client.send_json_auto_id(
|
await client.send_json_auto_id(
|
||||||
{
|
{
|
||||||
"type": "backup/delete",
|
"type": "backup/delete",
|
||||||
@ -716,9 +734,10 @@ async def test_agents_delete_error(
|
|||||||
assert response["success"]
|
assert response["success"]
|
||||||
assert response["result"] == {
|
assert response["result"] == {
|
||||||
"agent_errors": {
|
"agent_errors": {
|
||||||
"synology_dsm.mocked_syno_dsm_entry": "Failed to delete the backup"
|
"synology_dsm.mocked_syno_dsm_entry": "Failed to delete backup"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
assert f"Failed to delete backup: {expected_log}" in caplog.text
|
||||||
mock: AsyncMock = setup_dsm_with_filestation.file.delete_file
|
mock: AsyncMock = setup_dsm_with_filestation.file.delete_file
|
||||||
assert len(mock.mock_calls) == 1
|
assert len(mock.mock_calls) == 1
|
||||||
assert mock.call_args_list[0].kwargs["filename"] == "abcd12ef.tar"
|
assert mock.call_args_list[0].kwargs["filename"] == "abcd12ef.tar"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user