Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor local agent backup tests #132683

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 198 additions & 0 deletions tests/components/backup/snapshots/test_backup.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
# serializer version: 1
# name: test_delete_backup[found_backups0-True-1]
dict({
'id': 1,
'result': dict({
'agent_errors': dict({
}),
}),
'success': True,
'type': 'result',
})
# ---
# name: test_delete_backup[found_backups1-False-0]
dict({
'id': 1,
'result': dict({
'agent_errors': dict({
}),
}),
'success': True,
'type': 'result',
})
# ---
# name: test_delete_backup[found_backups2-True-0]
dict({
'id': 1,
'result': dict({
'agent_errors': dict({
}),
}),
'success': True,
'type': 'result',
})
# ---
# name: test_load_backups[None]
dict({
'id': 1,
'result': dict({
'agents': list([
dict({
'agent_id': 'backup.local',
}),
]),
}),
'success': True,
'type': 'result',
})
# ---
# name: test_load_backups[None].1
dict({
'id': 2,
'result': dict({
'agent_errors': dict({
}),
'backups': list([
dict({
'addons': list([
dict({
'name': 'Test',
'slug': 'test',
'version': '1.0.0',
}),
]),
'agent_ids': list([
'backup.local',
]),
'backup_id': 'abc123',
'database_included': True,
'date': '1970-01-01T00:00:00.000Z',
'folders': list([
'media',
'share',
]),
'homeassistant_included': True,
'homeassistant_version': '2024.12.0',
'name': 'Test',
'protected': False,
'size': 0.0,
}),
]),
'last_automatic_backup': None,
}),
'success': True,
'type': 'result',
})
# ---
# name: test_load_backups[side_effect1]
dict({
'id': 1,
'result': dict({
'agents': list([
dict({
'agent_id': 'backup.local',
}),
]),
}),
'success': True,
'type': 'result',
})
# ---
# name: test_load_backups[side_effect1].1
dict({
'id': 2,
'result': dict({
'agent_errors': dict({
}),
'backups': list([
]),
'last_automatic_backup': None,
}),
'success': True,
'type': 'result',
})
# ---
# name: test_load_backups[side_effect2]
dict({
'id': 1,
'result': dict({
'agents': list([
dict({
'agent_id': 'backup.local',
}),
]),
}),
'success': True,
'type': 'result',
})
# ---
# name: test_load_backups[side_effect2].1
dict({
'id': 2,
'result': dict({
'agent_errors': dict({
}),
'backups': list([
]),
'last_automatic_backup': None,
}),
'success': True,
'type': 'result',
})
# ---
# name: test_load_backups[side_effect3]
dict({
'id': 1,
'result': dict({
'agents': list([
dict({
'agent_id': 'backup.local',
}),
]),
}),
'success': True,
'type': 'result',
})
# ---
# name: test_load_backups[side_effect3].1
dict({
'id': 2,
'result': dict({
'agent_errors': dict({
}),
'backups': list([
]),
'last_automatic_backup': None,
}),
'success': True,
'type': 'result',
})
# ---
# name: test_load_backups[side_effect4]
dict({
'id': 1,
'result': dict({
'agents': list([
dict({
'agent_id': 'backup.local',
}),
]),
}),
'success': True,
'type': 'result',
})
# ---
# name: test_load_backups[side_effect4].1
dict({
'id': 2,
'result': dict({
'agent_errors': dict({
}),
'backups': list([
]),
'last_automatic_backup': None,
}),
'success': True,
'type': 'result',
})
# ---
22 changes: 0 additions & 22 deletions tests/components/backup/snapshots/test_manager.ambr

This file was deleted.

138 changes: 138 additions & 0 deletions tests/components/backup/test_backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
"""Test the builtin backup platform."""

from __future__ import annotations

from collections.abc import Generator
from io import StringIO
import json
from pathlib import Path
from tarfile import TarError
from unittest.mock import MagicMock, mock_open, patch

import pytest
from syrupy import SnapshotAssertion

from homeassistant.components.backup import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

from .common import TEST_BACKUP_ABC123, TEST_BACKUP_PATH_ABC123

from tests.typing import ClientSessionGenerator, WebSocketGenerator


@pytest.fixture(name="read_backup")
def read_backup_fixture(path_glob: MagicMock) -> Generator[MagicMock]:
"""Mock read backup."""
with patch(
"homeassistant.components.backup.backup.read_backup",
return_value=TEST_BACKUP_ABC123,
) as read_backup:
yield read_backup


@pytest.fixture(name="path_glob")
def path_glob_fixture() -> Generator[MagicMock]:
"""Mock path glob."""
with patch(
"pathlib.Path.glob", return_value=[TEST_BACKUP_PATH_ABC123]
) as path_glob:
yield path_glob


@pytest.mark.parametrize(
"side_effect",
[
None,
OSError("Boom"),
TarError("Boom"),
json.JSONDecodeError("Boom", "test", 1),
KeyError("Boom"),
],
)
async def test_load_backups(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
snapshot: SnapshotAssertion,
read_backup: MagicMock,
side_effect: Exception | None,
) -> None:
"""Test load backups."""
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
client = await hass_ws_client(hass)
read_backup.side_effect = side_effect

# list agents
await client.send_json_auto_id({"type": "backup/agents/info"})
assert await client.receive_json() == snapshot

# load and list backups
await client.send_json_auto_id({"type": "backup/info"})
assert await client.receive_json() == snapshot


async def test_upload(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
) -> None:
"""Test upload backup."""
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
client = await hass_client()
open_mock = mock_open()

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,
),
):
resp = await client.post(
"/api/backup/upload?agent_id=backup.local",
data={"file": StringIO("test")},
)

assert resp.status == 201
assert open_mock.call_count == 1
assert move_mock.call_count == 1
assert move_mock.mock_calls[0].args[1].name == "abc123.tar"


@pytest.mark.usefixtures("read_backup")
@pytest.mark.parametrize(
("found_backups", "backup_exists", "unlink_calls"),
[
([TEST_BACKUP_PATH_ABC123], True, 1),
([TEST_BACKUP_PATH_ABC123], False, 0),
(([], True, 0)),
],
)
async def test_delete_backup(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
hass_ws_client: WebSocketGenerator,
snapshot: SnapshotAssertion,
path_glob: MagicMock,
found_backups: list[Path],
backup_exists: bool,
unlink_calls: int,
) -> None:
"""Test delete backup."""
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
client = await hass_ws_client(hass)
path_glob.return_value = found_backups

with (
patch("pathlib.Path.exists", return_value=backup_exists),
patch("pathlib.Path.unlink") as unlink,
):
await client.send_json_auto_id(
{"type": "backup/delete", "backup_id": TEST_BACKUP_ABC123.backup_id}
)
assert await client.receive_json() == snapshot

assert unlink.call_count == unlink_calls
Loading
Loading