test_prune.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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_with_keep_13weekly_and_keep_3monthly():
  34. config = {
  35. 'keep_13weekly': 4,
  36. 'keep_3monthly': 5,
  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-13weekly',
  45. '4',
  46. '--keep-3monthly',
  47. '5',
  48. )
  49. assert result == expected
  50. def test_make_prune_flags_accepts_prefix_with_placeholders():
  51. config = {
  52. 'keep_daily': 1,
  53. 'prefix': 'Documents_{hostname}-{now}', # noqa: FS003
  54. }
  55. flexmock(module.feature).should_receive('available').and_return(True)
  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. '--match-archives',
  64. 'sh:Documents_{hostname}-{now}*', # noqa: FS003
  65. )
  66. assert result == expected
  67. def test_make_prune_flags_with_prefix_without_borg_features_uses_glob_archives():
  68. config = {
  69. 'keep_daily': 1,
  70. 'prefix': 'Documents_{hostname}-{now}', # noqa: FS003
  71. }
  72. flexmock(module.feature).should_receive('available').and_return(False)
  73. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  74. result = module.make_prune_flags(
  75. config, flexmock(match_archives=None), local_borg_version='1.2.3'
  76. )
  77. expected = (
  78. '--keep-daily',
  79. '1',
  80. '--glob-archives',
  81. 'Documents_{hostname}-{now}*', # noqa: FS003
  82. )
  83. assert result == expected
  84. def test_make_prune_flags_prefers_prefix_to_archive_name_format():
  85. config = {
  86. 'archive_name_format': 'bar-{now}', # noqa: FS003
  87. 'keep_daily': 1,
  88. 'prefix': 'bar-',
  89. }
  90. flexmock(module.feature).should_receive('available').and_return(True)
  91. flexmock(module.flags).should_receive('make_match_archives_flags').never()
  92. result = module.make_prune_flags(
  93. config, flexmock(match_archives=None), local_borg_version='1.2.3'
  94. )
  95. expected = (
  96. '--keep-daily',
  97. '1',
  98. '--match-archives',
  99. 'sh:bar-*', # noqa: FS003
  100. )
  101. assert result == expected
  102. def test_make_prune_flags_without_prefix_uses_archive_name_format_instead():
  103. config = {
  104. 'archive_name_format': 'bar-{now}', # noqa: FS003
  105. 'keep_daily': 1,
  106. 'prefix': None,
  107. }
  108. flexmock(module.feature).should_receive('available').and_return(True)
  109. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  110. None, 'bar-{now}', '1.2.3' # noqa: FS003
  111. ).and_return(('--match-archives', 'sh:bar-*')).once()
  112. result = module.make_prune_flags(
  113. config, flexmock(match_archives=None), local_borg_version='1.2.3'
  114. )
  115. expected = (
  116. '--keep-daily',
  117. '1',
  118. '--match-archives',
  119. 'sh:bar-*', # noqa: FS003
  120. )
  121. assert result == expected
  122. def test_make_prune_flags_without_prefix_uses_match_archives_option():
  123. config = {
  124. 'archive_name_format': 'bar-{now}', # noqa: FS003
  125. 'match_archives': 'foo*',
  126. 'keep_daily': 1,
  127. 'prefix': None,
  128. }
  129. flexmock(module.feature).should_receive('available').and_return(True)
  130. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  131. 'foo*', 'bar-{now}', '1.2.3' # noqa: FS003
  132. ).and_return(('--match-archives', 'sh:bar-*')).once()
  133. result = module.make_prune_flags(
  134. config, flexmock(match_archives=None), local_borg_version='1.2.3'
  135. )
  136. expected = (
  137. '--keep-daily',
  138. '1',
  139. '--match-archives',
  140. 'sh:bar-*', # noqa: FS003
  141. )
  142. assert result == expected
  143. def test_make_prune_flags_ignores_keep_exclude_tags_in_config():
  144. config = {
  145. 'keep_daily': 1,
  146. 'keep_exclude_tags': True,
  147. }
  148. flexmock(module.feature).should_receive('available').and_return(True)
  149. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  150. result = module.make_prune_flags(
  151. config, flexmock(match_archives=None), local_borg_version='1.2.3'
  152. )
  153. assert result == ('--keep-daily', '1')
  154. PRUNE_COMMAND = ('borg', 'prune', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly', '3')
  155. def test_prune_archives_calls_borg_with_flags():
  156. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  157. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  158. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  159. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  160. flexmock(module.feature).should_receive('available').with_args(
  161. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  162. ).and_return(False)
  163. insert_execute_command_mock(PRUNE_COMMAND + ('repo',), logging.INFO)
  164. prune_arguments = flexmock(statistics=False, list_details=False)
  165. module.prune_archives(
  166. dry_run=False,
  167. repository_path='repo',
  168. config={},
  169. local_borg_version='1.2.3',
  170. global_arguments=flexmock(),
  171. prune_arguments=prune_arguments,
  172. )
  173. def test_prune_archives_with_log_info_calls_borg_with_info_flag():
  174. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  175. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  176. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  177. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  178. flexmock(module.feature).should_receive('available').with_args(
  179. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  180. ).and_return(False)
  181. insert_execute_command_mock(PRUNE_COMMAND + ('--info', 'repo'), logging.INFO)
  182. insert_logging_mock(logging.INFO)
  183. prune_arguments = flexmock(statistics=False, list_details=False)
  184. module.prune_archives(
  185. repository_path='repo',
  186. config={},
  187. dry_run=False,
  188. local_borg_version='1.2.3',
  189. global_arguments=flexmock(),
  190. prune_arguments=prune_arguments,
  191. )
  192. def test_prune_archives_with_log_debug_calls_borg_with_debug_flag():
  193. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  194. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  195. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  196. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  197. flexmock(module.feature).should_receive('available').with_args(
  198. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  199. ).and_return(False)
  200. insert_execute_command_mock(PRUNE_COMMAND + ('--debug', '--show-rc', 'repo'), logging.INFO)
  201. insert_logging_mock(logging.DEBUG)
  202. prune_arguments = flexmock(statistics=False, list_details=False)
  203. module.prune_archives(
  204. repository_path='repo',
  205. config={},
  206. dry_run=False,
  207. local_borg_version='1.2.3',
  208. global_arguments=flexmock(),
  209. prune_arguments=prune_arguments,
  210. )
  211. def test_prune_archives_with_dry_run_calls_borg_with_dry_run_flag():
  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. flexmock(module.feature).should_receive('available').with_args(
  217. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  218. ).and_return(False)
  219. insert_execute_command_mock(PRUNE_COMMAND + ('--dry-run', 'repo'), logging.INFO)
  220. prune_arguments = flexmock(statistics=False, list_details=False)
  221. module.prune_archives(
  222. repository_path='repo',
  223. config={},
  224. dry_run=True,
  225. local_borg_version='1.2.3',
  226. global_arguments=flexmock(),
  227. prune_arguments=prune_arguments,
  228. )
  229. def test_prune_archives_with_local_path_calls_borg_via_local_path():
  230. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  231. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  232. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  233. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  234. flexmock(module.feature).should_receive('available').with_args(
  235. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  236. ).and_return(False)
  237. insert_execute_command_mock(('borg1',) + PRUNE_COMMAND[1:] + ('repo',), logging.INFO)
  238. prune_arguments = flexmock(statistics=False, list_details=False)
  239. module.prune_archives(
  240. dry_run=False,
  241. repository_path='repo',
  242. config={},
  243. local_borg_version='1.2.3',
  244. global_arguments=flexmock(),
  245. local_path='borg1',
  246. prune_arguments=prune_arguments,
  247. )
  248. def test_prune_archives_with_exit_codes_calls_borg_using_them():
  249. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  250. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  251. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  252. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  253. flexmock(module.feature).should_receive('available').with_args(
  254. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  255. ).and_return(False)
  256. borg_exit_codes = flexmock()
  257. insert_execute_command_mock(
  258. ('borg',) + PRUNE_COMMAND[1:] + ('repo',),
  259. logging.INFO,
  260. borg_exit_codes=borg_exit_codes,
  261. )
  262. prune_arguments = flexmock(statistics=False, list_details=False)
  263. module.prune_archives(
  264. dry_run=False,
  265. repository_path='repo',
  266. config={'borg_exit_codes': borg_exit_codes},
  267. local_borg_version='1.2.3',
  268. global_arguments=flexmock(),
  269. prune_arguments=prune_arguments,
  270. )
  271. def test_prune_archives_with_remote_path_calls_borg_with_remote_path_flags():
  272. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  273. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  274. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  275. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  276. flexmock(module.feature).should_receive('available').with_args(
  277. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  278. ).and_return(False)
  279. insert_execute_command_mock(PRUNE_COMMAND + ('--remote-path', 'borg1', 'repo'), logging.INFO)
  280. prune_arguments = flexmock(statistics=False, list_details=False)
  281. module.prune_archives(
  282. dry_run=False,
  283. repository_path='repo',
  284. config={},
  285. local_borg_version='1.2.3',
  286. global_arguments=flexmock(),
  287. remote_path='borg1',
  288. prune_arguments=prune_arguments,
  289. )
  290. def test_prune_archives_with_stats_config_calls_borg_with_stats_flag():
  291. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  292. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  293. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  294. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  295. flexmock(module.feature).should_receive('available').with_args(
  296. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  297. ).and_return(False)
  298. insert_execute_command_mock(PRUNE_COMMAND + ('--stats', 'repo'), module.borgmatic.logger.ANSWER)
  299. prune_arguments = flexmock(statistics=None, list_details=False)
  300. module.prune_archives(
  301. dry_run=False,
  302. repository_path='repo',
  303. config={'statistics': True},
  304. local_borg_version='1.2.3',
  305. global_arguments=flexmock(),
  306. prune_arguments=prune_arguments,
  307. )
  308. def test_prune_archives_with_list_config_calls_borg_with_list_flag():
  309. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  310. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  311. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  312. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  313. flexmock(module.feature).should_receive('available').with_args(
  314. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  315. ).and_return(False)
  316. insert_execute_command_mock(PRUNE_COMMAND + ('--list', 'repo'), module.borgmatic.logger.ANSWER)
  317. prune_arguments = flexmock(statistics=False, list_details=None)
  318. module.prune_archives(
  319. dry_run=False,
  320. repository_path='repo',
  321. config={'list_details': True},
  322. local_borg_version='1.2.3',
  323. global_arguments=flexmock(),
  324. prune_arguments=prune_arguments,
  325. )
  326. def test_prune_archives_with_umask_calls_borg_with_umask_flags():
  327. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  328. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  329. config = {'umask': '077'}
  330. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  331. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  332. flexmock(module.feature).should_receive('available').with_args(
  333. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  334. ).and_return(False)
  335. insert_execute_command_mock(PRUNE_COMMAND + ('--umask', '077', 'repo'), logging.INFO)
  336. prune_arguments = flexmock(statistics=False, list_details=False)
  337. module.prune_archives(
  338. dry_run=False,
  339. repository_path='repo',
  340. config=config,
  341. local_borg_version='1.2.3',
  342. global_arguments=flexmock(),
  343. prune_arguments=prune_arguments,
  344. )
  345. def test_prune_archives_with_log_json_calls_borg_with_log_json_flag():
  346. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  347. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  348. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  349. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  350. flexmock(module.feature).should_receive('available').with_args(
  351. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  352. ).and_return(False)
  353. insert_execute_command_mock(PRUNE_COMMAND + ('--log-json', 'repo'), logging.INFO)
  354. prune_arguments = flexmock(statistics=False, list_details=False)
  355. module.prune_archives(
  356. dry_run=False,
  357. repository_path='repo',
  358. config={'log_json': True},
  359. local_borg_version='1.2.3',
  360. global_arguments=flexmock(),
  361. prune_arguments=prune_arguments,
  362. )
  363. def test_prune_archives_with_lock_wait_calls_borg_with_lock_wait_flags():
  364. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  365. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  366. config = {'lock_wait': 5}
  367. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  368. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  369. flexmock(module.feature).should_receive('available').with_args(
  370. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  371. ).and_return(False)
  372. insert_execute_command_mock(PRUNE_COMMAND + ('--lock-wait', '5', 'repo'), logging.INFO)
  373. prune_arguments = flexmock(statistics=False, list_details=False)
  374. module.prune_archives(
  375. dry_run=False,
  376. repository_path='repo',
  377. config=config,
  378. local_borg_version='1.2.3',
  379. global_arguments=flexmock(),
  380. prune_arguments=prune_arguments,
  381. )
  382. def test_prune_archives_with_extra_borg_options_calls_borg_with_extra_options():
  383. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  384. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  385. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  386. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  387. flexmock(module.feature).should_receive('available').with_args(
  388. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  389. ).and_return(False)
  390. insert_execute_command_mock(PRUNE_COMMAND + ('--extra', '--options', 'repo'), logging.INFO)
  391. prune_arguments = flexmock(statistics=False, list_details=False)
  392. module.prune_archives(
  393. dry_run=False,
  394. repository_path='repo',
  395. config={'extra_borg_options': {'prune': '--extra --options'}},
  396. local_borg_version='1.2.3',
  397. global_arguments=flexmock(),
  398. prune_arguments=prune_arguments,
  399. )
  400. def test_prune_archives_with_date_based_matching_calls_borg_with_date_based_flags():
  401. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  402. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  403. flexmock(module.flags).should_receive('make_flags').and_return(())
  404. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  405. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  406. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  407. (
  408. '--newer',
  409. '1d',
  410. '--newest',
  411. '1y',
  412. '--older',
  413. '1m',
  414. '--oldest',
  415. '1w',
  416. '--match-archives',
  417. None,
  418. )
  419. )
  420. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  421. flexmock(module.feature).should_receive('available').with_args(
  422. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  423. ).and_return(False)
  424. flexmock(module.environment).should_receive('make_environment')
  425. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  426. flexmock(module).should_receive('execute_command').with_args(
  427. (
  428. 'borg',
  429. 'prune',
  430. '--keep-daily',
  431. '1',
  432. '--keep-weekly',
  433. '2',
  434. '--keep-monthly',
  435. '3',
  436. '--newer',
  437. '1d',
  438. '--newest',
  439. '1y',
  440. '--older',
  441. '1m',
  442. '--oldest',
  443. '1w',
  444. '--match-archives',
  445. None,
  446. '--repo',
  447. 'repo',
  448. ),
  449. output_log_level=logging.INFO,
  450. environment=None,
  451. working_directory=None,
  452. borg_local_path='borg',
  453. borg_exit_codes=None,
  454. )
  455. prune_arguments = flexmock(
  456. statistics=False, list_details=False, newer='1d', newest='1y', older='1m', oldest='1w'
  457. )
  458. module.prune_archives(
  459. dry_run=False,
  460. repository_path='repo',
  461. config={},
  462. local_borg_version='1.2.3',
  463. global_arguments=flexmock(),
  464. prune_arguments=prune_arguments,
  465. )
  466. def test_prune_archives_calls_borg_with_working_directory():
  467. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  468. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  469. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  470. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  471. flexmock(module.feature).should_receive('available').with_args(
  472. module.feature.Feature.NO_PRUNE_STATS, '1.2.3'
  473. ).and_return(False)
  474. insert_execute_command_mock(
  475. PRUNE_COMMAND + ('repo',), logging.INFO, working_directory='/working/dir'
  476. )
  477. prune_arguments = flexmock(statistics=False, list_details=False)
  478. module.prune_archives(
  479. dry_run=False,
  480. repository_path='repo',
  481. config={'working_directory': '/working/dir'},
  482. local_borg_version='1.2.3',
  483. global_arguments=flexmock(),
  484. prune_arguments=prune_arguments,
  485. )
  486. def test_prune_archives_calls_borg_without_stats_when_feature_is_not_available():
  487. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  488. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  489. flexmock(module).should_receive('make_prune_flags').and_return(BASE_PRUNE_FLAGS)
  490. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  491. flexmock(module.feature).should_receive('available').with_args(
  492. module.feature.Feature.NO_PRUNE_STATS, '2.0.0b10'
  493. ).and_return(True)
  494. insert_execute_command_mock(PRUNE_COMMAND + ('repo',), logging.ANSWER)
  495. prune_arguments = flexmock(statistics=True, list_details=False)
  496. module.prune_archives(
  497. dry_run=False,
  498. repository_path='repo',
  499. config={'statistics': True},
  500. local_borg_version='2.0.0b10',
  501. global_arguments=flexmock(),
  502. prune_arguments=prune_arguments,
  503. )