Skip to content

Commit 5facf4e

Browse files
authored
Fix logging error for invalid password for backup (#5747)
* Fix logging error for invalid password for backup * Improved test
1 parent 3475246 commit 5facf4e

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

supervisor/backups/backup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def _validate_file() -> None:
392392
return
393393
except tarfile.ReadError as ex:
394394
raise BackupInvalidError(
395-
f"Invalid password for backup {backup.slug}", _LOGGER.error
395+
f"Invalid password for backup {self.slug}", _LOGGER.error
396396
) from ex
397397

398398
try:

tests/backups/test_backup.py

+30-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Test backups."""
22

3+
from contextlib import AbstractContextManager, nullcontext as does_not_raise
34
from os import listdir
45
from pathlib import Path
56
from shutil import copy
@@ -116,28 +117,44 @@ async def test_consolidate(
116117
}
117118

118119

119-
@pytest.mark.asyncio
120120
@pytest.mark.parametrize(
121-
"tarfile_side_effect, securetar_side_effect, expected_exception",
121+
(
122+
"tarfile_side_effect",
123+
"securetar_side_effect",
124+
"expected_exception",
125+
),
122126
[
123-
(None, None, None), # Successful validation
124-
(FileNotFoundError, None, BackupFileNotFoundError), # File not found
125-
(None, tarfile.ReadError, BackupInvalidError), # Invalid password
127+
(None, None, does_not_raise()), # Successful validation
128+
(
129+
FileNotFoundError,
130+
None,
131+
pytest.raises(
132+
BackupFileNotFoundError,
133+
match=r"Cannot validate backup at [^, ]+, file does not exist!",
134+
),
135+
), # File not found
136+
(
137+
None,
138+
tarfile.ReadError,
139+
pytest.raises(
140+
BackupInvalidError, match="Invalid password for backup 93b462f8"
141+
),
142+
), # Invalid password
126143
],
127144
)
128145
async def test_validate_backup(
129146
coresys: CoreSys,
130147
tmp_path: Path,
131-
tarfile_side_effect,
132-
securetar_side_effect,
133-
expected_exception,
148+
tarfile_side_effect: type[Exception] | None,
149+
securetar_side_effect: type[Exception] | None,
150+
expected_exception: AbstractContextManager,
134151
):
135152
"""Parameterized test for validate_backup."""
136153
enc_tar = Path(copy(get_fixture_path("backup_example_enc.tar"), tmp_path))
137154
enc_backup = Backup(coresys, enc_tar, "test", None)
138155
await enc_backup.load()
139156

140-
backup_tar_mock = MagicMock()
157+
backup_tar_mock = MagicMock(spec_set=tarfile.TarFile)
141158
backup_tar_mock.getmembers.return_value = [
142159
MagicMock(name="test.tar.gz")
143160
] # Fake tar entries
@@ -150,16 +167,14 @@ async def test_validate_backup(
150167
patch(
151168
"tarfile.open",
152169
MagicMock(
153-
return_value=backup_context_mock, side_effect=tarfile_side_effect
170+
return_value=backup_context_mock,
171+
side_effect=tarfile_side_effect,
154172
),
155173
),
156174
patch(
157175
"supervisor.backups.backup.SecureTarFile",
158176
MagicMock(side_effect=securetar_side_effect),
159177
),
178+
expected_exception,
160179
):
161-
if expected_exception:
162-
with pytest.raises(expected_exception):
163-
await enc_backup.validate_backup(None)
164-
else:
165-
await enc_backup.validate_backup(None)
180+
await enc_backup.validate_backup(None)

0 commit comments

Comments
 (0)