mirror of
https://github.com/home-assistant/core.git
synced 2025-12-22 03:49:36 +00:00
* Reapply "Make WS command backup/generate send events" (#131530) This reverts commit9b8316df3f. * MVP implementation of Backup sync agents (#126122) * init sync agent * add syncing * root import * rename list to info and add sync state * Add base backup class * Revert unneded change * adjust tests * move to kitchen_sink * split * move * Adjustments * Adjustment * update * Tests * Test unknown agent * adjust * Adjust for different test environments * Change /info WS to contain a dictinary * reorder * Add websocket command to trigger sync from the supervisor * cleanup * Make mypy happier --------- Co-authored-by: Erik <erik@montnemery.com> * Make BackupSyncMetadata model a dataclass (#130555) Make backup BackupSyncMetadata model a dataclass * Rename backup sync agent to backup agent (#130575) * Rename sync agent module to agent * Rename BackupSyncAgent to BackupAgent * Fix test typo * Rename async_get_backup_sync_agents to async_get_backup_agents * Rename and clean up remaining sync things * Update kitchen sink * Apply suggestions from code review * Update test_manager.py --------- Co-authored-by: Erik Montnemery <erik@montnemery.com> * Add additional options to WS command backup/generate (#130530) * Add additional options to WS command backup/generate * Improve test * Improve test * Align parameter names in backup/agents/* WS commands (#130590) * Allow setting password for backups (#110630) * Allow setting password for backups * use is_hassio from helpers * move it * Fix getting psw * Fix restoring with psw * Address review comments * Improve docstring * Adjust kitchen sink * Adjust --------- Co-authored-by: Erik <erik@montnemery.com> * Export relevant names from backup integration (#130596) * Tweak backup agent interface (#130613) * Tweak backup agent interface * Adjust kitchen_sink * Test kitchen sink backup (#130609) * Test agents_list_backups * Test agents_info * Test agents_download * Export Backup from manager * Test agents_upload * Update tests after rebase * Use backup domain * Remove WS command backup/upload (#130588) * Remove WS command backup/upload * Disable failing kitchen_sink test * Make local backup a backup agent (#130623) * Make local backup a backup agent * Adjust * Adjust * Adjust * Adjust tests * Adjust * Adjust * Adjust docstring * Adjust * Protect members of CoreLocalBackupAgent * Remove redundant check for file * Make the backup.create service use the first local agent * Add BackupAgent.async_get_backup * Fix some TODOs * Add support for downloading backup from a remote agent * Fix restore * Fix test * Adjust kitchen_sink test * Remove unused method BackupManager.async_get_backup_path * Re-enable kitchen sink test * Remove BaseBackupManager.async_upload_backup * Support restore from remote agent * Fix review comments * Include backup agent error in response to WS command backup/info (#130884) * Adjust code related to WS command backup/info (#130890) * Include backup agent error in response to WS command backup/details (#130892) * Remove LOCAL_AGENT_ID constant from backup manager (#130895) * Add backup config storage (#130871) * Add base for backup config * Allow updating backup config * Test loading backup config * Add backup config update method * Add temporary check for BackupAgent.async_remove_backup (#130893) * Rename backup slug to backup_id (#130902) * Improve backup websocket API tests (#130912) * Improve backup websocket API tests * Add missing snapshot * Fix tests leaving files behind * Improve backup manager backup creation tests (#130916) * Remove class backup.backup.LocalBackup (#130919) * Add agent delete backup (#130921) * Add backup agent delete backup * Remove agents delete websocket command * Update docstring Co-authored-by: Erik Montnemery <erik@montnemery.com> --------- Co-authored-by: Erik Montnemery <erik@montnemery.com> * Disable core local backup agent in hassio (#130933) * Rename remove backup to delete backup (#130940) * Rename remove backup to delete backup * Revert "backup/delete" * Refactor BackupManager (#130947) * Refactor BackupManager * Adjust * Adjust backup creation * Copy in executor * Fix BackupManager.async_get_backup (#130975) * Fix typo in backup tests (#130978) * Adjust backup NewBackup class (#130976) * Remove class backup.BackupUploadMetadata (#130977) Remove class backup.BackupMetadata * Report backup size in bytes instead of MB (#131028) Co-authored-by: Robert Resch <robert@resch.dev> * Speed up CI for feature branch (#131030) * Speed up CI for feature branch * adjust * fix * fix * fix * fix * Rename remove to delete in backup websocket type (#131023) * Revert "Speed up CI for feature branch" (#131074) Revert "Speed up CI for feature branch (#131030)" This reverts commit791280506d. * Rename class BaseBackup to AgentBackup (#131083) * Rename class BaseBackup to AgentBackup * Update tests * Speed up CI for backup feature branch (#131079) * Add backup platform to the hassio integration (#130991) * Add backup platform to the hassio integration * Add hassio to after_dependencies of backup * Address review comments * Remove redundant hassio parametrization of tests * Add tests * Address review comments * Bump CI cache version * Revert "Bump CI cache version" This reverts commit2ab4d2b179. * Extend backup info class AgentBackup (#131110) * Extend backup info class AgentBackup * Update kitchen sink * Update kitchen sink test * Update kitchen sink test * Exclude cloud and hassio from core files (#131117) * Remove unnecessary **kwargs from backup API (#131124) * Fix backup tests (#131128) * Freeze backup dataclasses (#131122) * Protect CoreLocalBackupAgent.load_backups (#131126) * Use backup metadata v2 in core/container backups (#131125) * Extend backup creation API (#131121) * Extend backup creation API * Add tests * Fix merge * Fix merge * Return agent errors when deleting a backup (#131142) * Return agent errors when deleting a backup * Remove redundant calls to dict.keys() * Add enum type for backup folder (#131158) * Add method AgentBackup.from_dict (#131164) * Remove WS command backup/agents/list_backups (#131163) * Handle backup schedule (#131127) * Add backup schedule handling * Fix unrelated incorrect type annotation in test * Clarify delay save * Make the backup time compatible with the recorder nightly job * Update create backup parameters * Use typed dict for create backup parameters * Simplify schedule state * Group create backup parameters * Move parameter * Fix typo * Use Folder model * Handle deserialization of folders better * Fail on attempt to include addons or folders in core backup (#131204) * Fix AgentBackup test (#131201) * Add options to WS command backup/restore (#131194) * Add options to WS command backup/restore * Add tests * Fix test * Teach core backup to restore only database or only settings (#131225) * Exclude tmp_backups/*.tar from backups (#131243) * Add WS command backup/subscribe_events (#131250) * Clean up temporary directory after restoring backup (#131263) * Improve hassio backup agent list (#131268) * Include `last_automatic_backup` in reply to backup/info (#131293) Include last_automatic_backup in reply to backup/info * Handle backup delete after config (#131259) * Handle delete after copies * Handle delete after days * Add some test examples * Test config_delete_after_logic * Test config_delete_after_copies_logic * Test more delete after days * Add debug logs * Always delete the oldest backup first * Never remove the last backup * Clean up words Co-authored-by: Erik Montnemery <erik@montnemery.com> * Fix after cleaning words * Use utcnow * Remove duplicate guard * Simplify sorting * Delete backups even if there are agent errors on get backups --------- Co-authored-by: Erik Montnemery <erik@montnemery.com> * Rename backup delete after to backup retention (#131364) * Rename backup delete after to backup retention * Tweak * Remove length limit on `agent_ids` when configuring backup (#132057) Remove length limit on agent_ids when configuring backup * Rename backup retention_config to retention (#132068) * Modify backup agent API to be stream oriented (#132090) * Modify backup agent API to be stream oriented * Fix tests * Adjust after code review * Remove no longer needed pylint override * Improve test coverage * Change BackupAgent API to work with AsyncIterator objects * Don't close files in the event loop * Don't close files in the event loop * Fix backup manager create backup log (#132174) * Fix debug log level (#132186) * Add cloud backup agent (#129621) * Init cloud backup sync * Add more metadata * Fix typo * Adjust to base changes * Don't raise on list if more than one backup is available * Adjust to base branch * Fetch always and verify on download * Update homeassistant/components/cloud/backup.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Adjust to base branch changes * Not required anymore * Workaround * Fix blocking event loop * Fix * Add some tests * some tests * Add cloud backup delete functionality * Enable check * Fix ruff * Use fixture * Use iter_chunks instead * Remove read * Remove explicit export of read_backup * Align with BackupAgent API changes * Improve test coverage * Improve error handling * Adjust docstrings * Catch aiohttp.ClientError bubbling up from hass_nabucasa * Improve iteration --------- Co-authored-by: Erik <erik@montnemery.com> Co-authored-by: Robert Resch <robert@resch.dev> Co-authored-by: Martin Hjelmare <marhje52@gmail.com> Co-authored-by: Krisjanis Lejejs <krisjanis.lejejs@gmail.com> * Extract file receiver from `BackupManager.async_receive_backup` to util (#132271) * Extract file receiver from BackupManager.async_receive_backup to util * Apply suggestions from code review Co-authored-by: Martin Hjelmare <marhje52@gmail.com> --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Make sure backup directory exists (#132269) * Make sure backup directory exists * Hand off directory creation to executor * Use mkdir's exist_ok feeature * Organize BackupManager instance attributes (#132277) * Don't store received backups in a TempDir (#132272) * Don't store received backups in a TempDir * Fix tests * Make sure backup directory exists * Address review comments * Fix tests * Rewrite backup manager state handling (#132375) * Rewrite backup manager state handling * Address review comments * Modify backup reader/writer API to be stream oriented (#132464) * Internalize backup tasks (#132482) * Internalize backup tasks * Update test after rebase * Handle backup error during automatic backup (#132511) * Improve backup manager state logging (#132549) * Fix backup manager state when restore completes (#132548) * Remove WS command backup/agents/download (#132664) * Add WS command backup/generate_with_stored_settings (#132671) * Add WS command backup/generate_with_stored_settings * Register the new command, add tests * Refactor local agent backup tests (#132683) * Refactor test_load_backups * Refactor test loading agents * Refactor test_delete_backup * Refactor test_upload * Clean up duplicate tests * Refactor backup manager receive tests (#132701) * Refactor backup manager receive tests * Clean up * Refactor pre and post platform tests (#132708) * Refactor backup pre platform test * Refactor backup post platform test * Bump aiohasupervisor to version 0.2.2b0 (#132704) * Bump aiohasupervisor to version 0.2.2b0 * Adjust tests * Publish event when manager is idle after creating backup (#132724) * Handle busy backup manager when uploading backup (#132736) * Adjust hassio backup agent to supervisor changes (#132732) * Adjust hassio backup agent to supervisor changes * Fix typo * Refactor test for create backup with wrong parameters (#132763) * Refactor test not loading bad backup platforms (#132769) * Improve receive backup coverage (#132758) * Refactor initiate backup test (#132829) * Rename Backup to ManagerBackup (#132841) * Refactor backup config (#132845) * Refactor backup config * Remove unnecessary condition * Adjust tests * Improve initiate backup test (#132858) * Store the time of automatic backup attempts (#132860) * Store the time of automatic backup attempts * Address review comments * Update test * Update cloud test * Save agent failures when creating backups (#132850) * Save agent failures when creating backups * Update tests * Store KnownBackups * Add test * Only clear known_backups on no error, add tests * Address review comments * Store known backups as a list * Update tests * Track all backups created with backup strategy settings (#132916) * Track all backups created with saved settings * Rename * Add explicit call to save the store * Don't register service backup.create in HassOS installations (#132932) * Revert changes to action service backup.create (#132938) * Fix logic for cleaning up temporary backup file (#132934) * Fix logic for cleaning up temporary backup file * Reduce scope of patch * Fix with_strategy_settings info not sent over websocket (#132939) * Fix with_strategy_settings info not sent over websocket * Fix kitchen sink tests * Fix cloud and hassio tests * Revert backup ci changes (#132955) Revert changes speeding up CI * Fix revert of CI changes (#132960) --------- Co-authored-by: Joakim Sørensen <joasoe@gmail.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com> Co-authored-by: Robert Resch <robert@resch.dev> Co-authored-by: Paul Bottein <paul.bottein@gmail.com> Co-authored-by: Krisjanis Lejejs <krisjanis.lejejs@gmail.com>
914 lines
28 KiB
Python
914 lines
28 KiB
Python
"""Tests for the Backup integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from collections.abc import Generator
|
|
from io import StringIO
|
|
import json
|
|
from typing import Any
|
|
from unittest.mock import ANY, AsyncMock, MagicMock, Mock, call, mock_open, patch
|
|
|
|
import pytest
|
|
|
|
from homeassistant.components.backup import (
|
|
DOMAIN,
|
|
AgentBackup,
|
|
BackupAgentPlatformProtocol,
|
|
BackupManager,
|
|
BackupPlatformProtocol,
|
|
Folder,
|
|
backup as local_backup_platform,
|
|
)
|
|
from homeassistant.components.backup.const import DATA_MANAGER
|
|
from homeassistant.components.backup.manager import (
|
|
BackupManagerState,
|
|
CoreBackupReaderWriter,
|
|
CreateBackupEvent,
|
|
CreateBackupStage,
|
|
CreateBackupState,
|
|
NewBackup,
|
|
WrittenBackup,
|
|
)
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
from .common import (
|
|
LOCAL_AGENT_ID,
|
|
TEST_BACKUP_ABC123,
|
|
TEST_BACKUP_DEF456,
|
|
BackupAgentTest,
|
|
)
|
|
|
|
from tests.common import MockPlatform, mock_platform
|
|
from tests.typing import ClientSessionGenerator, WebSocketGenerator
|
|
|
|
_EXPECTED_FILES = [
|
|
"test.txt",
|
|
".storage",
|
|
"backups",
|
|
"backups/not_backup",
|
|
"tmp_backups",
|
|
"tmp_backups/not_backup",
|
|
]
|
|
_EXPECTED_FILES_WITH_DATABASE = {
|
|
True: [*_EXPECTED_FILES, "home-assistant_v2.db"],
|
|
False: _EXPECTED_FILES,
|
|
}
|
|
|
|
|
|
async def _setup_backup_platform(
|
|
hass: HomeAssistant,
|
|
*,
|
|
domain: str = "some_domain",
|
|
platform: BackupPlatformProtocol | BackupAgentPlatformProtocol | None = None,
|
|
) -> None:
|
|
"""Set up a mock domain."""
|
|
mock_platform(hass, f"{domain}.backup", platform or MockPlatform())
|
|
assert await async_setup_component(hass, domain, {})
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def mock_delay_save() -> Generator[None]:
|
|
"""Mock the delay save constant."""
|
|
with patch("homeassistant.components.backup.store.STORE_DELAY_SAVE", 0):
|
|
yield
|
|
|
|
|
|
@pytest.fixture(name="generate_backup_id")
|
|
def generate_backup_id_fixture() -> Generator[MagicMock]:
|
|
"""Mock generate backup id."""
|
|
with patch("homeassistant.components.backup.manager._generate_backup_id") as mock:
|
|
mock.return_value = "abc123"
|
|
yield mock
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_backup_generation")
|
|
async def test_async_create_backup(
|
|
hass: HomeAssistant,
|
|
caplog: pytest.LogCaptureFixture,
|
|
mocked_json_bytes: Mock,
|
|
mocked_tarfile: Mock,
|
|
) -> None:
|
|
"""Test create backup."""
|
|
assert await async_setup_component(hass, DOMAIN, {})
|
|
await hass.async_block_till_done()
|
|
|
|
new_backup = NewBackup(backup_job_id="time-123")
|
|
backup_task = AsyncMock(
|
|
return_value=WrittenBackup(
|
|
backup=TEST_BACKUP_ABC123,
|
|
open_stream=AsyncMock(),
|
|
release_stream=AsyncMock(),
|
|
),
|
|
)() # call it so that it can be awaited
|
|
|
|
with patch(
|
|
"homeassistant.components.backup.manager.CoreBackupReaderWriter.async_create_backup",
|
|
return_value=(new_backup, backup_task),
|
|
) as create_backup:
|
|
await hass.services.async_call(
|
|
DOMAIN,
|
|
"create",
|
|
blocking=True,
|
|
)
|
|
|
|
assert create_backup.called
|
|
assert create_backup.call_args == call(
|
|
agent_ids=["backup.local"],
|
|
backup_name="Core 2025.1.0",
|
|
include_addons=None,
|
|
include_all_addons=False,
|
|
include_database=True,
|
|
include_folders=None,
|
|
include_homeassistant=True,
|
|
on_progress=ANY,
|
|
password=None,
|
|
)
|
|
|
|
|
|
async def test_async_create_backup_when_backing_up(hass: HomeAssistant) -> None:
|
|
"""Test generate backup."""
|
|
manager = BackupManager(hass, CoreBackupReaderWriter(hass))
|
|
manager.last_event = CreateBackupEvent(
|
|
stage=None, state=CreateBackupState.IN_PROGRESS
|
|
)
|
|
with pytest.raises(HomeAssistantError, match="Backup manager busy"):
|
|
await manager.async_create_backup(
|
|
agent_ids=[LOCAL_AGENT_ID],
|
|
include_addons=[],
|
|
include_all_addons=False,
|
|
include_database=True,
|
|
include_folders=[],
|
|
include_homeassistant=True,
|
|
name=None,
|
|
password=None,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("parameters", "expected_error"),
|
|
[
|
|
({"agent_ids": []}, "At least one agent must be selected"),
|
|
({"agent_ids": ["non_existing"]}, "Invalid agent selected"),
|
|
(
|
|
{"include_addons": ["ssl"], "include_all_addons": True},
|
|
"Cannot include all addons and specify specific addons",
|
|
),
|
|
({"include_homeassistant": False}, "Home Assistant must be included in backup"),
|
|
],
|
|
)
|
|
async def test_create_backup_wrong_parameters(
|
|
hass: HomeAssistant,
|
|
hass_ws_client: WebSocketGenerator,
|
|
parameters: dict[str, Any],
|
|
expected_error: str,
|
|
) -> None:
|
|
"""Test create backup with wrong parameters."""
|
|
assert await async_setup_component(hass, DOMAIN, {})
|
|
await hass.async_block_till_done()
|
|
|
|
ws_client = await hass_ws_client(hass)
|
|
|
|
default_parameters = {
|
|
"agent_ids": [LOCAL_AGENT_ID],
|
|
"include_addons": [],
|
|
"include_all_addons": False,
|
|
"include_database": True,
|
|
"include_folders": [],
|
|
"include_homeassistant": True,
|
|
}
|
|
|
|
await ws_client.send_json_auto_id(
|
|
{"type": "backup/generate"} | default_parameters | parameters
|
|
)
|
|
result = await ws_client.receive_json()
|
|
|
|
assert result["success"] is False
|
|
assert result["error"]["code"] == "home_assistant_error"
|
|
assert result["error"]["message"] == expected_error
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_backup_generation")
|
|
@pytest.mark.parametrize(
|
|
("agent_ids", "backup_directory", "temp_file_unlink_call_count"),
|
|
[
|
|
([LOCAL_AGENT_ID], "backups", 0),
|
|
(["test.remote"], "tmp_backups", 1),
|
|
([LOCAL_AGENT_ID, "test.remote"], "backups", 0),
|
|
],
|
|
)
|
|
@pytest.mark.parametrize(
|
|
"params",
|
|
[
|
|
{},
|
|
{"include_database": True, "name": "abc123"},
|
|
{"include_database": False},
|
|
{"password": "pass123"},
|
|
],
|
|
)
|
|
async def test_async_initiate_backup(
|
|
hass: HomeAssistant,
|
|
hass_ws_client: WebSocketGenerator,
|
|
caplog: pytest.LogCaptureFixture,
|
|
mocked_json_bytes: Mock,
|
|
mocked_tarfile: Mock,
|
|
generate_backup_id: MagicMock,
|
|
path_glob: MagicMock,
|
|
params: dict[str, Any],
|
|
agent_ids: list[str],
|
|
backup_directory: str,
|
|
temp_file_unlink_call_count: int,
|
|
) -> None:
|
|
"""Test generate backup."""
|
|
local_agent = local_backup_platform.CoreLocalBackupAgent(hass)
|
|
remote_agent = BackupAgentTest("remote", backups=[])
|
|
agents = {
|
|
f"backup.{local_agent.name}": local_agent,
|
|
f"test.{remote_agent.name}": remote_agent,
|
|
}
|
|
with patch(
|
|
"homeassistant.components.backup.backup.async_get_backup_agents"
|
|
) as core_get_backup_agents:
|
|
core_get_backup_agents.return_value = [local_agent]
|
|
await async_setup_component(hass, DOMAIN, {})
|
|
await hass.async_block_till_done()
|
|
await _setup_backup_platform(
|
|
hass,
|
|
domain="test",
|
|
platform=Mock(
|
|
async_get_backup_agents=AsyncMock(return_value=[remote_agent]),
|
|
spec_set=BackupAgentPlatformProtocol,
|
|
),
|
|
)
|
|
|
|
ws_client = await hass_ws_client(hass)
|
|
|
|
include_database = params.get("include_database", True)
|
|
name = params.get("name", "Core 2025.1.0")
|
|
password = params.get("password")
|
|
path_glob.return_value = []
|
|
|
|
await ws_client.send_json_auto_id({"type": "backup/info"})
|
|
result = await ws_client.receive_json()
|
|
|
|
assert result["success"] is True
|
|
assert result["result"] == {
|
|
"backups": [],
|
|
"agent_errors": {},
|
|
"last_attempted_strategy_backup": None,
|
|
"last_completed_strategy_backup": None,
|
|
}
|
|
|
|
await ws_client.send_json_auto_id({"type": "backup/subscribe_events"})
|
|
|
|
result = await ws_client.receive_json()
|
|
assert result["event"] == {"manager_state": BackupManagerState.IDLE}
|
|
|
|
result = await ws_client.receive_json()
|
|
assert result["success"] is True
|
|
|
|
with (
|
|
patch("pathlib.Path.open", mock_open(read_data=b"test")),
|
|
patch("pathlib.Path.unlink") as unlink_mock,
|
|
):
|
|
await ws_client.send_json_auto_id(
|
|
{"type": "backup/generate", "agent_ids": agent_ids} | params
|
|
)
|
|
result = await ws_client.receive_json()
|
|
assert result["event"] == {
|
|
"manager_state": BackupManagerState.CREATE_BACKUP,
|
|
"stage": None,
|
|
"state": CreateBackupState.IN_PROGRESS,
|
|
}
|
|
result = await ws_client.receive_json()
|
|
assert result["success"] is True
|
|
|
|
backup_id = result["result"]["backup_job_id"]
|
|
assert backup_id == generate_backup_id.return_value
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
result = await ws_client.receive_json()
|
|
assert result["event"] == {
|
|
"manager_state": BackupManagerState.CREATE_BACKUP,
|
|
"stage": CreateBackupStage.HOME_ASSISTANT,
|
|
"state": CreateBackupState.IN_PROGRESS,
|
|
}
|
|
|
|
result = await ws_client.receive_json()
|
|
assert result["event"] == {
|
|
"manager_state": BackupManagerState.CREATE_BACKUP,
|
|
"stage": CreateBackupStage.UPLOAD_TO_AGENTS,
|
|
"state": CreateBackupState.IN_PROGRESS,
|
|
}
|
|
|
|
result = await ws_client.receive_json()
|
|
assert result["event"] == {
|
|
"manager_state": BackupManagerState.CREATE_BACKUP,
|
|
"stage": None,
|
|
"state": CreateBackupState.COMPLETED,
|
|
}
|
|
|
|
result = await ws_client.receive_json()
|
|
assert result["event"] == {"manager_state": BackupManagerState.IDLE}
|
|
|
|
assert unlink_mock.call_count == temp_file_unlink_call_count
|
|
|
|
assert mocked_json_bytes.call_count == 1
|
|
backup_json_dict = mocked_json_bytes.call_args[0][0]
|
|
assert isinstance(backup_json_dict, dict)
|
|
assert backup_json_dict == {
|
|
"compressed": True,
|
|
"date": ANY,
|
|
"homeassistant": {
|
|
"exclude_database": not include_database,
|
|
"version": "2025.1.0",
|
|
},
|
|
"name": name,
|
|
"protected": bool(password),
|
|
"slug": ANY,
|
|
"type": "partial",
|
|
"version": 2,
|
|
}
|
|
|
|
await ws_client.send_json_auto_id(
|
|
{"type": "backup/details", "backup_id": backup_id}
|
|
)
|
|
result = await ws_client.receive_json()
|
|
|
|
backup_data = result["result"]["backup"]
|
|
backup_agent_ids = backup_data.pop("agent_ids")
|
|
|
|
assert backup_agent_ids == agent_ids
|
|
|
|
backup = AgentBackup.from_dict(backup_data)
|
|
|
|
assert backup == AgentBackup(
|
|
addons=[],
|
|
backup_id=ANY,
|
|
database_included=include_database,
|
|
date=ANY,
|
|
folders=[],
|
|
homeassistant_included=True,
|
|
homeassistant_version="2025.1.0",
|
|
name=name,
|
|
protected=bool(password),
|
|
size=ANY,
|
|
)
|
|
for agent_id in agent_ids:
|
|
agent = agents[agent_id]
|
|
assert len(agent._backups) == 1
|
|
agent_backup = agent._backups[backup.backup_id]
|
|
assert agent_backup.backup_id == backup.backup_id
|
|
assert agent_backup.date == backup.date
|
|
assert agent_backup.name == backup.name
|
|
assert agent_backup.protected == backup.protected
|
|
assert agent_backup.size == backup.size
|
|
|
|
outer_tar = mocked_tarfile.return_value
|
|
core_tar = outer_tar.create_inner_tar.return_value.__enter__.return_value
|
|
expected_files = [call(hass.config.path(), arcname="data", recursive=False)] + [
|
|
call(file, arcname=f"data/{file}", recursive=False)
|
|
for file in _EXPECTED_FILES_WITH_DATABASE[include_database]
|
|
]
|
|
assert core_tar.add.call_args_list == expected_files
|
|
|
|
tar_file_path = str(mocked_tarfile.call_args_list[0][0][0])
|
|
backup_directory = hass.config.path(backup_directory)
|
|
assert tar_file_path == f"{backup_directory}/{backup.backup_id}.tar"
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_backup_generation")
|
|
async def test_async_initiate_backup_with_agent_error(
|
|
hass: HomeAssistant,
|
|
hass_ws_client: WebSocketGenerator,
|
|
mocked_json_bytes: Mock,
|
|
mocked_tarfile: Mock,
|
|
generate_backup_id: MagicMock,
|
|
path_glob: MagicMock,
|
|
hass_storage: dict[str, Any],
|
|
) -> None:
|
|
"""Test generate backup."""
|
|
agent_ids = [LOCAL_AGENT_ID, "test.remote"]
|
|
local_agent = local_backup_platform.CoreLocalBackupAgent(hass)
|
|
remote_agent = BackupAgentTest("remote", backups=[])
|
|
|
|
with patch(
|
|
"homeassistant.components.backup.backup.async_get_backup_agents"
|
|
) as core_get_backup_agents:
|
|
core_get_backup_agents.return_value = [local_agent]
|
|
await async_setup_component(hass, DOMAIN, {})
|
|
await hass.async_block_till_done()
|
|
await _setup_backup_platform(
|
|
hass,
|
|
domain="test",
|
|
platform=Mock(
|
|
async_get_backup_agents=AsyncMock(return_value=[remote_agent]),
|
|
spec_set=BackupAgentPlatformProtocol,
|
|
),
|
|
)
|
|
|
|
ws_client = await hass_ws_client(hass)
|
|
|
|
path_glob.return_value = []
|
|
|
|
await ws_client.send_json_auto_id({"type": "backup/info"})
|
|
result = await ws_client.receive_json()
|
|
|
|
assert result["success"] is True
|
|
assert result["result"] == {
|
|
"backups": [],
|
|
"agent_errors": {},
|
|
"last_attempted_strategy_backup": None,
|
|
"last_completed_strategy_backup": None,
|
|
}
|
|
|
|
await ws_client.send_json_auto_id({"type": "backup/subscribe_events"})
|
|
|
|
result = await ws_client.receive_json()
|
|
assert result["event"] == {"manager_state": BackupManagerState.IDLE}
|
|
|
|
result = await ws_client.receive_json()
|
|
assert result["success"] is True
|
|
|
|
with (
|
|
patch("pathlib.Path.open", mock_open(read_data=b"test")),
|
|
patch.object(
|
|
remote_agent, "async_upload_backup", side_effect=Exception("Test exception")
|
|
),
|
|
):
|
|
await ws_client.send_json_auto_id(
|
|
{"type": "backup/generate", "agent_ids": agent_ids}
|
|
)
|
|
result = await ws_client.receive_json()
|
|
assert result["event"] == {
|
|
"manager_state": BackupManagerState.CREATE_BACKUP,
|
|
"stage": None,
|
|
"state": CreateBackupState.IN_PROGRESS,
|
|
}
|
|
result = await ws_client.receive_json()
|
|
assert result["success"] is True
|
|
|
|
backup_id = result["result"]["backup_job_id"]
|
|
assert backup_id == generate_backup_id.return_value
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
result = await ws_client.receive_json()
|
|
assert result["event"] == {
|
|
"manager_state": BackupManagerState.CREATE_BACKUP,
|
|
"stage": CreateBackupStage.HOME_ASSISTANT,
|
|
"state": CreateBackupState.IN_PROGRESS,
|
|
}
|
|
|
|
result = await ws_client.receive_json()
|
|
assert result["event"] == {
|
|
"manager_state": BackupManagerState.CREATE_BACKUP,
|
|
"stage": CreateBackupStage.UPLOAD_TO_AGENTS,
|
|
"state": CreateBackupState.IN_PROGRESS,
|
|
}
|
|
|
|
result = await ws_client.receive_json()
|
|
assert result["event"] == {
|
|
"manager_state": BackupManagerState.CREATE_BACKUP,
|
|
"stage": None,
|
|
"state": CreateBackupState.COMPLETED,
|
|
}
|
|
|
|
result = await ws_client.receive_json()
|
|
assert result["event"] == {"manager_state": BackupManagerState.IDLE}
|
|
|
|
expected_backup_data = {
|
|
"addons": [],
|
|
"agent_ids": ["backup.local"],
|
|
"backup_id": "abc123",
|
|
"database_included": True,
|
|
"date": ANY,
|
|
"failed_agent_ids": ["test.remote"],
|
|
"folders": [],
|
|
"homeassistant_included": True,
|
|
"homeassistant_version": "2025.1.0",
|
|
"name": "Core 2025.1.0",
|
|
"protected": False,
|
|
"size": 123,
|
|
"with_strategy_settings": False,
|
|
}
|
|
|
|
await ws_client.send_json_auto_id(
|
|
{"type": "backup/details", "backup_id": backup_id}
|
|
)
|
|
result = await ws_client.receive_json()
|
|
assert result["result"] == {
|
|
"agent_errors": {},
|
|
"backup": expected_backup_data,
|
|
}
|
|
|
|
await ws_client.send_json_auto_id({"type": "backup/info"})
|
|
result = await ws_client.receive_json()
|
|
assert result["result"] == {
|
|
"agent_errors": {},
|
|
"backups": [expected_backup_data],
|
|
"last_attempted_strategy_backup": None,
|
|
"last_completed_strategy_backup": None,
|
|
}
|
|
|
|
await hass.async_block_till_done()
|
|
assert hass_storage[DOMAIN]["data"]["backups"] == [
|
|
{
|
|
"backup_id": "abc123",
|
|
"failed_agent_ids": ["test.remote"],
|
|
"with_strategy_settings": False,
|
|
}
|
|
]
|
|
|
|
|
|
async def test_loading_platforms(
|
|
hass: HomeAssistant,
|
|
caplog: pytest.LogCaptureFixture,
|
|
) -> None:
|
|
"""Test loading backup platforms."""
|
|
manager = BackupManager(hass, CoreBackupReaderWriter(hass))
|
|
|
|
assert not manager.platforms
|
|
|
|
await _setup_backup_platform(
|
|
hass,
|
|
platform=Mock(
|
|
async_pre_backup=AsyncMock(),
|
|
async_post_backup=AsyncMock(),
|
|
async_get_backup_agents=AsyncMock(),
|
|
),
|
|
)
|
|
await manager.load_platforms()
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(manager.platforms) == 1
|
|
|
|
assert "Loaded 1 platforms" in caplog.text
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"platform_mock",
|
|
[
|
|
Mock(async_pre_backup=AsyncMock(), spec=["async_pre_backup"]),
|
|
Mock(async_post_backup=AsyncMock(), spec=["async_post_backup"]),
|
|
Mock(spec=[]),
|
|
],
|
|
)
|
|
async def test_not_loading_bad_platforms(
|
|
hass: HomeAssistant,
|
|
platform_mock: Mock,
|
|
) -> None:
|
|
"""Test not loading bad backup platforms."""
|
|
await _setup_backup_platform(
|
|
hass,
|
|
domain="test",
|
|
platform=platform_mock,
|
|
)
|
|
assert await async_setup_component(hass, DOMAIN, {})
|
|
await hass.async_block_till_done()
|
|
|
|
assert platform_mock.mock_calls == []
|
|
|
|
|
|
async def test_exception_platform_pre(
|
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
|
) -> None:
|
|
"""Test exception in pre step."""
|
|
|
|
async def _mock_step(hass: HomeAssistant) -> None:
|
|
raise HomeAssistantError("Test exception")
|
|
|
|
remote_agent = BackupAgentTest("remote", backups=[])
|
|
await _setup_backup_platform(
|
|
hass,
|
|
domain="test",
|
|
platform=Mock(
|
|
async_pre_backup=_mock_step,
|
|
async_post_backup=AsyncMock(),
|
|
async_get_backup_agents=AsyncMock(return_value=[remote_agent]),
|
|
),
|
|
)
|
|
assert await async_setup_component(hass, DOMAIN, {})
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.services.async_call(
|
|
DOMAIN,
|
|
"create",
|
|
blocking=True,
|
|
)
|
|
|
|
assert "Generating backup failed" in caplog.text
|
|
assert "Test exception" in caplog.text
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_backup_generation")
|
|
async def test_exception_platform_post(
|
|
hass: HomeAssistant,
|
|
caplog: pytest.LogCaptureFixture,
|
|
) -> None:
|
|
"""Test exception in post step."""
|
|
|
|
async def _mock_step(hass: HomeAssistant) -> None:
|
|
raise HomeAssistantError("Test exception")
|
|
|
|
remote_agent = BackupAgentTest("remote", backups=[])
|
|
await _setup_backup_platform(
|
|
hass,
|
|
domain="test",
|
|
platform=Mock(
|
|
async_pre_backup=AsyncMock(),
|
|
async_post_backup=_mock_step,
|
|
async_get_backup_agents=AsyncMock(return_value=[remote_agent]),
|
|
),
|
|
)
|
|
assert await async_setup_component(hass, DOMAIN, {})
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.services.async_call(
|
|
DOMAIN,
|
|
"create",
|
|
blocking=True,
|
|
)
|
|
|
|
assert "Generating backup failed" in caplog.text
|
|
assert "Test exception" in caplog.text
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
(
|
|
"agent_id_params",
|
|
"open_call_count",
|
|
"move_call_count",
|
|
"move_path_names",
|
|
"remote_agent_backups",
|
|
"remote_agent_backup_data",
|
|
"temp_file_unlink_call_count",
|
|
),
|
|
[
|
|
(
|
|
"agent_id=backup.local&agent_id=test.remote",
|
|
2,
|
|
1,
|
|
["abc123.tar"],
|
|
{TEST_BACKUP_ABC123.backup_id: TEST_BACKUP_ABC123},
|
|
b"test",
|
|
0,
|
|
),
|
|
(
|
|
"agent_id=backup.local",
|
|
1,
|
|
1,
|
|
["abc123.tar"],
|
|
{},
|
|
None,
|
|
0,
|
|
),
|
|
(
|
|
"agent_id=test.remote",
|
|
2,
|
|
0,
|
|
[],
|
|
{TEST_BACKUP_ABC123.backup_id: TEST_BACKUP_ABC123},
|
|
b"test",
|
|
1,
|
|
),
|
|
],
|
|
)
|
|
async def test_receive_backup(
|
|
hass: HomeAssistant,
|
|
hass_client: ClientSessionGenerator,
|
|
agent_id_params: str,
|
|
open_call_count: int,
|
|
move_call_count: int,
|
|
move_path_names: list[str],
|
|
remote_agent_backups: dict[str, AgentBackup],
|
|
remote_agent_backup_data: bytes | None,
|
|
temp_file_unlink_call_count: int,
|
|
) -> None:
|
|
"""Test receive backup and upload to the local and a remote agent."""
|
|
remote_agent = BackupAgentTest("remote", backups=[])
|
|
await _setup_backup_platform(
|
|
hass,
|
|
domain="test",
|
|
platform=Mock(
|
|
async_get_backup_agents=AsyncMock(return_value=[remote_agent]),
|
|
spec_set=BackupAgentPlatformProtocol,
|
|
),
|
|
)
|
|
assert await async_setup_component(hass, DOMAIN, {})
|
|
await hass.async_block_till_done()
|
|
client = await hass_client()
|
|
|
|
upload_data = "test"
|
|
open_mock = mock_open(read_data=upload_data.encode(encoding="utf-8"))
|
|
|
|
with (
|
|
patch("pathlib.Path.open", open_mock),
|
|
patch("shutil.move") as move_mock,
|
|
patch(
|
|
"homeassistant.components.backup.manager.read_backup",
|
|
return_value=TEST_BACKUP_ABC123,
|
|
),
|
|
patch("pathlib.Path.unlink") as unlink_mock,
|
|
):
|
|
resp = await client.post(
|
|
f"/api/backup/upload?{agent_id_params}",
|
|
data={"file": StringIO(upload_data)},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert resp.status == 201
|
|
assert open_mock.call_count == open_call_count
|
|
assert move_mock.call_count == move_call_count
|
|
for index, name in enumerate(move_path_names):
|
|
assert move_mock.call_args_list[index].args[1].name == name
|
|
assert remote_agent._backups == remote_agent_backups
|
|
assert remote_agent._backup_data == remote_agent_backup_data
|
|
assert unlink_mock.call_count == temp_file_unlink_call_count
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_backup_generation")
|
|
async def test_receive_backup_busy_manager(
|
|
hass: HomeAssistant,
|
|
hass_client: ClientSessionGenerator,
|
|
hass_ws_client: WebSocketGenerator,
|
|
) -> None:
|
|
"""Test receive backup with a busy manager."""
|
|
assert await async_setup_component(hass, DOMAIN, {})
|
|
await hass.async_block_till_done()
|
|
client = await hass_client()
|
|
ws_client = await hass_ws_client(hass)
|
|
|
|
upload_data = "test"
|
|
|
|
await ws_client.send_json_auto_id({"type": "backup/subscribe_events"})
|
|
result = await ws_client.receive_json()
|
|
assert result["event"] == {"manager_state": "idle"}
|
|
|
|
result = await ws_client.receive_json()
|
|
assert result["success"] is True
|
|
|
|
new_backup = NewBackup(backup_job_id="time-123")
|
|
backup_task: asyncio.Future[WrittenBackup] = asyncio.Future()
|
|
with patch(
|
|
"homeassistant.components.backup.manager.CoreBackupReaderWriter.async_create_backup",
|
|
return_value=(new_backup, backup_task),
|
|
) as create_backup:
|
|
await ws_client.send_json_auto_id(
|
|
{"type": "backup/generate", "agent_ids": ["backup.local"]}
|
|
)
|
|
result = await ws_client.receive_json()
|
|
assert result["event"] == {
|
|
"manager_state": "create_backup",
|
|
"stage": None,
|
|
"state": "in_progress",
|
|
}
|
|
result = await ws_client.receive_json()
|
|
assert result["success"] is True
|
|
assert result["result"] == {"backup_job_id": "time-123"}
|
|
|
|
assert create_backup.call_count == 1
|
|
|
|
resp = await client.post(
|
|
"/api/backup/upload?agent_id=backup.local",
|
|
data={"file": StringIO(upload_data)},
|
|
)
|
|
|
|
assert resp.status == 500
|
|
assert (
|
|
await resp.text()
|
|
== "Can't upload backup file: Backup manager busy: create_backup"
|
|
)
|
|
|
|
# finish the backup
|
|
backup_task.set_result(
|
|
WrittenBackup(
|
|
backup=TEST_BACKUP_ABC123,
|
|
open_stream=AsyncMock(),
|
|
release_stream=AsyncMock(),
|
|
)
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("agent_id", "password", "restore_database", "restore_homeassistant", "dir"),
|
|
[
|
|
(LOCAL_AGENT_ID, None, True, False, "backups"),
|
|
(LOCAL_AGENT_ID, "abc123", False, True, "backups"),
|
|
("test.remote", None, True, True, "tmp_backups"),
|
|
],
|
|
)
|
|
async def test_async_trigger_restore(
|
|
hass: HomeAssistant,
|
|
agent_id: str,
|
|
password: str | None,
|
|
restore_database: bool,
|
|
restore_homeassistant: bool,
|
|
dir: str,
|
|
) -> None:
|
|
"""Test trigger restore."""
|
|
manager = BackupManager(hass, CoreBackupReaderWriter(hass))
|
|
hass.data[DATA_MANAGER] = manager
|
|
|
|
await _setup_backup_platform(hass, domain=DOMAIN, platform=local_backup_platform)
|
|
await _setup_backup_platform(
|
|
hass,
|
|
domain="test",
|
|
platform=Mock(
|
|
async_get_backup_agents=AsyncMock(
|
|
return_value=[BackupAgentTest("remote", backups=[TEST_BACKUP_ABC123])]
|
|
),
|
|
spec_set=BackupAgentPlatformProtocol,
|
|
),
|
|
)
|
|
await manager.load_platforms()
|
|
|
|
local_agent = manager.backup_agents[LOCAL_AGENT_ID]
|
|
local_agent._backups = {TEST_BACKUP_ABC123.backup_id: TEST_BACKUP_ABC123}
|
|
local_agent._loaded_backups = True
|
|
|
|
with (
|
|
patch("pathlib.Path.exists", return_value=True),
|
|
patch("pathlib.Path.open"),
|
|
patch("pathlib.Path.write_text") as mocked_write_text,
|
|
patch("homeassistant.core.ServiceRegistry.async_call") as mocked_service_call,
|
|
patch.object(BackupAgentTest, "async_download_backup") as download_mock,
|
|
):
|
|
download_mock.return_value.__aiter__.return_value = iter((b"backup data",))
|
|
await manager.async_restore_backup(
|
|
TEST_BACKUP_ABC123.backup_id,
|
|
agent_id=agent_id,
|
|
password=password,
|
|
restore_addons=None,
|
|
restore_database=restore_database,
|
|
restore_folders=None,
|
|
restore_homeassistant=restore_homeassistant,
|
|
)
|
|
expected_restore_file = json.dumps(
|
|
{
|
|
"path": f"{hass.config.path()}/{dir}/abc123.tar",
|
|
"password": password,
|
|
"remove_after_restore": agent_id != LOCAL_AGENT_ID,
|
|
"restore_database": restore_database,
|
|
"restore_homeassistant": restore_homeassistant,
|
|
}
|
|
)
|
|
assert mocked_write_text.call_args[0][0] == expected_restore_file
|
|
assert mocked_service_call.called
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("parameters", "expected_error"),
|
|
[
|
|
(
|
|
{"backup_id": TEST_BACKUP_DEF456.backup_id},
|
|
"Backup def456 not found",
|
|
),
|
|
(
|
|
{"restore_addons": ["blah"]},
|
|
"Addons and folders are not supported in core restore",
|
|
),
|
|
(
|
|
{"restore_folders": [Folder.ADDONS]},
|
|
"Addons and folders are not supported in core restore",
|
|
),
|
|
(
|
|
{"restore_database": False, "restore_homeassistant": False},
|
|
"Home Assistant or database must be included in restore",
|
|
),
|
|
],
|
|
)
|
|
async def test_async_trigger_restore_wrong_parameters(
|
|
hass: HomeAssistant, parameters: dict[str, Any], expected_error: str
|
|
) -> None:
|
|
"""Test trigger restore."""
|
|
manager = BackupManager(hass, CoreBackupReaderWriter(hass))
|
|
|
|
await _setup_backup_platform(hass, domain=DOMAIN, platform=local_backup_platform)
|
|
await manager.load_platforms()
|
|
|
|
local_agent = manager.backup_agents[LOCAL_AGENT_ID]
|
|
local_agent._backups = {TEST_BACKUP_ABC123.backup_id: TEST_BACKUP_ABC123}
|
|
local_agent._loaded_backups = True
|
|
|
|
default_parameters = {
|
|
"agent_id": LOCAL_AGENT_ID,
|
|
"backup_id": TEST_BACKUP_ABC123.backup_id,
|
|
"password": None,
|
|
"restore_addons": None,
|
|
"restore_database": True,
|
|
"restore_folders": None,
|
|
"restore_homeassistant": True,
|
|
}
|
|
|
|
with (
|
|
patch("pathlib.Path.exists", return_value=True),
|
|
pytest.raises(HomeAssistantError, match=expected_error),
|
|
):
|
|
await manager.async_restore_backup(**(default_parameters | parameters))
|