test_extract.py 27 KB

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