test_prune.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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_with_stats_calls_borg_with_stats_flag_and_answer_output_log_level():
  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={},
  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_with_files_calls_borg_with_list_flag_and_answer_output_log_level():
  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 + ('--list', 'repo'), module.borgmatic.logger.ANSWER)
  321. prune_arguments = flexmock(stats=False, list_archives=True)
  322. module.prune_archives(
  323. dry_run=False,
  324. repository_path='repo',
  325. config={},
  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_with_umask_calls_borg_with_umask_flags():
  331. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  332. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  333. config = {'umask': '077'}
  334. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  335. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  336. flexmock(module.feature).should_receive('available').with_args(
  337. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  338. ).and_return(False)
  339. insert_execute_command_mock(PRUNE_COMMAND + ('--umask', '077', 'repo'), logging.INFO)
  340. prune_arguments = flexmock(stats=False, list_archives=False)
  341. module.prune_archives(
  342. dry_run=False,
  343. repository_path='repo',
  344. config=config,
  345. local_borg_version='1.2.3',
  346. global_arguments=flexmock(log_json=False),
  347. prune_arguments=prune_arguments,
  348. )
  349. def test_prune_archives_with_log_json_calls_borg_with_log_json_flag():
  350. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  351. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  352. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  353. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  354. flexmock(module.feature).should_receive('available').with_args(
  355. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  356. ).and_return(False)
  357. insert_execute_command_mock(PRUNE_COMMAND + ('--log-json', 'repo'), logging.INFO)
  358. prune_arguments = flexmock(stats=False, list_archives=False)
  359. module.prune_archives(
  360. dry_run=False,
  361. repository_path='repo',
  362. config={},
  363. local_borg_version='1.2.3',
  364. global_arguments=flexmock(log_json=True),
  365. prune_arguments=prune_arguments,
  366. )
  367. def test_prune_archives_with_lock_wait_calls_borg_with_lock_wait_flags():
  368. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  369. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  370. config = {'lock_wait': 5}
  371. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  372. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  373. flexmock(module.feature).should_receive('available').with_args(
  374. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  375. ).and_return(False)
  376. insert_execute_command_mock(PRUNE_COMMAND + ('--lock-wait', '5', 'repo'), logging.INFO)
  377. prune_arguments = flexmock(stats=False, list_archives=False)
  378. module.prune_archives(
  379. dry_run=False,
  380. repository_path='repo',
  381. config=config,
  382. local_borg_version='1.2.3',
  383. global_arguments=flexmock(log_json=False),
  384. prune_arguments=prune_arguments,
  385. )
  386. def test_prune_archives_with_extra_borg_options_calls_borg_with_extra_options():
  387. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  388. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  389. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  390. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  391. flexmock(module.feature).should_receive('available').with_args(
  392. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  393. ).and_return(False)
  394. insert_execute_command_mock(PRUNE_COMMAND + ('--extra', '--options', 'repo'), logging.INFO)
  395. prune_arguments = flexmock(stats=False, list_archives=False)
  396. module.prune_archives(
  397. dry_run=False,
  398. repository_path='repo',
  399. config={'extra_borg_options': {'prune': '--extra --options'}},
  400. local_borg_version='1.2.3',
  401. global_arguments=flexmock(log_json=False),
  402. prune_arguments=prune_arguments,
  403. )
  404. def test_prune_archives_with_date_based_matching_calls_borg_with_date_based_flags():
  405. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  406. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  407. flexmock(module.flags).should_receive('make_flags').and_return(())
  408. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  409. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  410. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  411. (
  412. '--newer',
  413. '1d',
  414. '--newest',
  415. '1y',
  416. '--older',
  417. '1m',
  418. '--oldest',
  419. '1w',
  420. '--match-archives',
  421. None,
  422. )
  423. )
  424. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  425. flexmock(module.feature).should_receive('available').with_args(
  426. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  427. ).and_return(False)
  428. flexmock(module.environment).should_receive('make_environment')
  429. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  430. flexmock(module).should_receive('execute_command').with_args(
  431. (
  432. 'borg',
  433. 'prune',
  434. '--keep-daily',
  435. '1',
  436. '--keep-weekly',
  437. '2',
  438. '--keep-monthly',
  439. '3',
  440. '--newer',
  441. '1d',
  442. '--newest',
  443. '1y',
  444. '--older',
  445. '1m',
  446. '--oldest',
  447. '1w',
  448. '--match-archives',
  449. None,
  450. '--repo',
  451. 'repo',
  452. ),
  453. output_log_level=logging.INFO,
  454. environment=None,
  455. working_directory=None,
  456. borg_local_path='borg',
  457. borg_exit_codes=None,
  458. )
  459. prune_arguments = flexmock(
  460. stats=False, list_archives=False, newer='1d', newest='1y', older='1m', oldest='1w'
  461. )
  462. module.prune_archives(
  463. dry_run=False,
  464. repository_path='repo',
  465. config={},
  466. local_borg_version='1.2.3',
  467. global_arguments=flexmock(log_json=False),
  468. prune_arguments=prune_arguments,
  469. )
  470. def test_prune_archives_calls_borg_with_working_directory():
  471. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  472. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  473. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  474. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  475. flexmock(module.feature).should_receive('available').with_args(
  476. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  477. ).and_return(False)
  478. insert_execute_command_mock(
  479. PRUNE_COMMAND + ('repo',), logging.INFO, working_directory='/working/dir'
  480. )
  481. prune_arguments = flexmock(stats=False, list_archives=False)
  482. module.prune_archives(
  483. dry_run=False,
  484. repository_path='repo',
  485. config={'working_directory': '/working/dir'},
  486. local_borg_version='1.2.3',
  487. global_arguments=flexmock(log_json=False),
  488. prune_arguments=prune_arguments,
  489. )
  490. def test_prune_archives_calls_borg_with_flags_and_when_feature_available():
  491. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  492. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  493. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  494. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  495. flexmock(module.feature).should_receive('available').with_args(
  496. module.feature.Feature.NO_PRUNE_STATS, '2.0.0b10'
  497. ).and_return(True)
  498. insert_execute_command_mock(PRUNE_COMMAND + ('repo',), logging.ANSWER)
  499. prune_arguments = flexmock(stats=True, list_archives=False)
  500. module.prune_archives(
  501. dry_run=False,
  502. repository_path='repo',
  503. config={},
  504. local_borg_version='2.0.0b10',
  505. global_arguments=flexmock(log_json=False),
  506. prune_arguments=prune_arguments,
  507. )