test_prune.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. 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. flexmock(module.feature).should_receive('available').with_args(
  165. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  166. ).and_return(False)
  167. insert_execute_command_mock(PRUNE_COMMAND + ('repo',), logging.INFO)
  168. prune_arguments = flexmock(stats=False, list_archives=False)
  169. module.prune_archives(
  170. dry_run=False,
  171. repository_path='repo',
  172. config={},
  173. local_borg_version='1.2.3',
  174. global_arguments=flexmock(log_json=False),
  175. prune_arguments=prune_arguments,
  176. )
  177. def test_prune_archives_with_log_info_calls_borg_with_info_flag():
  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. flexmock(module.feature).should_receive('available').with_args(
  183. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  184. ).and_return(False)
  185. insert_execute_command_mock(PRUNE_COMMAND + ('--info', 'repo'), logging.INFO)
  186. insert_logging_mock(logging.INFO)
  187. prune_arguments = flexmock(stats=False, list_archives=False)
  188. module.prune_archives(
  189. repository_path='repo',
  190. config={},
  191. dry_run=False,
  192. local_borg_version='1.2.3',
  193. global_arguments=flexmock(log_json=False),
  194. prune_arguments=prune_arguments,
  195. )
  196. def test_prune_archives_with_log_debug_calls_borg_with_debug_flag():
  197. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  198. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  199. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  200. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  201. flexmock(module.feature).should_receive('available').with_args(
  202. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  203. ).and_return(False)
  204. insert_execute_command_mock(PRUNE_COMMAND + ('--debug', '--show-rc', 'repo'), logging.INFO)
  205. insert_logging_mock(logging.DEBUG)
  206. prune_arguments = flexmock(stats=False, list_archives=False)
  207. module.prune_archives(
  208. repository_path='repo',
  209. config={},
  210. dry_run=False,
  211. local_borg_version='1.2.3',
  212. global_arguments=flexmock(log_json=False),
  213. prune_arguments=prune_arguments,
  214. )
  215. def test_prune_archives_with_dry_run_calls_borg_with_dry_run_flag():
  216. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  217. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  218. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  219. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  220. flexmock(module.feature).should_receive('available').with_args(
  221. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  222. ).and_return(False)
  223. insert_execute_command_mock(PRUNE_COMMAND + ('--dry-run', 'repo'), logging.INFO)
  224. prune_arguments = flexmock(stats=False, list_archives=False)
  225. module.prune_archives(
  226. repository_path='repo',
  227. config={},
  228. dry_run=True,
  229. local_borg_version='1.2.3',
  230. global_arguments=flexmock(log_json=False),
  231. prune_arguments=prune_arguments,
  232. )
  233. def test_prune_archives_with_local_path_calls_borg_via_local_path():
  234. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  235. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  236. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  237. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  238. flexmock(module.feature).should_receive('available').with_args(
  239. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  240. ).and_return(False)
  241. insert_execute_command_mock(('borg1',) + PRUNE_COMMAND[1:] + ('repo',), logging.INFO)
  242. prune_arguments = flexmock(stats=False, list_archives=False)
  243. module.prune_archives(
  244. dry_run=False,
  245. repository_path='repo',
  246. config={},
  247. local_borg_version='1.2.3',
  248. global_arguments=flexmock(log_json=False),
  249. local_path='borg1',
  250. prune_arguments=prune_arguments,
  251. )
  252. def test_prune_archives_with_exit_codes_calls_borg_using_them():
  253. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  254. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  255. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  256. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  257. flexmock(module.feature).should_receive('available').with_args(
  258. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  259. ).and_return(False)
  260. borg_exit_codes = flexmock()
  261. insert_execute_command_mock(
  262. ('borg',) + PRUNE_COMMAND[1:] + ('repo',),
  263. logging.INFO,
  264. borg_exit_codes=borg_exit_codes,
  265. )
  266. prune_arguments = flexmock(stats=False, list_archives=False)
  267. module.prune_archives(
  268. dry_run=False,
  269. repository_path='repo',
  270. config={'borg_exit_codes': borg_exit_codes},
  271. local_borg_version='1.2.3',
  272. global_arguments=flexmock(log_json=False),
  273. prune_arguments=prune_arguments,
  274. )
  275. def test_prune_archives_with_remote_path_calls_borg_with_remote_path_flags():
  276. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  277. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  278. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  279. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  280. flexmock(module.feature).should_receive('available').with_args(
  281. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  282. ).and_return(False)
  283. insert_execute_command_mock(PRUNE_COMMAND + ('--remote-path', 'borg1', 'repo'), logging.INFO)
  284. prune_arguments = flexmock(stats=False, list_archives=False)
  285. module.prune_archives(
  286. dry_run=False,
  287. repository_path='repo',
  288. config={},
  289. local_borg_version='1.2.3',
  290. global_arguments=flexmock(log_json=False),
  291. remote_path='borg1',
  292. prune_arguments=prune_arguments,
  293. )
  294. def test_prune_archives_favors_stats_flag_over_config():
  295. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  296. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  297. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  298. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  299. flexmock(module.feature).should_receive('available').with_args(
  300. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  301. ).and_return(False)
  302. insert_execute_command_mock(PRUNE_COMMAND + ('--stats', 'repo'), module.borgmatic.logger.ANSWER)
  303. prune_arguments = flexmock(stats=True, list_archives=False)
  304. module.prune_archives(
  305. dry_run=False,
  306. repository_path='repo',
  307. config={'statistics': False},
  308. local_borg_version='1.2.3',
  309. global_arguments=flexmock(log_json=False),
  310. prune_arguments=prune_arguments,
  311. )
  312. def test_prune_archives_defaults_to_stats_config():
  313. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  314. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  315. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  316. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  317. flexmock(module.feature).should_receive('available').with_args(
  318. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  319. ).and_return(False)
  320. insert_execute_command_mock(PRUNE_COMMAND + ('--stats', 'repo'), module.borgmatic.logger.ANSWER)
  321. prune_arguments = flexmock(stats=None, list_archives=False)
  322. module.prune_archives(
  323. dry_run=False,
  324. repository_path='repo',
  325. config={'statistics': True},
  326. local_borg_version='1.2.3',
  327. global_arguments=flexmock(log_json=False),
  328. prune_arguments=prune_arguments,
  329. )
  330. def test_prune_archives_favors_list_flag_over_config():
  331. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  332. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  333. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  334. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  335. flexmock(module.feature).should_receive('available').with_args(
  336. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  337. ).and_return(False)
  338. insert_execute_command_mock(PRUNE_COMMAND + ('--list', 'repo'), module.borgmatic.logger.ANSWER)
  339. prune_arguments = flexmock(stats=False, list_archives=True)
  340. module.prune_archives(
  341. dry_run=False,
  342. repository_path='repo',
  343. config={'list_details': False},
  344. local_borg_version='1.2.3',
  345. global_arguments=flexmock(log_json=False),
  346. prune_arguments=prune_arguments,
  347. )
  348. def test_prune_archives_defaults_to_list_config():
  349. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  350. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  351. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  352. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  353. flexmock(module.feature).should_receive('available').with_args(
  354. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  355. ).and_return(False)
  356. insert_execute_command_mock(PRUNE_COMMAND + ('--list', 'repo'), module.borgmatic.logger.ANSWER)
  357. prune_arguments = flexmock(stats=False, list_archives=None)
  358. module.prune_archives(
  359. dry_run=False,
  360. repository_path='repo',
  361. config={'list_details': True},
  362. local_borg_version='1.2.3',
  363. global_arguments=flexmock(log_json=False),
  364. prune_arguments=prune_arguments,
  365. )
  366. def test_prune_archives_with_umask_calls_borg_with_umask_flags():
  367. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  368. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  369. config = {'umask': '077'}
  370. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  371. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  372. flexmock(module.feature).should_receive('available').with_args(
  373. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  374. ).and_return(False)
  375. insert_execute_command_mock(PRUNE_COMMAND + ('--umask', '077', 'repo'), logging.INFO)
  376. prune_arguments = flexmock(stats=False, list_archives=False)
  377. module.prune_archives(
  378. dry_run=False,
  379. repository_path='repo',
  380. config=config,
  381. local_borg_version='1.2.3',
  382. global_arguments=flexmock(log_json=False),
  383. prune_arguments=prune_arguments,
  384. )
  385. def test_prune_archives_with_log_json_calls_borg_with_log_json_flag():
  386. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  387. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  388. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  389. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  390. flexmock(module.feature).should_receive('available').with_args(
  391. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  392. ).and_return(False)
  393. insert_execute_command_mock(PRUNE_COMMAND + ('--log-json', 'repo'), logging.INFO)
  394. prune_arguments = flexmock(stats=False, list_archives=False)
  395. module.prune_archives(
  396. dry_run=False,
  397. repository_path='repo',
  398. config={},
  399. local_borg_version='1.2.3',
  400. global_arguments=flexmock(log_json=True),
  401. prune_arguments=prune_arguments,
  402. )
  403. def test_prune_archives_with_lock_wait_calls_borg_with_lock_wait_flags():
  404. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  405. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  406. config = {'lock_wait': 5}
  407. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  408. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  409. flexmock(module.feature).should_receive('available').with_args(
  410. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  411. ).and_return(False)
  412. insert_execute_command_mock(PRUNE_COMMAND + ('--lock-wait', '5', 'repo'), logging.INFO)
  413. prune_arguments = flexmock(stats=False, list_archives=False)
  414. module.prune_archives(
  415. dry_run=False,
  416. repository_path='repo',
  417. config=config,
  418. local_borg_version='1.2.3',
  419. global_arguments=flexmock(log_json=False),
  420. prune_arguments=prune_arguments,
  421. )
  422. def test_prune_archives_with_extra_borg_options_calls_borg_with_extra_options():
  423. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  424. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  425. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  426. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  427. flexmock(module.feature).should_receive('available').with_args(
  428. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  429. ).and_return(False)
  430. insert_execute_command_mock(PRUNE_COMMAND + ('--extra', '--options', 'repo'), logging.INFO)
  431. prune_arguments = flexmock(stats=False, list_archives=False)
  432. module.prune_archives(
  433. dry_run=False,
  434. repository_path='repo',
  435. config={'extra_borg_options': {'prune': '--extra --options'}},
  436. local_borg_version='1.2.3',
  437. global_arguments=flexmock(log_json=False),
  438. prune_arguments=prune_arguments,
  439. )
  440. def test_prune_archives_with_date_based_matching_calls_borg_with_date_based_flags():
  441. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  442. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  443. flexmock(module.flags).should_receive('make_flags').and_return(())
  444. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  445. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  446. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  447. (
  448. '--newer',
  449. '1d',
  450. '--newest',
  451. '1y',
  452. '--older',
  453. '1m',
  454. '--oldest',
  455. '1w',
  456. '--match-archives',
  457. None,
  458. )
  459. )
  460. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  461. flexmock(module.feature).should_receive('available').with_args(
  462. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  463. ).and_return(False)
  464. flexmock(module.environment).should_receive('make_environment')
  465. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  466. flexmock(module).should_receive('execute_command').with_args(
  467. (
  468. 'borg',
  469. 'prune',
  470. '--keep-daily',
  471. '1',
  472. '--keep-weekly',
  473. '2',
  474. '--keep-monthly',
  475. '3',
  476. '--newer',
  477. '1d',
  478. '--newest',
  479. '1y',
  480. '--older',
  481. '1m',
  482. '--oldest',
  483. '1w',
  484. '--match-archives',
  485. None,
  486. '--repo',
  487. 'repo',
  488. ),
  489. output_log_level=logging.INFO,
  490. environment=None,
  491. working_directory=None,
  492. borg_local_path='borg',
  493. borg_exit_codes=None,
  494. )
  495. prune_arguments = flexmock(
  496. stats=False, list_archives=False, newer='1d', newest='1y', older='1m', oldest='1w'
  497. )
  498. module.prune_archives(
  499. dry_run=False,
  500. repository_path='repo',
  501. config={},
  502. local_borg_version='1.2.3',
  503. global_arguments=flexmock(log_json=False),
  504. prune_arguments=prune_arguments,
  505. )
  506. def test_prune_archives_calls_borg_with_working_directory():
  507. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  508. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  509. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  510. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  511. flexmock(module.feature).should_receive('available').with_args(
  512. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  513. ).and_return(False)
  514. insert_execute_command_mock(
  515. PRUNE_COMMAND + ('repo',), logging.INFO, working_directory='/working/dir'
  516. )
  517. prune_arguments = flexmock(stats=False, list_archives=False)
  518. module.prune_archives(
  519. dry_run=False,
  520. repository_path='repo',
  521. config={'working_directory': '/working/dir'},
  522. local_borg_version='1.2.3',
  523. global_arguments=flexmock(log_json=False),
  524. prune_arguments=prune_arguments,
  525. )
  526. def test_prune_archives_calls_borg_with_flags_and_when_feature_available():
  527. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  528. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  529. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  530. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  531. flexmock(module.feature).should_receive('available').with_args(
  532. module.feature.Feature.NO_PRUNE_STATS, '2.0.0b10'
  533. ).and_return(True)
  534. insert_execute_command_mock(PRUNE_COMMAND + ('repo',), logging.ANSWER)
  535. prune_arguments = flexmock(stats=True, list_archives=False)
  536. module.prune_archives(
  537. dry_run=False,
  538. repository_path='repo',
  539. config={},
  540. local_borg_version='2.0.0b10',
  541. global_arguments=flexmock(log_json=False),
  542. prune_arguments=prune_arguments,
  543. )