From dc62f21ee7b7e8ceb68e4084764961aae8de6df1 Mon Sep 17 00:00:00 2001 From: Michael Wallace Date: Fri, 11 Aug 2023 16:44:22 +0100 Subject: [PATCH] Add test for minimum_redundancy with REDUNDANCY policy Adds a test which verifies how minimum_redundancy interacts with the REDUNDANCY retention policy. This does not really make sense as a command which a user would run however it is still a valid retention policy and Barman will apply it. --- tests/test_barman_cloud_backup_delete.py | 66 ++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tests/test_barman_cloud_backup_delete.py b/tests/test_barman_cloud_backup_delete.py index 5cb5b1940..637028bd6 100644 --- a/tests/test_barman_cloud_backup_delete.py +++ b/tests/test_barman_cloud_backup_delete.py @@ -472,6 +472,72 @@ def test_delete_by_recovery_window_policy_with_redundancy( get_cloud_interface_mock, backup_metadata, expected_deleted_backups ) + @pytest.mark.parametrize( + ("minimum_redundancy", "num_backups_deleted"), + ( + # With a minimum_redundancy of zero, three of the four backups should be + # deleted (the newest is retained due to REDUNDANCY 1). + (0, 3), + # With a minimum_redundancy of one, three of the four backups should be + # deleted. + (1, 3), + # With a minimum_redundancy of two, two of the four backups should be + # deleted. + (2, 2), + # With a minimum_redundancy of three, one of the four backups should be + # deleted. + (3, 1), + # With a minimum_redundancy of four, none of the four backups should be + # deleted. + (4, 0), + ), + ) + @mock.patch("barman.retention_policies.datetime") + @mock.patch("barman.clients.cloud_backup_delete.CloudBackupCatalog") + @mock.patch("barman.clients.cloud_backup_delete.get_cloud_interface") + def test_delete_by_redundancy_policy_with_minimum_redundancy( + self, + get_cloud_interface_mock, + cloud_backup_catalog_mock, + datetime_mock, + minimum_redundancy, + num_backups_deleted, + ): + """ + Test that the minimum redundancy value is applied to recovery window + retention policy + """ + # GIVEN a backup catalog with four daily backups and no WALs + out_of_policy_backup_ids = [ + "20210723T095432", # Eligible for deletion + "20210724T095432", # Eligible for deletion + "20210725T095432", # Eligible for deletion + "20210726T095432", # Required by REDUNDANCY 1 + ] + backup_metadata = self._create_backup_metadata(out_of_policy_backup_ids) + + # AND a CloudBackupCatalog which returns the backup_info for only those backups + cloud_backup_catalog_mock.return_value = self._create_catalog(backup_metadata) + + # WHEN barman-cloud-backup-delete runs, specifying a retention policy of + # `REDUNDANCY 1` and a given minimum_redundancy. + cloud_backup_delete.main( + [ + "cloud_storage_url", + "test_server", + "--retention-policy", + "REDUNDANCY 1", + "--minimum-redundancy", + str(minimum_redundancy), + ] + ) + + # THEN only the expected backups are deleted + expected_deleted_backups = out_of_policy_backup_ids[:num_backups_deleted] + self._verify_only_these_backups_deleted( + get_cloud_interface_mock, backup_metadata, expected_deleted_backups + ) + @mock.patch("barman.clients.cloud_backup_delete.CloudBackupCatalog") @mock.patch( "barman.clients.cloud_backup_delete.get_snapshot_interface_from_backup_info"