2
0

test_extract.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.borg import extract as module
  5. from ..test_verbosity import insert_logging_mock
  6. def insert_execute_command_mock(command, destination_path=None, borg_exit_codes=None):
  7. flexmock(module.environment).should_receive('make_environment')
  8. flexmock(module).should_receive('execute_command').with_args(
  9. command,
  10. extra_environment=None,
  11. working_directory=destination_path,
  12. borg_local_path=command[0],
  13. borg_exit_codes=borg_exit_codes,
  14. ).once()
  15. def test_extract_last_archive_dry_run_calls_borg_with_last_archive():
  16. flexmock(module.repo_list).should_receive('resolve_archive_name').and_return('archive')
  17. insert_execute_command_mock(('borg', 'extract', '--dry-run', 'repo::archive'))
  18. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  19. ('repo::archive',)
  20. )
  21. module.extract_last_archive_dry_run(
  22. config={},
  23. local_borg_version='1.2.3',
  24. global_arguments=flexmock(log_json=False),
  25. repository_path='repo',
  26. lock_wait=None,
  27. )
  28. def test_extract_last_archive_dry_run_without_any_archives_should_not_raise():
  29. flexmock(module.repo_list).should_receive('resolve_archive_name').and_raise(ValueError)
  30. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(('repo',))
  31. module.extract_last_archive_dry_run(
  32. config={},
  33. local_borg_version='1.2.3',
  34. global_arguments=flexmock(log_json=False),
  35. repository_path='repo',
  36. lock_wait=None,
  37. )
  38. def test_extract_last_archive_dry_run_with_log_info_calls_borg_with_info_parameter():
  39. flexmock(module.repo_list).should_receive('resolve_archive_name').and_return('archive')
  40. insert_execute_command_mock(('borg', 'extract', '--dry-run', '--info', 'repo::archive'))
  41. insert_logging_mock(logging.INFO)
  42. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  43. ('repo::archive',)
  44. )
  45. module.extract_last_archive_dry_run(
  46. config={},
  47. local_borg_version='1.2.3',
  48. global_arguments=flexmock(log_json=False),
  49. repository_path='repo',
  50. lock_wait=None,
  51. )
  52. def test_extract_last_archive_dry_run_with_log_debug_calls_borg_with_debug_parameter():
  53. flexmock(module.repo_list).should_receive('resolve_archive_name').and_return('archive')
  54. insert_execute_command_mock(
  55. ('borg', 'extract', '--dry-run', '--debug', '--show-rc', '--list', 'repo::archive')
  56. )
  57. insert_logging_mock(logging.DEBUG)
  58. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  59. ('repo::archive',)
  60. )
  61. module.extract_last_archive_dry_run(
  62. config={},
  63. local_borg_version='1.2.3',
  64. global_arguments=flexmock(log_json=False),
  65. repository_path='repo',
  66. lock_wait=None,
  67. )
  68. def test_extract_last_archive_dry_run_calls_borg_via_local_path():
  69. flexmock(module.repo_list).should_receive('resolve_archive_name').and_return('archive')
  70. insert_execute_command_mock(('borg1', 'extract', '--dry-run', 'repo::archive'))
  71. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  72. ('repo::archive',)
  73. )
  74. module.extract_last_archive_dry_run(
  75. config={},
  76. local_borg_version='1.2.3',
  77. global_arguments=flexmock(log_json=False),
  78. repository_path='repo',
  79. lock_wait=None,
  80. local_path='borg1',
  81. )
  82. def test_extract_last_archive_dry_run_calls_borg_using_exit_codes():
  83. flexmock(module.repo_list).should_receive('resolve_archive_name').and_return('archive')
  84. borg_exit_codes = flexmock()
  85. insert_execute_command_mock(
  86. ('borg', 'extract', '--dry-run', 'repo::archive'), borg_exit_codes=borg_exit_codes
  87. )
  88. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  89. ('repo::archive',)
  90. )
  91. module.extract_last_archive_dry_run(
  92. config={'borg_exit_codes': borg_exit_codes},
  93. local_borg_version='1.2.3',
  94. global_arguments=flexmock(log_json=False),
  95. repository_path='repo',
  96. lock_wait=None,
  97. )
  98. def test_extract_last_archive_dry_run_calls_borg_with_remote_path_flags():
  99. flexmock(module.repo_list).should_receive('resolve_archive_name').and_return('archive')
  100. insert_execute_command_mock(
  101. ('borg', 'extract', '--dry-run', '--remote-path', 'borg1', 'repo::archive')
  102. )
  103. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  104. ('repo::archive',)
  105. )
  106. module.extract_last_archive_dry_run(
  107. config={},
  108. local_borg_version='1.2.3',
  109. global_arguments=flexmock(log_json=False),
  110. repository_path='repo',
  111. lock_wait=None,
  112. remote_path='borg1',
  113. )
  114. def test_extract_last_archive_dry_run_calls_borg_with_log_json_flag():
  115. flexmock(module.repo_list).should_receive('resolve_archive_name').and_return('archive')
  116. insert_execute_command_mock(('borg', 'extract', '--dry-run', '--log-json', 'repo::archive'))
  117. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  118. ('repo::archive',)
  119. )
  120. module.extract_last_archive_dry_run(
  121. config={},
  122. local_borg_version='1.2.3',
  123. global_arguments=flexmock(log_json=True),
  124. repository_path='repo',
  125. lock_wait=None,
  126. )
  127. def test_extract_last_archive_dry_run_calls_borg_with_lock_wait_flags():
  128. flexmock(module.repo_list).should_receive('resolve_archive_name').and_return('archive')
  129. insert_execute_command_mock(
  130. ('borg', 'extract', '--dry-run', '--lock-wait', '5', 'repo::archive')
  131. )
  132. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  133. ('repo::archive',)
  134. )
  135. module.extract_last_archive_dry_run(
  136. config={},
  137. local_borg_version='1.2.3',
  138. global_arguments=flexmock(log_json=False),
  139. repository_path='repo',
  140. lock_wait=5,
  141. )
  142. def test_extract_archive_calls_borg_with_path_flags():
  143. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  144. insert_execute_command_mock(('borg', 'extract', 'repo::archive', 'path1', 'path2'))
  145. flexmock(module.feature).should_receive('available').and_return(True)
  146. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  147. None
  148. )
  149. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  150. ('repo::archive',)
  151. )
  152. flexmock(module.borgmatic.config.validate).should_receive(
  153. 'normalize_repository_path'
  154. ).and_return('repo')
  155. module.extract_archive(
  156. dry_run=False,
  157. repository='repo',
  158. archive='archive',
  159. paths=['path1', 'path2'],
  160. config={},
  161. local_borg_version='1.2.3',
  162. global_arguments=flexmock(log_json=False),
  163. )
  164. def test_extract_archive_calls_borg_with_local_path():
  165. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  166. insert_execute_command_mock(('borg1', 'extract', 'repo::archive'))
  167. flexmock(module.feature).should_receive('available').and_return(True)
  168. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  169. None
  170. )
  171. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  172. ('repo::archive',)
  173. )
  174. flexmock(module.borgmatic.config.validate).should_receive(
  175. 'normalize_repository_path'
  176. ).and_return('repo')
  177. module.extract_archive(
  178. dry_run=False,
  179. repository='repo',
  180. archive='archive',
  181. paths=None,
  182. config={},
  183. local_borg_version='1.2.3',
  184. global_arguments=flexmock(log_json=False),
  185. local_path='borg1',
  186. )
  187. def test_extract_archive_calls_borg_with_exit_codes():
  188. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  189. borg_exit_codes = flexmock()
  190. insert_execute_command_mock(
  191. ('borg', 'extract', 'repo::archive'), borg_exit_codes=borg_exit_codes
  192. )
  193. flexmock(module.feature).should_receive('available').and_return(True)
  194. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  195. None
  196. )
  197. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  198. ('repo::archive',)
  199. )
  200. flexmock(module.borgmatic.config.validate).should_receive(
  201. 'normalize_repository_path'
  202. ).and_return('repo')
  203. module.extract_archive(
  204. dry_run=False,
  205. repository='repo',
  206. archive='archive',
  207. paths=None,
  208. config={'borg_exit_codes': borg_exit_codes},
  209. local_borg_version='1.2.3',
  210. global_arguments=flexmock(log_json=False),
  211. )
  212. def test_extract_archive_calls_borg_with_remote_path_flags():
  213. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  214. insert_execute_command_mock(('borg', 'extract', '--remote-path', 'borg1', 'repo::archive'))
  215. flexmock(module.feature).should_receive('available').and_return(True)
  216. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  217. None
  218. )
  219. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  220. ('repo::archive',)
  221. )
  222. flexmock(module.borgmatic.config.validate).should_receive(
  223. 'normalize_repository_path'
  224. ).and_return('repo')
  225. module.extract_archive(
  226. dry_run=False,
  227. repository='repo',
  228. archive='archive',
  229. paths=None,
  230. config={},
  231. local_borg_version='1.2.3',
  232. global_arguments=flexmock(log_json=False),
  233. remote_path='borg1',
  234. )
  235. @pytest.mark.parametrize(
  236. 'feature_available,option_flag',
  237. (
  238. (True, '--numeric-ids'),
  239. (False, '--numeric-owner'),
  240. ),
  241. )
  242. def test_extract_archive_calls_borg_with_numeric_ids_parameter(feature_available, option_flag):
  243. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  244. insert_execute_command_mock(('borg', 'extract', option_flag, 'repo::archive'))
  245. flexmock(module.feature).should_receive('available').and_return(feature_available)
  246. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  247. None
  248. )
  249. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  250. ('repo::archive',)
  251. )
  252. flexmock(module.borgmatic.config.validate).should_receive(
  253. 'normalize_repository_path'
  254. ).and_return('repo')
  255. module.extract_archive(
  256. dry_run=False,
  257. repository='repo',
  258. archive='archive',
  259. paths=None,
  260. config={'numeric_ids': True},
  261. local_borg_version='1.2.3',
  262. global_arguments=flexmock(log_json=False),
  263. )
  264. def test_extract_archive_calls_borg_with_umask_flags():
  265. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  266. insert_execute_command_mock(('borg', 'extract', '--umask', '0770', 'repo::archive'))
  267. flexmock(module.feature).should_receive('available').and_return(True)
  268. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  269. None
  270. )
  271. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  272. ('repo::archive',)
  273. )
  274. flexmock(module.borgmatic.config.validate).should_receive(
  275. 'normalize_repository_path'
  276. ).and_return('repo')
  277. module.extract_archive(
  278. dry_run=False,
  279. repository='repo',
  280. archive='archive',
  281. paths=None,
  282. config={'umask': '0770'},
  283. local_borg_version='1.2.3',
  284. global_arguments=flexmock(log_json=False),
  285. )
  286. def test_extract_archive_calls_borg_with_log_json_flags():
  287. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  288. insert_execute_command_mock(('borg', 'extract', '--log-json', 'repo::archive'))
  289. flexmock(module.feature).should_receive('available').and_return(True)
  290. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  291. None
  292. )
  293. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  294. ('repo::archive',)
  295. )
  296. module.extract_archive(
  297. dry_run=False,
  298. repository='repo',
  299. archive='archive',
  300. paths=None,
  301. config={},
  302. local_borg_version='1.2.3',
  303. global_arguments=flexmock(log_json=True),
  304. )
  305. def test_extract_archive_calls_borg_with_lock_wait_flags():
  306. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  307. insert_execute_command_mock(('borg', 'extract', '--lock-wait', '5', 'repo::archive'))
  308. flexmock(module.feature).should_receive('available').and_return(True)
  309. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  310. None
  311. )
  312. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  313. ('repo::archive',)
  314. )
  315. flexmock(module.borgmatic.config.validate).should_receive(
  316. 'normalize_repository_path'
  317. ).and_return('repo')
  318. module.extract_archive(
  319. dry_run=False,
  320. repository='repo',
  321. archive='archive',
  322. paths=None,
  323. config={'lock_wait': '5'},
  324. local_borg_version='1.2.3',
  325. global_arguments=flexmock(log_json=False),
  326. )
  327. def test_extract_archive_with_log_info_calls_borg_with_info_parameter():
  328. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  329. insert_execute_command_mock(('borg', 'extract', '--info', 'repo::archive'))
  330. insert_logging_mock(logging.INFO)
  331. flexmock(module.feature).should_receive('available').and_return(True)
  332. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  333. None
  334. )
  335. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  336. ('repo::archive',)
  337. )
  338. flexmock(module.borgmatic.config.validate).should_receive(
  339. 'normalize_repository_path'
  340. ).and_return('repo')
  341. module.extract_archive(
  342. dry_run=False,
  343. repository='repo',
  344. archive='archive',
  345. paths=None,
  346. config={},
  347. local_borg_version='1.2.3',
  348. global_arguments=flexmock(log_json=False),
  349. )
  350. def test_extract_archive_with_log_debug_calls_borg_with_debug_flags():
  351. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  352. insert_execute_command_mock(
  353. ('borg', 'extract', '--debug', '--list', '--show-rc', 'repo::archive')
  354. )
  355. insert_logging_mock(logging.DEBUG)
  356. flexmock(module.feature).should_receive('available').and_return(True)
  357. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  358. None
  359. )
  360. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  361. ('repo::archive',)
  362. )
  363. flexmock(module.borgmatic.config.validate).should_receive(
  364. 'normalize_repository_path'
  365. ).and_return('repo')
  366. module.extract_archive(
  367. dry_run=False,
  368. repository='repo',
  369. archive='archive',
  370. paths=None,
  371. config={},
  372. local_borg_version='1.2.3',
  373. global_arguments=flexmock(log_json=False),
  374. )
  375. def test_extract_archive_calls_borg_with_dry_run_parameter():
  376. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  377. insert_execute_command_mock(('borg', 'extract', '--dry-run', 'repo::archive'))
  378. flexmock(module.feature).should_receive('available').and_return(True)
  379. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  380. None
  381. )
  382. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  383. ('repo::archive',)
  384. )
  385. flexmock(module.borgmatic.config.validate).should_receive(
  386. 'normalize_repository_path'
  387. ).and_return('repo')
  388. module.extract_archive(
  389. dry_run=True,
  390. repository='repo',
  391. archive='archive',
  392. paths=None,
  393. config={},
  394. local_borg_version='1.2.3',
  395. global_arguments=flexmock(log_json=False),
  396. )
  397. def test_extract_archive_calls_borg_with_destination_path():
  398. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  399. insert_execute_command_mock(('borg', 'extract', 'repo::archive'), destination_path='/dest')
  400. flexmock(module.feature).should_receive('available').and_return(True)
  401. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  402. None
  403. )
  404. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  405. ('repo::archive',)
  406. )
  407. flexmock(module.borgmatic.config.validate).should_receive(
  408. 'normalize_repository_path'
  409. ).and_return('repo')
  410. module.extract_archive(
  411. dry_run=False,
  412. repository='repo',
  413. archive='archive',
  414. paths=None,
  415. config={},
  416. local_borg_version='1.2.3',
  417. global_arguments=flexmock(log_json=False),
  418. destination_path='/dest',
  419. )
  420. def test_extract_archive_calls_borg_with_strip_components():
  421. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  422. insert_execute_command_mock(('borg', 'extract', '--strip-components', '5', 'repo::archive'))
  423. flexmock(module.feature).should_receive('available').and_return(True)
  424. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  425. None
  426. )
  427. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  428. ('repo::archive',)
  429. )
  430. flexmock(module.borgmatic.config.validate).should_receive(
  431. 'normalize_repository_path'
  432. ).and_return('repo')
  433. module.extract_archive(
  434. dry_run=False,
  435. repository='repo',
  436. archive='archive',
  437. paths=None,
  438. config={},
  439. local_borg_version='1.2.3',
  440. global_arguments=flexmock(log_json=False),
  441. strip_components=5,
  442. )
  443. def test_extract_archive_calls_borg_with_strip_components_calculated_from_all():
  444. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  445. insert_execute_command_mock(
  446. (
  447. 'borg',
  448. 'extract',
  449. '--strip-components',
  450. '2',
  451. 'repo::archive',
  452. 'foo/bar/baz.txt',
  453. 'foo/bar.txt',
  454. )
  455. )
  456. flexmock(module.feature).should_receive('available').and_return(True)
  457. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  458. None
  459. )
  460. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  461. ('repo::archive',)
  462. )
  463. flexmock(module.borgmatic.config.validate).should_receive(
  464. 'normalize_repository_path'
  465. ).and_return('repo')
  466. module.extract_archive(
  467. dry_run=False,
  468. repository='repo',
  469. archive='archive',
  470. paths=['foo/bar/baz.txt', 'foo/bar.txt'],
  471. config={},
  472. local_borg_version='1.2.3',
  473. global_arguments=flexmock(log_json=False),
  474. strip_components='all',
  475. )
  476. def test_extract_archive_calls_borg_with_strip_components_calculated_from_all_with_leading_slash():
  477. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  478. insert_execute_command_mock(
  479. (
  480. 'borg',
  481. 'extract',
  482. '--strip-components',
  483. '2',
  484. 'repo::archive',
  485. '/foo/bar/baz.txt',
  486. '/foo/bar.txt',
  487. )
  488. )
  489. flexmock(module.feature).should_receive('available').and_return(True)
  490. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  491. None
  492. )
  493. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  494. ('repo::archive',)
  495. )
  496. flexmock(module.borgmatic.config.validate).should_receive(
  497. 'normalize_repository_path'
  498. ).and_return('repo')
  499. module.extract_archive(
  500. dry_run=False,
  501. repository='repo',
  502. archive='archive',
  503. paths=['/foo/bar/baz.txt', '/foo/bar.txt'],
  504. config={},
  505. local_borg_version='1.2.3',
  506. global_arguments=flexmock(log_json=False),
  507. strip_components='all',
  508. )
  509. def test_extract_archive_with_strip_components_all_and_no_paths_raises():
  510. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  511. flexmock(module.feature).should_receive('available').and_return(True)
  512. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  513. None
  514. )
  515. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  516. ('repo::archive',)
  517. )
  518. flexmock(module.borgmatic.config.validate).should_receive(
  519. 'normalize_repository_path'
  520. ).and_return('repo')
  521. flexmock(module).should_receive('execute_command').never()
  522. with pytest.raises(ValueError):
  523. module.extract_archive(
  524. dry_run=False,
  525. repository='repo',
  526. archive='archive',
  527. paths=None,
  528. config={},
  529. local_borg_version='1.2.3',
  530. global_arguments=flexmock(log_json=False),
  531. strip_components='all',
  532. )
  533. def test_extract_archive_calls_borg_with_progress_parameter():
  534. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  535. flexmock(module.environment).should_receive('make_environment')
  536. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  537. None
  538. )
  539. flexmock(module).should_receive('execute_command').with_args(
  540. ('borg', 'extract', '--progress', 'repo::archive'),
  541. output_file=module.DO_NOT_CAPTURE,
  542. extra_environment=None,
  543. working_directory=None,
  544. borg_local_path='borg',
  545. borg_exit_codes=None,
  546. ).once()
  547. flexmock(module.feature).should_receive('available').and_return(True)
  548. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  549. None
  550. )
  551. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  552. ('repo::archive',)
  553. )
  554. flexmock(module.borgmatic.config.validate).should_receive(
  555. 'normalize_repository_path'
  556. ).and_return('repo')
  557. module.extract_archive(
  558. dry_run=False,
  559. repository='repo',
  560. archive='archive',
  561. paths=None,
  562. config={},
  563. local_borg_version='1.2.3',
  564. global_arguments=flexmock(log_json=False),
  565. progress=True,
  566. )
  567. def test_extract_archive_with_progress_and_extract_to_stdout_raises():
  568. flexmock(module).should_receive('execute_command').never()
  569. with pytest.raises(ValueError):
  570. module.extract_archive(
  571. dry_run=False,
  572. repository='repo',
  573. archive='archive',
  574. paths=None,
  575. config={},
  576. local_borg_version='1.2.3',
  577. global_arguments=flexmock(log_json=False),
  578. progress=True,
  579. extract_to_stdout=True,
  580. )
  581. def test_extract_archive_calls_borg_with_stdout_parameter_and_returns_process():
  582. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  583. process = flexmock()
  584. flexmock(module.environment).should_receive('make_environment')
  585. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  586. None
  587. )
  588. flexmock(module).should_receive('execute_command').with_args(
  589. ('borg', 'extract', '--stdout', 'repo::archive'),
  590. output_file=module.subprocess.PIPE,
  591. run_to_completion=False,
  592. extra_environment=None,
  593. working_directory=None,
  594. borg_local_path='borg',
  595. borg_exit_codes=None,
  596. ).and_return(process).once()
  597. flexmock(module.feature).should_receive('available').and_return(True)
  598. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  599. None
  600. )
  601. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  602. ('repo::archive',)
  603. )
  604. flexmock(module.borgmatic.config.validate).should_receive(
  605. 'normalize_repository_path'
  606. ).and_return('repo')
  607. assert (
  608. module.extract_archive(
  609. dry_run=False,
  610. repository='repo',
  611. archive='archive',
  612. paths=None,
  613. config={},
  614. local_borg_version='1.2.3',
  615. global_arguments=flexmock(log_json=False),
  616. extract_to_stdout=True,
  617. )
  618. == process
  619. )
  620. def test_extract_archive_skips_abspath_for_remote_repository():
  621. flexmock(module.os.path).should_receive('abspath').never()
  622. flexmock(module.environment).should_receive('make_environment')
  623. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  624. None
  625. )
  626. flexmock(module).should_receive('execute_command').with_args(
  627. ('borg', 'extract', 'server:repo::archive'),
  628. extra_environment=None,
  629. working_directory=None,
  630. borg_local_path='borg',
  631. borg_exit_codes=None,
  632. ).once()
  633. flexmock(module.feature).should_receive('available').and_return(True)
  634. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  635. None
  636. )
  637. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  638. ('server:repo::archive',)
  639. )
  640. flexmock(module.borgmatic.config.validate).should_receive(
  641. 'normalize_repository_path'
  642. ).and_return('repo')
  643. module.extract_archive(
  644. dry_run=False,
  645. repository='server:repo',
  646. archive='archive',
  647. paths=None,
  648. config={},
  649. local_borg_version='1.2.3',
  650. global_arguments=flexmock(log_json=False),
  651. )
  652. def test_extract_archive_uses_configured_working_directory_in_repo_path_and_destination_path():
  653. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  654. insert_execute_command_mock(
  655. ('borg', 'extract', '/working/dir/repo::archive'), destination_path='/working/dir/dest'
  656. )
  657. flexmock(module.feature).should_receive('available').and_return(True)
  658. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  659. ('/working/dir/repo::archive',)
  660. )
  661. flexmock(module.borgmatic.config.validate).should_receive(
  662. 'normalize_repository_path'
  663. ).with_args('/working/dir/repo').and_return('/working/dir/repo').once()
  664. module.extract_archive(
  665. dry_run=False,
  666. repository='repo',
  667. archive='archive',
  668. paths=None,
  669. config={'working_directory': '/working/dir'},
  670. local_borg_version='1.2.3',
  671. global_arguments=flexmock(log_json=False),
  672. destination_path='dest',
  673. )
  674. def test_extract_archive_uses_configured_working_directory_in_repo_path_when_destination_path_is_not_set():
  675. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  676. insert_execute_command_mock(('borg', 'extract', '/working/dir/repo::archive'))
  677. flexmock(module.feature).should_receive('available').and_return(True)
  678. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  679. ('/working/dir/repo::archive',)
  680. )
  681. flexmock(module.borgmatic.config.validate).should_receive(
  682. 'normalize_repository_path'
  683. ).with_args('/working/dir/repo').and_return('/working/dir/repo').once()
  684. module.extract_archive(
  685. dry_run=False,
  686. repository='repo',
  687. archive='archive',
  688. paths=None,
  689. config={'working_directory': '/working/dir'},
  690. local_borg_version='1.2.3',
  691. global_arguments=flexmock(log_json=False),
  692. )