mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 15:47:12 +00:00
Refactor cloud backup agent to use updated file handling methods (#149231)
This commit is contained in:
parent
dd399ef59f
commit
e5f9788d24
@ -10,14 +10,8 @@ import random
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from aiohttp import ClientError, ClientResponseError
|
from aiohttp import ClientError, ClientResponseError
|
||||||
from hass_nabucasa import Cloud, CloudError
|
from hass_nabucasa import Cloud, CloudApiError, CloudApiNonRetryableError, CloudError
|
||||||
from hass_nabucasa.api import CloudApiError, CloudApiNonRetryableError
|
from hass_nabucasa.files import FilesError, StorageType, StoredFile, calculate_b64md5
|
||||||
from hass_nabucasa.cloud_api import (
|
|
||||||
FilesHandlerListEntry,
|
|
||||||
async_files_delete_file,
|
|
||||||
async_files_list,
|
|
||||||
)
|
|
||||||
from hass_nabucasa.files import FilesError, StorageType, calculate_b64md5
|
|
||||||
|
|
||||||
from homeassistant.components.backup import (
|
from homeassistant.components.backup import (
|
||||||
AgentBackup,
|
AgentBackup,
|
||||||
@ -186,8 +180,7 @@ class CloudBackupAgent(BackupAgent):
|
|||||||
"""
|
"""
|
||||||
backup = await self._async_get_backup(backup_id)
|
backup = await self._async_get_backup(backup_id)
|
||||||
try:
|
try:
|
||||||
await async_files_delete_file(
|
await self._cloud.files.delete(
|
||||||
self._cloud,
|
|
||||||
storage_type=StorageType.BACKUP,
|
storage_type=StorageType.BACKUP,
|
||||||
filename=backup["Key"],
|
filename=backup["Key"],
|
||||||
)
|
)
|
||||||
@ -199,12 +192,10 @@ class CloudBackupAgent(BackupAgent):
|
|||||||
backups = await self._async_list_backups()
|
backups = await self._async_list_backups()
|
||||||
return [AgentBackup.from_dict(backup["Metadata"]) for backup in backups]
|
return [AgentBackup.from_dict(backup["Metadata"]) for backup in backups]
|
||||||
|
|
||||||
async def _async_list_backups(self) -> list[FilesHandlerListEntry]:
|
async def _async_list_backups(self) -> list[StoredFile]:
|
||||||
"""List backups."""
|
"""List backups."""
|
||||||
try:
|
try:
|
||||||
backups = await async_files_list(
|
backups = await self._cloud.files.list(storage_type=StorageType.BACKUP)
|
||||||
self._cloud, storage_type=StorageType.BACKUP
|
|
||||||
)
|
|
||||||
except (ClientError, CloudError) as err:
|
except (ClientError, CloudError) as err:
|
||||||
raise BackupAgentError("Failed to list backups") from err
|
raise BackupAgentError("Failed to list backups") from err
|
||||||
|
|
||||||
@ -220,7 +211,7 @@ class CloudBackupAgent(BackupAgent):
|
|||||||
backup = await self._async_get_backup(backup_id)
|
backup = await self._async_get_backup(backup_id)
|
||||||
return AgentBackup.from_dict(backup["Metadata"])
|
return AgentBackup.from_dict(backup["Metadata"])
|
||||||
|
|
||||||
async def _async_get_backup(self, backup_id: str) -> FilesHandlerListEntry:
|
async def _async_get_backup(self, backup_id: str) -> StoredFile:
|
||||||
"""Return a backup."""
|
"""Return a backup."""
|
||||||
backups = await self._async_list_backups()
|
backups = await self._async_list_backups()
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
from collections.abc import AsyncGenerator, Generator
|
from collections.abc import AsyncGenerator, Generator
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import ANY, Mock, PropertyMock, patch
|
from unittest.mock import ANY, AsyncMock, Mock, PropertyMock, patch
|
||||||
|
|
||||||
from aiohttp import ClientError, ClientResponseError
|
from aiohttp import ClientError, ClientResponseError
|
||||||
from hass_nabucasa import CloudError
|
from hass_nabucasa import CloudError
|
||||||
@ -48,62 +48,56 @@ async def setup_integration(
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_delete_file() -> Generator[MagicMock]:
|
def mock_delete_file(cloud: MagicMock) -> Generator[AsyncMock]:
|
||||||
"""Mock list files."""
|
"""Mock delete files."""
|
||||||
with patch(
|
cloud.files.delete = AsyncMock()
|
||||||
"homeassistant.components.cloud.backup.async_files_delete_file",
|
return cloud.files.delete
|
||||||
spec_set=True,
|
|
||||||
) as delete_file:
|
|
||||||
yield delete_file
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_list_files() -> Generator[MagicMock]:
|
def mock_list_files(cloud: MagicMock) -> Generator[MagicMock]:
|
||||||
"""Mock list files."""
|
"""Mock list files."""
|
||||||
with patch(
|
cloud.files.list.return_value = [
|
||||||
"homeassistant.components.cloud.backup.async_files_list", spec_set=True
|
{
|
||||||
) as list_files:
|
"Key": "462e16810d6841228828d9dd2f9e341e.tar",
|
||||||
list_files.return_value = [
|
"LastModified": "2024-11-22T10:49:01.182Z",
|
||||||
{
|
"Size": 34519040,
|
||||||
"Key": "462e16810d6841228828d9dd2f9e341e.tar",
|
"Metadata": {
|
||||||
"LastModified": "2024-11-22T10:49:01.182Z",
|
"addons": [],
|
||||||
"Size": 34519040,
|
"backup_id": "23e64aec",
|
||||||
"Metadata": {
|
"date": "2024-11-22T11:48:48.727189+01:00",
|
||||||
"addons": [],
|
"database_included": True,
|
||||||
"backup_id": "23e64aec",
|
"extra_metadata": {},
|
||||||
"date": "2024-11-22T11:48:48.727189+01:00",
|
"folders": [],
|
||||||
"database_included": True,
|
"homeassistant_included": True,
|
||||||
"extra_metadata": {},
|
"homeassistant_version": "2024.12.0.dev0",
|
||||||
"folders": [],
|
"name": "Core 2024.12.0.dev0",
|
||||||
"homeassistant_included": True,
|
"protected": False,
|
||||||
"homeassistant_version": "2024.12.0.dev0",
|
"size": 34519040,
|
||||||
"name": "Core 2024.12.0.dev0",
|
"storage-type": "backup",
|
||||||
"protected": False,
|
|
||||||
"size": 34519040,
|
|
||||||
"storage-type": "backup",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
},
|
||||||
"Key": "462e16810d6841228828d9dd2f9e341f.tar",
|
{
|
||||||
"LastModified": "2024-11-22T10:49:01.182Z",
|
"Key": "462e16810d6841228828d9dd2f9e341f.tar",
|
||||||
"Size": 34519040,
|
"LastModified": "2024-11-22T10:49:01.182Z",
|
||||||
"Metadata": {
|
"Size": 34519040,
|
||||||
"addons": [],
|
"Metadata": {
|
||||||
"backup_id": "23e64aed",
|
"addons": [],
|
||||||
"date": "2024-11-22T11:48:48.727189+01:00",
|
"backup_id": "23e64aed",
|
||||||
"database_included": True,
|
"date": "2024-11-22T11:48:48.727189+01:00",
|
||||||
"extra_metadata": {},
|
"database_included": True,
|
||||||
"folders": [],
|
"extra_metadata": {},
|
||||||
"homeassistant_included": True,
|
"folders": [],
|
||||||
"homeassistant_version": "2024.12.0.dev0",
|
"homeassistant_included": True,
|
||||||
"name": "Core 2024.12.0.dev0",
|
"homeassistant_version": "2024.12.0.dev0",
|
||||||
"protected": False,
|
"name": "Core 2024.12.0.dev0",
|
||||||
"size": 34519040,
|
"protected": False,
|
||||||
"storage-type": "backup",
|
"size": 34519040,
|
||||||
},
|
"storage-type": "backup",
|
||||||
},
|
},
|
||||||
]
|
},
|
||||||
yield list_files
|
]
|
||||||
|
return cloud.files.list
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@ -141,7 +135,7 @@ async def test_agents_list_backups(
|
|||||||
client = await hass_ws_client(hass)
|
client = await hass_ws_client(hass)
|
||||||
await client.send_json_auto_id({"type": "backup/info"})
|
await client.send_json_auto_id({"type": "backup/info"})
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
mock_list_files.assert_called_once_with(cloud, storage_type="backup")
|
mock_list_files.assert_called_once_with(storage_type="backup")
|
||||||
|
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
assert response["result"]["agent_errors"] == {}
|
assert response["result"]["agent_errors"] == {}
|
||||||
@ -250,7 +244,7 @@ async def test_agents_get_backup(
|
|||||||
client = await hass_ws_client(hass)
|
client = await hass_ws_client(hass)
|
||||||
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()
|
||||||
mock_list_files.assert_called_once_with(cloud, storage_type="backup")
|
mock_list_files.assert_called_once_with(storage_type="backup")
|
||||||
|
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
assert response["result"]["agent_errors"] == {}
|
assert response["result"]["agent_errors"] == {}
|
||||||
@ -726,7 +720,6 @@ async def test_agents_delete(
|
|||||||
assert response["success"]
|
assert response["success"]
|
||||||
assert response["result"] == {"agent_errors": {}}
|
assert response["result"] == {"agent_errors": {}}
|
||||||
mock_delete_file.assert_called_once_with(
|
mock_delete_file.assert_called_once_with(
|
||||||
cloud,
|
|
||||||
filename="462e16810d6841228828d9dd2f9e341e.tar",
|
filename="462e16810d6841228828d9dd2f9e341e.tar",
|
||||||
storage_type=StorageType.BACKUP,
|
storage_type=StorageType.BACKUP,
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user