test_recreate.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. import logging
  2. import shlex
  3. from flexmock import flexmock
  4. from borgmatic.borg import recreate as module
  5. from ..test_verbosity import insert_logging_mock
  6. def insert_execute_command_mock(command, working_directory=None, borg_exit_codes=None):
  7. flexmock(module.borgmatic.borg.environment).should_receive('make_environment')
  8. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  9. full_command=command,
  10. output_log_level=module.logging.INFO,
  11. environment=None,
  12. working_directory=working_directory,
  13. borg_local_path=command[0],
  14. borg_exit_codes=borg_exit_codes,
  15. ).once()
  16. def test_recreate_archive_dry_run_skips_execution():
  17. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  18. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  19. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  20. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  21. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  22. flexmock(module.borgmatic.borg.flags).should_receive(
  23. 'make_repository_archive_flags',
  24. ).and_return(
  25. (
  26. '--repo',
  27. 'repo',
  28. ),
  29. )
  30. flexmock(module.borgmatic.execute).should_receive('execute_command').never()
  31. recreate_arguments = flexmock(
  32. repository=flexmock(),
  33. list=None,
  34. target=None,
  35. comment=None,
  36. timestamp=None,
  37. match_archives=None,
  38. )
  39. result = module.recreate_archive(
  40. repository='repo',
  41. archive='archive',
  42. config={},
  43. local_borg_version='1.2.3',
  44. recreate_arguments=recreate_arguments,
  45. global_arguments=flexmock(dry_run=True),
  46. local_path='borg',
  47. )
  48. assert result is None
  49. def test_recreate_calls_borg_with_required_flags():
  50. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  51. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  52. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  53. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  54. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  55. flexmock(module.borgmatic.borg.flags).should_receive(
  56. 'make_repository_archive_flags',
  57. ).and_return(
  58. (
  59. '--repo',
  60. 'repo',
  61. ),
  62. )
  63. insert_execute_command_mock(('borg', 'recreate', '--repo', 'repo'))
  64. module.recreate_archive(
  65. repository='repo',
  66. archive='archive',
  67. config={},
  68. local_borg_version='1.2.3',
  69. recreate_arguments=flexmock(
  70. list=None,
  71. target=None,
  72. comment=None,
  73. timestamp=None,
  74. match_archives=None,
  75. ),
  76. global_arguments=flexmock(dry_run=False),
  77. local_path='borg',
  78. remote_path=None,
  79. patterns=None,
  80. )
  81. def test_recreate_with_remote_path():
  82. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  83. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  84. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  85. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  86. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  87. flexmock(module.borgmatic.borg.flags).should_receive(
  88. 'make_repository_archive_flags',
  89. ).and_return(
  90. (
  91. '--repo',
  92. 'repo',
  93. ),
  94. )
  95. insert_execute_command_mock(('borg', 'recreate', '--remote-path', 'borg1', '--repo', 'repo'))
  96. module.recreate_archive(
  97. repository='repo',
  98. archive='archive',
  99. config={},
  100. local_borg_version='1.2.3',
  101. recreate_arguments=flexmock(
  102. list=None,
  103. target=None,
  104. comment=None,
  105. timestamp=None,
  106. match_archives=None,
  107. ),
  108. global_arguments=flexmock(dry_run=False),
  109. local_path='borg',
  110. remote_path='borg1',
  111. patterns=None,
  112. )
  113. def test_recreate_with_lock_wait():
  114. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  115. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  116. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  117. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  118. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  119. flexmock(module.borgmatic.borg.flags).should_receive(
  120. 'make_repository_archive_flags',
  121. ).and_return(
  122. (
  123. '--repo',
  124. 'repo',
  125. ),
  126. )
  127. insert_execute_command_mock(('borg', 'recreate', '--lock-wait', '5', '--repo', 'repo'))
  128. module.recreate_archive(
  129. repository='repo',
  130. archive='archive',
  131. config={'lock_wait': '5'},
  132. local_borg_version='1.2.3',
  133. recreate_arguments=flexmock(
  134. list=None,
  135. target=None,
  136. comment=None,
  137. timestamp=None,
  138. match_archives=None,
  139. ),
  140. global_arguments=flexmock(dry_run=False),
  141. local_path='borg',
  142. patterns=None,
  143. )
  144. def test_recreate_with_extra_borg_options():
  145. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  146. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  147. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  148. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  149. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  150. flexmock(module.borgmatic.borg.flags).should_receive(
  151. 'make_repository_archive_flags',
  152. ).and_return(
  153. (
  154. '--repo',
  155. 'repo',
  156. ),
  157. )
  158. insert_execute_command_mock(
  159. ('borg', 'recreate', '--extra', 'value with space', '--repo', 'repo')
  160. )
  161. module.recreate_archive(
  162. repository='repo',
  163. archive='archive',
  164. config={'extra_borg_options': {'recreate': '--extra "value with space"'}},
  165. local_borg_version='1.2.3',
  166. recreate_arguments=flexmock(
  167. list=None,
  168. target=None,
  169. comment=None,
  170. timestamp=None,
  171. match_archives=None,
  172. ),
  173. global_arguments=flexmock(dry_run=False),
  174. local_path='borg',
  175. patterns=None,
  176. )
  177. def test_recreate_with_log_info():
  178. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  179. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  180. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  181. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  182. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  183. flexmock(module.borgmatic.borg.flags).should_receive(
  184. 'make_repository_archive_flags',
  185. ).and_return(
  186. (
  187. '--repo',
  188. 'repo',
  189. ),
  190. )
  191. insert_execute_command_mock(('borg', 'recreate', '--info', '--repo', 'repo'))
  192. insert_logging_mock(logging.INFO)
  193. module.recreate_archive(
  194. repository='repo',
  195. archive='archive',
  196. config={},
  197. local_borg_version='1.2.3',
  198. recreate_arguments=flexmock(
  199. list=None,
  200. target=None,
  201. comment=None,
  202. timestamp=None,
  203. match_archives=None,
  204. ),
  205. global_arguments=flexmock(dry_run=False),
  206. local_path='borg',
  207. patterns=None,
  208. )
  209. def test_recreate_with_log_debug():
  210. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  211. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  212. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  213. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  214. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  215. flexmock(module.borgmatic.borg.flags).should_receive(
  216. 'make_repository_archive_flags',
  217. ).and_return(
  218. (
  219. '--repo',
  220. 'repo',
  221. ),
  222. )
  223. insert_execute_command_mock(('borg', 'recreate', '--debug', '--show-rc', '--repo', 'repo'))
  224. insert_logging_mock(logging.DEBUG)
  225. module.recreate_archive(
  226. repository='repo',
  227. archive='archive',
  228. config={},
  229. local_borg_version='1.2.3',
  230. recreate_arguments=flexmock(
  231. list=None,
  232. target=None,
  233. comment=None,
  234. timestamp=None,
  235. match_archives=None,
  236. ),
  237. global_arguments=flexmock(dry_run=False),
  238. local_path='borg',
  239. patterns=None,
  240. )
  241. def test_recreate_with_log_json():
  242. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  243. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  244. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  245. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  246. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  247. flexmock(module.borgmatic.borg.flags).should_receive(
  248. 'make_repository_archive_flags',
  249. ).and_return(
  250. (
  251. '--repo',
  252. 'repo',
  253. ),
  254. )
  255. insert_execute_command_mock(('borg', 'recreate', '--log-json', '--repo', 'repo'))
  256. module.recreate_archive(
  257. repository='repo',
  258. archive='archive',
  259. config={'log_json': True},
  260. local_borg_version='1.2.3',
  261. recreate_arguments=flexmock(
  262. list=None,
  263. target=None,
  264. comment=None,
  265. timestamp=None,
  266. match_archives=None,
  267. ),
  268. global_arguments=flexmock(dry_run=False),
  269. local_path='borg',
  270. patterns=None,
  271. )
  272. def test_recreate_with_list_config_calls_borg_with_list_flag():
  273. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  274. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  275. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  276. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  277. flexmock(module.borgmatic.borg.flags).should_receive(
  278. 'make_repository_archive_flags',
  279. ).and_return(
  280. (
  281. '--repo',
  282. 'repo',
  283. ),
  284. )
  285. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return(
  286. 'AME+-',
  287. )
  288. insert_execute_command_mock(
  289. ('borg', 'recreate', '--list', '--filter', 'AME+-', '--repo', 'repo'),
  290. )
  291. module.recreate_archive(
  292. repository='repo',
  293. archive='archive',
  294. config={'list_details': True},
  295. local_borg_version='1.2.3',
  296. recreate_arguments=flexmock(
  297. list=None,
  298. target=None,
  299. comment=None,
  300. timestamp=None,
  301. match_archives=None,
  302. ),
  303. global_arguments=flexmock(dry_run=False),
  304. local_path='borg',
  305. patterns=None,
  306. )
  307. def test_recreate_with_patterns_from_flag():
  308. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  309. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  310. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  311. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  312. flexmock(module.borgmatic.borg.flags).should_receive(
  313. 'make_repository_archive_flags',
  314. ).and_return(
  315. (
  316. '--repo',
  317. 'repo',
  318. ),
  319. )
  320. mock_patterns_file = flexmock(name='patterns_file')
  321. flexmock(module).should_receive('write_patterns_file').and_return(mock_patterns_file)
  322. insert_execute_command_mock(
  323. ('borg', 'recreate', '--patterns-from', 'patterns_file', '--repo', 'repo'),
  324. )
  325. module.recreate_archive(
  326. repository='repo',
  327. archive='archive',
  328. config={},
  329. local_borg_version='1.2.3',
  330. recreate_arguments=flexmock(
  331. list=None,
  332. target=None,
  333. comment=None,
  334. timestamp=None,
  335. match_archives=None,
  336. ),
  337. global_arguments=flexmock(dry_run=False),
  338. local_path='borg',
  339. patterns=['pattern1', 'pattern2'],
  340. )
  341. def test_recreate_with_exclude_flags():
  342. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  343. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  344. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  345. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  346. flexmock(module.borgmatic.borg.flags).should_receive(
  347. 'make_repository_archive_flags',
  348. ).and_return(
  349. (
  350. '--repo',
  351. 'repo',
  352. ),
  353. )
  354. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(
  355. ('--exclude', 'pattern'),
  356. )
  357. insert_execute_command_mock(('borg', 'recreate', '--exclude', 'pattern', '--repo', 'repo'))
  358. module.recreate_archive(
  359. repository='repo',
  360. archive='archive',
  361. config={'exclude_patterns': ['pattern']},
  362. local_borg_version='1.2.3',
  363. recreate_arguments=flexmock(
  364. list=None,
  365. target=None,
  366. comment=None,
  367. timestamp=None,
  368. match_archives=None,
  369. ),
  370. global_arguments=flexmock(dry_run=False),
  371. local_path='borg',
  372. patterns=None,
  373. )
  374. def test_recreate_with_target_flag():
  375. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  376. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  377. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  378. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  379. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  380. flexmock(module.borgmatic.borg.flags).should_receive(
  381. 'make_repository_archive_flags',
  382. ).and_return(
  383. (
  384. '--repo',
  385. 'repo',
  386. ),
  387. )
  388. insert_execute_command_mock(('borg', 'recreate', '--target', 'new-archive', '--repo', 'repo'))
  389. module.recreate_archive(
  390. repository='repo',
  391. archive='archive',
  392. config={},
  393. local_borg_version='1.2.3',
  394. recreate_arguments=flexmock(
  395. list=None,
  396. target='new-archive',
  397. comment=None,
  398. timestamp=None,
  399. match_archives=None,
  400. ),
  401. global_arguments=flexmock(dry_run=False),
  402. local_path='borg',
  403. patterns=None,
  404. )
  405. def test_recreate_with_comment_flag():
  406. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  407. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  408. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  409. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  410. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  411. flexmock(module.borgmatic.borg.flags).should_receive(
  412. 'make_repository_archive_flags',
  413. ).and_return(
  414. (
  415. '--repo',
  416. 'repo',
  417. ),
  418. )
  419. insert_execute_command_mock(
  420. ('borg', 'recreate', '--comment', shlex.quote('This is a test comment'), '--repo', 'repo'),
  421. )
  422. module.recreate_archive(
  423. repository='repo',
  424. archive='archive',
  425. config={},
  426. local_borg_version='1.2.3',
  427. recreate_arguments=flexmock(
  428. list=None,
  429. target=None,
  430. comment='This is a test comment',
  431. timestamp=None,
  432. match_archives=None,
  433. ),
  434. global_arguments=flexmock(dry_run=False),
  435. local_path='borg',
  436. patterns=None,
  437. )
  438. def test_recreate_with_timestamp_flag():
  439. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  440. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  441. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  442. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  443. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  444. flexmock(module.borgmatic.borg.flags).should_receive(
  445. 'make_repository_archive_flags',
  446. ).and_return(
  447. (
  448. '--repo',
  449. 'repo',
  450. ),
  451. )
  452. insert_execute_command_mock(
  453. ('borg', 'recreate', '--timestamp', '2023-10-01T12:00:00', '--repo', 'repo'),
  454. )
  455. module.recreate_archive(
  456. repository='repo',
  457. archive='archive',
  458. config={},
  459. local_borg_version='1.2.3',
  460. recreate_arguments=flexmock(
  461. list=None,
  462. target=None,
  463. comment=None,
  464. timestamp='2023-10-01T12:00:00',
  465. match_archives=None,
  466. ),
  467. global_arguments=flexmock(dry_run=False),
  468. local_path='borg',
  469. patterns=None,
  470. )
  471. def test_recreate_with_compression_flag():
  472. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  473. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  474. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  475. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  476. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  477. flexmock(module.borgmatic.borg.flags).should_receive(
  478. 'make_repository_archive_flags',
  479. ).and_return(
  480. (
  481. '--repo',
  482. 'repo',
  483. ),
  484. )
  485. insert_execute_command_mock(('borg', 'recreate', '--compression', 'lz4', '--repo', 'repo'))
  486. module.recreate_archive(
  487. repository='repo',
  488. archive='archive',
  489. config={'compression': 'lz4'},
  490. local_borg_version='1.2.3',
  491. recreate_arguments=flexmock(
  492. list=None,
  493. target=None,
  494. comment=None,
  495. timestamp=None,
  496. match_archives=None,
  497. ),
  498. global_arguments=flexmock(dry_run=False),
  499. local_path='borg',
  500. patterns=None,
  501. )
  502. def test_recreate_with_chunker_params_flag():
  503. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  504. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  505. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  506. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  507. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  508. flexmock(module.borgmatic.borg.flags).should_receive(
  509. 'make_repository_archive_flags',
  510. ).and_return(
  511. (
  512. '--repo',
  513. 'repo',
  514. ),
  515. )
  516. insert_execute_command_mock(
  517. ('borg', 'recreate', '--chunker-params', '19,23,21,4095', '--repo', 'repo'),
  518. )
  519. module.recreate_archive(
  520. repository='repo',
  521. archive='archive',
  522. config={'chunker_params': '19,23,21,4095'},
  523. local_borg_version='1.2.3',
  524. recreate_arguments=flexmock(
  525. list=None,
  526. target=None,
  527. comment=None,
  528. timestamp=None,
  529. match_archives=None,
  530. ),
  531. global_arguments=flexmock(dry_run=False),
  532. local_path='borg',
  533. patterns=None,
  534. )
  535. def test_recreate_with_recompress_flag():
  536. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  537. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  538. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  539. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  540. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  541. flexmock(module.borgmatic.borg.flags).should_receive(
  542. 'make_repository_archive_flags',
  543. ).and_return(
  544. (
  545. '--repo',
  546. 'repo',
  547. ),
  548. )
  549. insert_execute_command_mock(('borg', 'recreate', '--recompress', 'always', '--repo', 'repo'))
  550. module.recreate_archive(
  551. repository='repo',
  552. archive='archive',
  553. config={'recompress': 'always'},
  554. local_borg_version='1.2.3',
  555. recreate_arguments=flexmock(
  556. list=None,
  557. target=None,
  558. comment=None,
  559. timestamp=None,
  560. match_archives=None,
  561. ),
  562. global_arguments=flexmock(dry_run=False),
  563. local_path='borg',
  564. patterns=None,
  565. )
  566. def test_recreate_with_match_archives_star():
  567. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  568. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  569. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  570. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  571. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  572. flexmock(module.borgmatic.borg.flags).should_receive(
  573. 'make_repository_archive_flags',
  574. ).and_return(
  575. (
  576. '--repo',
  577. 'repo',
  578. ),
  579. )
  580. insert_execute_command_mock(('borg', 'recreate', '--repo', 'repo'))
  581. module.recreate_archive(
  582. repository='repo',
  583. archive='archive',
  584. config={},
  585. local_borg_version='1.2.3',
  586. recreate_arguments=flexmock(
  587. list=None,
  588. target=None,
  589. comment=None,
  590. timestamp=None,
  591. match_archives='*',
  592. ),
  593. global_arguments=flexmock(dry_run=False),
  594. local_path='borg',
  595. patterns=None,
  596. )
  597. def test_recreate_with_match_archives_regex():
  598. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  599. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  600. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  601. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  602. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  603. flexmock(module.borgmatic.borg.flags).should_receive(
  604. 'make_repository_archive_flags',
  605. ).and_return(
  606. (
  607. '--repo',
  608. 'repo',
  609. ),
  610. )
  611. insert_execute_command_mock(('borg', 'recreate', '--repo', 'repo'))
  612. module.recreate_archive(
  613. repository='repo',
  614. archive='archive',
  615. config={},
  616. local_borg_version='1.2.3',
  617. recreate_arguments=flexmock(
  618. list=None,
  619. target=None,
  620. comment=None,
  621. timestamp=None,
  622. match_archives='re:.*',
  623. ),
  624. global_arguments=flexmock(dry_run=False),
  625. local_path='borg',
  626. patterns=None,
  627. )
  628. def test_recreate_with_match_archives_shell():
  629. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  630. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  631. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  632. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  633. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  634. flexmock(module.borgmatic.borg.flags).should_receive(
  635. 'make_repository_archive_flags',
  636. ).and_return(
  637. (
  638. '--repo',
  639. 'repo',
  640. ),
  641. )
  642. insert_execute_command_mock(('borg', 'recreate', '--repo', 'repo'))
  643. module.recreate_archive(
  644. repository='repo',
  645. archive='archive',
  646. config={},
  647. local_borg_version='1.2.3',
  648. recreate_arguments=flexmock(
  649. list=None,
  650. target=None,
  651. comment=None,
  652. timestamp=None,
  653. match_archives='sh:*',
  654. ),
  655. global_arguments=flexmock(dry_run=False),
  656. local_path='borg',
  657. patterns=None,
  658. )
  659. def test_recreate_with_match_archives_and_feature_available_calls_borg_with_match_archives():
  660. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  661. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  662. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  663. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').with_args(
  664. 'foo-*',
  665. None,
  666. '1.2.3',
  667. ).and_return(('--match-archives', 'foo-*'))
  668. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  669. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  670. ('--repo', 'repo'),
  671. )
  672. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_archive_flags').never()
  673. insert_execute_command_mock(('borg', 'recreate', '--repo', 'repo', '--match-archives', 'foo-*'))
  674. module.recreate_archive(
  675. repository='repo',
  676. archive=None,
  677. config={'match_archives': 'foo-*'},
  678. local_borg_version='1.2.3',
  679. recreate_arguments=flexmock(
  680. list=None,
  681. target=None,
  682. comment=None,
  683. timestamp=None,
  684. match_archives='foo-*',
  685. ),
  686. global_arguments=flexmock(dry_run=False),
  687. local_path='borg',
  688. patterns=None,
  689. )
  690. def test_recreate_with_archives_flag_and_feature_available_calls_borg_with_match_archives():
  691. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  692. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  693. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  694. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').with_args(
  695. 'archive',
  696. None,
  697. '1.2.3',
  698. ).and_return(('--match-archives', 'archive'))
  699. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  700. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  701. ('--repo', 'repo'),
  702. )
  703. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_archive_flags').never()
  704. insert_execute_command_mock(
  705. ('borg', 'recreate', '--repo', 'repo', '--match-archives', 'archive'),
  706. )
  707. module.recreate_archive(
  708. repository='repo',
  709. archive='archive',
  710. config={'match_archives': 'foo-*'},
  711. local_borg_version='1.2.3',
  712. recreate_arguments=flexmock(
  713. list=None,
  714. target=None,
  715. comment=None,
  716. timestamp=None,
  717. match_archives='foo-*',
  718. ),
  719. global_arguments=flexmock(dry_run=False),
  720. local_path='borg',
  721. patterns=None,
  722. )
  723. def test_recreate_with_match_archives_and_feature_not_available_calls_borg_without_match_archives():
  724. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  725. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  726. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  727. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').never()
  728. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(False)
  729. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  730. ('repo',),
  731. )
  732. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_archive_flags').never()
  733. insert_execute_command_mock(('borg', 'recreate', 'repo'))
  734. module.recreate_archive(
  735. repository='repo',
  736. archive=None,
  737. config={'match_archives': 'foo-*'},
  738. local_borg_version='1.2.3',
  739. recreate_arguments=flexmock(
  740. list=None,
  741. target=None,
  742. comment=None,
  743. timestamp=None,
  744. match_archives='foo-*',
  745. ),
  746. global_arguments=flexmock(dry_run=False),
  747. local_path='borg',
  748. patterns=None,
  749. )
  750. def test_recreate_with_archives_flags_and_feature_not_available_calls_borg_with_combined_repo_and_archive():
  751. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  752. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  753. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  754. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').never()
  755. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(False)
  756. flexmock(module.borgmatic.borg.flags).should_receive(
  757. 'make_repository_archive_flags',
  758. ).and_return(('repo::archive',))
  759. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').never()
  760. insert_execute_command_mock(('borg', 'recreate', 'repo::archive'))
  761. module.recreate_archive(
  762. repository='repo',
  763. archive='archive',
  764. config={'match_archives': 'foo-*'},
  765. local_borg_version='1.2.3',
  766. recreate_arguments=flexmock(
  767. list=None,
  768. target=None,
  769. comment=None,
  770. timestamp=None,
  771. match_archives='foo-*',
  772. ),
  773. global_arguments=flexmock(dry_run=False),
  774. local_path='borg',
  775. patterns=None,
  776. )