diff --git a/homeassistant/components/backup/util.py b/homeassistant/components/backup/util.py index c4d69789aac1cc..0cedc07443a84e 100644 --- a/homeassistant/components/backup/util.py +++ b/homeassistant/components/backup/util.py @@ -61,7 +61,7 @@ def read_backup(backup_path: Path) -> AgentBackup: backup_id=cast(str, data["slug"]), database_included=database_included, date=cast(str, data["date"]), - extra_metadata=cast(dict[str, bool | str], data.get("metadata", {})), + extra_metadata=cast(dict[str, bool | str], data.get("extra", {})), folders=folders, homeassistant_included=homeassistant_included, homeassistant_version=homeassistant_version, diff --git a/tests/components/backup/test_util.py b/tests/components/backup/test_util.py new file mode 100644 index 00000000000000..888029f2e35428 --- /dev/null +++ b/tests/components/backup/test_util.py @@ -0,0 +1,88 @@ +"""Tests for the Backup integration's utility functions.""" + +from __future__ import annotations + +from unittest.mock import Mock, patch + +import pytest + +from homeassistant.components.backup import AddonInfo, AgentBackup, Folder +from homeassistant.components.backup.util import read_backup + + +@pytest.mark.parametrize( + ("backup_json_content", "expected_backup"), + [ + ( + b'{"compressed":true,"date":"2024-12-02T07:23:58.261875-05:00","homeassistant":' + b'{"exclude_database":true,"version":"2024.12.0.dev0"},"name":"test",' + b'"protected":true,"slug":"455645fe","type":"partial","version":2}', + AgentBackup( + addons=[], + backup_id="455645fe", + date="2024-12-02T07:23:58.261875-05:00", + database_included=False, + extra_metadata={}, + folders=[], + homeassistant_included=True, + homeassistant_version="2024.12.0.dev0", + name="test", + protected=True, + size=1234, + ), + ), + ( + b'{"slug":"d4b8fdc6","version":2,"name":"Core 2025.1.0.dev0",' + b'"date":"2024-12-20T11:27:51.119062+00:00","type":"partial",' + b'"supervisor_version":"2024.12.1.dev1803",' + b'"extra":{"instance_id":"6b453733d2d74d2a9ae432ff2fbaaa64",' + b'"with_automatic_settings":false},"homeassistant":' + b'{"version":"2025.1.0.dev202412200230","exclude_database":false,"size":0.0},' + b'"compressed":true,"protected":true,"repositories":[' + b'"https://github.com/home-assistant/hassio-addons-development","local",' + b'"https://github.com/esphome/home-assistant-addon","core",' + b'"https://github.com/music-assistant/home-assistant-addon",' + b'"https://github.com/hassio-addons/repository"],"crypto":"aes128",' + b'"folders":["share","media"],"addons":[{"slug":"core_configurator",' + b'"name":"File editor","version":"5.5.0","size":0.0},' + b'{"slug":"ae6e943c_remote_api","name":"Remote API proxy",' + b'"version":"1.3.0","size":0.0}],"docker":{"registries":{}}}', + AgentBackup( + addons=[ + AddonInfo( + name="File editor", + slug="core_configurator", + version="5.5.0", + ), + AddonInfo( + name="Remote API proxy", + slug="ae6e943c_remote_api", + version="1.3.0", + ), + ], + backup_id="d4b8fdc6", + date="2024-12-20T11:27:51.119062+00:00", + database_included=True, + extra_metadata={ + "instance_id": "6b453733d2d74d2a9ae432ff2fbaaa64", + "with_automatic_settings": False, + }, + folders=[Folder.SHARE, Folder.MEDIA], + homeassistant_included=True, + homeassistant_version="2025.1.0.dev202412200230", + name="Core 2025.1.0.dev0", + protected=True, + size=1234, + ), + ), + ], +) +def test_read_backup(backup_json_content: bytes, expected_backup: AgentBackup) -> None: + """Test reading a backup.""" + mock_path = Mock() + mock_path.stat.return_value.st_size = 1234 + + with patch("homeassistant.components.backup.util.tarfile.open") as mock_open_tar: + mock_open_tar.return_value.__enter__.return_value.extractfile().read.return_value = backup_json_content + backup = read_backup(mock_path) + assert backup == expected_backup