From 39e4719a43051d364d13195e49452c1fcf5612a5 Mon Sep 17 00:00:00 2001 From: Martin Hjelmare Date: Thu, 12 Dec 2024 18:47:37 +0100 Subject: [PATCH] Fix backup strategy retention filter (#133060) * Fix lint * Update tests * Fix backup strategy retention filter --- homeassistant/components/backup/config.py | 9 +- tests/components/backup/test_websocket.py | 307 +++++++++++++++++++--- 2 files changed, 275 insertions(+), 41 deletions(-) diff --git a/homeassistant/components/backup/config.py b/homeassistant/components/backup/config.py index 32dfa95509c..26ce691a4cc 100644 --- a/homeassistant/components/backup/config.py +++ b/homeassistant/components/backup/config.py @@ -423,7 +423,14 @@ async def _delete_filtered_backups( get_agent_errors, ) - LOGGER.debug("Total backups: %s", backups) + # only delete backups that are created by the backup strategy + backups = { + backup_id: backup + for backup_id, backup in backups.items() + if backup.with_strategy_settings + } + + LOGGER.debug("Total strategy backups: %s", backups) filtered_backups = backup_filter(backups) diff --git a/tests/components/backup/test_websocket.py b/tests/components/backup/test_websocket.py index 518005e8470..4a94689c19e 100644 --- a/tests/components/backup/test_websocket.py +++ b/tests/components/backup/test_websocket.py @@ -14,6 +14,7 @@ from homeassistant.components.backup.const import DATA_MANAGER, DOMAIN from homeassistant.components.backup.manager import ( CreateBackupEvent, CreateBackupState, + ManagerBackup, NewBackup, ) from homeassistant.core import HomeAssistant @@ -42,7 +43,7 @@ BACKUP_CALL = call( on_progress=ANY, ) -DEFAULT_STORAGE_DATA = { +DEFAULT_STORAGE_DATA: dict[str, Any] = { "backups": {}, "config": { "create_backup": { @@ -1248,9 +1249,26 @@ async def test_config_schedule_logic( "schedule": "daily", }, { - "backup-1": MagicMock(date="2024-11-10T04:45:00+01:00"), - "backup-2": MagicMock(date="2024-11-11T04:45:00+01:00"), - "backup-3": MagicMock(date="2024-11-12T04:45:00+01:00"), + "backup-1": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-2": MagicMock( + date="2024-11-11T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-3": MagicMock( + date="2024-11-12T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-4": MagicMock( + date="2024-11-12T04:45:00+01:00", + with_strategy_settings=False, + spec=ManagerBackup, + ), }, {}, {}, @@ -1270,9 +1288,26 @@ async def test_config_schedule_logic( "schedule": "daily", }, { - "backup-1": MagicMock(date="2024-11-10T04:45:00+01:00"), - "backup-2": MagicMock(date="2024-11-11T04:45:00+01:00"), - "backup-3": MagicMock(date="2024-11-12T04:45:00+01:00"), + "backup-1": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-2": MagicMock( + date="2024-11-11T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-3": MagicMock( + date="2024-11-12T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-4": MagicMock( + date="2024-11-12T04:45:00+01:00", + with_strategy_settings=False, + spec=ManagerBackup, + ), }, {}, {}, @@ -1292,10 +1327,31 @@ async def test_config_schedule_logic( "schedule": "daily", }, { - "backup-1": MagicMock(date="2024-11-09T04:45:00+01:00"), - "backup-2": MagicMock(date="2024-11-10T04:45:00+01:00"), - "backup-3": MagicMock(date="2024-11-11T04:45:00+01:00"), - "backup-4": MagicMock(date="2024-11-12T04:45:00+01:00"), + "backup-1": MagicMock( + date="2024-11-09T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-2": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-3": MagicMock( + date="2024-11-11T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-4": MagicMock( + date="2024-11-12T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-5": MagicMock( + date="2024-11-12T04:45:00+01:00", + with_strategy_settings=False, + spec=ManagerBackup, + ), }, {}, {}, @@ -1315,10 +1371,31 @@ async def test_config_schedule_logic( "schedule": "daily", }, { - "backup-1": MagicMock(date="2024-11-09T04:45:00+01:00"), - "backup-2": MagicMock(date="2024-11-10T04:45:00+01:00"), - "backup-3": MagicMock(date="2024-11-11T04:45:00+01:00"), - "backup-4": MagicMock(date="2024-11-12T04:45:00+01:00"), + "backup-1": MagicMock( + date="2024-11-09T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-2": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-3": MagicMock( + date="2024-11-11T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-4": MagicMock( + date="2024-11-12T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-5": MagicMock( + date="2024-11-12T04:45:00+01:00", + with_strategy_settings=False, + spec=ManagerBackup, + ), }, {}, {}, @@ -1338,9 +1415,26 @@ async def test_config_schedule_logic( "schedule": "daily", }, { - "backup-1": MagicMock(date="2024-11-10T04:45:00+01:00"), - "backup-2": MagicMock(date="2024-11-11T04:45:00+01:00"), - "backup-3": MagicMock(date="2024-11-12T04:45:00+01:00"), + "backup-1": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-2": MagicMock( + date="2024-11-11T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-3": MagicMock( + date="2024-11-12T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-4": MagicMock( + date="2024-11-12T04:45:00+01:00", + with_strategy_settings=False, + spec=ManagerBackup, + ), }, {"test-agent": BackupAgentError("Boom!")}, {}, @@ -1360,9 +1454,26 @@ async def test_config_schedule_logic( "schedule": "daily", }, { - "backup-1": MagicMock(date="2024-11-10T04:45:00+01:00"), - "backup-2": MagicMock(date="2024-11-11T04:45:00+01:00"), - "backup-3": MagicMock(date="2024-11-12T04:45:00+01:00"), + "backup-1": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-2": MagicMock( + date="2024-11-11T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-3": MagicMock( + date="2024-11-12T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-4": MagicMock( + date="2024-11-12T04:45:00+01:00", + with_strategy_settings=False, + spec=ManagerBackup, + ), }, {}, {"test-agent": BackupAgentError("Boom!")}, @@ -1382,10 +1493,31 @@ async def test_config_schedule_logic( "schedule": "daily", }, { - "backup-1": MagicMock(date="2024-11-09T04:45:00+01:00"), - "backup-2": MagicMock(date="2024-11-10T04:45:00+01:00"), - "backup-3": MagicMock(date="2024-11-11T04:45:00+01:00"), - "backup-4": MagicMock(date="2024-11-12T04:45:00+01:00"), + "backup-1": MagicMock( + date="2024-11-09T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-2": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-3": MagicMock( + date="2024-11-11T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-4": MagicMock( + date="2024-11-12T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-5": MagicMock( + date="2024-11-12T04:45:00+01:00", + with_strategy_settings=False, + spec=ManagerBackup, + ), }, {}, {}, @@ -1405,7 +1537,16 @@ async def test_config_schedule_logic( "schedule": "daily", }, { - "backup-1": MagicMock(date="2024-11-12T04:45:00+01:00"), + "backup-1": MagicMock( + date="2024-11-12T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-2": MagicMock( + date="2024-11-12T04:45:00+01:00", + with_strategy_settings=False, + spec=ManagerBackup, + ), }, {}, {}, @@ -1518,8 +1659,21 @@ async def test_config_retention_copies_logic( "schedule": "never", }, { - "backup-1": MagicMock(date="2024-11-10T04:45:00+01:00"), - "backup-2": MagicMock(date="2024-11-11T04:45:00+01:00"), + "backup-1": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-2": MagicMock( + date="2024-11-11T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-3": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=False, + spec=ManagerBackup, + ), }, {}, {}, @@ -1538,8 +1692,21 @@ async def test_config_retention_copies_logic( "schedule": "never", }, { - "backup-1": MagicMock(date="2024-11-10T04:45:00+01:00"), - "backup-2": MagicMock(date="2024-11-11T04:45:00+01:00"), + "backup-1": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-2": MagicMock( + date="2024-11-11T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-3": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=False, + spec=ManagerBackup, + ), }, {}, {}, @@ -1558,9 +1725,26 @@ async def test_config_retention_copies_logic( "schedule": "never", }, { - "backup-1": MagicMock(date="2024-11-09T04:45:00+01:00"), - "backup-2": MagicMock(date="2024-11-10T04:45:00+01:00"), - "backup-3": MagicMock(date="2024-11-11T04:45:00+01:00"), + "backup-1": MagicMock( + date="2024-11-09T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-2": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-3": MagicMock( + date="2024-11-11T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-4": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=False, + spec=ManagerBackup, + ), }, {}, {}, @@ -1579,8 +1763,21 @@ async def test_config_retention_copies_logic( "schedule": "never", }, { - "backup-1": MagicMock(date="2024-11-10T04:45:00+01:00"), - "backup-2": MagicMock(date="2024-11-11T04:45:00+01:00"), + "backup-1": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-2": MagicMock( + date="2024-11-11T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-3": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=False, + spec=ManagerBackup, + ), }, {"test-agent": BackupAgentError("Boom!")}, {}, @@ -1599,8 +1796,21 @@ async def test_config_retention_copies_logic( "schedule": "never", }, { - "backup-1": MagicMock(date="2024-11-10T04:45:00+01:00"), - "backup-2": MagicMock(date="2024-11-11T04:45:00+01:00"), + "backup-1": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-2": MagicMock( + date="2024-11-11T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-3": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=False, + spec=ManagerBackup, + ), }, {}, {"test-agent": BackupAgentError("Boom!")}, @@ -1619,9 +1829,26 @@ async def test_config_retention_copies_logic( "schedule": "never", }, { - "backup-1": MagicMock(date="2024-11-09T04:45:00+01:00"), - "backup-2": MagicMock(date="2024-11-10T04:45:00+01:00"), - "backup-3": MagicMock(date="2024-11-11T04:45:00+01:00"), + "backup-1": MagicMock( + date="2024-11-09T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-2": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-3": MagicMock( + date="2024-11-11T04:45:00+01:00", + with_strategy_settings=True, + spec=ManagerBackup, + ), + "backup-4": MagicMock( + date="2024-11-10T04:45:00+01:00", + with_strategy_settings=False, + spec=ManagerBackup, + ), }, {}, {},