test_prune.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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.feature).should_receive('available').and_return(False)
  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_borg_features_calls_borg_with_repo_flag():
  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.feature).should_receive('available').and_return(True)
  55. insert_execute_command_mock(PRUNE_COMMAND + ('--repo', 'repo'), logging.INFO)
  56. module.prune_archives(
  57. dry_run=False,
  58. repository='repo',
  59. storage_config={},
  60. retention_config=retention_config,
  61. local_borg_version='1.2.3',
  62. )
  63. def test_prune_archives_with_log_info_calls_borg_with_info_parameter():
  64. retention_config = flexmock()
  65. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  66. BASE_PRUNE_FLAGS
  67. )
  68. flexmock(module.feature).should_receive('available').and_return(False)
  69. insert_execute_command_mock(PRUNE_COMMAND + ('--info', 'repo'), logging.INFO)
  70. insert_logging_mock(logging.INFO)
  71. module.prune_archives(
  72. repository='repo',
  73. storage_config={},
  74. dry_run=False,
  75. retention_config=retention_config,
  76. local_borg_version='1.2.3',
  77. )
  78. def test_prune_archives_with_log_debug_calls_borg_with_debug_parameter():
  79. retention_config = flexmock()
  80. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  81. BASE_PRUNE_FLAGS
  82. )
  83. flexmock(module.feature).should_receive('available').and_return(False)
  84. insert_execute_command_mock(PRUNE_COMMAND + ('--debug', '--show-rc', 'repo'), logging.INFO)
  85. insert_logging_mock(logging.DEBUG)
  86. module.prune_archives(
  87. repository='repo',
  88. storage_config={},
  89. dry_run=False,
  90. retention_config=retention_config,
  91. local_borg_version='1.2.3',
  92. )
  93. def test_prune_archives_with_dry_run_calls_borg_with_dry_run_parameter():
  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.feature).should_receive('available').and_return(False)
  99. insert_execute_command_mock(PRUNE_COMMAND + ('--dry-run', 'repo'), logging.INFO)
  100. module.prune_archives(
  101. repository='repo',
  102. storage_config={},
  103. dry_run=True,
  104. retention_config=retention_config,
  105. local_borg_version='1.2.3',
  106. )
  107. def test_prune_archives_with_local_path_calls_borg_via_local_path():
  108. retention_config = flexmock()
  109. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  110. BASE_PRUNE_FLAGS
  111. )
  112. flexmock(module.feature).should_receive('available').and_return(False)
  113. insert_execute_command_mock(('borg1',) + PRUNE_COMMAND[1:] + ('repo',), logging.INFO)
  114. module.prune_archives(
  115. dry_run=False,
  116. repository='repo',
  117. storage_config={},
  118. retention_config=retention_config,
  119. local_borg_version='1.2.3',
  120. local_path='borg1',
  121. )
  122. def test_prune_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  123. retention_config = flexmock()
  124. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  125. BASE_PRUNE_FLAGS
  126. )
  127. flexmock(module.feature).should_receive('available').and_return(False)
  128. insert_execute_command_mock(PRUNE_COMMAND + ('--remote-path', 'borg1', 'repo'), logging.INFO)
  129. module.prune_archives(
  130. dry_run=False,
  131. repository='repo',
  132. storage_config={},
  133. retention_config=retention_config,
  134. local_borg_version='1.2.3',
  135. remote_path='borg1',
  136. )
  137. def test_prune_archives_with_stats_calls_borg_with_stats_parameter_and_warning_output_log_level():
  138. retention_config = flexmock()
  139. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  140. BASE_PRUNE_FLAGS
  141. )
  142. flexmock(module.feature).should_receive('available').and_return(False)
  143. insert_execute_command_mock(PRUNE_COMMAND + ('--stats', 'repo'), logging.WARNING)
  144. module.prune_archives(
  145. dry_run=False,
  146. repository='repo',
  147. storage_config={},
  148. retention_config=retention_config,
  149. local_borg_version='1.2.3',
  150. stats=True,
  151. )
  152. def test_prune_archives_with_stats_and_log_info_calls_borg_with_stats_parameter_and_info_output_log_level():
  153. retention_config = flexmock()
  154. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  155. BASE_PRUNE_FLAGS
  156. )
  157. flexmock(module.feature).should_receive('available').and_return(False)
  158. insert_logging_mock(logging.INFO)
  159. insert_execute_command_mock(PRUNE_COMMAND + ('--stats', '--info', 'repo'), logging.INFO)
  160. module.prune_archives(
  161. dry_run=False,
  162. repository='repo',
  163. storage_config={},
  164. retention_config=retention_config,
  165. local_borg_version='1.2.3',
  166. stats=True,
  167. )
  168. def test_prune_archives_with_files_calls_borg_with_list_parameter_and_warning_output_log_level():
  169. retention_config = flexmock()
  170. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  171. BASE_PRUNE_FLAGS
  172. )
  173. flexmock(module.feature).should_receive('available').and_return(False)
  174. insert_execute_command_mock(PRUNE_COMMAND + ('--list', 'repo'), logging.WARNING)
  175. module.prune_archives(
  176. dry_run=False,
  177. repository='repo',
  178. storage_config={},
  179. retention_config=retention_config,
  180. local_borg_version='1.2.3',
  181. files=True,
  182. )
  183. def test_prune_archives_with_files_and_log_info_calls_borg_with_list_parameter_and_info_output_log_level():
  184. retention_config = flexmock()
  185. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  186. BASE_PRUNE_FLAGS
  187. )
  188. flexmock(module.feature).should_receive('available').and_return(False)
  189. insert_logging_mock(logging.INFO)
  190. insert_execute_command_mock(PRUNE_COMMAND + ('--info', '--list', 'repo'), logging.INFO)
  191. module.prune_archives(
  192. dry_run=False,
  193. repository='repo',
  194. storage_config={},
  195. retention_config=retention_config,
  196. local_borg_version='1.2.3',
  197. files=True,
  198. )
  199. def test_prune_archives_with_umask_calls_borg_with_umask_parameters():
  200. storage_config = {'umask': '077'}
  201. retention_config = flexmock()
  202. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  203. BASE_PRUNE_FLAGS
  204. )
  205. flexmock(module.feature).should_receive('available').and_return(False)
  206. insert_execute_command_mock(PRUNE_COMMAND + ('--umask', '077', 'repo'), logging.INFO)
  207. module.prune_archives(
  208. dry_run=False,
  209. repository='repo',
  210. storage_config=storage_config,
  211. retention_config=retention_config,
  212. local_borg_version='1.2.3',
  213. )
  214. def test_prune_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  215. storage_config = {'lock_wait': 5}
  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.feature).should_receive('available').and_return(False)
  221. insert_execute_command_mock(PRUNE_COMMAND + ('--lock-wait', '5', 'repo'), logging.INFO)
  222. module.prune_archives(
  223. dry_run=False,
  224. repository='repo',
  225. storage_config=storage_config,
  226. retention_config=retention_config,
  227. local_borg_version='1.2.3',
  228. )
  229. def test_prune_archives_with_extra_borg_options_calls_borg_with_extra_options():
  230. retention_config = flexmock()
  231. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  232. BASE_PRUNE_FLAGS
  233. )
  234. flexmock(module.feature).should_receive('available').and_return(False)
  235. insert_execute_command_mock(PRUNE_COMMAND + ('--extra', '--options', 'repo'), logging.INFO)
  236. module.prune_archives(
  237. dry_run=False,
  238. repository='repo',
  239. storage_config={'extra_borg_options': {'prune': '--extra --options'}},
  240. retention_config=retention_config,
  241. local_borg_version='1.2.3',
  242. )