|
4 | 4 | from pathlib import Path, PurePath
|
5 | 5 | from shutil import copy
|
6 | 6 | from typing import Any
|
7 |
| -from unittest.mock import ANY, AsyncMock, PropertyMock, patch |
| 7 | +from unittest.mock import ANY, AsyncMock, MagicMock, PropertyMock, patch |
8 | 8 |
|
9 | 9 | from aiohttp import MultipartWriter
|
10 | 10 | from aiohttp.test_utils import TestClient
|
@@ -955,6 +955,68 @@ async def test_restore_backup_from_location(
|
955 | 955 | assert test_file.is_file()
|
956 | 956 |
|
957 | 957 |
|
| 958 | +@pytest.mark.usefixtures("tmp_supervisor_data") |
| 959 | +async def test_restore_backup_unencrypted_after_encrypted( |
| 960 | + api_client: TestClient, |
| 961 | + coresys: CoreSys, |
| 962 | +): |
| 963 | + """Test restoring an unencrypted backup after an encrypted backup and vis-versa.""" |
| 964 | + enc_tar = copy(get_fixture_path("test_consolidate.tar"), coresys.config.path_backup) |
| 965 | + unc_tar = copy( |
| 966 | + get_fixture_path("test_consolidate_unc.tar"), coresys.config.path_core_backup |
| 967 | + ) |
| 968 | + await coresys.backups.reload() |
| 969 | + |
| 970 | + backup = coresys.backups.get("d9c48f8b") |
| 971 | + assert backup.all_locations == { |
| 972 | + None: {"path": Path(enc_tar), "protected": True}, |
| 973 | + ".cloud_backup": {"path": Path(unc_tar), "protected": False}, |
| 974 | + } |
| 975 | + |
| 976 | + # pylint: disable=fixme |
| 977 | + # TODO: There is a bug in the restore code that causes the restore to fail |
| 978 | + # if the backup contains a Docker registry configuration and one location |
| 979 | + # is encrypted and the other is not (just like our test fixture). |
| 980 | + # We punt the ball on this one for this PR since this is a rare edge case. |
| 981 | + backup.restore_dockerconfig = MagicMock() |
| 982 | + |
| 983 | + coresys.core.state = CoreState.RUNNING |
| 984 | + coresys.hardware.disk.get_disk_free_space = lambda x: 5000 |
| 985 | + |
| 986 | + # Restore encrypted backup |
| 987 | + (test_file := coresys.config.path_ssl / "test.txt").touch() |
| 988 | + resp = await api_client.post( |
| 989 | + f"/backups/{backup.slug}/restore/partial", |
| 990 | + json={"location": None, "password": "test", "folders": ["ssl"]}, |
| 991 | + ) |
| 992 | + assert resp.status == 200 |
| 993 | + body = await resp.json() |
| 994 | + assert body["result"] == "ok" |
| 995 | + assert not test_file.is_file() |
| 996 | + |
| 997 | + # Restore unencrypted backup |
| 998 | + test_file.touch() |
| 999 | + resp = await api_client.post( |
| 1000 | + f"/backups/{backup.slug}/restore/partial", |
| 1001 | + json={"location": ".cloud_backup", "folders": ["ssl"]}, |
| 1002 | + ) |
| 1003 | + assert resp.status == 200 |
| 1004 | + body = await resp.json() |
| 1005 | + assert body["result"] == "ok" |
| 1006 | + assert not test_file.is_file() |
| 1007 | + |
| 1008 | + # Restore encrypted backup |
| 1009 | + test_file.touch() |
| 1010 | + resp = await api_client.post( |
| 1011 | + f"/backups/{backup.slug}/restore/partial", |
| 1012 | + json={"location": None, "password": "test", "folders": ["ssl"]}, |
| 1013 | + ) |
| 1014 | + assert resp.status == 200 |
| 1015 | + body = await resp.json() |
| 1016 | + assert body["result"] == "ok" |
| 1017 | + assert not test_file.is_file() |
| 1018 | + |
| 1019 | + |
958 | 1020 | @pytest.mark.parametrize(
|
959 | 1021 | ("backup_type", "postbody"), [("partial", {"homeassistant": True}), ("full", {})]
|
960 | 1022 | )
|
|
0 commit comments