2
0

test_recreate.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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.create).should_receive('make_exclude_flags').and_return(())
  18. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  19. flexmock(module.borgmatic.borg.create).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.create).should_receive('make_exclude_flags').and_return(())
  51. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  52. flexmock(module.borgmatic.borg.create).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.create).should_receive('make_exclude_flags').and_return(())
  83. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  84. flexmock(module.borgmatic.borg.create).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.create).should_receive('make_exclude_flags').and_return(())
  115. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  116. flexmock(module.borgmatic.borg.create).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.create).should_receive('make_exclude_flags').and_return(())
  146. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  147. flexmock(module.borgmatic.borg.create).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.create).should_receive('make_exclude_flags').and_return(())
  178. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  179. flexmock(module.borgmatic.borg.create).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.create).should_receive('make_exclude_flags').and_return(())
  210. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  211. flexmock(module.borgmatic.borg.create).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.create).should_receive('make_exclude_flags').and_return(())
  241. flexmock(module.borgmatic.borg.create).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).should_receive('make_list_filter_flags').and_return('AME+-')
  253. insert_execute_command_mock(
  254. ('borg', 'recreate', '--list', '--filter', 'AME+-', '--repo', 'repo')
  255. )
  256. module.recreate_archive(
  257. repository='repo',
  258. archive='archive',
  259. config={'list_details': 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_patterns_from_flag():
  273. flexmock(module.borgmatic.borg.create).should_receive('make_exclude_flags').and_return(())
  274. flexmock(module.borgmatic.borg.create).should_receive('make_list_filter_flags').and_return('')
  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. mock_patterns_file = flexmock(name='patterns_file')
  286. flexmock(module).should_receive('write_patterns_file').and_return(mock_patterns_file)
  287. insert_execute_command_mock(
  288. ('borg', 'recreate', '--patterns-from', 'patterns_file', '--repo', 'repo')
  289. )
  290. module.recreate_archive(
  291. repository='repo',
  292. archive='archive',
  293. config={},
  294. local_borg_version='1.2.3',
  295. recreate_arguments=flexmock(
  296. list=None,
  297. target=None,
  298. comment=None,
  299. timestamp=None,
  300. match_archives=None,
  301. ),
  302. global_arguments=flexmock(dry_run=False),
  303. local_path='borg',
  304. patterns=['pattern1', 'pattern2'],
  305. )
  306. def test_recreate_with_exclude_flags():
  307. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  308. flexmock(module.borgmatic.borg.create).should_receive('make_list_filter_flags').and_return('')
  309. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  310. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  311. flexmock(module.borgmatic.borg.flags).should_receive(
  312. 'make_repository_archive_flags'
  313. ).and_return(
  314. (
  315. '--repo',
  316. 'repo',
  317. )
  318. )
  319. flexmock(module).should_receive('make_exclude_flags').and_return(('--exclude', 'pattern'))
  320. insert_execute_command_mock(('borg', 'recreate', '--exclude', 'pattern', '--repo', 'repo'))
  321. module.recreate_archive(
  322. repository='repo',
  323. archive='archive',
  324. config={'exclude_patterns': ['pattern']},
  325. local_borg_version='1.2.3',
  326. recreate_arguments=flexmock(
  327. list=None,
  328. target=None,
  329. comment=None,
  330. timestamp=None,
  331. match_archives=None,
  332. ),
  333. global_arguments=flexmock(dry_run=False),
  334. local_path='borg',
  335. patterns=None,
  336. )
  337. def test_recreate_with_target_flag():
  338. flexmock(module.borgmatic.borg.create).should_receive('make_exclude_flags').and_return(())
  339. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  340. flexmock(module.borgmatic.borg.create).should_receive('make_list_filter_flags').and_return('')
  341. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  342. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  343. flexmock(module.borgmatic.borg.flags).should_receive(
  344. 'make_repository_archive_flags'
  345. ).and_return(
  346. (
  347. '--repo',
  348. 'repo',
  349. )
  350. )
  351. insert_execute_command_mock(('borg', 'recreate', '--target', 'new-archive', '--repo', 'repo'))
  352. module.recreate_archive(
  353. repository='repo',
  354. archive='archive',
  355. config={},
  356. local_borg_version='1.2.3',
  357. recreate_arguments=flexmock(
  358. list=None,
  359. target='new-archive',
  360. comment=None,
  361. timestamp=None,
  362. match_archives=None,
  363. ),
  364. global_arguments=flexmock(dry_run=False),
  365. local_path='borg',
  366. patterns=None,
  367. )
  368. def test_recreate_with_comment_flag():
  369. flexmock(module.borgmatic.borg.create).should_receive('make_exclude_flags').and_return(())
  370. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  371. flexmock(module.borgmatic.borg.create).should_receive('make_list_filter_flags').and_return('')
  372. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  373. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  374. flexmock(module.borgmatic.borg.flags).should_receive(
  375. 'make_repository_archive_flags'
  376. ).and_return(
  377. (
  378. '--repo',
  379. 'repo',
  380. )
  381. )
  382. insert_execute_command_mock(
  383. ('borg', 'recreate', '--comment', shlex.quote('This is a test comment'), '--repo', 'repo')
  384. )
  385. module.recreate_archive(
  386. repository='repo',
  387. archive='archive',
  388. config={},
  389. local_borg_version='1.2.3',
  390. recreate_arguments=flexmock(
  391. list=None,
  392. target=None,
  393. comment='This is a test comment',
  394. timestamp=None,
  395. match_archives=None,
  396. ),
  397. global_arguments=flexmock(dry_run=False),
  398. local_path='borg',
  399. patterns=None,
  400. )
  401. def test_recreate_with_timestamp_flag():
  402. flexmock(module.borgmatic.borg.create).should_receive('make_exclude_flags').and_return(())
  403. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  404. flexmock(module.borgmatic.borg.create).should_receive('make_list_filter_flags').and_return('')
  405. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  406. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  407. flexmock(module.borgmatic.borg.flags).should_receive(
  408. 'make_repository_archive_flags'
  409. ).and_return(
  410. (
  411. '--repo',
  412. 'repo',
  413. )
  414. )
  415. insert_execute_command_mock(
  416. ('borg', 'recreate', '--timestamp', '2023-10-01T12:00:00', '--repo', 'repo')
  417. )
  418. module.recreate_archive(
  419. repository='repo',
  420. archive='archive',
  421. config={},
  422. local_borg_version='1.2.3',
  423. recreate_arguments=flexmock(
  424. list=None,
  425. target=None,
  426. comment=None,
  427. timestamp='2023-10-01T12:00:00',
  428. match_archives=None,
  429. ),
  430. global_arguments=flexmock(dry_run=False),
  431. local_path='borg',
  432. patterns=None,
  433. )
  434. def test_recreate_with_compression_flag():
  435. flexmock(module.borgmatic.borg.create).should_receive('make_exclude_flags').and_return(())
  436. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  437. flexmock(module.borgmatic.borg.create).should_receive('make_list_filter_flags').and_return('')
  438. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  439. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  440. flexmock(module.borgmatic.borg.flags).should_receive(
  441. 'make_repository_archive_flags'
  442. ).and_return(
  443. (
  444. '--repo',
  445. 'repo',
  446. )
  447. )
  448. insert_execute_command_mock(('borg', 'recreate', '--compression', 'lz4', '--repo', 'repo'))
  449. module.recreate_archive(
  450. repository='repo',
  451. archive='archive',
  452. config={'compression': 'lz4'},
  453. local_borg_version='1.2.3',
  454. recreate_arguments=flexmock(
  455. list=None,
  456. target=None,
  457. comment=None,
  458. timestamp=None,
  459. match_archives=None,
  460. ),
  461. global_arguments=flexmock(dry_run=False),
  462. local_path='borg',
  463. patterns=None,
  464. )
  465. def test_recreate_with_chunker_params_flag():
  466. flexmock(module.borgmatic.borg.create).should_receive('make_exclude_flags').and_return(())
  467. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  468. flexmock(module.borgmatic.borg.create).should_receive('make_list_filter_flags').and_return('')
  469. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  470. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  471. flexmock(module.borgmatic.borg.flags).should_receive(
  472. 'make_repository_archive_flags'
  473. ).and_return(
  474. (
  475. '--repo',
  476. 'repo',
  477. )
  478. )
  479. insert_execute_command_mock(
  480. ('borg', 'recreate', '--chunker-params', '19,23,21,4095', '--repo', 'repo')
  481. )
  482. module.recreate_archive(
  483. repository='repo',
  484. archive='archive',
  485. config={'chunker_params': '19,23,21,4095'},
  486. local_borg_version='1.2.3',
  487. recreate_arguments=flexmock(
  488. list=None,
  489. target=None,
  490. comment=None,
  491. timestamp=None,
  492. match_archives=None,
  493. ),
  494. global_arguments=flexmock(dry_run=False),
  495. local_path='borg',
  496. patterns=None,
  497. )
  498. def test_recreate_with_recompress_flag():
  499. flexmock(module.borgmatic.borg.create).should_receive('make_exclude_flags').and_return(())
  500. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  501. flexmock(module.borgmatic.borg.create).should_receive('make_list_filter_flags').and_return('')
  502. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  503. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  504. flexmock(module.borgmatic.borg.flags).should_receive(
  505. 'make_repository_archive_flags'
  506. ).and_return(
  507. (
  508. '--repo',
  509. 'repo',
  510. )
  511. )
  512. insert_execute_command_mock(('borg', 'recreate', '--recompress', 'always', '--repo', 'repo'))
  513. module.recreate_archive(
  514. repository='repo',
  515. archive='archive',
  516. config={'recompress': 'always'},
  517. local_borg_version='1.2.3',
  518. recreate_arguments=flexmock(
  519. list=None,
  520. target=None,
  521. comment=None,
  522. timestamp=None,
  523. match_archives=None,
  524. ),
  525. global_arguments=flexmock(dry_run=False),
  526. local_path='borg',
  527. patterns=None,
  528. )
  529. def test_recreate_with_match_archives_star():
  530. flexmock(module.borgmatic.borg.create).should_receive('make_exclude_flags').and_return(())
  531. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  532. flexmock(module.borgmatic.borg.create).should_receive('make_list_filter_flags').and_return('')
  533. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  534. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  535. flexmock(module.borgmatic.borg.flags).should_receive(
  536. 'make_repository_archive_flags'
  537. ).and_return(
  538. (
  539. '--repo',
  540. 'repo',
  541. )
  542. )
  543. insert_execute_command_mock(('borg', 'recreate', '--repo', 'repo'))
  544. module.recreate_archive(
  545. repository='repo',
  546. archive='archive',
  547. config={},
  548. local_borg_version='1.2.3',
  549. recreate_arguments=flexmock(
  550. list=None,
  551. target=None,
  552. comment=None,
  553. timestamp=None,
  554. match_archives='*',
  555. ),
  556. global_arguments=flexmock(dry_run=False),
  557. local_path='borg',
  558. patterns=None,
  559. )
  560. def test_recreate_with_match_archives_regex():
  561. flexmock(module.borgmatic.borg.create).should_receive('make_exclude_flags').and_return(())
  562. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  563. flexmock(module.borgmatic.borg.create).should_receive('make_list_filter_flags').and_return('')
  564. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  565. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  566. flexmock(module.borgmatic.borg.flags).should_receive(
  567. 'make_repository_archive_flags'
  568. ).and_return(
  569. (
  570. '--repo',
  571. 'repo',
  572. )
  573. )
  574. insert_execute_command_mock(('borg', 'recreate', '--repo', 'repo'))
  575. module.recreate_archive(
  576. repository='repo',
  577. archive='archive',
  578. config={},
  579. local_borg_version='1.2.3',
  580. recreate_arguments=flexmock(
  581. list=None,
  582. target=None,
  583. comment=None,
  584. timestamp=None,
  585. match_archives='re:.*',
  586. ),
  587. global_arguments=flexmock(dry_run=False),
  588. local_path='borg',
  589. patterns=None,
  590. )
  591. def test_recreate_with_match_archives_shell():
  592. flexmock(module.borgmatic.borg.create).should_receive('make_exclude_flags').and_return(())
  593. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  594. flexmock(module.borgmatic.borg.create).should_receive('make_list_filter_flags').and_return('')
  595. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  596. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  597. flexmock(module.borgmatic.borg.flags).should_receive(
  598. 'make_repository_archive_flags'
  599. ).and_return(
  600. (
  601. '--repo',
  602. 'repo',
  603. )
  604. )
  605. insert_execute_command_mock(('borg', 'recreate', '--repo', 'repo'))
  606. module.recreate_archive(
  607. repository='repo',
  608. archive='archive',
  609. config={},
  610. local_borg_version='1.2.3',
  611. recreate_arguments=flexmock(
  612. list=None,
  613. target=None,
  614. comment=None,
  615. timestamp=None,
  616. match_archives='sh:*',
  617. ),
  618. global_arguments=flexmock(dry_run=False),
  619. local_path='borg',
  620. patterns=None,
  621. )
  622. def test_recreate_with_match_archives_and_feature_available_calls_borg_with_match_archives():
  623. flexmock(module.borgmatic.borg.create).should_receive('make_exclude_flags').and_return(())
  624. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  625. flexmock(module.borgmatic.borg.create).should_receive('make_list_filter_flags').and_return('')
  626. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').with_args(
  627. 'foo-*', None, '1.2.3'
  628. ).and_return(('--match-archives', 'foo-*'))
  629. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  630. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  631. ('--repo', 'repo')
  632. )
  633. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_archive_flags').never()
  634. insert_execute_command_mock(('borg', 'recreate', '--repo', 'repo', '--match-archives', 'foo-*'))
  635. module.recreate_archive(
  636. repository='repo',
  637. archive=None,
  638. config={'match_archives': 'foo-*'},
  639. local_borg_version='1.2.3',
  640. recreate_arguments=flexmock(
  641. list=None,
  642. target=None,
  643. comment=None,
  644. timestamp=None,
  645. match_archives='foo-*',
  646. ),
  647. global_arguments=flexmock(dry_run=False),
  648. local_path='borg',
  649. patterns=None,
  650. )
  651. def test_recreate_with_archives_flag_and_feature_available_calls_borg_with_match_archives():
  652. flexmock(module.borgmatic.borg.create).should_receive('make_exclude_flags').and_return(())
  653. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  654. flexmock(module.borgmatic.borg.create).should_receive('make_list_filter_flags').and_return('')
  655. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').with_args(
  656. 'archive', None, '1.2.3'
  657. ).and_return(('--match-archives', 'archive'))
  658. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  659. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  660. ('--repo', 'repo')
  661. )
  662. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_archive_flags').never()
  663. insert_execute_command_mock(
  664. ('borg', 'recreate', '--repo', 'repo', '--match-archives', 'archive')
  665. )
  666. module.recreate_archive(
  667. repository='repo',
  668. archive='archive',
  669. config={'match_archives': 'foo-*'},
  670. local_borg_version='1.2.3',
  671. recreate_arguments=flexmock(
  672. list=None,
  673. target=None,
  674. comment=None,
  675. timestamp=None,
  676. match_archives='foo-*',
  677. ),
  678. global_arguments=flexmock(dry_run=False),
  679. local_path='borg',
  680. patterns=None,
  681. )
  682. def test_recreate_with_match_archives_and_feature_not_available_calls_borg_without_match_archives():
  683. flexmock(module.borgmatic.borg.create).should_receive('make_exclude_flags').and_return(())
  684. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  685. flexmock(module.borgmatic.borg.create).should_receive('make_list_filter_flags').and_return('')
  686. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').never()
  687. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(False)
  688. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  689. ('repo',)
  690. )
  691. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_archive_flags').never()
  692. insert_execute_command_mock(('borg', 'recreate', 'repo'))
  693. module.recreate_archive(
  694. repository='repo',
  695. archive=None,
  696. config={'match_archives': 'foo-*'},
  697. local_borg_version='1.2.3',
  698. recreate_arguments=flexmock(
  699. list=None,
  700. target=None,
  701. comment=None,
  702. timestamp=None,
  703. match_archives='foo-*',
  704. ),
  705. global_arguments=flexmock(dry_run=False),
  706. local_path='borg',
  707. patterns=None,
  708. )
  709. def test_recreate_with_archives_flags_and_feature_not_available_calls_borg_with_combined_repo_and_archive():
  710. flexmock(module.borgmatic.borg.create).should_receive('make_exclude_flags').and_return(())
  711. flexmock(module.borgmatic.borg.create).should_receive('write_patterns_file').and_return(None)
  712. flexmock(module.borgmatic.borg.create).should_receive('make_list_filter_flags').and_return('')
  713. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').never()
  714. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(False)
  715. flexmock(module.borgmatic.borg.flags).should_receive(
  716. 'make_repository_archive_flags'
  717. ).and_return(('repo::archive',))
  718. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').never()
  719. insert_execute_command_mock(('borg', 'recreate', 'repo::archive'))
  720. module.recreate_archive(
  721. repository='repo',
  722. archive='archive',
  723. config={'match_archives': 'foo-*'},
  724. local_borg_version='1.2.3',
  725. recreate_arguments=flexmock(
  726. list=None,
  727. target=None,
  728. comment=None,
  729. timestamp=None,
  730. match_archives='foo-*',
  731. ),
  732. global_arguments=flexmock(dry_run=False),
  733. local_path='borg',
  734. patterns=None,
  735. )