2018-09-08 22:53:37 +02:00
|
|
|
import logging
|
2017-08-06 01:21:39 +02:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
|
|
|
from flexmock import flexmock
|
|
|
|
|
|
|
|
from borgmatic.borg import prune as module
|
2019-05-13 23:39:10 +02:00
|
|
|
|
2018-09-30 22:57:20 +02:00
|
|
|
from ..test_verbosity import insert_logging_mock
|
2017-08-06 01:21:39 +02:00
|
|
|
|
|
|
|
|
2019-09-23 22:07:51 +02:00
|
|
|
def insert_execute_command_mock(prune_command, output_log_level):
|
2022-06-30 22:42:17 +02:00
|
|
|
flexmock(module.environment).should_receive('make_environment')
|
2019-09-23 22:07:51 +02:00
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2022-06-30 22:42:17 +02:00
|
|
|
prune_command,
|
|
|
|
output_log_level=output_log_level,
|
|
|
|
borg_local_path=prune_command[0],
|
|
|
|
extra_environment=None,
|
2019-09-23 22:07:51 +02:00
|
|
|
).once()
|
2017-08-06 01:21:39 +02:00
|
|
|
|
|
|
|
|
2023-04-01 00:21:08 +02:00
|
|
|
BASE_PRUNE_FLAGS = ('--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly', '3')
|
2017-08-06 01:21:39 +02:00
|
|
|
|
|
|
|
|
2023-04-01 00:21:08 +02:00
|
|
|
def test_make_prune_flags_returns_flags_from_config():
|
2018-09-30 07:45:00 +02:00
|
|
|
retention_config = OrderedDict((('keep_daily', 1), ('keep_weekly', 2), ('keep_monthly', 3)))
|
2022-10-04 07:50:37 +02:00
|
|
|
flexmock(module.feature).should_receive('available').and_return(True)
|
2023-04-01 00:21:08 +02:00
|
|
|
flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
|
2017-08-06 01:21:39 +02:00
|
|
|
|
2023-04-01 00:21:08 +02:00
|
|
|
result = module.make_prune_flags({}, retention_config, local_borg_version='1.2.3')
|
2017-08-06 01:21:39 +02:00
|
|
|
|
2023-04-01 00:21:08 +02:00
|
|
|
assert result == BASE_PRUNE_FLAGS
|
2017-08-06 01:21:39 +02:00
|
|
|
|
|
|
|
|
2017-09-03 20:13:14 +02:00
|
|
|
def test_make_prune_flags_accepts_prefix_with_placeholders():
|
2023-03-24 07:11:14 +01:00
|
|
|
retention_config = OrderedDict(
|
|
|
|
(('keep_daily', 1), ('prefix', 'Documents_{hostname}-{now}')) # noqa: FS003
|
|
|
|
)
|
2022-10-04 07:50:37 +02:00
|
|
|
flexmock(module.feature).should_receive('available').and_return(True)
|
2023-04-01 00:21:08 +02:00
|
|
|
flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
|
2017-09-03 20:13:14 +02:00
|
|
|
|
2023-04-01 00:21:08 +02:00
|
|
|
result = module.make_prune_flags({}, retention_config, local_borg_version='1.2.3')
|
2022-10-04 07:50:37 +02:00
|
|
|
|
2023-03-24 07:11:14 +01:00
|
|
|
expected = (
|
2023-04-01 00:21:08 +02:00
|
|
|
'--keep-daily',
|
|
|
|
'1',
|
|
|
|
'--match-archives',
|
|
|
|
'sh:Documents_{hostname}-{now}*', # noqa: FS003
|
2023-03-24 07:11:14 +01:00
|
|
|
)
|
2022-10-04 07:50:37 +02:00
|
|
|
|
2023-04-01 00:21:08 +02:00
|
|
|
assert result == expected
|
2022-10-04 07:50:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_make_prune_flags_with_prefix_without_borg_features_uses_glob_archives():
|
2023-03-24 07:11:14 +01:00
|
|
|
retention_config = OrderedDict(
|
|
|
|
(('keep_daily', 1), ('prefix', 'Documents_{hostname}-{now}')) # noqa: FS003
|
|
|
|
)
|
2022-10-04 07:50:37 +02:00
|
|
|
flexmock(module.feature).should_receive('available').and_return(False)
|
2023-04-01 00:21:08 +02:00
|
|
|
flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
|
2022-10-04 07:50:37 +02:00
|
|
|
|
2023-04-01 00:21:08 +02:00
|
|
|
result = module.make_prune_flags({}, retention_config, local_borg_version='1.2.3')
|
2017-09-03 20:13:14 +02:00
|
|
|
|
2023-03-24 07:11:14 +01:00
|
|
|
expected = (
|
2023-04-01 00:21:08 +02:00
|
|
|
'--keep-daily',
|
|
|
|
'1',
|
|
|
|
'--glob-archives',
|
|
|
|
'Documents_{hostname}-{now}*', # noqa: FS003
|
2023-03-24 07:11:14 +01:00
|
|
|
)
|
2017-09-03 20:13:14 +02:00
|
|
|
|
2023-04-01 00:21:08 +02:00
|
|
|
assert result == expected
|
2019-07-27 23:04:13 +02:00
|
|
|
|
|
|
|
|
2023-04-01 00:21:08 +02:00
|
|
|
def test_make_prune_flags_without_prefix_uses_archive_name_format_instead():
|
|
|
|
storage_config = {'archive_name_format': 'bar-{now}'} # noqa: FS003
|
2019-07-27 23:04:13 +02:00
|
|
|
retention_config = OrderedDict((('keep_daily', 1), ('prefix', None)))
|
2022-10-04 07:50:37 +02:00
|
|
|
flexmock(module.feature).should_receive('available').and_return(True)
|
2023-04-01 00:21:08 +02:00
|
|
|
flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
|
2023-04-02 08:57:55 +02:00
|
|
|
None, 'bar-{now}', '1.2.3' # noqa: FS003
|
2023-04-01 00:21:08 +02:00
|
|
|
).and_return(('--match-archives', 'sh:bar-*'))
|
2019-07-27 23:04:13 +02:00
|
|
|
|
2023-04-01 00:21:08 +02:00
|
|
|
result = module.make_prune_flags(storage_config, retention_config, local_borg_version='1.2.3')
|
2019-07-27 23:04:13 +02:00
|
|
|
|
2023-04-01 00:21:08 +02:00
|
|
|
expected = (
|
|
|
|
'--keep-daily',
|
|
|
|
'1',
|
|
|
|
'--match-archives',
|
|
|
|
'sh:bar-*', # noqa: FS003
|
|
|
|
)
|
2019-07-27 23:04:13 +02:00
|
|
|
|
2023-04-01 00:21:08 +02:00
|
|
|
assert result == expected
|
2019-07-27 23:04:13 +02:00
|
|
|
|
|
|
|
|
2019-09-13 00:27:04 +02:00
|
|
|
PRUNE_COMMAND = ('borg', 'prune', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly', '3')
|
2017-08-06 01:21:39 +02:00
|
|
|
|
|
|
|
|
2018-01-15 01:35:24 +01:00
|
|
|
def test_prune_archives_calls_borg_with_parameters():
|
2022-12-02 21:12:10 +01:00
|
|
|
flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
|
|
|
|
flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
|
2022-10-04 07:50:37 +02:00
|
|
|
flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
|
2022-08-14 07:07:15 +02:00
|
|
|
flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
|
2019-09-23 22:07:51 +02:00
|
|
|
insert_execute_command_mock(PRUNE_COMMAND + ('repo',), logging.INFO)
|
2017-08-06 01:21:39 +02:00
|
|
|
|
|
|
|
module.prune_archives(
|
2022-08-14 02:26:51 +02:00
|
|
|
dry_run=False,
|
2023-03-26 20:22:25 +02:00
|
|
|
repository_path='repo',
|
2022-08-14 02:26:51 +02:00
|
|
|
storage_config={},
|
2022-10-04 07:50:37 +02:00
|
|
|
retention_config=flexmock(),
|
2022-08-14 02:26:51 +02:00
|
|
|
local_borg_version='1.2.3',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-09-08 22:53:37 +02:00
|
|
|
def test_prune_archives_with_log_info_calls_borg_with_info_parameter():
|
2022-12-02 21:12:10 +01:00
|
|
|
flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
|
|
|
|
flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
|
2022-10-04 07:50:37 +02:00
|
|
|
flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
|
2022-08-14 07:07:15 +02:00
|
|
|
flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
|
2020-01-11 16:38:07 +01:00
|
|
|
insert_execute_command_mock(PRUNE_COMMAND + ('--info', 'repo'), logging.INFO)
|
2018-09-08 22:53:37 +02:00
|
|
|
insert_logging_mock(logging.INFO)
|
2017-08-06 01:21:39 +02:00
|
|
|
|
|
|
|
module.prune_archives(
|
2023-03-26 20:22:25 +02:00
|
|
|
repository_path='repo',
|
2022-08-14 02:26:51 +02:00
|
|
|
storage_config={},
|
|
|
|
dry_run=False,
|
2022-10-04 07:50:37 +02:00
|
|
|
retention_config=flexmock(),
|
2022-08-14 02:26:51 +02:00
|
|
|
local_borg_version='1.2.3',
|
2017-08-06 01:21:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-09-08 22:53:37 +02:00
|
|
|
def test_prune_archives_with_log_debug_calls_borg_with_debug_parameter():
|
2022-12-02 21:12:10 +01:00
|
|
|
flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
|
|
|
|
flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
|
2022-10-04 07:50:37 +02:00
|
|
|
flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
|
2022-08-14 07:07:15 +02:00
|
|
|
flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
|
2020-01-23 00:23:49 +01:00
|
|
|
insert_execute_command_mock(PRUNE_COMMAND + ('--debug', '--show-rc', 'repo'), logging.INFO)
|
2018-09-08 22:53:37 +02:00
|
|
|
insert_logging_mock(logging.DEBUG)
|
2017-08-06 01:21:39 +02:00
|
|
|
|
|
|
|
module.prune_archives(
|
2023-03-26 20:22:25 +02:00
|
|
|
repository_path='repo',
|
2022-08-14 02:26:51 +02:00
|
|
|
storage_config={},
|
|
|
|
dry_run=False,
|
2022-10-04 07:50:37 +02:00
|
|
|
retention_config=flexmock(),
|
2022-08-14 02:26:51 +02:00
|
|
|
local_borg_version='1.2.3',
|
2018-01-16 05:55:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_prune_archives_with_dry_run_calls_borg_with_dry_run_parameter():
|
2022-12-02 21:12:10 +01:00
|
|
|
flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
|
|
|
|
flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
|
2022-10-04 07:50:37 +02:00
|
|
|
flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
|
2022-08-14 07:07:15 +02:00
|
|
|
flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
|
2019-09-23 22:07:51 +02:00
|
|
|
insert_execute_command_mock(PRUNE_COMMAND + ('--dry-run', 'repo'), logging.INFO)
|
2018-01-16 05:55:27 +01:00
|
|
|
|
|
|
|
module.prune_archives(
|
2023-03-26 20:22:25 +02:00
|
|
|
repository_path='repo',
|
2022-08-14 02:26:51 +02:00
|
|
|
storage_config={},
|
|
|
|
dry_run=True,
|
2022-10-04 07:50:37 +02:00
|
|
|
retention_config=flexmock(),
|
2022-08-14 02:26:51 +02:00
|
|
|
local_borg_version='1.2.3',
|
2017-08-06 01:21:39 +02:00
|
|
|
)
|
|
|
|
|
2017-09-03 20:13:14 +02:00
|
|
|
|
2018-01-15 01:35:24 +01:00
|
|
|
def test_prune_archives_with_local_path_calls_borg_via_local_path():
|
2022-12-02 21:12:10 +01:00
|
|
|
flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
|
|
|
|
flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
|
2022-10-04 07:50:37 +02:00
|
|
|
flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
|
2022-08-14 07:07:15 +02:00
|
|
|
flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
|
2019-09-23 22:07:51 +02:00
|
|
|
insert_execute_command_mock(('borg1',) + PRUNE_COMMAND[1:] + ('repo',), logging.INFO)
|
2018-01-15 01:35:24 +01:00
|
|
|
|
|
|
|
module.prune_archives(
|
2018-01-16 05:55:27 +01:00
|
|
|
dry_run=False,
|
2023-03-26 20:22:25 +02:00
|
|
|
repository_path='repo',
|
2018-02-20 00:51:04 +01:00
|
|
|
storage_config={},
|
2022-10-04 07:50:37 +02:00
|
|
|
retention_config=flexmock(),
|
2022-08-14 02:26:51 +02:00
|
|
|
local_borg_version='1.2.3',
|
2018-01-15 01:35:24 +01:00
|
|
|
local_path='borg1',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_prune_archives_with_remote_path_calls_borg_with_remote_path_parameters():
|
2022-12-02 21:12:10 +01:00
|
|
|
flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
|
|
|
|
flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
|
2022-10-04 07:50:37 +02:00
|
|
|
flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
|
2022-08-14 07:07:15 +02:00
|
|
|
flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
|
2019-09-23 22:07:51 +02:00
|
|
|
insert_execute_command_mock(PRUNE_COMMAND + ('--remote-path', 'borg1', 'repo'), logging.INFO)
|
2017-08-06 01:21:39 +02:00
|
|
|
|
|
|
|
module.prune_archives(
|
2018-01-16 05:55:27 +01:00
|
|
|
dry_run=False,
|
2023-03-26 20:22:25 +02:00
|
|
|
repository_path='repo',
|
2018-02-20 00:51:04 +01:00
|
|
|
storage_config={},
|
2022-10-04 07:50:37 +02:00
|
|
|
retention_config=flexmock(),
|
2022-08-14 02:26:51 +02:00
|
|
|
local_borg_version='1.2.3',
|
2017-08-06 01:21:39 +02:00
|
|
|
remote_path='borg1',
|
|
|
|
)
|
2018-02-20 00:51:04 +01:00
|
|
|
|
|
|
|
|
2022-12-02 21:12:10 +01:00
|
|
|
def test_prune_archives_with_stats_calls_borg_with_stats_parameter_and_answer_output_log_level():
|
|
|
|
flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
|
|
|
|
flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
|
2022-10-04 07:50:37 +02:00
|
|
|
flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
|
2022-08-14 07:07:15 +02:00
|
|
|
flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
|
2022-12-02 21:12:10 +01:00
|
|
|
insert_execute_command_mock(PRUNE_COMMAND + ('--stats', 'repo'), module.borgmatic.logger.ANSWER)
|
2019-09-23 22:07:51 +02:00
|
|
|
|
|
|
|
module.prune_archives(
|
|
|
|
dry_run=False,
|
2023-03-26 20:22:25 +02:00
|
|
|
repository_path='repo',
|
2019-09-23 22:07:51 +02:00
|
|
|
storage_config={},
|
2022-10-04 07:50:37 +02:00
|
|
|
retention_config=flexmock(),
|
2022-08-14 02:26:51 +02:00
|
|
|
local_borg_version='1.2.3',
|
2019-09-23 22:07:51 +02:00
|
|
|
stats=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-12-02 21:12:10 +01:00
|
|
|
def test_prune_archives_with_files_calls_borg_with_list_parameter_and_answer_output_log_level():
|
|
|
|
flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
|
|
|
|
flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
|
2022-10-04 07:50:37 +02:00
|
|
|
flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
|
2022-08-14 07:07:15 +02:00
|
|
|
flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
|
2022-12-02 21:12:10 +01:00
|
|
|
insert_execute_command_mock(PRUNE_COMMAND + ('--list', 'repo'), module.borgmatic.logger.ANSWER)
|
2020-01-22 22:28:24 +01:00
|
|
|
|
|
|
|
module.prune_archives(
|
|
|
|
dry_run=False,
|
2023-03-26 20:22:25 +02:00
|
|
|
repository_path='repo',
|
2020-01-22 22:28:24 +01:00
|
|
|
storage_config={},
|
2022-10-04 07:50:37 +02:00
|
|
|
retention_config=flexmock(),
|
2022-08-14 02:26:51 +02:00
|
|
|
local_borg_version='1.2.3',
|
2022-08-21 23:25:16 +02:00
|
|
|
list_archives=True,
|
2020-01-22 22:28:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-06-18 00:12:43 +02:00
|
|
|
def test_prune_archives_with_umask_calls_borg_with_umask_parameters():
|
2022-12-02 21:12:10 +01:00
|
|
|
flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
|
|
|
|
flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
|
2018-06-18 00:12:43 +02:00
|
|
|
storage_config = {'umask': '077'}
|
2022-10-04 07:50:37 +02:00
|
|
|
flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
|
2022-08-14 07:07:15 +02:00
|
|
|
flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
|
2019-09-23 22:07:51 +02:00
|
|
|
insert_execute_command_mock(PRUNE_COMMAND + ('--umask', '077', 'repo'), logging.INFO)
|
2018-06-18 00:12:43 +02:00
|
|
|
|
|
|
|
module.prune_archives(
|
|
|
|
dry_run=False,
|
2023-03-26 20:22:25 +02:00
|
|
|
repository_path='repo',
|
2018-06-18 00:12:43 +02:00
|
|
|
storage_config=storage_config,
|
2022-10-04 07:50:37 +02:00
|
|
|
retention_config=flexmock(),
|
2022-08-14 02:26:51 +02:00
|
|
|
local_borg_version='1.2.3',
|
2018-06-18 00:12:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-02-20 00:51:04 +01:00
|
|
|
def test_prune_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
|
2022-12-02 21:12:10 +01:00
|
|
|
flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
|
|
|
|
flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
|
2018-02-20 00:51:04 +01:00
|
|
|
storage_config = {'lock_wait': 5}
|
2022-10-04 07:50:37 +02:00
|
|
|
flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
|
2022-08-14 07:07:15 +02:00
|
|
|
flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
|
2019-09-23 22:07:51 +02:00
|
|
|
insert_execute_command_mock(PRUNE_COMMAND + ('--lock-wait', '5', 'repo'), logging.INFO)
|
2018-02-20 00:51:04 +01:00
|
|
|
|
|
|
|
module.prune_archives(
|
|
|
|
dry_run=False,
|
2023-03-26 20:22:25 +02:00
|
|
|
repository_path='repo',
|
2018-02-20 00:51:04 +01:00
|
|
|
storage_config=storage_config,
|
2022-10-04 07:50:37 +02:00
|
|
|
retention_config=flexmock(),
|
2022-08-14 02:26:51 +02:00
|
|
|
local_borg_version='1.2.3',
|
2018-02-20 00:51:04 +01:00
|
|
|
)
|
2019-12-05 00:48:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_prune_archives_with_extra_borg_options_calls_borg_with_extra_options():
|
2022-12-02 21:12:10 +01:00
|
|
|
flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
|
|
|
|
flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
|
2022-10-04 07:50:37 +02:00
|
|
|
flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
|
2022-08-14 07:07:15 +02:00
|
|
|
flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
|
2019-12-05 00:48:10 +01:00
|
|
|
insert_execute_command_mock(PRUNE_COMMAND + ('--extra', '--options', 'repo'), logging.INFO)
|
|
|
|
|
|
|
|
module.prune_archives(
|
|
|
|
dry_run=False,
|
2023-03-26 20:22:25 +02:00
|
|
|
repository_path='repo',
|
2019-12-05 00:48:10 +01:00
|
|
|
storage_config={'extra_borg_options': {'prune': '--extra --options'}},
|
2022-10-04 07:50:37 +02:00
|
|
|
retention_config=flexmock(),
|
2022-08-14 02:26:51 +02:00
|
|
|
local_borg_version='1.2.3',
|
2019-12-05 00:48:10 +01:00
|
|
|
)
|