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
This commit is contained in:
Martin Hjelmare 2024-11-14 16:03:48 +01:00 committed by GitHub
parent e8179f7a73
commit 8fc470b943
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 165 additions and 2 deletions

View file

@ -11,11 +11,12 @@ from homeassistant.helpers.typing import ConfigType
from .agent import BackupAgent, BackupAgentPlatformProtocol, UploadedBackup
from .const import DOMAIN, LOGGER
from .http import async_register_http_views
from .manager import BackupManager, BackupPlatformProtocol
from .manager import Backup, BackupManager, BackupPlatformProtocol
from .models import BackupUploadMetadata, BaseBackup
from .websocket import async_register_websocket_handlers
__all__ = [
"Backup",
"BackupAgent",
"BackupAgentPlatformProtocol",
"BackupPlatformProtocol",

View file

@ -17,7 +17,7 @@ from homeassistant.core import HomeAssistant
LOGGER = logging.getLogger(__name__)
async def async_get_backup_sync_agents(
async def async_get_backup_agents(
hass: HomeAssistant,
) -> list[BackupAgent]:
"""Register the backup agents."""

View file

@ -0,0 +1,162 @@
"""Test the Kitchen Sink backup platform."""
from collections.abc import AsyncGenerator
from pathlib import Path
from unittest.mock import patch
from uuid import UUID
import pytest
from homeassistant.components.backup import DOMAIN as BACKUP_DOMAIN, Backup
from homeassistant.components.kitchen_sink import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from tests.typing import WebSocketGenerator
@pytest.fixture(autouse=True)
async def backup_only() -> AsyncGenerator[None]:
"""Enable only the backup platform.
The backup platform is not an entity platform.
"""
with patch(
"homeassistant.components.kitchen_sink.COMPONENTS_WITH_DEMO_PLATFORM",
[],
):
yield
@pytest.fixture(autouse=True)
async def setup_integration(hass: HomeAssistant) -> AsyncGenerator[None]:
"""Set up Kitchen Sink integration."""
with patch("homeassistant.components.backup.is_hassio", return_value=True):
assert await async_setup_component(hass, BACKUP_DOMAIN, {BACKUP_DOMAIN: {}})
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
await hass.async_block_till_done()
yield
async def test_agents_info(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test backup agents info."""
client = await hass_ws_client(hass)
await client.send_json_auto_id({"type": "backup/agents/info"})
response = await client.receive_json()
assert response["success"]
assert response["result"] == {
"agents": [{"agent_id": "kitchen_sink.syncer"}],
"syncing": False,
}
async def test_agents_list_backups(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test backup agents list backups."""
client = await hass_ws_client(hass)
await client.send_json_auto_id({"type": "backup/agents/list_backups"})
response = await client.receive_json()
assert response["success"]
assert response["result"] == [
{
"agent_id": "kitchen_sink.syncer",
"date": "1970-01-01T00:00:00Z",
"id": "def456",
"slug": "abc123",
"size": 1234,
"name": "Kitchen sink syncer",
"protected": False,
}
]
async def test_agents_download(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test backup agents download."""
client = await hass_ws_client(hass)
backup_id = "def456"
slug = "abc123"
await client.send_json_auto_id(
{
"type": "backup/agents/download",
"slug": slug,
"agent_id": "kitchen_sink.syncer",
"backup_id": backup_id,
}
)
response = await client.receive_json()
assert response["success"]
path = hass.config.path(f"backup/{slug}.tar")
assert f"Downloading backup {backup_id} to {path}" in caplog.text
async def test_agents_upload(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
caplog: pytest.LogCaptureFixture,
hass_supervisor_access_token: str,
) -> None:
"""Test backup agents upload."""
client = await hass_ws_client(hass, hass_supervisor_access_token)
slug = "test-backup"
test_backup = Backup(
slug=slug,
name="Test",
date="1970-01-01T00:00:00.000Z",
path=Path(hass.config.path(f"backups/{slug}.tar")),
size=0.0,
protected=False,
)
uuid = UUID(int=123456)
with (
patch("homeassistant.components.kitchen_sink.backup.uuid4", return_value=uuid),
patch(
"homeassistant.components.backup.manager.BackupManager.async_get_backup",
) as fetch_backup,
):
fetch_backup.return_value = test_backup
await client.send_json_auto_id(
{
"type": "backup/upload",
"data": {
"slug": slug,
},
}
)
response = await client.receive_json()
assert response["success"]
backup_name = f"{slug}.tar"
assert f"Uploading backup {backup_name}" in caplog.text
with patch("homeassistant.components.kitchen_sink.backup.uuid4", return_value=uuid):
await client.send_json_auto_id({"type": "backup/agents/list_backups"})
response = await client.receive_json()
assert response["success"]
backup_list = response["result"]
assert len(backup_list) == 2
assert backup_list[1] == {
"agent_id": "kitchen_sink.syncer",
"date": test_backup.date,
"id": uuid.hex,
"slug": slug,
"size": 0.0,
"name": test_backup.name,
"protected": test_backup.protected,
}