test_recreate.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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_log_info():
  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(('borg', 'recreate', '--info', '--repo', 'repo'))
  159. insert_logging_mock(logging.INFO)
  160. module.recreate_archive(
  161. repository='repo',
  162. archive='archive',
  163. config={},
  164. local_borg_version='1.2.3',
  165. recreate_arguments=flexmock(
  166. list=None,
  167. target=None,
  168. comment=None,
  169. timestamp=None,
  170. match_archives=None,
  171. ),
  172. global_arguments=flexmock(dry_run=False),
  173. local_path='borg',
  174. patterns=None,
  175. )
  176. def test_recreate_with_log_debug():
  177. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  178. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  179. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  180. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  181. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  182. flexmock(module.borgmatic.borg.flags).should_receive(
  183. 'make_repository_archive_flags'
  184. ).and_return(
  185. (
  186. '--repo',
  187. 'repo',
  188. )
  189. )
  190. insert_execute_command_mock(('borg', 'recreate', '--debug', '--show-rc', '--repo', 'repo'))
  191. insert_logging_mock(logging.DEBUG)
  192. module.recreate_archive(
  193. repository='repo',
  194. archive='archive',
  195. config={},
  196. local_borg_version='1.2.3',
  197. recreate_arguments=flexmock(
  198. list=None,
  199. target=None,
  200. comment=None,
  201. timestamp=None,
  202. match_archives=None,
  203. ),
  204. global_arguments=flexmock(dry_run=False),
  205. local_path='borg',
  206. patterns=None,
  207. )
  208. def test_recreate_with_log_json():
  209. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  210. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  211. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  212. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  213. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  214. flexmock(module.borgmatic.borg.flags).should_receive(
  215. 'make_repository_archive_flags'
  216. ).and_return(
  217. (
  218. '--repo',
  219. 'repo',
  220. )
  221. )
  222. insert_execute_command_mock(('borg', 'recreate', '--log-json', '--repo', 'repo'))
  223. module.recreate_archive(
  224. repository='repo',
  225. archive='archive',
  226. config={'log_json': True},
  227. local_borg_version='1.2.3',
  228. recreate_arguments=flexmock(
  229. list=None,
  230. target=None,
  231. comment=None,
  232. timestamp=None,
  233. match_archives=None,
  234. ),
  235. global_arguments=flexmock(dry_run=False),
  236. local_path='borg',
  237. patterns=None,
  238. )
  239. def test_recreate_with_list_config_calls_borg_with_list_flag():
  240. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  241. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  242. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  243. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  244. flexmock(module.borgmatic.borg.flags).should_receive(
  245. 'make_repository_archive_flags'
  246. ).and_return(
  247. (
  248. '--repo',
  249. 'repo',
  250. )
  251. )
  252. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return(
  253. 'AME+-'
  254. )
  255. insert_execute_command_mock(
  256. ('borg', 'recreate', '--list', '--filter', 'AME+-', '--repo', 'repo')
  257. )
  258. module.recreate_archive(
  259. repository='repo',
  260. archive='archive',
  261. config={'list_details': True},
  262. local_borg_version='1.2.3',
  263. recreate_arguments=flexmock(
  264. list=None,
  265. target=None,
  266. comment=None,
  267. timestamp=None,
  268. match_archives=None,
  269. ),
  270. global_arguments=flexmock(dry_run=False),
  271. local_path='borg',
  272. patterns=None,
  273. )
  274. def test_recreate_with_patterns_from_flag():
  275. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  276. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  277. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  278. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  279. flexmock(module.borgmatic.borg.flags).should_receive(
  280. 'make_repository_archive_flags'
  281. ).and_return(
  282. (
  283. '--repo',
  284. 'repo',
  285. )
  286. )
  287. mock_patterns_file = flexmock(name='patterns_file')
  288. flexmock(module).should_receive('write_patterns_file').and_return(mock_patterns_file)
  289. insert_execute_command_mock(
  290. ('borg', 'recreate', '--patterns-from', 'patterns_file', '--repo', 'repo')
  291. )
  292. module.recreate_archive(
  293. repository='repo',
  294. archive='archive',
  295. config={},
  296. local_borg_version='1.2.3',
  297. recreate_arguments=flexmock(
  298. list=None,
  299. target=None,
  300. comment=None,
  301. timestamp=None,
  302. match_archives=None,
  303. ),
  304. global_arguments=flexmock(dry_run=False),
  305. local_path='borg',
  306. patterns=['pattern1', 'pattern2'],
  307. )
  308. def test_recreate_with_exclude_flags():
  309. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  310. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  311. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  312. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  313. flexmock(module.borgmatic.borg.flags).should_receive(
  314. 'make_repository_archive_flags'
  315. ).and_return(
  316. (
  317. '--repo',
  318. 'repo',
  319. )
  320. )
  321. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(
  322. ('--exclude', 'pattern')
  323. )
  324. insert_execute_command_mock(('borg', 'recreate', '--exclude', 'pattern', '--repo', 'repo'))
  325. module.recreate_archive(
  326. repository='repo',
  327. archive='archive',
  328. config={'exclude_patterns': ['pattern']},
  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=None,
  340. )
  341. def test_recreate_with_target_flag():
  342. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  343. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  344. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  345. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  346. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  347. flexmock(module.borgmatic.borg.flags).should_receive(
  348. 'make_repository_archive_flags'
  349. ).and_return(
  350. (
  351. '--repo',
  352. 'repo',
  353. )
  354. )
  355. insert_execute_command_mock(('borg', 'recreate', '--target', 'new-archive', '--repo', 'repo'))
  356. module.recreate_archive(
  357. repository='repo',
  358. archive='archive',
  359. config={},
  360. local_borg_version='1.2.3',
  361. recreate_arguments=flexmock(
  362. list=None,
  363. target='new-archive',
  364. comment=None,
  365. timestamp=None,
  366. match_archives=None,
  367. ),
  368. global_arguments=flexmock(dry_run=False),
  369. local_path='borg',
  370. patterns=None,
  371. )
  372. def test_recreate_with_comment_flag():
  373. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  374. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  375. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  376. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  377. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  378. flexmock(module.borgmatic.borg.flags).should_receive(
  379. 'make_repository_archive_flags'
  380. ).and_return(
  381. (
  382. '--repo',
  383. 'repo',
  384. )
  385. )
  386. insert_execute_command_mock(
  387. ('borg', 'recreate', '--comment', shlex.quote('This is a test comment'), '--repo', 'repo')
  388. )
  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=None,
  397. comment='This is a test comment',
  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_timestamp_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', '--timestamp', '2023-10-01T12:00:00', '--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=None,
  431. timestamp='2023-10-01T12:00:00',
  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_compression_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(('borg', 'recreate', '--compression', 'lz4', '--repo', 'repo'))
  453. module.recreate_archive(
  454. repository='repo',
  455. archive='archive',
  456. config={'compression': 'lz4'},
  457. local_borg_version='1.2.3',
  458. recreate_arguments=flexmock(
  459. list=None,
  460. target=None,
  461. comment=None,
  462. timestamp=None,
  463. match_archives=None,
  464. ),
  465. global_arguments=flexmock(dry_run=False),
  466. local_path='borg',
  467. patterns=None,
  468. )
  469. def test_recreate_with_chunker_params_flag():
  470. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  471. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  472. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  473. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  474. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  475. flexmock(module.borgmatic.borg.flags).should_receive(
  476. 'make_repository_archive_flags'
  477. ).and_return(
  478. (
  479. '--repo',
  480. 'repo',
  481. )
  482. )
  483. insert_execute_command_mock(
  484. ('borg', 'recreate', '--chunker-params', '19,23,21,4095', '--repo', 'repo')
  485. )
  486. module.recreate_archive(
  487. repository='repo',
  488. archive='archive',
  489. config={'chunker_params': '19,23,21,4095'},
  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_recompress_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(('borg', 'recreate', '--recompress', 'always', '--repo', 'repo'))
  517. module.recreate_archive(
  518. repository='repo',
  519. archive='archive',
  520. config={'recompress': 'always'},
  521. local_borg_version='1.2.3',
  522. recreate_arguments=flexmock(
  523. list=None,
  524. target=None,
  525. comment=None,
  526. timestamp=None,
  527. match_archives=None,
  528. ),
  529. global_arguments=flexmock(dry_run=False),
  530. local_path='borg',
  531. patterns=None,
  532. )
  533. def test_recreate_with_match_archives_star():
  534. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  535. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  536. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  537. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  538. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  539. flexmock(module.borgmatic.borg.flags).should_receive(
  540. 'make_repository_archive_flags'
  541. ).and_return(
  542. (
  543. '--repo',
  544. 'repo',
  545. )
  546. )
  547. insert_execute_command_mock(('borg', 'recreate', '--repo', 'repo'))
  548. module.recreate_archive(
  549. repository='repo',
  550. archive='archive',
  551. config={},
  552. local_borg_version='1.2.3',
  553. recreate_arguments=flexmock(
  554. list=None,
  555. target=None,
  556. comment=None,
  557. timestamp=None,
  558. match_archives='*',
  559. ),
  560. global_arguments=flexmock(dry_run=False),
  561. local_path='borg',
  562. patterns=None,
  563. )
  564. def test_recreate_with_match_archives_regex():
  565. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  566. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  567. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  568. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  569. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  570. flexmock(module.borgmatic.borg.flags).should_receive(
  571. 'make_repository_archive_flags'
  572. ).and_return(
  573. (
  574. '--repo',
  575. 'repo',
  576. )
  577. )
  578. insert_execute_command_mock(('borg', 'recreate', '--repo', 'repo'))
  579. module.recreate_archive(
  580. repository='repo',
  581. archive='archive',
  582. config={},
  583. local_borg_version='1.2.3',
  584. recreate_arguments=flexmock(
  585. list=None,
  586. target=None,
  587. comment=None,
  588. timestamp=None,
  589. match_archives='re:.*',
  590. ),
  591. global_arguments=flexmock(dry_run=False),
  592. local_path='borg',
  593. patterns=None,
  594. )
  595. def test_recreate_with_match_archives_shell():
  596. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  597. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  598. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  599. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  600. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  601. flexmock(module.borgmatic.borg.flags).should_receive(
  602. 'make_repository_archive_flags'
  603. ).and_return(
  604. (
  605. '--repo',
  606. 'repo',
  607. )
  608. )
  609. insert_execute_command_mock(('borg', 'recreate', '--repo', 'repo'))
  610. module.recreate_archive(
  611. repository='repo',
  612. archive='archive',
  613. config={},
  614. local_borg_version='1.2.3',
  615. recreate_arguments=flexmock(
  616. list=None,
  617. target=None,
  618. comment=None,
  619. timestamp=None,
  620. match_archives='sh:*',
  621. ),
  622. global_arguments=flexmock(dry_run=False),
  623. local_path='borg',
  624. patterns=None,
  625. )
  626. def test_recreate_with_match_archives_and_feature_available_calls_borg_with_match_archives():
  627. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  628. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  629. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  630. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').with_args(
  631. 'foo-*', None, '1.2.3'
  632. ).and_return(('--match-archives', 'foo-*'))
  633. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  634. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  635. ('--repo', 'repo')
  636. )
  637. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_archive_flags').never()
  638. insert_execute_command_mock(('borg', 'recreate', '--repo', 'repo', '--match-archives', 'foo-*'))
  639. module.recreate_archive(
  640. repository='repo',
  641. archive=None,
  642. config={'match_archives': 'foo-*'},
  643. local_borg_version='1.2.3',
  644. recreate_arguments=flexmock(
  645. list=None,
  646. target=None,
  647. comment=None,
  648. timestamp=None,
  649. match_archives='foo-*',
  650. ),
  651. global_arguments=flexmock(dry_run=False),
  652. local_path='borg',
  653. patterns=None,
  654. )
  655. def test_recreate_with_archives_flag_and_feature_available_calls_borg_with_match_archives():
  656. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  657. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  658. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  659. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').with_args(
  660. 'archive', None, '1.2.3'
  661. ).and_return(('--match-archives', 'archive'))
  662. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  663. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  664. ('--repo', 'repo')
  665. )
  666. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_archive_flags').never()
  667. insert_execute_command_mock(
  668. ('borg', 'recreate', '--repo', 'repo', '--match-archives', 'archive')
  669. )
  670. module.recreate_archive(
  671. repository='repo',
  672. archive='archive',
  673. config={'match_archives': 'foo-*'},
  674. local_borg_version='1.2.3',
  675. recreate_arguments=flexmock(
  676. list=None,
  677. target=None,
  678. comment=None,
  679. timestamp=None,
  680. match_archives='foo-*',
  681. ),
  682. global_arguments=flexmock(dry_run=False),
  683. local_path='borg',
  684. patterns=None,
  685. )
  686. def test_recreate_with_match_archives_and_feature_not_available_calls_borg_without_match_archives():
  687. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  688. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  689. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  690. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').never()
  691. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(False)
  692. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  693. ('repo',)
  694. )
  695. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_archive_flags').never()
  696. insert_execute_command_mock(('borg', 'recreate', 'repo'))
  697. module.recreate_archive(
  698. repository='repo',
  699. archive=None,
  700. config={'match_archives': 'foo-*'},
  701. local_borg_version='1.2.3',
  702. recreate_arguments=flexmock(
  703. list=None,
  704. target=None,
  705. comment=None,
  706. timestamp=None,
  707. match_archives='foo-*',
  708. ),
  709. global_arguments=flexmock(dry_run=False),
  710. local_path='borg',
  711. patterns=None,
  712. )
  713. def test_recreate_with_archives_flags_and_feature_not_available_calls_borg_with_combined_repo_and_archive():
  714. flexmock(module.borgmatic.borg.flags).should_receive('make_exclude_flags').and_return(())
  715. flexmock(module.borgmatic.borg.pattern).should_receive('write_patterns_file').and_return(None)
  716. flexmock(module.borgmatic.borg.flags).should_receive('make_list_filter_flags').and_return('')
  717. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').never()
  718. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(False)
  719. flexmock(module.borgmatic.borg.flags).should_receive(
  720. 'make_repository_archive_flags'
  721. ).and_return(('repo::archive',))
  722. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').never()
  723. insert_execute_command_mock(('borg', 'recreate', 'repo::archive'))
  724. module.recreate_archive(
  725. repository='repo',
  726. archive='archive',
  727. config={'match_archives': 'foo-*'},
  728. local_borg_version='1.2.3',
  729. recreate_arguments=flexmock(
  730. list=None,
  731. target=None,
  732. comment=None,
  733. timestamp=None,
  734. match_archives='foo-*',
  735. ),
  736. global_arguments=flexmock(dry_run=False),
  737. local_path='borg',
  738. patterns=None,
  739. )