mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-12-24 12:29:08 +00:00
Always validate Backup before restoring (#5632)
* Validate Backup always before restoring Since #5519 we check the encryption password early in restore case. This has the side effect that we check the file existance early too. However, in the non-encryption case, the file is not checked early. This PR changes the behavior to always validate the backup file before restoring, ensuring both encryption and non-encryption cases are handled consistently. In particular, the last case of test_restore_immediate_errors actually validates that behavior. That test should actually have failed so far. But it seems that because we validate the backup shortly after freeze anyways, the exception still got raised early enough. A simply `await asyncio.sleep(10)` right after the freeze makes the test case fail. With this change, the test works consistently. * Address pylint * Fix backup_manager tests * Drop warning message
This commit is contained in:
@@ -11,7 +11,7 @@ import pytest
|
||||
from supervisor.backups.backup import Backup
|
||||
from supervisor.backups.const import BackupType
|
||||
from supervisor.coresys import CoreSys
|
||||
from supervisor.exceptions import BackupFileNotFoundError
|
||||
from supervisor.exceptions import BackupFileNotFoundError, BackupInvalidError
|
||||
|
||||
from tests.common import get_fixture_path
|
||||
|
||||
@@ -79,22 +79,21 @@ async def test_consolidate(
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
"tarfile_side_effect, securetar_side_effect, expected_result, expect_exception",
|
||||
"tarfile_side_effect, securetar_side_effect, expected_exception",
|
||||
[
|
||||
(None, None, True, False), # Successful validation
|
||||
(FileNotFoundError, None, None, True), # File not found
|
||||
(None, tarfile.ReadError, False, False), # Invalid password
|
||||
(None, None, None), # Successful validation
|
||||
(FileNotFoundError, None, BackupFileNotFoundError), # File not found
|
||||
(None, tarfile.ReadError, BackupInvalidError), # Invalid password
|
||||
],
|
||||
)
|
||||
async def test_validate_password(
|
||||
async def test_validate_backup(
|
||||
coresys: CoreSys,
|
||||
tmp_path: Path,
|
||||
tarfile_side_effect,
|
||||
securetar_side_effect,
|
||||
expected_result,
|
||||
expect_exception,
|
||||
expected_exception,
|
||||
):
|
||||
"""Parameterized test for validate_password."""
|
||||
"""Parameterized test for validate_backup."""
|
||||
enc_tar = Path(copy(get_fixture_path("backup_example_enc.tar"), tmp_path))
|
||||
enc_backup = Backup(coresys, enc_tar, "test", None)
|
||||
await enc_backup.load()
|
||||
@@ -120,9 +119,8 @@ async def test_validate_password(
|
||||
MagicMock(side_effect=securetar_side_effect),
|
||||
),
|
||||
):
|
||||
if expect_exception:
|
||||
with pytest.raises(BackupFileNotFoundError):
|
||||
await enc_backup.validate_password(None)
|
||||
if expected_exception:
|
||||
with pytest.raises(expected_exception):
|
||||
await enc_backup.validate_backup(None)
|
||||
else:
|
||||
result = await enc_backup.validate_password(None)
|
||||
assert result == expected_result
|
||||
await enc_backup.validate_backup(None)
|
||||
|
||||
Reference in New Issue
Block a user