test_prune.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic.borg import prune as module
  4. from ..test_verbosity import insert_logging_mock
  5. def insert_execute_command_mock(prune_command, output_log_level, borg_exit_codes=None):
  6. flexmock(module.environment).should_receive('make_environment')
  7. flexmock(module).should_receive('execute_command').with_args(
  8. prune_command,
  9. output_log_level=output_log_level,
  10. borg_local_path=prune_command[0],
  11. borg_exit_codes=borg_exit_codes,
  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():
  16. config = {
  17. 'keep_daily': 1,
  18. 'keep_weekly': 2,
  19. 'keep_monthly': 3,
  20. }
  21. flexmock(module.feature).should_receive('available').and_return(True)
  22. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  23. result = module.make_prune_flags(config, local_borg_version='1.2.3')
  24. assert result == BASE_PRUNE_FLAGS
  25. def test_make_prune_flags_accepts_prefix_with_placeholders():
  26. config = {
  27. 'keep_daily': 1,
  28. 'prefix': 'Documents_{hostname}-{now}', # noqa: FS003
  29. }
  30. flexmock(module.feature).should_receive('available').and_return(True)
  31. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  32. result = module.make_prune_flags(config, local_borg_version='1.2.3')
  33. expected = (
  34. '--keep-daily',
  35. '1',
  36. '--match-archives',
  37. 'sh:Documents_{hostname}-{now}*', # noqa: FS003
  38. )
  39. assert result == expected
  40. def test_make_prune_flags_with_prefix_without_borg_features_uses_glob_archives():
  41. config = {
  42. 'keep_daily': 1,
  43. 'prefix': 'Documents_{hostname}-{now}', # noqa: FS003
  44. }
  45. flexmock(module.feature).should_receive('available').and_return(False)
  46. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  47. result = module.make_prune_flags(config, local_borg_version='1.2.3')
  48. expected = (
  49. '--keep-daily',
  50. '1',
  51. '--glob-archives',
  52. 'Documents_{hostname}-{now}*', # noqa: FS003
  53. )
  54. assert result == expected
  55. def test_make_prune_flags_prefers_prefix_to_archive_name_format():
  56. config = {
  57. 'archive_name_format': 'bar-{now}', # noqa: FS003
  58. 'keep_daily': 1,
  59. 'prefix': 'bar-',
  60. }
  61. flexmock(module.feature).should_receive('available').and_return(True)
  62. flexmock(module.flags).should_receive('make_match_archives_flags').never()
  63. result = module.make_prune_flags(config, local_borg_version='1.2.3')
  64. expected = (
  65. '--keep-daily',
  66. '1',
  67. '--match-archives',
  68. 'sh:bar-*', # noqa: FS003
  69. )
  70. assert result == expected
  71. def test_make_prune_flags_without_prefix_uses_archive_name_format_instead():
  72. config = {
  73. 'archive_name_format': 'bar-{now}', # noqa: FS003
  74. 'keep_daily': 1,
  75. 'prefix': None,
  76. }
  77. flexmock(module.feature).should_receive('available').and_return(True)
  78. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  79. None, 'bar-{now}', '1.2.3' # noqa: FS003
  80. ).and_return(('--match-archives', 'sh:bar-*'))
  81. result = module.make_prune_flags(config, local_borg_version='1.2.3')
  82. expected = (
  83. '--keep-daily',
  84. '1',
  85. '--match-archives',
  86. 'sh:bar-*', # noqa: FS003
  87. )
  88. assert result == expected
  89. def test_make_prune_flags_ignores_keep_exclude_tags_in_config():
  90. config = {
  91. 'keep_daily': 1,
  92. 'keep_exclude_tags': True,
  93. }
  94. flexmock(module.feature).should_receive('available').and_return(True)
  95. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  96. result = module.make_prune_flags(config, local_borg_version='1.2.3')
  97. assert result == ('--keep-daily', '1')
  98. PRUNE_COMMAND = ('borg', 'prune', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly', '3')
  99. def test_prune_archives_calls_borg_with_flags():
  100. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  101. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  102. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  103. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  104. insert_execute_command_mock(PRUNE_COMMAND + ('repo',), logging.INFO)
  105. prune_arguments = flexmock(stats=False, list_archives=False)
  106. module.prune_archives(
  107. dry_run=False,
  108. repository_path='repo',
  109. config={},
  110. local_borg_version='1.2.3',
  111. global_arguments=flexmock(log_json=False),
  112. prune_arguments=prune_arguments,
  113. )
  114. def test_prune_archives_with_log_info_calls_borg_with_info_flag():
  115. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  116. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  117. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  118. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  119. insert_execute_command_mock(PRUNE_COMMAND + ('--info', 'repo'), logging.INFO)
  120. insert_logging_mock(logging.INFO)
  121. prune_arguments = flexmock(stats=False, list_archives=False)
  122. module.prune_archives(
  123. repository_path='repo',
  124. config={},
  125. dry_run=False,
  126. local_borg_version='1.2.3',
  127. global_arguments=flexmock(log_json=False),
  128. prune_arguments=prune_arguments,
  129. )
  130. def test_prune_archives_with_log_debug_calls_borg_with_debug_flag():
  131. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  132. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  133. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  134. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  135. insert_execute_command_mock(PRUNE_COMMAND + ('--debug', '--show-rc', 'repo'), logging.INFO)
  136. insert_logging_mock(logging.DEBUG)
  137. prune_arguments = flexmock(stats=False, list_archives=False)
  138. module.prune_archives(
  139. repository_path='repo',
  140. config={},
  141. dry_run=False,
  142. local_borg_version='1.2.3',
  143. global_arguments=flexmock(log_json=False),
  144. prune_arguments=prune_arguments,
  145. )
  146. def test_prune_archives_with_dry_run_calls_borg_with_dry_run_flag():
  147. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  148. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  149. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  150. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  151. insert_execute_command_mock(PRUNE_COMMAND + ('--dry-run', 'repo'), logging.INFO)
  152. prune_arguments = flexmock(stats=False, list_archives=False)
  153. module.prune_archives(
  154. repository_path='repo',
  155. config={},
  156. dry_run=True,
  157. local_borg_version='1.2.3',
  158. global_arguments=flexmock(log_json=False),
  159. prune_arguments=prune_arguments,
  160. )
  161. def test_prune_archives_with_local_path_calls_borg_via_local_path():
  162. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  163. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  164. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  165. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  166. insert_execute_command_mock(('borg1',) + PRUNE_COMMAND[1:] + ('repo',), logging.INFO)
  167. prune_arguments = flexmock(stats=False, list_archives=False)
  168. module.prune_archives(
  169. dry_run=False,
  170. repository_path='repo',
  171. config={},
  172. local_borg_version='1.2.3',
  173. global_arguments=flexmock(log_json=False),
  174. local_path='borg1',
  175. prune_arguments=prune_arguments,
  176. )
  177. def test_prune_archives_with_exit_codes_calls_borg_using_them():
  178. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  179. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  180. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  181. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  182. borg_exit_codes = flexmock()
  183. insert_execute_command_mock(
  184. ('borg',) + PRUNE_COMMAND[1:] + ('repo',), logging.INFO, borg_exit_codes
  185. )
  186. prune_arguments = flexmock(stats=False, list_archives=False)
  187. module.prune_archives(
  188. dry_run=False,
  189. repository_path='repo',
  190. config={'borg_exit_codes': borg_exit_codes},
  191. local_borg_version='1.2.3',
  192. global_arguments=flexmock(log_json=False),
  193. prune_arguments=prune_arguments,
  194. )
  195. def test_prune_archives_with_remote_path_calls_borg_with_remote_path_flags():
  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 + ('--remote-path', 'borg1', 'repo'), logging.INFO)
  201. prune_arguments = flexmock(stats=False, list_archives=False)
  202. module.prune_archives(
  203. dry_run=False,
  204. repository_path='repo',
  205. config={},
  206. local_borg_version='1.2.3',
  207. global_arguments=flexmock(log_json=False),
  208. remote_path='borg1',
  209. prune_arguments=prune_arguments,
  210. )
  211. def test_prune_archives_with_stats_calls_borg_with_stats_flag_and_answer_output_log_level():
  212. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  213. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  214. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  215. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  216. insert_execute_command_mock(PRUNE_COMMAND + ('--stats', 'repo'), module.borgmatic.logger.ANSWER)
  217. prune_arguments = flexmock(stats=True, list_archives=False)
  218. module.prune_archives(
  219. dry_run=False,
  220. repository_path='repo',
  221. config={},
  222. local_borg_version='1.2.3',
  223. global_arguments=flexmock(log_json=False),
  224. prune_arguments=prune_arguments,
  225. )
  226. def test_prune_archives_with_files_calls_borg_with_list_flag_and_answer_output_log_level():
  227. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  228. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  229. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  230. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  231. insert_execute_command_mock(PRUNE_COMMAND + ('--list', 'repo'), module.borgmatic.logger.ANSWER)
  232. prune_arguments = flexmock(stats=False, list_archives=True)
  233. module.prune_archives(
  234. dry_run=False,
  235. repository_path='repo',
  236. config={},
  237. local_borg_version='1.2.3',
  238. global_arguments=flexmock(log_json=False),
  239. prune_arguments=prune_arguments,
  240. )
  241. def test_prune_archives_with_umask_calls_borg_with_umask_flags():
  242. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  243. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  244. config = {'umask': '077'}
  245. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  246. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  247. insert_execute_command_mock(PRUNE_COMMAND + ('--umask', '077', 'repo'), logging.INFO)
  248. prune_arguments = flexmock(stats=False, list_archives=False)
  249. module.prune_archives(
  250. dry_run=False,
  251. repository_path='repo',
  252. config=config,
  253. local_borg_version='1.2.3',
  254. global_arguments=flexmock(log_json=False),
  255. prune_arguments=prune_arguments,
  256. )
  257. def test_prune_archives_with_log_json_calls_borg_with_log_json_flag():
  258. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  259. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  260. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  261. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  262. insert_execute_command_mock(PRUNE_COMMAND + ('--log-json', 'repo'), logging.INFO)
  263. prune_arguments = flexmock(stats=False, list_archives=False)
  264. module.prune_archives(
  265. dry_run=False,
  266. repository_path='repo',
  267. config={},
  268. local_borg_version='1.2.3',
  269. global_arguments=flexmock(log_json=True),
  270. prune_arguments=prune_arguments,
  271. )
  272. def test_prune_archives_with_lock_wait_calls_borg_with_lock_wait_flags():
  273. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  274. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  275. config = {'lock_wait': 5}
  276. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  277. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  278. insert_execute_command_mock(PRUNE_COMMAND + ('--lock-wait', '5', 'repo'), logging.INFO)
  279. prune_arguments = flexmock(stats=False, list_archives=False)
  280. module.prune_archives(
  281. dry_run=False,
  282. repository_path='repo',
  283. config=config,
  284. local_borg_version='1.2.3',
  285. global_arguments=flexmock(log_json=False),
  286. prune_arguments=prune_arguments,
  287. )
  288. def test_prune_archives_with_extra_borg_options_calls_borg_with_extra_options():
  289. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  290. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  291. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  292. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  293. insert_execute_command_mock(PRUNE_COMMAND + ('--extra', '--options', 'repo'), logging.INFO)
  294. prune_arguments = flexmock(stats=False, list_archives=False)
  295. module.prune_archives(
  296. dry_run=False,
  297. repository_path='repo',
  298. config={'extra_borg_options': {'prune': '--extra --options'}},
  299. local_borg_version='1.2.3',
  300. global_arguments=flexmock(log_json=False),
  301. prune_arguments=prune_arguments,
  302. )
  303. def test_prune_archives_with_date_based_matching_calls_borg_with_date_based_flags():
  304. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  305. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  306. flexmock(module.flags).should_receive('make_flags').and_return(())
  307. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  308. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  309. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  310. (
  311. '--newer',
  312. '1d',
  313. '--newest',
  314. '1y',
  315. '--older',
  316. '1m',
  317. '--oldest',
  318. '1w',
  319. '--match-archives',
  320. None,
  321. )
  322. )
  323. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  324. flexmock(module.environment).should_receive('make_environment')
  325. flexmock(module).should_receive('execute_command').with_args(
  326. (
  327. 'borg',
  328. 'prune',
  329. '--keep-daily',
  330. '1',
  331. '--keep-weekly',
  332. '2',
  333. '--keep-monthly',
  334. '3',
  335. '--newer',
  336. '1d',
  337. '--newest',
  338. '1y',
  339. '--older',
  340. '1m',
  341. '--oldest',
  342. '1w',
  343. '--match-archives',
  344. None,
  345. '--repo',
  346. 'repo',
  347. ),
  348. output_log_level=logging.INFO,
  349. borg_local_path='borg',
  350. borg_exit_codes=None,
  351. extra_environment=None,
  352. )
  353. prune_arguments = flexmock(
  354. stats=False, list_archives=False, newer='1d', newest='1y', older='1m', oldest='1w'
  355. )
  356. module.prune_archives(
  357. dry_run=False,
  358. repository_path='repo',
  359. config={},
  360. local_borg_version='1.2.3',
  361. global_arguments=flexmock(log_json=False),
  362. prune_arguments=prune_arguments,
  363. )