Adjust backup filename scheme (#137424)

* Adjust backup filename scheme

* Update tests
This commit is contained in:
Erik Montnemery 2025-02-05 12:02:58 +01:00 committed by Franck Nijhof
parent e8314fb286
commit 72a69d7e41
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
7 changed files with 20 additions and 22 deletions

View File

@ -122,7 +122,7 @@ def read_backup(backup_path: Path) -> AgentBackup:
def suggested_filename_from_name_date(name: str, date_str: str) -> str: def suggested_filename_from_name_date(name: str, date_str: str) -> str:
"""Suggest a filename for the backup.""" """Suggest a filename for the backup."""
date = dt_util.parse_datetime(date_str, raise_on_error=True) date = dt_util.parse_datetime(date_str, raise_on_error=True)
return "_".join(f"{name} - {date.strftime('%Y-%m-%d %H.%M %S%f')}.tar".split()) return "_".join(f"{name} {date.strftime('%Y-%m-%d %H.%M %S%f')}.tar".split())
def suggested_filename(backup: AgentBackup) -> str: def suggested_filename(backup: AgentBackup) -> str:

View File

@ -103,9 +103,7 @@ async def test_upload(
assert resp.status == 201 assert resp.status == 201
assert open_mock.call_count == 1 assert open_mock.call_count == 1
assert move_mock.call_count == 1 assert move_mock.call_count == 1
assert ( assert move_mock.mock_calls[0].args[1].name == "Test_1970-01-01_00.00_00000000.tar"
move_mock.mock_calls[0].args[1].name == "Test_-_1970-01-01_00.00_00000000.tar"
)
@pytest.mark.usefixtures("read_backup") @pytest.mark.usefixtures("read_backup")

View File

@ -427,7 +427,7 @@ async def test_create_backup_wrong_parameters(
"backups", "backups",
None, None,
"Custom backup 2025.1.0", "Custom backup 2025.1.0",
"Custom_backup_2025.1.0_-_2025-01-30_05.42_12345678.tar", "Custom_backup_2025.1.0_2025-01-30_05.42_12345678.tar",
[LOCAL_AGENT_ID], [LOCAL_AGENT_ID],
[], [],
0, 0,
@ -447,7 +447,7 @@ async def test_create_backup_wrong_parameters(
"backups", "backups",
None, None,
"Custom backup 2025.1.0", "Custom backup 2025.1.0",
"Custom_backup_2025.1.0_-_2025-01-30_05.42_12345678.tar", "Custom_backup_2025.1.0_2025-01-30_05.42_12345678.tar",
[LOCAL_AGENT_ID, "test.remote"], [LOCAL_AGENT_ID, "test.remote"],
[], [],
0, 0,
@ -457,7 +457,7 @@ async def test_create_backup_wrong_parameters(
"backups", "backups",
"custom_name", "custom_name",
"custom_name", "custom_name",
"custom_name_-_2025-01-30_05.42_12345678.tar", "custom_name_2025-01-30_05.42_12345678.tar",
[LOCAL_AGENT_ID], [LOCAL_AGENT_ID],
[], [],
0, 0,
@ -477,7 +477,7 @@ async def test_create_backup_wrong_parameters(
"backups", "backups",
"custom_name", "custom_name",
"custom_name", "custom_name",
"custom_name_-_2025-01-30_05.42_12345678.tar", "custom_name_2025-01-30_05.42_12345678.tar",
[LOCAL_AGENT_ID, "test.remote"], [LOCAL_AGENT_ID, "test.remote"],
[], [],
0, 0,
@ -488,7 +488,7 @@ async def test_create_backup_wrong_parameters(
"backups", "backups",
"custom_name", "custom_name",
"custom_name", "custom_name",
"custom_name_-_2025-01-30_05.42_12345678.tar", "custom_name_2025-01-30_05.42_12345678.tar",
[LOCAL_AGENT_ID], [LOCAL_AGENT_ID],
["test.unavailable"], ["test.unavailable"],
0, 0,
@ -1677,7 +1677,7 @@ async def test_exception_platform_post(hass: HomeAssistant) -> None:
"agent_id=backup.local&agent_id=test.remote", "agent_id=backup.local&agent_id=test.remote",
2, 2,
1, 1,
["Test_-_1970-01-01_00.00_00000000.tar"], ["Test_1970-01-01_00.00_00000000.tar"],
{TEST_BACKUP_ABC123.backup_id: TEST_BACKUP_ABC123}, {TEST_BACKUP_ABC123.backup_id: TEST_BACKUP_ABC123},
b"test", b"test",
0, 0,
@ -1686,7 +1686,7 @@ async def test_exception_platform_post(hass: HomeAssistant) -> None:
"agent_id=backup.local", "agent_id=backup.local",
1, 1,
1, 1,
["Test_-_1970-01-01_00.00_00000000.tar"], ["Test_1970-01-01_00.00_00000000.tar"],
{}, {},
None, None,
0, 0,

View File

@ -529,10 +529,10 @@ async def test_encrypted_backup_streamer_error(hass: HomeAssistant) -> None:
@pytest.mark.parametrize( @pytest.mark.parametrize(
("name", "resulting_filename"), ("name", "resulting_filename"),
[ [
("test", "test_-_2025-01-30_13.42_12345678.tar"), ("test", "test_2025-01-30_13.42_12345678.tar"),
(" leading spaces", "leading_spaces_-_2025-01-30_13.42_12345678.tar"), (" leading spaces", "leading_spaces_2025-01-30_13.42_12345678.tar"),
("trailing spaces ", "trailing_spaces_-_2025-01-30_13.42_12345678.tar"), ("trailing spaces ", "trailing_spaces_2025-01-30_13.42_12345678.tar"),
("double spaces ", "double_spaces_-_2025-01-30_13.42_12345678.tar"), ("double spaces ", "double_spaces_2025-01-30_13.42_12345678.tar"),
], ],
) )
def test_suggested_filename(name: str, resulting_filename: str) -> None: def test_suggested_filename(name: str, resulting_filename: str) -> None:

View File

@ -140,7 +140,7 @@
tuple( tuple(
dict({ dict({
'description': '{"addons": [{"name": "Test", "slug": "test", "version": "1.0.0"}], "backup_id": "test-backup", "date": "2025-01-01T01:23:45.678Z", "database_included": true, "extra_metadata": {"with_automatic_settings": false}, "folders": [], "homeassistant_included": true, "homeassistant_version": "2024.12.0", "name": "Test", "protected": false, "size": 987}', 'description': '{"addons": [{"name": "Test", "slug": "test", "version": "1.0.0"}], "backup_id": "test-backup", "date": "2025-01-01T01:23:45.678Z", "database_included": true, "extra_metadata": {"with_automatic_settings": false}, "folders": [], "homeassistant_included": true, "homeassistant_version": "2024.12.0", "name": "Test", "protected": false, "size": 987}',
'name': 'Test_-_2025-01-01_01.23_45678000.tar', 'name': 'Test_2025-01-01_01.23_45678000.tar',
'parents': list([ 'parents': list([
'HA folder ID', 'HA folder ID',
]), ]),
@ -211,7 +211,7 @@
tuple( tuple(
dict({ dict({
'description': '{"addons": [{"name": "Test", "slug": "test", "version": "1.0.0"}], "backup_id": "test-backup", "date": "2025-01-01T01:23:45.678Z", "database_included": true, "extra_metadata": {"with_automatic_settings": false}, "folders": [], "homeassistant_included": true, "homeassistant_version": "2024.12.0", "name": "Test", "protected": false, "size": 987}', 'description': '{"addons": [{"name": "Test", "slug": "test", "version": "1.0.0"}], "backup_id": "test-backup", "date": "2025-01-01T01:23:45.678Z", "database_included": true, "extra_metadata": {"with_automatic_settings": false}, "folders": [], "homeassistant_included": true, "homeassistant_version": "2024.12.0", "name": "Test", "protected": false, "size": 987}',
'name': 'Test_-_2025-01-01_01.23_45678000.tar', 'name': 'Test_2025-01-01_01.23_45678000.tar',
'parents': list([ 'parents': list([
'new folder id', 'new folder id',
]), ]),

View File

@ -887,7 +887,7 @@ DEFAULT_BACKUP_OPTIONS = supervisor_backups.PartialBackupOptions(
"supervisor.backup_request_date": "2025-01-30T05:42:12.345678-08:00", "supervisor.backup_request_date": "2025-01-30T05:42:12.345678-08:00",
"with_automatic_settings": False, "with_automatic_settings": False,
}, },
filename=PurePath("Test_-_2025-01-30_05.42_12345678.tar"), filename=PurePath("Test_2025-01-30_05.42_12345678.tar"),
folders={"ssl"}, folders={"ssl"},
homeassistant_exclude_database=False, homeassistant_exclude_database=False,
homeassistant=True, homeassistant=True,
@ -1400,7 +1400,7 @@ async def test_reader_writer_create_per_agent_encryption(
upload_locations upload_locations
) )
for call in supervisor_client.backups.upload_backup.mock_calls: for call in supervisor_client.backups.upload_backup.mock_calls:
assert call.args[1].filename == PurePath("Test_-_2025-01-30_05.42_12345678.tar") assert call.args[1].filename == PurePath("Test_2025-01-30_05.42_12345678.tar")
upload_call_locations: set = call.args[1].location upload_call_locations: set = call.args[1].location
assert len(upload_call_locations) == 1 assert len(upload_call_locations) == 1
assert upload_call_locations.pop() in upload_locations assert upload_call_locations.pop() in upload_locations

View File

@ -36,7 +36,7 @@ from .consts import HOST, MACS, PASSWORD, PORT, SERIAL, USE_SSL, USERNAME
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
from tests.typing import ClientSessionGenerator, WebSocketGenerator from tests.typing import ClientSessionGenerator, WebSocketGenerator
BASE_FILENAME = "Automatic_backup_2025.2.0.dev0_-_2025-01-09_20.14_35457323" BASE_FILENAME = "Automatic_backup_2025.2.0.dev0_2025-01-09_20.14_35457323"
class MockStreamReaderChunked(MockStreamReader): class MockStreamReaderChunked(MockStreamReader):
@ -525,7 +525,7 @@ async def test_agents_upload(
protected=True, protected=True,
size=0, size=0,
) )
base_filename = "Test_-_1970-01-01_00.00_00000000" base_filename = "Test_1970-01-01_00.00_00000000"
with ( with (
patch( patch(
@ -576,7 +576,7 @@ async def test_agents_upload_error(
protected=True, protected=True,
size=0, size=0,
) )
base_filename = "Test_-_1970-01-01_00.00_00000000" base_filename = "Test_1970-01-01_00.00_00000000"
# fail to upload the tar file # fail to upload the tar file
with ( with (