test_prune.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. result = module.make_prune_flags(retention_config)
  18. assert tuple(result) == BASE_PRUNE_FLAGS + (('--glob-archives', '{hostname}-*'),)
  19. def test_make_prune_flags_accepts_prefix_with_placeholders():
  20. retention_config = OrderedDict((('keep_daily', 1), ('prefix', 'Documents_{hostname}-{now}')))
  21. result = module.make_prune_flags(retention_config)
  22. expected = (('--keep-daily', '1'), ('--glob-archives', 'Documents_{hostname}-{now}*'))
  23. assert tuple(result) == expected
  24. def test_make_prune_flags_treats_empty_prefix_as_no_prefix():
  25. retention_config = OrderedDict((('keep_daily', 1), ('prefix', '')))
  26. result = module.make_prune_flags(retention_config)
  27. expected = (('--keep-daily', '1'),)
  28. assert tuple(result) == expected
  29. def test_make_prune_flags_treats_none_prefix_as_no_prefix():
  30. retention_config = OrderedDict((('keep_daily', 1), ('prefix', None)))
  31. result = module.make_prune_flags(retention_config)
  32. expected = (('--keep-daily', '1'),)
  33. assert tuple(result) == expected
  34. PRUNE_COMMAND = ('borg', 'prune', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly', '3')
  35. def test_prune_archives_calls_borg_with_parameters():
  36. retention_config = flexmock()
  37. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  38. BASE_PRUNE_FLAGS
  39. )
  40. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  41. insert_execute_command_mock(PRUNE_COMMAND + ('repo',), logging.INFO)
  42. module.prune_archives(
  43. dry_run=False,
  44. repository='repo',
  45. storage_config={},
  46. retention_config=retention_config,
  47. local_borg_version='1.2.3',
  48. )
  49. def test_prune_archives_with_log_info_calls_borg_with_info_parameter():
  50. retention_config = flexmock()
  51. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  52. BASE_PRUNE_FLAGS
  53. )
  54. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  55. insert_execute_command_mock(PRUNE_COMMAND + ('--info', 'repo'), logging.INFO)
  56. insert_logging_mock(logging.INFO)
  57. module.prune_archives(
  58. repository='repo',
  59. storage_config={},
  60. dry_run=False,
  61. retention_config=retention_config,
  62. local_borg_version='1.2.3',
  63. )
  64. def test_prune_archives_with_log_debug_calls_borg_with_debug_parameter():
  65. retention_config = flexmock()
  66. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  67. BASE_PRUNE_FLAGS
  68. )
  69. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  70. insert_execute_command_mock(PRUNE_COMMAND + ('--debug', '--show-rc', 'repo'), logging.INFO)
  71. insert_logging_mock(logging.DEBUG)
  72. module.prune_archives(
  73. repository='repo',
  74. storage_config={},
  75. dry_run=False,
  76. retention_config=retention_config,
  77. local_borg_version='1.2.3',
  78. )
  79. def test_prune_archives_with_dry_run_calls_borg_with_dry_run_parameter():
  80. retention_config = flexmock()
  81. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  82. BASE_PRUNE_FLAGS
  83. )
  84. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  85. insert_execute_command_mock(PRUNE_COMMAND + ('--dry-run', 'repo'), logging.INFO)
  86. module.prune_archives(
  87. repository='repo',
  88. storage_config={},
  89. dry_run=True,
  90. retention_config=retention_config,
  91. local_borg_version='1.2.3',
  92. )
  93. def test_prune_archives_with_local_path_calls_borg_via_local_path():
  94. retention_config = flexmock()
  95. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  96. BASE_PRUNE_FLAGS
  97. )
  98. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  99. insert_execute_command_mock(('borg1',) + PRUNE_COMMAND[1:] + ('repo',), logging.INFO)
  100. module.prune_archives(
  101. dry_run=False,
  102. repository='repo',
  103. storage_config={},
  104. retention_config=retention_config,
  105. local_borg_version='1.2.3',
  106. local_path='borg1',
  107. )
  108. def test_prune_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  109. retention_config = flexmock()
  110. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  111. BASE_PRUNE_FLAGS
  112. )
  113. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  114. insert_execute_command_mock(PRUNE_COMMAND + ('--remote-path', 'borg1', 'repo'), logging.INFO)
  115. module.prune_archives(
  116. dry_run=False,
  117. repository='repo',
  118. storage_config={},
  119. retention_config=retention_config,
  120. local_borg_version='1.2.3',
  121. remote_path='borg1',
  122. )
  123. def test_prune_archives_with_stats_calls_borg_with_stats_parameter_and_warning_output_log_level():
  124. retention_config = flexmock()
  125. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  126. BASE_PRUNE_FLAGS
  127. )
  128. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  129. insert_execute_command_mock(PRUNE_COMMAND + ('--stats', 'repo'), logging.WARNING)
  130. module.prune_archives(
  131. dry_run=False,
  132. repository='repo',
  133. storage_config={},
  134. retention_config=retention_config,
  135. local_borg_version='1.2.3',
  136. stats=True,
  137. )
  138. def test_prune_archives_with_stats_and_log_info_calls_borg_with_stats_parameter_and_info_output_log_level():
  139. retention_config = flexmock()
  140. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  141. BASE_PRUNE_FLAGS
  142. )
  143. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  144. insert_logging_mock(logging.INFO)
  145. insert_execute_command_mock(PRUNE_COMMAND + ('--stats', '--info', 'repo'), logging.INFO)
  146. module.prune_archives(
  147. dry_run=False,
  148. repository='repo',
  149. storage_config={},
  150. retention_config=retention_config,
  151. local_borg_version='1.2.3',
  152. stats=True,
  153. )
  154. def test_prune_archives_with_files_calls_borg_with_list_parameter_and_warning_output_log_level():
  155. retention_config = flexmock()
  156. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  157. BASE_PRUNE_FLAGS
  158. )
  159. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  160. insert_execute_command_mock(PRUNE_COMMAND + ('--list', 'repo'), logging.WARNING)
  161. module.prune_archives(
  162. dry_run=False,
  163. repository='repo',
  164. storage_config={},
  165. retention_config=retention_config,
  166. local_borg_version='1.2.3',
  167. list_archives=True,
  168. )
  169. def test_prune_archives_with_files_and_log_info_calls_borg_with_list_parameter_and_info_output_log_level():
  170. retention_config = flexmock()
  171. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  172. BASE_PRUNE_FLAGS
  173. )
  174. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  175. insert_logging_mock(logging.INFO)
  176. insert_execute_command_mock(PRUNE_COMMAND + ('--info', '--list', 'repo'), logging.INFO)
  177. module.prune_archives(
  178. dry_run=False,
  179. repository='repo',
  180. storage_config={},
  181. retention_config=retention_config,
  182. local_borg_version='1.2.3',
  183. list_archives=True,
  184. )
  185. def test_prune_archives_with_umask_calls_borg_with_umask_parameters():
  186. storage_config = {'umask': '077'}
  187. retention_config = flexmock()
  188. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  189. BASE_PRUNE_FLAGS
  190. )
  191. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  192. insert_execute_command_mock(PRUNE_COMMAND + ('--umask', '077', 'repo'), logging.INFO)
  193. module.prune_archives(
  194. dry_run=False,
  195. repository='repo',
  196. storage_config=storage_config,
  197. retention_config=retention_config,
  198. local_borg_version='1.2.3',
  199. )
  200. def test_prune_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  201. storage_config = {'lock_wait': 5}
  202. retention_config = flexmock()
  203. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  204. BASE_PRUNE_FLAGS
  205. )
  206. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  207. insert_execute_command_mock(PRUNE_COMMAND + ('--lock-wait', '5', 'repo'), logging.INFO)
  208. module.prune_archives(
  209. dry_run=False,
  210. repository='repo',
  211. storage_config=storage_config,
  212. retention_config=retention_config,
  213. local_borg_version='1.2.3',
  214. )
  215. def test_prune_archives_with_extra_borg_options_calls_borg_with_extra_options():
  216. retention_config = flexmock()
  217. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  218. BASE_PRUNE_FLAGS
  219. )
  220. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  221. insert_execute_command_mock(PRUNE_COMMAND + ('--extra', '--options', 'repo'), logging.INFO)
  222. module.prune_archives(
  223. dry_run=False,
  224. repository='repo',
  225. storage_config={'extra_borg_options': {'prune': '--extra --options'}},
  226. retention_config=retention_config,
  227. local_borg_version='1.2.3',
  228. )