test_prune.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import logging
  2. from collections import OrderedDict
  3. from flexmock import flexmock
  4. from borgmatic.borg import prune as module
  5. from ..test_verbosity import insert_logging_mock
  6. def insert_execute_command_mock(prune_command, output_log_level):
  7. flexmock(module.environment).should_receive('make_environment')
  8. flexmock(module).should_receive('execute_command').with_args(
  9. prune_command,
  10. output_log_level=output_log_level,
  11. borg_local_path=prune_command[0],
  12. extra_environment=None,
  13. ).once()
  14. BASE_PRUNE_FLAGS = (('--keep-daily', '1'), ('--keep-weekly', '2'), ('--keep-monthly', '3'))
  15. def test_make_prune_flags_returns_flags_from_config_plus_default_prefix_glob():
  16. retention_config = OrderedDict((('keep_daily', 1), ('keep_weekly', 2), ('keep_monthly', 3)))
  17. flexmock(module.feature).should_receive('available').and_return(True)
  18. result = module.make_prune_flags(retention_config, local_borg_version='1.2.3')
  19. assert tuple(result) == BASE_PRUNE_FLAGS + (
  20. ('--match-archives', 'sh:{hostname}-*'), # noqa: FS003
  21. )
  22. def test_make_prune_flags_accepts_prefix_with_placeholders():
  23. retention_config = OrderedDict(
  24. (('keep_daily', 1), ('prefix', 'Documents_{hostname}-{now}')) # noqa: FS003
  25. )
  26. flexmock(module.feature).should_receive('available').and_return(True)
  27. result = module.make_prune_flags(retention_config, local_borg_version='1.2.3')
  28. expected = (
  29. ('--keep-daily', '1'),
  30. ('--match-archives', 'sh:Documents_{hostname}-{now}*'), # noqa: FS003
  31. )
  32. assert tuple(result) == expected
  33. def test_make_prune_flags_with_prefix_without_borg_features_uses_glob_archives():
  34. retention_config = OrderedDict(
  35. (('keep_daily', 1), ('prefix', 'Documents_{hostname}-{now}')) # noqa: FS003
  36. )
  37. flexmock(module.feature).should_receive('available').and_return(False)
  38. result = module.make_prune_flags(retention_config, local_borg_version='1.2.3')
  39. expected = (
  40. ('--keep-daily', '1'),
  41. ('--glob-archives', 'Documents_{hostname}-{now}*'), # noqa: FS003
  42. )
  43. assert tuple(result) == expected
  44. def test_make_prune_flags_treats_empty_prefix_as_no_prefix():
  45. retention_config = OrderedDict((('keep_daily', 1), ('prefix', '')))
  46. flexmock(module.feature).should_receive('available').and_return(True)
  47. result = module.make_prune_flags(retention_config, local_borg_version='1.2.3')
  48. expected = (('--keep-daily', '1'),)
  49. assert tuple(result) == expected
  50. def test_make_prune_flags_treats_none_prefix_as_no_prefix():
  51. retention_config = OrderedDict((('keep_daily', 1), ('prefix', None)))
  52. flexmock(module.feature).should_receive('available').and_return(True)
  53. result = module.make_prune_flags(retention_config, local_borg_version='1.2.3')
  54. expected = (('--keep-daily', '1'),)
  55. assert tuple(result) == expected
  56. PRUNE_COMMAND = ('borg', 'prune', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly', '3')
  57. def test_prune_archives_calls_borg_with_parameters():
  58. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  59. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  60. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  61. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  62. insert_execute_command_mock(PRUNE_COMMAND + ('repo',), logging.INFO)
  63. module.prune_archives(
  64. dry_run=False,
  65. repository_path='repo',
  66. storage_config={},
  67. retention_config=flexmock(),
  68. local_borg_version='1.2.3',
  69. )
  70. def test_prune_archives_with_log_info_calls_borg_with_info_parameter():
  71. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  72. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  73. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  74. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  75. insert_execute_command_mock(PRUNE_COMMAND + ('--info', 'repo'), logging.INFO)
  76. insert_logging_mock(logging.INFO)
  77. module.prune_archives(
  78. repository_path='repo',
  79. storage_config={},
  80. dry_run=False,
  81. retention_config=flexmock(),
  82. local_borg_version='1.2.3',
  83. )
  84. def test_prune_archives_with_log_debug_calls_borg_with_debug_parameter():
  85. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  86. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  87. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  88. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  89. insert_execute_command_mock(PRUNE_COMMAND + ('--debug', '--show-rc', 'repo'), logging.INFO)
  90. insert_logging_mock(logging.DEBUG)
  91. module.prune_archives(
  92. repository_path='repo',
  93. storage_config={},
  94. dry_run=False,
  95. retention_config=flexmock(),
  96. local_borg_version='1.2.3',
  97. )
  98. def test_prune_archives_with_dry_run_calls_borg_with_dry_run_parameter():
  99. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  100. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  101. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  102. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  103. insert_execute_command_mock(PRUNE_COMMAND + ('--dry-run', 'repo'), logging.INFO)
  104. module.prune_archives(
  105. repository_path='repo',
  106. storage_config={},
  107. dry_run=True,
  108. retention_config=flexmock(),
  109. local_borg_version='1.2.3',
  110. )
  111. def test_prune_archives_with_local_path_calls_borg_via_local_path():
  112. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  113. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  114. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  115. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  116. insert_execute_command_mock(('borg1',) + PRUNE_COMMAND[1:] + ('repo',), logging.INFO)
  117. module.prune_archives(
  118. dry_run=False,
  119. repository_path='repo',
  120. storage_config={},
  121. retention_config=flexmock(),
  122. local_borg_version='1.2.3',
  123. local_path='borg1',
  124. )
  125. def test_prune_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  126. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  127. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  128. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  129. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  130. insert_execute_command_mock(PRUNE_COMMAND + ('--remote-path', 'borg1', 'repo'), logging.INFO)
  131. module.prune_archives(
  132. dry_run=False,
  133. repository_path='repo',
  134. storage_config={},
  135. retention_config=flexmock(),
  136. local_borg_version='1.2.3',
  137. remote_path='borg1',
  138. )
  139. def test_prune_archives_with_stats_calls_borg_with_stats_parameter_and_answer_output_log_level():
  140. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  141. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  142. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  143. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  144. insert_execute_command_mock(PRUNE_COMMAND + ('--stats', 'repo'), module.borgmatic.logger.ANSWER)
  145. module.prune_archives(
  146. dry_run=False,
  147. repository_path='repo',
  148. storage_config={},
  149. retention_config=flexmock(),
  150. local_borg_version='1.2.3',
  151. stats=True,
  152. )
  153. def test_prune_archives_with_files_calls_borg_with_list_parameter_and_answer_output_log_level():
  154. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  155. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  156. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  157. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  158. insert_execute_command_mock(PRUNE_COMMAND + ('--list', 'repo'), module.borgmatic.logger.ANSWER)
  159. module.prune_archives(
  160. dry_run=False,
  161. repository_path='repo',
  162. storage_config={},
  163. retention_config=flexmock(),
  164. local_borg_version='1.2.3',
  165. list_archives=True,
  166. )
  167. def test_prune_archives_with_umask_calls_borg_with_umask_parameters():
  168. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  169. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  170. storage_config = {'umask': '077'}
  171. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  172. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  173. insert_execute_command_mock(PRUNE_COMMAND + ('--umask', '077', 'repo'), logging.INFO)
  174. module.prune_archives(
  175. dry_run=False,
  176. repository_path='repo',
  177. storage_config=storage_config,
  178. retention_config=flexmock(),
  179. local_borg_version='1.2.3',
  180. )
  181. def test_prune_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  182. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  183. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  184. storage_config = {'lock_wait': 5}
  185. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  186. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  187. insert_execute_command_mock(PRUNE_COMMAND + ('--lock-wait', '5', 'repo'), logging.INFO)
  188. module.prune_archives(
  189. dry_run=False,
  190. repository_path='repo',
  191. storage_config=storage_config,
  192. retention_config=flexmock(),
  193. local_borg_version='1.2.3',
  194. )
  195. def test_prune_archives_with_extra_borg_options_calls_borg_with_extra_options():
  196. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  197. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  198. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  199. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  200. insert_execute_command_mock(PRUNE_COMMAND + ('--extra', '--options', 'repo'), logging.INFO)
  201. module.prune_archives(
  202. dry_run=False,
  203. repository_path='repo',
  204. storage_config={'extra_borg_options': {'prune': '--extra --options'}},
  205. retention_config=flexmock(),
  206. local_borg_version='1.2.3',
  207. )