Fix logging error for invalid password for backup (#5747)

* Fix logging error for invalid password for backup

* Improved test
This commit is contained in:
Mike Degatano 2025-03-12 15:21:10 -04:00 committed by GitHub
parent 34752466d5
commit 5facf4e790
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 16 deletions

View File

@ -392,7 +392,7 @@ class Backup(JobGroup):
return
except tarfile.ReadError as ex:
raise BackupInvalidError(
f"Invalid password for backup {backup.slug}", _LOGGER.error
f"Invalid password for backup {self.slug}", _LOGGER.error
) from ex
try:

View File

@ -1,5 +1,6 @@
"""Test backups."""
from contextlib import AbstractContextManager, nullcontext as does_not_raise
from os import listdir
from pathlib import Path
from shutil import copy
@ -116,28 +117,44 @@ async def test_consolidate(
}
@pytest.mark.asyncio
@pytest.mark.parametrize(
"tarfile_side_effect, securetar_side_effect, expected_exception",
(
"tarfile_side_effect",
"securetar_side_effect",
"expected_exception",
),
[
(None, None, None), # Successful validation
(FileNotFoundError, None, BackupFileNotFoundError), # File not found
(None, tarfile.ReadError, BackupInvalidError), # Invalid password
(None, None, does_not_raise()), # Successful validation
(
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(
coresys: CoreSys,
tmp_path: Path,
tarfile_side_effect,
securetar_side_effect,
expected_exception,
tarfile_side_effect: type[Exception] | None,
securetar_side_effect: type[Exception] | None,
expected_exception: AbstractContextManager,
):
"""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()
backup_tar_mock = MagicMock()
backup_tar_mock = MagicMock(spec_set=tarfile.TarFile)
backup_tar_mock.getmembers.return_value = [
MagicMock(name="test.tar.gz")
] # Fake tar entries
@ -150,16 +167,14 @@ async def test_validate_backup(
patch(
"tarfile.open",
MagicMock(
return_value=backup_context_mock, side_effect=tarfile_side_effect
return_value=backup_context_mock,
side_effect=tarfile_side_effect,
),
),
patch(
"supervisor.backups.backup.SecureTarFile",
MagicMock(side_effect=securetar_side_effect),
),
expected_exception,
):
if expected_exception:
with pytest.raises(expected_exception):
await enc_backup.validate_backup(None)
else:
await enc_backup.validate_backup(None)