test_prune.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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(
  6. prune_command, output_log_level, working_directory=None, borg_exit_codes=None
  7. ):
  8. flexmock(module.environment).should_receive('make_environment')
  9. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  10. working_directory,
  11. )
  12. flexmock(module).should_receive('execute_command').with_args(
  13. prune_command,
  14. output_log_level=output_log_level,
  15. extra_environment=None,
  16. working_directory=working_directory,
  17. borg_local_path=prune_command[0],
  18. borg_exit_codes=borg_exit_codes,
  19. ).once()
  20. BASE_PRUNE_FLAGS = ('--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly', '3')
  21. def test_make_prune_flags_returns_flags_from_config():
  22. config = {
  23. 'keep_daily': 1,
  24. 'keep_weekly': 2,
  25. 'keep_monthly': 3,
  26. }
  27. flexmock(module.feature).should_receive('available').and_return(True)
  28. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  29. result = module.make_prune_flags(
  30. config, flexmock(match_archives=None), local_borg_version='1.2.3'
  31. )
  32. assert result == BASE_PRUNE_FLAGS
  33. def test_make_prune_flags_accepts_prefix_with_placeholders():
  34. config = {
  35. 'keep_daily': 1,
  36. 'prefix': 'Documents_{hostname}-{now}', # noqa: FS003
  37. }
  38. flexmock(module.feature).should_receive('available').and_return(True)
  39. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  40. result = module.make_prune_flags(
  41. config, flexmock(match_archives=None), local_borg_version='1.2.3'
  42. )
  43. expected = (
  44. '--keep-daily',
  45. '1',
  46. '--match-archives',
  47. 'sh:Documents_{hostname}-{now}*', # noqa: FS003
  48. )
  49. assert result == expected
  50. def test_make_prune_flags_with_prefix_without_borg_features_uses_glob_archives():
  51. config = {
  52. 'keep_daily': 1,
  53. 'prefix': 'Documents_{hostname}-{now}', # noqa: FS003
  54. }
  55. flexmock(module.feature).should_receive('available').and_return(False)
  56. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  57. result = module.make_prune_flags(
  58. config, flexmock(match_archives=None), local_borg_version='1.2.3'
  59. )
  60. expected = (
  61. '--keep-daily',
  62. '1',
  63. '--glob-archives',
  64. 'Documents_{hostname}-{now}*', # noqa: FS003
  65. )
  66. assert result == expected
  67. def test_make_prune_flags_prefers_prefix_to_archive_name_format():
  68. config = {
  69. 'archive_name_format': 'bar-{now}', # noqa: FS003
  70. 'keep_daily': 1,
  71. 'prefix': 'bar-',
  72. }
  73. flexmock(module.feature).should_receive('available').and_return(True)
  74. flexmock(module.flags).should_receive('make_match_archives_flags').never()
  75. result = module.make_prune_flags(
  76. config, flexmock(match_archives=None), local_borg_version='1.2.3'
  77. )
  78. expected = (
  79. '--keep-daily',
  80. '1',
  81. '--match-archives',
  82. 'sh:bar-*', # noqa: FS003
  83. )
  84. assert result == expected
  85. def test_make_prune_flags_without_prefix_uses_archive_name_format_instead():
  86. config = {
  87. 'archive_name_format': 'bar-{now}', # noqa: FS003
  88. 'keep_daily': 1,
  89. 'prefix': None,
  90. }
  91. flexmock(module.feature).should_receive('available').and_return(True)
  92. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  93. None, 'bar-{now}', '1.2.3' # noqa: FS003
  94. ).and_return(('--match-archives', 'sh:bar-*')).once()
  95. result = module.make_prune_flags(
  96. config, flexmock(match_archives=None), local_borg_version='1.2.3'
  97. )
  98. expected = (
  99. '--keep-daily',
  100. '1',
  101. '--match-archives',
  102. 'sh:bar-*', # noqa: FS003
  103. )
  104. assert result == expected
  105. def test_make_prune_flags_without_prefix_uses_match_archives_flag_instead_of_option():
  106. config = {
  107. 'archive_name_format': 'bar-{now}', # noqa: FS003
  108. 'match_archives': 'foo*',
  109. 'keep_daily': 1,
  110. 'prefix': None,
  111. }
  112. flexmock(module.feature).should_receive('available').and_return(True)
  113. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  114. 'baz*', 'bar-{now}', '1.2.3' # noqa: FS003
  115. ).and_return(('--match-archives', 'sh:bar-*')).once()
  116. result = module.make_prune_flags(
  117. config, flexmock(match_archives='baz*'), local_borg_version='1.2.3'
  118. )
  119. expected = (
  120. '--keep-daily',
  121. '1',
  122. '--match-archives',
  123. 'sh:bar-*', # noqa: FS003
  124. )
  125. assert result == expected
  126. def test_make_prune_flags_without_prefix_uses_match_archives_option():
  127. config = {
  128. 'archive_name_format': 'bar-{now}', # noqa: FS003
  129. 'match_archives': 'foo*',
  130. 'keep_daily': 1,
  131. 'prefix': None,
  132. }
  133. flexmock(module.feature).should_receive('available').and_return(True)
  134. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  135. 'foo*', 'bar-{now}', '1.2.3' # noqa: FS003
  136. ).and_return(('--match-archives', 'sh:bar-*')).once()
  137. result = module.make_prune_flags(
  138. config, flexmock(match_archives=None), local_borg_version='1.2.3'
  139. )
  140. expected = (
  141. '--keep-daily',
  142. '1',
  143. '--match-archives',
  144. 'sh:bar-*', # noqa: FS003
  145. )
  146. assert result == expected
  147. def test_make_prune_flags_ignores_keep_exclude_tags_in_config():
  148. config = {
  149. 'keep_daily': 1,
  150. 'keep_exclude_tags': True,
  151. }
  152. flexmock(module.feature).should_receive('available').and_return(True)
  153. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  154. result = module.make_prune_flags(
  155. config, flexmock(match_archives=None), local_borg_version='1.2.3'
  156. )
  157. assert result == ('--keep-daily', '1')
  158. PRUNE_COMMAND = ('borg', 'prune', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly', '3')
  159. def test_prune_archives_calls_borg_with_flags():
  160. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  161. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  162. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  163. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  164. insert_execute_command_mock(PRUNE_COMMAND + ('repo',), logging.INFO)
  165. prune_arguments = flexmock(stats=False, list_archives=False)
  166. module.prune_archives(
  167. dry_run=False,
  168. repository_path='repo',
  169. config={},
  170. local_borg_version='1.2.3',
  171. global_arguments=flexmock(log_json=False),
  172. prune_arguments=prune_arguments,
  173. )
  174. def test_prune_archives_with_log_info_calls_borg_with_info_flag():
  175. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  176. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  177. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  178. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  179. insert_execute_command_mock(PRUNE_COMMAND + ('--info', 'repo'), logging.INFO)
  180. insert_logging_mock(logging.INFO)
  181. prune_arguments = flexmock(stats=False, list_archives=False)
  182. module.prune_archives(
  183. repository_path='repo',
  184. config={},
  185. dry_run=False,
  186. local_borg_version='1.2.3',
  187. global_arguments=flexmock(log_json=False),
  188. prune_arguments=prune_arguments,
  189. )
  190. def test_prune_archives_with_log_debug_calls_borg_with_debug_flag():
  191. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  192. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  193. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  194. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  195. insert_execute_command_mock(PRUNE_COMMAND + ('--debug', '--show-rc', 'repo'), logging.INFO)
  196. insert_logging_mock(logging.DEBUG)
  197. prune_arguments = flexmock(stats=False, list_archives=False)
  198. module.prune_archives(
  199. repository_path='repo',
  200. config={},
  201. dry_run=False,
  202. local_borg_version='1.2.3',
  203. global_arguments=flexmock(log_json=False),
  204. prune_arguments=prune_arguments,
  205. )
  206. def test_prune_archives_with_dry_run_calls_borg_with_dry_run_flag():
  207. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  208. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  209. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  210. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  211. insert_execute_command_mock(PRUNE_COMMAND + ('--dry-run', 'repo'), logging.INFO)
  212. prune_arguments = flexmock(stats=False, list_archives=False)
  213. module.prune_archives(
  214. repository_path='repo',
  215. config={},
  216. dry_run=True,
  217. local_borg_version='1.2.3',
  218. global_arguments=flexmock(log_json=False),
  219. prune_arguments=prune_arguments,
  220. )
  221. def test_prune_archives_with_local_path_calls_borg_via_local_path():
  222. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  223. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  224. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  225. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  226. insert_execute_command_mock(('borg1',) + PRUNE_COMMAND[1:] + ('repo',), logging.INFO)
  227. prune_arguments = flexmock(stats=False, list_archives=False)
  228. module.prune_archives(
  229. dry_run=False,
  230. repository_path='repo',
  231. config={},
  232. local_borg_version='1.2.3',
  233. global_arguments=flexmock(log_json=False),
  234. local_path='borg1',
  235. prune_arguments=prune_arguments,
  236. )
  237. def test_prune_archives_with_exit_codes_calls_borg_using_them():
  238. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  239. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  240. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  241. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  242. borg_exit_codes = flexmock()
  243. insert_execute_command_mock(
  244. ('borg',) + PRUNE_COMMAND[1:] + ('repo',),
  245. logging.INFO,
  246. borg_exit_codes=borg_exit_codes,
  247. )
  248. prune_arguments = flexmock(stats=False, list_archives=False)
  249. module.prune_archives(
  250. dry_run=False,
  251. repository_path='repo',
  252. config={'borg_exit_codes': borg_exit_codes},
  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_remote_path_calls_borg_with_remote_path_flags():
  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 + ('--remote-path', 'borg1', '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=False),
  270. remote_path='borg1',
  271. prune_arguments=prune_arguments,
  272. )
  273. def test_prune_archives_with_stats_calls_borg_with_stats_flag_and_answer_output_log_level():
  274. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  275. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  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 + ('--stats', 'repo'), module.borgmatic.logger.ANSWER)
  279. prune_arguments = flexmock(stats=True, list_archives=False)
  280. module.prune_archives(
  281. dry_run=False,
  282. repository_path='repo',
  283. 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_files_calls_borg_with_list_flag_and_answer_output_log_level():
  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 + ('--list', 'repo'), module.borgmatic.logger.ANSWER)
  294. prune_arguments = flexmock(stats=False, list_archives=True)
  295. module.prune_archives(
  296. dry_run=False,
  297. repository_path='repo',
  298. config={},
  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_umask_calls_borg_with_umask_flags():
  304. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  305. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  306. config = {'umask': '077'}
  307. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  308. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  309. insert_execute_command_mock(PRUNE_COMMAND + ('--umask', '077', 'repo'), logging.INFO)
  310. prune_arguments = flexmock(stats=False, list_archives=False)
  311. module.prune_archives(
  312. dry_run=False,
  313. repository_path='repo',
  314. config=config,
  315. local_borg_version='1.2.3',
  316. global_arguments=flexmock(log_json=False),
  317. prune_arguments=prune_arguments,
  318. )
  319. def test_prune_archives_with_log_json_calls_borg_with_log_json_flag():
  320. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  321. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  322. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  323. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  324. insert_execute_command_mock(PRUNE_COMMAND + ('--log-json', 'repo'), logging.INFO)
  325. prune_arguments = flexmock(stats=False, list_archives=False)
  326. module.prune_archives(
  327. dry_run=False,
  328. repository_path='repo',
  329. config={},
  330. local_borg_version='1.2.3',
  331. global_arguments=flexmock(log_json=True),
  332. prune_arguments=prune_arguments,
  333. )
  334. def test_prune_archives_with_lock_wait_calls_borg_with_lock_wait_flags():
  335. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  336. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  337. config = {'lock_wait': 5}
  338. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  339. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  340. insert_execute_command_mock(PRUNE_COMMAND + ('--lock-wait', '5', 'repo'), logging.INFO)
  341. prune_arguments = flexmock(stats=False, list_archives=False)
  342. module.prune_archives(
  343. dry_run=False,
  344. repository_path='repo',
  345. config=config,
  346. local_borg_version='1.2.3',
  347. global_arguments=flexmock(log_json=False),
  348. prune_arguments=prune_arguments,
  349. )
  350. def test_prune_archives_with_extra_borg_options_calls_borg_with_extra_options():
  351. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  352. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  353. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  354. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  355. insert_execute_command_mock(PRUNE_COMMAND + ('--extra', '--options', 'repo'), logging.INFO)
  356. prune_arguments = flexmock(stats=False, list_archives=False)
  357. module.prune_archives(
  358. dry_run=False,
  359. repository_path='repo',
  360. config={'extra_borg_options': {'prune': '--extra --options'}},
  361. local_borg_version='1.2.3',
  362. global_arguments=flexmock(log_json=False),
  363. prune_arguments=prune_arguments,
  364. )
  365. def test_prune_archives_with_date_based_matching_calls_borg_with_date_based_flags():
  366. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  367. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  368. flexmock(module.flags).should_receive('make_flags').and_return(())
  369. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  370. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  371. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  372. (
  373. '--newer',
  374. '1d',
  375. '--newest',
  376. '1y',
  377. '--older',
  378. '1m',
  379. '--oldest',
  380. '1w',
  381. '--match-archives',
  382. None,
  383. )
  384. )
  385. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  386. flexmock(module.environment).should_receive('make_environment')
  387. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  388. flexmock(module).should_receive('execute_command').with_args(
  389. (
  390. 'borg',
  391. 'prune',
  392. '--keep-daily',
  393. '1',
  394. '--keep-weekly',
  395. '2',
  396. '--keep-monthly',
  397. '3',
  398. '--newer',
  399. '1d',
  400. '--newest',
  401. '1y',
  402. '--older',
  403. '1m',
  404. '--oldest',
  405. '1w',
  406. '--match-archives',
  407. None,
  408. '--repo',
  409. 'repo',
  410. ),
  411. output_log_level=logging.INFO,
  412. extra_environment=None,
  413. working_directory=None,
  414. borg_local_path='borg',
  415. borg_exit_codes=None,
  416. )
  417. prune_arguments = flexmock(
  418. stats=False, list_archives=False, newer='1d', newest='1y', older='1m', oldest='1w'
  419. )
  420. module.prune_archives(
  421. dry_run=False,
  422. repository_path='repo',
  423. config={},
  424. local_borg_version='1.2.3',
  425. global_arguments=flexmock(log_json=False),
  426. prune_arguments=prune_arguments,
  427. )
  428. def test_prune_archives_calls_borg_with_working_directory():
  429. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  430. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  431. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  432. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  433. insert_execute_command_mock(
  434. PRUNE_COMMAND + ('repo',), logging.INFO, working_directory='/working/dir'
  435. )
  436. prune_arguments = flexmock(stats=False, list_archives=False)
  437. module.prune_archives(
  438. dry_run=False,
  439. repository_path='repo',
  440. config={'working_directory': '/working/dir'},
  441. local_borg_version='1.2.3',
  442. global_arguments=flexmock(log_json=False),
  443. prune_arguments=prune_arguments,
  444. )