Use setup_backup_integration test helper in backup tests (#138362)

This commit is contained in:
Erik Montnemery 2025-02-12 09:49:01 +01:00 committed by GitHub
parent 2033dbdd90
commit 6ef1178a35
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 158 additions and 441 deletions

View File

@ -20,7 +20,7 @@ from homeassistant.components.backup.const import DATA_MANAGER
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import MockPlatform, mock_platform from tests.common import mock_platform
LOCAL_AGENT_ID = f"{DOMAIN}.local" LOCAL_AGENT_ID = f"{DOMAIN}.local"
@ -138,15 +138,15 @@ async def setup_backup_integration(
raise ValueError(f"Invalid agent_id: {agent}") raise ValueError(f"Invalid agent_id: {agent}")
name = agent.partition(".")[2] name = agent.partition(".")[2]
remote_agents_dict[agent] = mock_backup_agent(name, backups.get(agent)) remote_agents_dict[agent] = mock_backup_agent(name, backups.get(agent))
if remote_agents:
platform = Mock( platform = Mock(
async_get_backup_agents=AsyncMock( async_get_backup_agents=AsyncMock(
return_value=list(remote_agents_dict.values()) return_value=list(remote_agents_dict.values())
), ),
spec_set=BackupAgentPlatformProtocol, spec_set=BackupAgentPlatformProtocol,
) )
await setup_backup_platform(hass, domain=TEST_DOMAIN, platform=platform)
mock_platform(hass, f"{TEST_DOMAIN}.backup", platform or MockPlatform())
assert await async_setup_component(hass, TEST_DOMAIN, {})
assert await async_setup_component(hass, DOMAIN, {}) assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done() await hass.async_block_till_done()

View File

@ -16,12 +16,12 @@
'result': dict({ 'result': dict({
'agents': list([ 'agents': list([
dict({ dict({
'agent_id': 'backup.local', 'agent_id': 'test.remote',
'name': 'local', 'name': 'remote',
}), }),
dict({ dict({
'agent_id': 'test.test', 'agent_id': 'backup.local',
'name': 'test', 'name': 'local',
}), }),
]), ]),
}), }),
@ -3457,7 +3457,7 @@
'id': 1, 'id': 1,
'result': dict({ 'result': dict({
'agent_errors': dict({ 'agent_errors': dict({
'domain.test': 'The backup agent is unreachable.', 'test.remote': 'The backup agent is unreachable.',
}), }),
}), }),
'success': True, 'success': True,
@ -3480,7 +3480,7 @@
}), }),
]), ]),
'agents': dict({ 'agents': dict({
'domain.test': dict({ 'test.remote': dict({
'protected': False, 'protected': False,
'size': 0, 'size': 0,
}), }),
@ -3520,7 +3520,7 @@
'id': 1, 'id': 1,
'result': dict({ 'result': dict({
'agent_errors': dict({ 'agent_errors': dict({
'domain.test': 'The backup agent is unreachable.', 'test.remote': 'The backup agent is unreachable.',
}), }),
}), }),
'success': True, 'success': True,
@ -3543,7 +3543,7 @@
}), }),
]), ]),
'agents': dict({ 'agents': dict({
'domain.test': dict({ 'test.remote': dict({
'protected': False, 'protected': False,
'size': 0, 'size': 0,
}), }),
@ -3606,7 +3606,7 @@
}), }),
]), ]),
'agents': dict({ 'agents': dict({
'domain.test': dict({ 'test.remote': dict({
'protected': False, 'protected': False,
'size': 0, 'size': 0,
}), }),
@ -3668,7 +3668,7 @@
}), }),
]), ]),
'agents': dict({ 'agents': dict({
'domain.test': dict({ 'test.remote': dict({
'protected': False, 'protected': False,
'size': 0, 'size': 0,
}), }),
@ -3708,7 +3708,7 @@
'id': 1, 'id': 1,
'result': dict({ 'result': dict({
'agent_errors': dict({ 'agent_errors': dict({
'domain.test': 'Boom!', 'test.remote': 'Boom!',
}), }),
}), }),
'success': True, 'success': True,
@ -3731,7 +3731,7 @@
}), }),
]), ]),
'agents': dict({ 'agents': dict({
'domain.test': dict({ 'test.remote': dict({
'protected': False, 'protected': False,
'size': 0, 'size': 0,
}), }),
@ -3771,7 +3771,7 @@
'id': 1, 'id': 1,
'result': dict({ 'result': dict({
'agent_errors': dict({ 'agent_errors': dict({
'domain.test': 'Boom!', 'test.remote': 'Boom!',
}), }),
}), }),
'success': True, 'success': True,
@ -3794,7 +3794,7 @@
}), }),
]), ]),
'agents': dict({ 'agents': dict({
'domain.test': dict({ 'test.remote': dict({
'protected': False, 'protected': False,
'size': 0, 'size': 0,
}), }),
@ -3992,7 +3992,7 @@
'id': 1, 'id': 1,
'result': dict({ 'result': dict({
'agent_errors': dict({ 'agent_errors': dict({
'domain.test': 'The backup agent is unreachable.', 'test.remote': 'The backup agent is unreachable.',
}), }),
'backup': dict({ 'backup': dict({
'addons': list([ 'addons': list([
@ -4036,7 +4036,7 @@
'id': 1, 'id': 1,
'result': dict({ 'result': dict({
'agent_errors': dict({ 'agent_errors': dict({
'domain.test': 'Oops', 'test.remote': 'Oops',
}), }),
'backup': dict({ 'backup': dict({
'addons': list([ 'addons': list([
@ -4080,7 +4080,7 @@
'id': 1, 'id': 1,
'result': dict({ 'result': dict({
'agent_errors': dict({ 'agent_errors': dict({
'domain.test': 'Boom!', 'test.remote': 'Boom!',
}), }),
'backup': dict({ 'backup': dict({
'addons': list([ 'addons': list([
@ -4584,7 +4584,7 @@
'id': 1, 'id': 1,
'result': dict({ 'result': dict({
'agent_errors': dict({ 'agent_errors': dict({
'domain.test': 'The backup agent is unreachable.', 'test.remote': 'The backup agent is unreachable.',
}), }),
'backups': list([ 'backups': list([
dict({ dict({
@ -4636,7 +4636,7 @@
'id': 1, 'id': 1,
'result': dict({ 'result': dict({
'agent_errors': dict({ 'agent_errors': dict({
'domain.test': 'Oops', 'test.remote': 'Oops',
}), }),
'backups': list([ 'backups': list([
dict({ dict({
@ -4688,7 +4688,7 @@
'id': 1, 'id': 1,
'result': dict({ 'result': dict({
'agent_errors': dict({ 'agent_errors': dict({
'domain.test': 'Boom!', 'test.remote': 'Boom!',
}), }),
'backups': list([ 'backups': list([
dict({ dict({

View File

@ -18,19 +18,28 @@ from homeassistant.components.backup import (
BackupNotFound, BackupNotFound,
Folder, Folder,
) )
from homeassistant.components.backup.const import DATA_MANAGER, DOMAIN from homeassistant.components.backup.const import DOMAIN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .common import ( from .common import TEST_BACKUP_ABC123, aiter_from_iter, setup_backup_integration
TEST_BACKUP_ABC123,
aiter_from_iter,
mock_backup_agent,
setup_backup_integration,
)
from tests.common import MockUser, get_fixture_path from tests.common import MockUser, get_fixture_path
from tests.typing import ClientSessionGenerator from tests.typing import ClientSessionGenerator
PROTECTED_BACKUP = AgentBackup(
addons=[AddonInfo(name="Test", slug="test", version="1.0.0")],
backup_id="c0cb53bd",
database_included=True,
date="1970-01-01T00:00:00Z",
extra_metadata={},
folders=[Folder.MEDIA, Folder.SHARE],
homeassistant_included=True,
homeassistant_version="2024.12.0",
name="Test",
protected=True,
size=13,
)
async def test_downloading_local_backup( async def test_downloading_local_backup(
hass: HomeAssistant, hass: HomeAssistant,
@ -115,32 +124,15 @@ async def test_downloading_remote_encrypted_backup(
) -> None: ) -> None:
"""Test downloading a local backup file.""" """Test downloading a local backup file."""
backup_path = get_fixture_path("test_backups/c0cb53bd.tar", DOMAIN) backup_path = get_fixture_path("test_backups/c0cb53bd.tar", DOMAIN)
await setup_backup_integration(hass) mock_agents = await setup_backup_integration(
mock_agent = mock_backup_agent( hass, remote_agents=["test.test"], backups={"test.test": [PROTECTED_BACKUP]}
"test",
[
AgentBackup(
addons=[AddonInfo(name="Test", slug="test", version="1.0.0")],
backup_id="c0cb53bd",
database_included=True,
date="1970-01-01T00:00:00Z",
extra_metadata={},
folders=[Folder.MEDIA, Folder.SHARE],
homeassistant_included=True,
homeassistant_version="2024.12.0",
name="Test",
protected=True,
size=13,
) )
],
)
hass.data[DATA_MANAGER].backup_agents["domain.test"] = mock_agent
async def download_backup(backup_id: str, **kwargs: Any) -> AsyncIterator[bytes]: async def download_backup(backup_id: str, **kwargs: Any) -> AsyncIterator[bytes]:
return aiter_from_iter((backup_path.read_bytes(),)) return aiter_from_iter((backup_path.read_bytes(),))
mock_agent.async_download_backup.side_effect = download_backup mock_agents["test.test"].async_download_backup.side_effect = download_backup
await _test_downloading_encrypted_backup(hass_client, "domain.test") await _test_downloading_encrypted_backup(hass_client, "test.test")
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -157,31 +149,14 @@ async def test_downloading_remote_encrypted_backup_with_error(
status: int, status: int,
) -> None: ) -> None:
"""Test downloading a local backup file.""" """Test downloading a local backup file."""
await setup_backup_integration(hass) mock_agents = await setup_backup_integration(
mock_agent = mock_backup_agent( hass, remote_agents=["test.test"], backups={"test.test": [PROTECTED_BACKUP]}
"test",
[
AgentBackup(
addons=[AddonInfo(name="Test", slug="test", version="1.0.0")],
backup_id="abc123",
database_included=True,
date="1970-01-01T00:00:00Z",
extra_metadata={},
folders=[Folder.MEDIA, Folder.SHARE],
homeassistant_included=True,
homeassistant_version="2024.12.0",
name="Test",
protected=True,
size=13,
) )
],
)
hass.data[DATA_MANAGER].backup_agents["domain.test"] = mock_agent
mock_agent.async_download_backup.side_effect = error mock_agents["test.test"].async_download_backup.side_effect = error
client = await hass_client() client = await hass_client()
resp = await client.get( resp = await client.get(
"/api/backup/download/abc123?agent_id=domain.test&password=blah" f"/api/backup/download/{PROTECTED_BACKUP.backup_id}?agent_id=test.test&password=blah"
) )
assert resp.status == status assert resp.status == status

View File

@ -27,11 +27,9 @@ import pytest
from homeassistant.components.backup import ( from homeassistant.components.backup import (
DOMAIN, DOMAIN,
AgentBackup, AgentBackup,
BackupAgentPlatformProtocol,
BackupReaderWriterError, BackupReaderWriterError,
Folder, Folder,
LocalBackupAgent, LocalBackupAgent,
backup as local_backup_platform,
) )
from homeassistant.components.backup.agent import BackupAgentError from homeassistant.components.backup.agent import BackupAgentError
from homeassistant.components.backup.const import DATA_MANAGER from homeassistant.components.backup.const import DATA_MANAGER
@ -50,7 +48,6 @@ from homeassistant.components.backup.util import password_to_key
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import issue_registry as ir from homeassistant.helpers import issue_registry as ir
from homeassistant.setup import async_setup_component
from .common import ( from .common import (
LOCAL_AGENT_ID, LOCAL_AGENT_ID,
@ -59,6 +56,7 @@ from .common import (
TEST_BACKUP_PATH_ABC123, TEST_BACKUP_PATH_ABC123,
TEST_BACKUP_PATH_DEF456, TEST_BACKUP_PATH_DEF456,
mock_backup_agent, mock_backup_agent,
setup_backup_integration,
setup_backup_platform, setup_backup_platform,
) )
@ -110,8 +108,7 @@ async def test_create_backup_service(
mocked_tarfile: Mock, mocked_tarfile: Mock,
) -> None: ) -> None:
"""Test create backup service.""" """Test create backup service."""
assert await async_setup_component(hass, DOMAIN, {}) await setup_backup_integration(hass)
await hass.async_block_till_done()
new_backup = NewBackup(backup_job_id="time-123") new_backup = NewBackup(backup_job_id="time-123")
backup_task = AsyncMock( backup_task = AsyncMock(
@ -307,8 +304,7 @@ async def test_async_create_backup(
expected_writer_kwargs: dict[str, Any], expected_writer_kwargs: dict[str, Any],
) -> None: ) -> None:
"""Test create backup.""" """Test create backup."""
assert await async_setup_component(hass, DOMAIN, {}) await setup_backup_integration(hass)
await hass.async_block_till_done()
manager = hass.data[DATA_MANAGER] manager = hass.data[DATA_MANAGER]
new_backup = NewBackup(backup_job_id="time-123") new_backup = NewBackup(backup_job_id="time-123")
@ -336,8 +332,7 @@ async def test_create_backup_when_busy(
hass_ws_client: WebSocketGenerator, hass_ws_client: WebSocketGenerator,
) -> None: ) -> None:
"""Test generate backup with busy manager.""" """Test generate backup with busy manager."""
assert await async_setup_component(hass, DOMAIN, {}) await setup_backup_integration(hass)
await hass.async_block_till_done()
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
await ws_client.send_json_auto_id( await ws_client.send_json_auto_id(
@ -385,8 +380,7 @@ async def test_create_backup_wrong_parameters(
expected_error: str, expected_error: str,
) -> None: ) -> None:
"""Test create backup with wrong parameters.""" """Test create backup with wrong parameters."""
assert await async_setup_component(hass, DOMAIN, {}) await setup_backup_integration(hass)
await hass.async_block_till_done()
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
@ -523,23 +517,7 @@ async def test_initiate_backup(
temp_file_unlink_call_count: int, temp_file_unlink_call_count: int,
) -> None: ) -> None:
"""Test generate backup.""" """Test generate backup."""
local_agent = local_backup_platform.CoreLocalBackupAgent(hass) await setup_backup_integration(hass, remote_agents=["test.remote"])
remote_agent = mock_backup_agent("remote")
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) ws_client = await hass_ws_client(hass)
freezer.move_to("2025-01-30 13:42:12.345678") freezer.move_to("2025-01-30 13:42:12.345678")
@ -693,7 +671,6 @@ async def test_initiate_backup_with_agent_error(
) -> None: ) -> None:
"""Test agent upload error during backup generation.""" """Test agent upload error during backup generation."""
agent_ids = [LOCAL_AGENT_ID, "test.remote"] agent_ids = [LOCAL_AGENT_ID, "test.remote"]
local_agent = local_backup_platform.CoreLocalBackupAgent(hass)
backup_1 = replace(TEST_BACKUP_ABC123, backup_id="backup1") # matching instance id backup_1 = replace(TEST_BACKUP_ABC123, backup_id="backup1") # matching instance id
backup_2 = replace(TEST_BACKUP_DEF456, backup_id="backup2") # other instance id backup_2 = replace(TEST_BACKUP_DEF456, backup_id="backup2") # other instance id
backup_3 = replace(TEST_BACKUP_ABC123, backup_id="backup3") # matching instance id backup_3 = replace(TEST_BACKUP_ABC123, backup_id="backup3") # matching instance id
@ -771,21 +748,11 @@ async def test_initiate_backup_with_agent_error(
"with_automatic_settings": True, "with_automatic_settings": True,
}, },
] ]
remote_agent = mock_backup_agent("remote", backups=[backup_1, backup_2, backup_3])
with patch( mock_agents = await setup_backup_integration(
"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, hass,
domain="test", remote_agents=["test.remote"],
platform=Mock( backups={"test.remote": [backup_1, backup_2, backup_3]},
async_get_backup_agents=AsyncMock(return_value=[remote_agent]),
spec_set=BackupAgentPlatformProtocol,
),
) )
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
@ -821,17 +788,8 @@ async def test_initiate_backup_with_agent_error(
result = await ws_client.receive_json() result = await ws_client.receive_json()
assert result["success"] is True assert result["success"] is True
delete_backup = AsyncMock() mock_agents["test.remote"].async_upload_backup.side_effect = exception
with patch("pathlib.Path.open", mock_open(read_data=b"test")):
with (
patch("pathlib.Path.open", mock_open(read_data=b"test")),
patch.object(
remote_agent,
"async_upload_backup",
side_effect=exception,
),
patch.object(remote_agent, "async_delete_backup", delete_backup),
):
await ws_client.send_json_auto_id( await ws_client.send_json_auto_id(
{"type": "backup/generate", "agent_ids": agent_ids} {"type": "backup/generate", "agent_ids": agent_ids}
) )
@ -922,7 +880,7 @@ async def test_initiate_backup_with_agent_error(
] ]
# one of the two matching backups with the remote agent should have been deleted # one of the two matching backups with the remote agent should have been deleted
assert delete_backup.call_count == 1 assert mock_agents["test.remote"].async_delete_backup.call_count == 1
@pytest.mark.usefixtures("mock_backup_generation") @pytest.mark.usefixtures("mock_backup_generation")
@ -946,8 +904,7 @@ async def test_create_backup_success_clears_issue(
issues_after_create_backup: set[tuple[str, str]], issues_after_create_backup: set[tuple[str, str]],
) -> None: ) -> None:
"""Test backup issue is cleared after backup is created.""" """Test backup issue is cleared after backup is created."""
await async_setup_component(hass, DOMAIN, {}) await setup_backup_integration(hass)
await hass.async_block_till_done()
# Create a backup issue # Create a backup issue
ir.async_create_issue( ir.async_create_issue(
@ -996,7 +953,7 @@ async def delayed_boom(*args, **kwargs) -> tuple[NewBackup, Any]:
"automatic_agents", "automatic_agents",
"create_backup_command", "create_backup_command",
"create_backup_side_effect", "create_backup_side_effect",
"agent_upload_side_effect", "upload_side_effect",
"create_backup_result", "create_backup_result",
"issues_after_create_backup", "issues_after_create_backup",
), ),
@ -1115,26 +1072,12 @@ async def test_create_backup_failure_raises_issue(
automatic_agents: list[str], automatic_agents: list[str],
create_backup_command: dict[str, Any], create_backup_command: dict[str, Any],
create_backup_side_effect: Exception | None, create_backup_side_effect: Exception | None,
agent_upload_side_effect: Exception | None, upload_side_effect: Exception | None,
create_backup_result: bool, create_backup_result: bool,
issues_after_create_backup: dict[tuple[str, str], dict[str, Any]], issues_after_create_backup: dict[tuple[str, str], dict[str, Any]],
) -> None: ) -> None:
"""Test backup issue is cleared after backup is created.""" """Test backup issue is cleared after backup is created."""
remote_agent = mock_backup_agent("remote") mock_agents = await setup_backup_integration(hass, remote_agents=["test.remote"])
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,
),
)
await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
@ -1149,9 +1092,7 @@ async def test_create_backup_failure_raises_issue(
result = await ws_client.receive_json() result = await ws_client.receive_json()
assert result["success"] is True assert result["success"] is True
with patch.object( mock_agents["test.remote"].async_upload_backup.side_effect = upload_side_effect
remote_agent, "async_upload_backup", side_effect=agent_upload_side_effect
):
await ws_client.send_json_auto_id(create_backup_command) await ws_client.send_json_auto_id(create_backup_command)
result = await ws_client.receive_json() result = await ws_client.receive_json()
assert result["success"] == create_backup_result assert result["success"] == create_backup_result
@ -1179,23 +1120,7 @@ async def test_initiate_backup_non_agent_upload_error(
) -> None: ) -> None:
"""Test an unknown or writer upload error during backup generation.""" """Test an unknown or writer upload error during backup generation."""
agent_ids = [LOCAL_AGENT_ID, "test.remote"] agent_ids = [LOCAL_AGENT_ID, "test.remote"]
local_agent = local_backup_platform.CoreLocalBackupAgent(hass) mock_agents = await setup_backup_integration(hass, remote_agents=["test.remote"])
remote_agent = mock_backup_agent("remote")
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) ws_client = await hass_ws_client(hass)
@ -1224,14 +1149,8 @@ async def test_initiate_backup_non_agent_upload_error(
result = await ws_client.receive_json() result = await ws_client.receive_json()
assert result["success"] is True assert result["success"] is True
with ( mock_agents["test.remote"].async_upload_backup.side_effect = exception
patch("pathlib.Path.open", mock_open(read_data=b"test")), with patch("pathlib.Path.open", mock_open(read_data=b"test")):
patch.object(
remote_agent,
"async_upload_backup",
side_effect=exception,
),
):
await ws_client.send_json_auto_id( await ws_client.send_json_auto_id(
{"type": "backup/generate", "agent_ids": agent_ids} {"type": "backup/generate", "agent_ids": agent_ids}
) )
@ -1297,23 +1216,8 @@ async def test_initiate_backup_with_task_error(
backup_task.set_exception(exception) backup_task.set_exception(exception)
create_backup.return_value = (NewBackup(backup_job_id="abc123"), backup_task) create_backup.return_value = (NewBackup(backup_job_id="abc123"), backup_task)
agent_ids = [LOCAL_AGENT_ID, "test.remote"] agent_ids = [LOCAL_AGENT_ID, "test.remote"]
local_agent = local_backup_platform.CoreLocalBackupAgent(hass)
remote_agent = mock_backup_agent("remote")
with patch( await setup_backup_integration(hass, remote_agents=["test.remote"])
"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) ws_client = await hass_ws_client(hass)
@ -1408,23 +1312,8 @@ async def test_initiate_backup_file_error(
) -> None: ) -> None:
"""Test file error during generate backup.""" """Test file error during generate backup."""
agent_ids = ["test.remote"] agent_ids = ["test.remote"]
local_agent = local_backup_platform.CoreLocalBackupAgent(hass)
remote_agent = mock_backup_agent("remote") await setup_backup_integration(hass, remote_agents=["test.remote"])
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) ws_client = await hass_ws_client(hass)
@ -1533,7 +1422,7 @@ async def test_loading_platform_with_listener(
) -> None: ) -> None:
"""Test loading a backup agent platform which can be listened to.""" """Test loading a backup agent platform which can be listened to."""
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
assert await async_setup_component(hass, DOMAIN, {}) await setup_backup_integration(hass)
manager = hass.data[DATA_MANAGER] manager = hass.data[DATA_MANAGER]
get_agents_mock = AsyncMock(return_value=[agent_creator("remote1")]) get_agents_mock = AsyncMock(return_value=[agent_creator("remote1")])
@ -1593,8 +1482,7 @@ async def test_not_loading_bad_platforms(
domain="test", domain="test",
platform=platform_mock, platform=platform_mock,
) )
assert await async_setup_component(hass, DOMAIN, {}) await setup_backup_integration(hass)
await hass.async_block_till_done()
assert platform_mock.mock_calls == [] assert platform_mock.mock_calls == []
@ -1615,8 +1503,7 @@ async def test_exception_platform_pre(hass: HomeAssistant) -> None:
async_get_backup_agents=AsyncMock(return_value=[remote_agent]), async_get_backup_agents=AsyncMock(return_value=[remote_agent]),
), ),
) )
assert await async_setup_component(hass, DOMAIN, {}) await setup_backup_integration(hass)
await hass.async_block_till_done()
with pytest.raises(BackupManagerError) as err: with pytest.raises(BackupManagerError) as err:
await hass.services.async_call( await hass.services.async_call(
@ -1645,8 +1532,7 @@ async def test_exception_platform_post(hass: HomeAssistant) -> None:
async_get_backup_agents=AsyncMock(return_value=[remote_agent]), async_get_backup_agents=AsyncMock(return_value=[remote_agent]),
), ),
) )
assert await async_setup_component(hass, DOMAIN, {}) await setup_backup_integration(hass)
await hass.async_block_till_done()
with pytest.raises(BackupManagerError) as err: with pytest.raises(BackupManagerError) as err:
await hass.services.async_call( await hass.services.async_call(
@ -1710,17 +1596,7 @@ async def test_receive_backup(
temp_file_unlink_call_count: int, temp_file_unlink_call_count: int,
) -> None: ) -> None:
"""Test receive backup and upload to the local and a remote agent.""" """Test receive backup and upload to the local and a remote agent."""
remote_agent = mock_backup_agent("remote") mock_agents = await setup_backup_integration(hass, remote_agents=["test.remote"])
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() client = await hass_client()
upload_data = "test" upload_data = "test"
@ -1750,6 +1626,7 @@ async def test_receive_backup(
assert move_mock.call_count == move_call_count assert move_mock.call_count == move_call_count
for index, name in enumerate(move_path_names): for index, name in enumerate(move_path_names):
assert move_mock.call_args_list[index].args[1].name == name assert move_mock.call_args_list[index].args[1].name == name
remote_agent = mock_agents["test.remote"]
for backup_id, (backup, expected_backup_data) in remote_agent_backups.items(): for backup_id, (backup, expected_backup_data) in remote_agent_backups.items():
assert await remote_agent.async_get_backup(backup_id) == backup assert await remote_agent.async_get_backup(backup_id) == backup
backup_data = bytearray() backup_data = bytearray()
@ -1770,8 +1647,7 @@ async def test_receive_backup_busy_manager(
new_backup = NewBackup(backup_job_id="time-123") new_backup = NewBackup(backup_job_id="time-123")
backup_task: asyncio.Future[WrittenBackup] = asyncio.Future() backup_task: asyncio.Future[WrittenBackup] = asyncio.Future()
create_backup.return_value = (new_backup, backup_task) create_backup.return_value = (new_backup, backup_task)
assert await async_setup_component(hass, DOMAIN, {}) await setup_backup_integration(hass)
await hass.async_block_till_done()
client = await hass_client() client = await hass_client()
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
@ -1833,7 +1709,6 @@ async def test_receive_backup_agent_error(
exception: Exception, exception: Exception,
) -> None: ) -> None:
"""Test upload error during backup receive.""" """Test upload error during backup receive."""
local_agent = local_backup_platform.CoreLocalBackupAgent(hass)
backup_1 = replace(TEST_BACKUP_ABC123, backup_id="backup1") # matching instance id backup_1 = replace(TEST_BACKUP_ABC123, backup_id="backup1") # matching instance id
backup_2 = replace(TEST_BACKUP_DEF456, backup_id="backup2") # other instance id backup_2 = replace(TEST_BACKUP_DEF456, backup_id="backup2") # other instance id
backup_3 = replace(TEST_BACKUP_ABC123, backup_id="backup3") # matching instance id backup_3 = replace(TEST_BACKUP_ABC123, backup_id="backup3") # matching instance id
@ -1911,21 +1786,11 @@ async def test_receive_backup_agent_error(
"with_automatic_settings": True, "with_automatic_settings": True,
}, },
] ]
remote_agent = mock_backup_agent("remote", backups=[backup_1, backup_2, backup_3])
with patch( mock_agents = await setup_backup_integration(
"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, hass,
domain="test", remote_agents=["test.remote"],
platform=Mock( backups={"test.remote": [backup_1, backup_2, backup_3]},
async_get_backup_agents=AsyncMock(return_value=[remote_agent]),
spec_set=BackupAgentPlatformProtocol,
),
) )
client = await hass_client() client = await hass_client()
@ -1962,13 +1827,11 @@ async def test_receive_backup_agent_error(
result = await ws_client.receive_json() result = await ws_client.receive_json()
assert result["success"] is True assert result["success"] is True
delete_backup = AsyncMock()
upload_data = "test" upload_data = "test"
open_mock = mock_open(read_data=upload_data.encode(encoding="utf-8")) open_mock = mock_open(read_data=upload_data.encode(encoding="utf-8"))
mock_agents["test.remote"].async_upload_backup.side_effect = exception
with ( with (
patch.object(remote_agent, "async_delete_backup", delete_backup),
patch.object(remote_agent, "async_upload_backup", side_effect=exception),
patch("pathlib.Path.open", open_mock), patch("pathlib.Path.open", open_mock),
patch("shutil.move") as move_mock, patch("shutil.move") as move_mock,
patch( patch(
@ -2050,7 +1913,7 @@ async def test_receive_backup_agent_error(
assert open_mock.call_count == 1 assert open_mock.call_count == 1
assert move_mock.call_count == 0 assert move_mock.call_count == 0
assert unlink_mock.call_count == 1 assert unlink_mock.call_count == 1
assert delete_backup.call_count == 0 assert mock_agents["test.remote"].async_delete_backup.call_count == 0
@pytest.mark.usefixtures("mock_backup_generation") @pytest.mark.usefixtures("mock_backup_generation")
@ -2064,23 +1927,7 @@ async def test_receive_backup_non_agent_upload_error(
exception: Exception, exception: Exception,
) -> None: ) -> None:
"""Test non agent upload error during backup receive.""" """Test non agent upload error during backup receive."""
local_agent = local_backup_platform.CoreLocalBackupAgent(hass) mock_agents = await setup_backup_integration(hass, remote_agents=["test.remote"])
remote_agent = mock_backup_agent("remote")
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,
),
)
client = await hass_client() client = await hass_client()
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
@ -2113,8 +1960,8 @@ async def test_receive_backup_non_agent_upload_error(
upload_data = "test" upload_data = "test"
open_mock = mock_open(read_data=upload_data.encode(encoding="utf-8")) open_mock = mock_open(read_data=upload_data.encode(encoding="utf-8"))
mock_agents["test.remote"].async_upload_backup.side_effect = exception
with ( with (
patch.object(remote_agent, "async_upload_backup", side_effect=exception),
patch("pathlib.Path.open", open_mock), patch("pathlib.Path.open", open_mock),
patch("shutil.move") as move_mock, patch("shutil.move") as move_mock,
patch( patch(
@ -2192,22 +2039,7 @@ async def test_receive_backup_file_write_error(
close_exception: Exception | None, close_exception: Exception | None,
) -> None: ) -> None:
"""Test file write error during backup receive.""" """Test file write error during backup receive."""
local_agent = local_backup_platform.CoreLocalBackupAgent(hass) await setup_backup_integration(hass, remote_agents=["test.remote"])
remote_agent = mock_backup_agent("remote")
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,
),
)
client = await hass_client() client = await hass_client()
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
@ -2303,22 +2135,7 @@ async def test_receive_backup_read_tar_error(
exception: Exception, exception: Exception,
) -> None: ) -> None:
"""Test read tar error during backup receive.""" """Test read tar error during backup receive."""
local_agent = local_backup_platform.CoreLocalBackupAgent(hass) await setup_backup_integration(hass, remote_agents=["test.remote"])
remote_agent = mock_backup_agent("remote")
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,
),
)
client = await hass_client() client = await hass_client()
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
@ -2483,23 +2300,7 @@ async def test_receive_backup_file_read_error(
response_status: int, response_status: int,
) -> None: ) -> None:
"""Test file read error during backup receive.""" """Test file read error during backup receive."""
local_agent = local_backup_platform.CoreLocalBackupAgent(hass) await setup_backup_integration(hass, remote_agents=["test.remote"])
remote_agent = mock_backup_agent("remote")
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,
),
)
client = await hass_client() client = await hass_client()
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
@ -2655,16 +2456,10 @@ async def test_restore_backup(
) -> None: ) -> None:
"""Test restore backup.""" """Test restore backup."""
password = password_param.get("password") password = password_param.get("password")
remote_agent = mock_backup_agent("remote", backups=[TEST_BACKUP_ABC123]) await setup_backup_integration(
await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
await setup_backup_platform(
hass, hass,
domain="test", remote_agents=["test.remote"],
platform=Mock( backups={"test.remote": [TEST_BACKUP_ABC123]},
async_get_backup_agents=AsyncMock(return_value=[remote_agent]),
spec_set=BackupAgentPlatformProtocol,
),
) )
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
@ -2685,13 +2480,11 @@ async def test_restore_backup(
patch( patch(
"homeassistant.components.backup.manager.validate_password" "homeassistant.components.backup.manager.validate_password"
) as validate_password_mock, ) as validate_password_mock,
patch.object(remote_agent, "async_download_backup") as download_mock,
patch( patch(
"homeassistant.components.backup.backup.read_backup", "homeassistant.components.backup.backup.read_backup",
side_effect=mock_read_backup, side_effect=mock_read_backup,
), ),
): ):
download_mock.return_value.__aiter__.return_value = iter((b"backup data",))
await ws_client.send_json_auto_id( await ws_client.send_json_auto_id(
{ {
"type": "backup/restore", "type": "backup/restore",
@ -2762,16 +2555,10 @@ async def test_restore_backup_wrong_password(
) -> None: ) -> None:
"""Test restore backup wrong password.""" """Test restore backup wrong password."""
password = "hunter2" password = "hunter2"
remote_agent = mock_backup_agent("remote", backups=[TEST_BACKUP_ABC123]) await setup_backup_integration(
await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
await setup_backup_platform(
hass, hass,
domain="test", remote_agents=["test.remote"],
platform=Mock( backups={"test.remote": [TEST_BACKUP_ABC123]},
async_get_backup_agents=AsyncMock(return_value=[remote_agent]),
spec_set=BackupAgentPlatformProtocol,
),
) )
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
@ -2792,13 +2579,11 @@ async def test_restore_backup_wrong_password(
patch( patch(
"homeassistant.components.backup.manager.validate_password" "homeassistant.components.backup.manager.validate_password"
) as validate_password_mock, ) as validate_password_mock,
patch.object(remote_agent, "async_download_backup") as download_mock,
patch( patch(
"homeassistant.components.backup.backup.read_backup", "homeassistant.components.backup.backup.read_backup",
side_effect=mock_read_backup, side_effect=mock_read_backup,
), ),
): ):
download_mock.return_value.__aiter__.return_value = iter((b"backup data",))
validate_password_mock.return_value = False validate_password_mock.return_value = False
await ws_client.send_json_auto_id( await ws_client.send_json_auto_id(
{ {
@ -2872,8 +2657,7 @@ async def test_restore_backup_wrong_parameters(
expected_reason: str, expected_reason: str,
) -> None: ) -> None:
"""Test restore backup wrong parameters.""" """Test restore backup wrong parameters."""
await async_setup_component(hass, DOMAIN, {}) await setup_backup_integration(hass)
await hass.async_block_till_done()
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
@ -2937,8 +2721,7 @@ async def test_restore_backup_when_busy(
hass_ws_client: WebSocketGenerator, hass_ws_client: WebSocketGenerator,
) -> None: ) -> None:
"""Test restore backup with busy manager.""" """Test restore backup with busy manager."""
assert await async_setup_component(hass, DOMAIN, {}) await setup_backup_integration(hass)
await hass.async_block_till_done()
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
await ws_client.send_json_auto_id( await ws_client.send_json_auto_id(
@ -2989,16 +2772,10 @@ async def test_restore_backup_agent_error(
expected_reason: str, expected_reason: str,
) -> None: ) -> None:
"""Test restore backup with agent error.""" """Test restore backup with agent error."""
remote_agent = mock_backup_agent("remote", backups=[TEST_BACKUP_ABC123]) mock_agents = await setup_backup_integration(
await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
await setup_backup_platform(
hass, hass,
domain="test", remote_agents=["test.remote"],
platform=Mock( backups={"test.remote": [TEST_BACKUP_ABC123]},
async_get_backup_agents=AsyncMock(return_value=[remote_agent]),
spec_set=BackupAgentPlatformProtocol,
),
) )
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
@ -3010,19 +2787,17 @@ async def test_restore_backup_agent_error(
result = await ws_client.receive_json() result = await ws_client.receive_json()
assert result["success"] is True assert result["success"] is True
mock_agents["test.remote"].async_download_backup.side_effect = exception
with ( with (
patch("pathlib.Path.open"), patch("pathlib.Path.open"),
patch("pathlib.Path.write_text") as mocked_write_text, patch("pathlib.Path.write_text") as mocked_write_text,
patch("homeassistant.core.ServiceRegistry.async_call") as mocked_service_call, patch("homeassistant.core.ServiceRegistry.async_call") as mocked_service_call,
patch.object(
remote_agent, "async_download_backup", side_effect=exception
) as download_mock,
): ):
await ws_client.send_json_auto_id( await ws_client.send_json_auto_id(
{ {
"type": "backup/restore", "type": "backup/restore",
"backup_id": TEST_BACKUP_ABC123.backup_id, "backup_id": TEST_BACKUP_ABC123.backup_id,
"agent_id": remote_agent.agent_id, "agent_id": "test.remote",
} }
) )
@ -3050,7 +2825,7 @@ async def test_restore_backup_agent_error(
assert result["error"]["code"] == error_code assert result["error"]["code"] == error_code
assert result["error"]["message"] == error_message assert result["error"]["message"] == error_message
assert download_mock.call_count == 1 assert mock_agents["test.remote"].async_download_backup.call_count == 1
assert mocked_write_text.call_count == 0 assert mocked_write_text.call_count == 0
assert mocked_service_call.call_count == 0 assert mocked_service_call.call_count == 0
@ -3129,16 +2904,10 @@ async def test_restore_backup_file_error(
validate_password_call_count: int, validate_password_call_count: int,
) -> None: ) -> None:
"""Test restore backup with file error.""" """Test restore backup with file error."""
remote_agent = mock_backup_agent("remote", backups=[TEST_BACKUP_ABC123]) mock_agents = await setup_backup_integration(
await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
await setup_backup_platform(
hass, hass,
domain="test", remote_agents=["test.remote"],
platform=Mock( backups={"test.remote": [TEST_BACKUP_ABC123]},
async_get_backup_agents=AsyncMock(return_value=[remote_agent]),
spec_set=BackupAgentPlatformProtocol,
),
) )
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
@ -3164,14 +2933,12 @@ async def test_restore_backup_file_error(
patch( patch(
"homeassistant.components.backup.manager.validate_password" "homeassistant.components.backup.manager.validate_password"
) as validate_password_mock, ) as validate_password_mock,
patch.object(remote_agent, "async_download_backup") as download_mock,
): ):
download_mock.return_value.__aiter__.return_value = iter((b"backup data",))
await ws_client.send_json_auto_id( await ws_client.send_json_auto_id(
{ {
"type": "backup/restore", "type": "backup/restore",
"backup_id": TEST_BACKUP_ABC123.backup_id, "backup_id": TEST_BACKUP_ABC123.backup_id,
"agent_id": remote_agent.agent_id, "agent_id": "test.remote",
} }
) )
@ -3199,7 +2966,7 @@ async def test_restore_backup_file_error(
assert result["error"]["code"] == "unknown_error" assert result["error"]["code"] == "unknown_error"
assert result["error"]["message"] == "Unknown error" assert result["error"]["message"] == "Unknown error"
assert download_mock.call_count == 1 assert mock_agents["test.remote"].async_download_backup.call_count == 1
assert validate_password_mock.call_count == validate_password_call_count assert validate_password_mock.call_count == validate_password_call_count
assert open_mock.call_count == open_call_count assert open_mock.call_count == open_call_count
assert open_mock.return_value.write.call_count == write_call_count assert open_mock.return_value.write.call_count == write_call_count
@ -3346,23 +3113,7 @@ async def test_initiate_backup_per_agent_encryption(
inner_tar_key: bytes | None, inner_tar_key: bytes | None,
) -> None: ) -> None:
"""Test generate backup where encryption is selectively set on agents.""" """Test generate backup where encryption is selectively set on agents."""
local_agent = local_backup_platform.CoreLocalBackupAgent(hass) await setup_backup_integration(hass, remote_agents=["test.remote"])
remote_agent = mock_backup_agent("remote")
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) ws_client = await hass_ws_client(hass)
@ -3512,8 +3263,7 @@ async def test_restore_progress_after_restart(
with patch( with patch(
"pathlib.Path.read_bytes", return_value=json.dumps(restore_result).encode() "pathlib.Path.read_bytes", return_value=json.dumps(restore_result).encode()
): ):
await async_setup_component(hass, DOMAIN, {}) await setup_backup_integration(hass)
await hass.async_block_till_done()
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
await ws_client.send_json_auto_id({"type": "backup/info"}) await ws_client.send_json_auto_id({"type": "backup/info"})
@ -3539,8 +3289,7 @@ async def test_restore_progress_after_restart_fail_to_remove(
"""Test restore backup progress after restart when failing to remove result file.""" """Test restore backup progress after restart when failing to remove result file."""
with patch("pathlib.Path.unlink", side_effect=OSError("Boom!")): with patch("pathlib.Path.unlink", side_effect=OSError("Boom!")):
await async_setup_component(hass, DOMAIN, {}) await setup_backup_integration(hass)
await hass.async_block_till_done()
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
await ws_client.send_json_auto_id({"type": "backup/info"}) await ws_client.send_json_auto_id({"type": "backup/info"})

View File

@ -11,7 +11,6 @@ from syrupy import SnapshotAssertion
from homeassistant.components.backup import ( from homeassistant.components.backup import (
AgentBackup, AgentBackup,
BackupAgentError, BackupAgentError,
BackupAgentPlatformProtocol,
BackupNotFound, BackupNotFound,
BackupReaderWriterError, BackupReaderWriterError,
Folder, Folder,
@ -28,15 +27,12 @@ from homeassistant.components.backup.manager import (
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.setup import async_setup_component
from .common import ( from .common import (
LOCAL_AGENT_ID, LOCAL_AGENT_ID,
TEST_BACKUP_ABC123, TEST_BACKUP_ABC123,
TEST_BACKUP_DEF456, TEST_BACKUP_DEF456,
mock_backup_agent,
setup_backup_integration, setup_backup_integration,
setup_backup_platform,
) )
from tests.common import async_fire_time_changed, async_mock_service from tests.common import async_fire_time_changed, async_mock_service
@ -150,12 +146,13 @@ async def test_info_with_errors(
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test getting backup info with one unavailable agent.""" """Test getting backup info with one unavailable agent."""
await setup_backup_integration( mock_agents = await setup_backup_integration(
hass, with_hassio=False, backups={LOCAL_AGENT_ID: [TEST_BACKUP_ABC123]} hass,
with_hassio=False,
backups={LOCAL_AGENT_ID: [TEST_BACKUP_ABC123]},
remote_agents=["test.remote"],
) )
mock_agent = mock_backup_agent("test") mock_agents["test.remote"].async_list_backups.side_effect = side_effect
mock_agent.async_list_backups.side_effect = side_effect
hass.data[DATA_MANAGER].backup_agents["domain.test"] = mock_agent
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -213,12 +210,13 @@ async def test_details_with_errors(
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test getting backup info with one unavailable agent.""" """Test getting backup info with one unavailable agent."""
await setup_backup_integration( mock_agents = await setup_backup_integration(
hass, with_hassio=False, backups={LOCAL_AGENT_ID: [TEST_BACKUP_ABC123]} hass,
with_hassio=False,
backups={LOCAL_AGENT_ID: [TEST_BACKUP_ABC123]},
remote_agents=["test.remote"],
) )
mock_agent = mock_backup_agent("test") mock_agents["test.remote"].async_get_backup.side_effect = side_effect
mock_agent.async_get_backup.side_effect = side_effect
hass.data[DATA_MANAGER].backup_agents["domain.test"] = mock_agent
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -304,12 +302,16 @@ async def test_delete_with_errors(
"version": store.STORAGE_VERSION, "version": store.STORAGE_VERSION,
"minor_version": store.STORAGE_VERSION_MINOR, "minor_version": store.STORAGE_VERSION_MINOR,
} }
await setup_backup_integration( mock_agents = await setup_backup_integration(
hass, with_hassio=False, backups={LOCAL_AGENT_ID: [TEST_BACKUP_ABC123]} hass,
with_hassio=False,
backups={
LOCAL_AGENT_ID: [TEST_BACKUP_ABC123],
"test.remote": [TEST_BACKUP_ABC123],
},
remote_agents=["test.remote"],
) )
mock_agent = mock_backup_agent("test", [TEST_BACKUP_ABC123]) mock_agents["test.remote"].async_delete_backup.side_effect = side_effect
mock_agent.async_delete_backup.side_effect = side_effect
hass.data[DATA_MANAGER].backup_agents["domain.test"] = mock_agent
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -327,9 +329,9 @@ async def test_agent_delete_backup(
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test deleting a backup file with a mock agent.""" """Test deleting a backup file with a mock agent."""
await setup_backup_integration(hass) mock_agents = await setup_backup_integration(
mock_agent = mock_backup_agent("test") hass, with_hassio=False, remote_agents=["test.remote"]
hass.data[DATA_MANAGER].backup_agents = {"domain.test": mock_agent} )
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -342,7 +344,7 @@ async def test_agent_delete_backup(
) )
assert await client.receive_json() == snapshot assert await client.receive_json() == snapshot
assert mock_agent.async_delete_backup.call_args == call("abc123") assert mock_agents["test.remote"].async_delete_backup.call_args == call("abc123")
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -589,17 +591,9 @@ async def test_generate_with_default_settings_calls_create(
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await hass.config.async_set_time_zone("Europe/Amsterdam") await hass.config.async_set_time_zone("Europe/Amsterdam")
freezer.move_to("2024-11-13T12:01:00+01:00") freezer.move_to("2024-11-13T12:01:00+01:00")
remote_agent = mock_backup_agent("remote") mock_agents = await setup_backup_integration(
await setup_backup_platform( hass, with_hassio=False, remote_agents=["test.remote"]
hass,
domain="test",
platform=Mock(
async_get_backup_agents=AsyncMock(return_value=[remote_agent]),
spec_set=BackupAgentPlatformProtocol,
),
) )
await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
await client.send_json_auto_id( await client.send_json_auto_id(
{"type": "backup/config/update", "create_backup": create_backup_settings} {"type": "backup/config/update", "create_backup": create_backup_settings}
@ -624,10 +618,8 @@ async def test_generate_with_default_settings_calls_create(
is None is None
) )
with patch.object(remote_agent, "async_upload_backup", side_effect=side_effect): mock_agents["test.remote"].async_upload_backup.side_effect = side_effect
await client.send_json_auto_id( await client.send_json_auto_id({"type": "backup/generate_with_automatic_settings"})
{"type": "backup/generate_with_automatic_settings"}
)
result = await client.receive_json() result = await client.receive_json()
assert result["success"] assert result["success"]
assert result["result"] == {"backup_job_id": "abc123"} assert result["result"] == {"backup_job_id": "abc123"}
@ -893,8 +885,9 @@ async def test_agents_info(
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test getting backup agents info.""" """Test getting backup agents info."""
await setup_backup_integration(hass, with_hassio=False) await setup_backup_integration(
hass.data[DATA_MANAGER].backup_agents["domain.test"] = mock_backup_agent("test") hass, with_hassio=False, remote_agents=["test.remote"]
)
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await hass.async_block_till_done() await hass.async_block_till_done()