Add upload capability to the backup integration (#128546)

* Add upload capability to the backup integration

* Limit context switch

* rename

* coverage for http

* Test receiving a backup file

* Update test_manager.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

---------

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Joakim Sørensen
2024-11-12 15:27:53 +01:00
committed by GitHub
parent cb9cc0f801
commit ac0c75a598
4 changed files with 195 additions and 7 deletions

View File

@@ -3,8 +3,10 @@
from __future__ import annotations
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock, Mock, patch
from unittest.mock import AsyncMock, MagicMock, Mock, mock_open, patch
import aiohttp
from multidict import CIMultiDict, CIMultiDictProxy
import pytest
from homeassistant.components.backup import BackupManager
@@ -335,6 +337,40 @@ async def test_loading_platforms_when_running_async_post_backup_actions(
assert "Loaded 1 platforms" in caplog.text
async def test_async_receive_backup(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test receiving a backup file."""
manager = BackupManager(hass)
size = 2 * 2**16
protocol = Mock(_reading_paused=False)
stream = aiohttp.StreamReader(protocol, 2**16)
stream.feed_data(b"0" * size + b"\r\n--:--")
stream.feed_eof()
open_mock = mock_open()
with patch("pathlib.Path.open", open_mock), patch("shutil.move") as mover_mock:
await manager.async_receive_backup(
contents=aiohttp.BodyPartReader(
b"--:",
CIMultiDictProxy(
CIMultiDict(
{
aiohttp.hdrs.CONTENT_DISPOSITION: "attachment; filename=abc123.tar"
}
)
),
stream,
)
)
assert open_mock.call_count == 1
assert mover_mock.call_count == 1
assert mover_mock.mock_calls[0].args[1].name == "abc123.tar"
async def test_async_trigger_restore(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,