mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-23 09:06:29 +00:00
Fix logging error for invalid password for backup (#5747)
* Fix logging error for invalid password for backup * Improved test
This commit is contained in:
parent
34752466d5
commit
5facf4e790
@ -392,7 +392,7 @@ class Backup(JobGroup):
|
|||||||
return
|
return
|
||||||
except tarfile.ReadError as ex:
|
except tarfile.ReadError as ex:
|
||||||
raise BackupInvalidError(
|
raise BackupInvalidError(
|
||||||
f"Invalid password for backup {backup.slug}", _LOGGER.error
|
f"Invalid password for backup {self.slug}", _LOGGER.error
|
||||||
) from ex
|
) from ex
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
"""Test backups."""
|
"""Test backups."""
|
||||||
|
|
||||||
|
from contextlib import AbstractContextManager, nullcontext as does_not_raise
|
||||||
from os import listdir
|
from os import listdir
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from shutil import copy
|
from shutil import copy
|
||||||
@ -116,28 +117,44 @@ async def test_consolidate(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"tarfile_side_effect, securetar_side_effect, expected_exception",
|
(
|
||||||
|
"tarfile_side_effect",
|
||||||
|
"securetar_side_effect",
|
||||||
|
"expected_exception",
|
||||||
|
),
|
||||||
[
|
[
|
||||||
(None, None, None), # Successful validation
|
(None, None, does_not_raise()), # Successful validation
|
||||||
(FileNotFoundError, None, BackupFileNotFoundError), # File not found
|
(
|
||||||
(None, tarfile.ReadError, BackupInvalidError), # Invalid password
|
FileNotFoundError,
|
||||||
|
None,
|
||||||
|
pytest.raises(
|
||||||
|
BackupFileNotFoundError,
|
||||||
|
match=r"Cannot validate backup at [^, ]+, file does not exist!",
|
||||||
|
),
|
||||||
|
), # File not found
|
||||||
|
(
|
||||||
|
None,
|
||||||
|
tarfile.ReadError,
|
||||||
|
pytest.raises(
|
||||||
|
BackupInvalidError, match="Invalid password for backup 93b462f8"
|
||||||
|
),
|
||||||
|
), # Invalid password
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def test_validate_backup(
|
async def test_validate_backup(
|
||||||
coresys: CoreSys,
|
coresys: CoreSys,
|
||||||
tmp_path: Path,
|
tmp_path: Path,
|
||||||
tarfile_side_effect,
|
tarfile_side_effect: type[Exception] | None,
|
||||||
securetar_side_effect,
|
securetar_side_effect: type[Exception] | None,
|
||||||
expected_exception,
|
expected_exception: AbstractContextManager,
|
||||||
):
|
):
|
||||||
"""Parameterized test for validate_backup."""
|
"""Parameterized test for validate_backup."""
|
||||||
enc_tar = Path(copy(get_fixture_path("backup_example_enc.tar"), tmp_path))
|
enc_tar = Path(copy(get_fixture_path("backup_example_enc.tar"), tmp_path))
|
||||||
enc_backup = Backup(coresys, enc_tar, "test", None)
|
enc_backup = Backup(coresys, enc_tar, "test", None)
|
||||||
await enc_backup.load()
|
await enc_backup.load()
|
||||||
|
|
||||||
backup_tar_mock = MagicMock()
|
backup_tar_mock = MagicMock(spec_set=tarfile.TarFile)
|
||||||
backup_tar_mock.getmembers.return_value = [
|
backup_tar_mock.getmembers.return_value = [
|
||||||
MagicMock(name="test.tar.gz")
|
MagicMock(name="test.tar.gz")
|
||||||
] # Fake tar entries
|
] # Fake tar entries
|
||||||
@ -150,16 +167,14 @@ async def test_validate_backup(
|
|||||||
patch(
|
patch(
|
||||||
"tarfile.open",
|
"tarfile.open",
|
||||||
MagicMock(
|
MagicMock(
|
||||||
return_value=backup_context_mock, side_effect=tarfile_side_effect
|
return_value=backup_context_mock,
|
||||||
|
side_effect=tarfile_side_effect,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
patch(
|
patch(
|
||||||
"supervisor.backups.backup.SecureTarFile",
|
"supervisor.backups.backup.SecureTarFile",
|
||||||
MagicMock(side_effect=securetar_side_effect),
|
MagicMock(side_effect=securetar_side_effect),
|
||||||
),
|
),
|
||||||
|
expected_exception,
|
||||||
):
|
):
|
||||||
if expected_exception:
|
await enc_backup.validate_backup(None)
|
||||||
with pytest.raises(expected_exception):
|
|
||||||
await enc_backup.validate_backup(None)
|
|
||||||
else:
|
|
||||||
await enc_backup.validate_backup(None)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user