2
0

test_extract.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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.paths).should_receive('get_working_directory').and_return(None)
  147. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  148. ('repo::archive',)
  149. )
  150. flexmock(module.borgmatic.config.validate).should_receive(
  151. 'normalize_repository_path'
  152. ).and_return('repo')
  153. module.extract_archive(
  154. dry_run=False,
  155. repository='repo',
  156. archive='archive',
  157. paths=['path1', 'path2'],
  158. config={},
  159. local_borg_version='1.2.3',
  160. global_arguments=flexmock(log_json=False),
  161. )
  162. def test_extract_archive_calls_borg_with_local_path():
  163. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  164. insert_execute_command_mock(('borg1', 'extract', 'repo::archive'))
  165. flexmock(module.feature).should_receive('available').and_return(True)
  166. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  167. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  168. ('repo::archive',)
  169. )
  170. flexmock(module.borgmatic.config.validate).should_receive(
  171. 'normalize_repository_path'
  172. ).and_return('repo')
  173. module.extract_archive(
  174. dry_run=False,
  175. repository='repo',
  176. archive='archive',
  177. paths=None,
  178. config={},
  179. local_borg_version='1.2.3',
  180. global_arguments=flexmock(log_json=False),
  181. local_path='borg1',
  182. )
  183. def test_extract_archive_calls_borg_with_exit_codes():
  184. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  185. borg_exit_codes = flexmock()
  186. insert_execute_command_mock(
  187. ('borg', 'extract', 'repo::archive'), borg_exit_codes=borg_exit_codes
  188. )
  189. flexmock(module.feature).should_receive('available').and_return(True)
  190. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  191. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  192. ('repo::archive',)
  193. )
  194. flexmock(module.borgmatic.config.validate).should_receive(
  195. 'normalize_repository_path'
  196. ).and_return('repo')
  197. module.extract_archive(
  198. dry_run=False,
  199. repository='repo',
  200. archive='archive',
  201. paths=None,
  202. config={'borg_exit_codes': borg_exit_codes},
  203. local_borg_version='1.2.3',
  204. global_arguments=flexmock(log_json=False),
  205. )
  206. def test_extract_archive_calls_borg_with_remote_path_flags():
  207. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  208. insert_execute_command_mock(('borg', 'extract', '--remote-path', 'borg1', 'repo::archive'))
  209. flexmock(module.feature).should_receive('available').and_return(True)
  210. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  211. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  212. ('repo::archive',)
  213. )
  214. flexmock(module.borgmatic.config.validate).should_receive(
  215. 'normalize_repository_path'
  216. ).and_return('repo')
  217. module.extract_archive(
  218. dry_run=False,
  219. repository='repo',
  220. archive='archive',
  221. paths=None,
  222. config={},
  223. local_borg_version='1.2.3',
  224. global_arguments=flexmock(log_json=False),
  225. remote_path='borg1',
  226. )
  227. @pytest.mark.parametrize(
  228. 'feature_available,option_flag',
  229. (
  230. (True, '--numeric-ids'),
  231. (False, '--numeric-owner'),
  232. ),
  233. )
  234. def test_extract_archive_calls_borg_with_numeric_ids_parameter(feature_available, option_flag):
  235. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  236. insert_execute_command_mock(('borg', 'extract', option_flag, 'repo::archive'))
  237. flexmock(module.feature).should_receive('available').and_return(feature_available)
  238. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  239. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  240. ('repo::archive',)
  241. )
  242. flexmock(module.borgmatic.config.validate).should_receive(
  243. 'normalize_repository_path'
  244. ).and_return('repo')
  245. module.extract_archive(
  246. dry_run=False,
  247. repository='repo',
  248. archive='archive',
  249. paths=None,
  250. config={'numeric_ids': True},
  251. local_borg_version='1.2.3',
  252. global_arguments=flexmock(log_json=False),
  253. )
  254. def test_extract_archive_calls_borg_with_umask_flags():
  255. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  256. insert_execute_command_mock(('borg', 'extract', '--umask', '0770', 'repo::archive'))
  257. flexmock(module.feature).should_receive('available').and_return(True)
  258. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  259. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  260. ('repo::archive',)
  261. )
  262. flexmock(module.borgmatic.config.validate).should_receive(
  263. 'normalize_repository_path'
  264. ).and_return('repo')
  265. module.extract_archive(
  266. dry_run=False,
  267. repository='repo',
  268. archive='archive',
  269. paths=None,
  270. config={'umask': '0770'},
  271. local_borg_version='1.2.3',
  272. global_arguments=flexmock(log_json=False),
  273. )
  274. def test_extract_archive_calls_borg_with_log_json_flags():
  275. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  276. insert_execute_command_mock(('borg', 'extract', '--log-json', 'repo::archive'))
  277. flexmock(module.feature).should_receive('available').and_return(True)
  278. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  279. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  280. ('repo::archive',)
  281. )
  282. module.extract_archive(
  283. dry_run=False,
  284. repository='repo',
  285. archive='archive',
  286. paths=None,
  287. config={},
  288. local_borg_version='1.2.3',
  289. global_arguments=flexmock(log_json=True),
  290. )
  291. def test_extract_archive_calls_borg_with_lock_wait_flags():
  292. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  293. insert_execute_command_mock(('borg', 'extract', '--lock-wait', '5', 'repo::archive'))
  294. flexmock(module.feature).should_receive('available').and_return(True)
  295. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  296. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  297. ('repo::archive',)
  298. )
  299. flexmock(module.borgmatic.config.validate).should_receive(
  300. 'normalize_repository_path'
  301. ).and_return('repo')
  302. module.extract_archive(
  303. dry_run=False,
  304. repository='repo',
  305. archive='archive',
  306. paths=None,
  307. config={'lock_wait': '5'},
  308. local_borg_version='1.2.3',
  309. global_arguments=flexmock(log_json=False),
  310. )
  311. def test_extract_archive_with_log_info_calls_borg_with_info_parameter():
  312. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  313. insert_execute_command_mock(('borg', 'extract', '--info', 'repo::archive'))
  314. insert_logging_mock(logging.INFO)
  315. flexmock(module.feature).should_receive('available').and_return(True)
  316. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  317. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  318. ('repo::archive',)
  319. )
  320. flexmock(module.borgmatic.config.validate).should_receive(
  321. 'normalize_repository_path'
  322. ).and_return('repo')
  323. module.extract_archive(
  324. dry_run=False,
  325. repository='repo',
  326. archive='archive',
  327. paths=None,
  328. config={},
  329. local_borg_version='1.2.3',
  330. global_arguments=flexmock(log_json=False),
  331. )
  332. def test_extract_archive_with_log_debug_calls_borg_with_debug_flags():
  333. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  334. insert_execute_command_mock(
  335. ('borg', 'extract', '--debug', '--list', '--show-rc', 'repo::archive')
  336. )
  337. insert_logging_mock(logging.DEBUG)
  338. flexmock(module.feature).should_receive('available').and_return(True)
  339. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  340. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  341. ('repo::archive',)
  342. )
  343. flexmock(module.borgmatic.config.validate).should_receive(
  344. 'normalize_repository_path'
  345. ).and_return('repo')
  346. module.extract_archive(
  347. dry_run=False,
  348. repository='repo',
  349. archive='archive',
  350. paths=None,
  351. config={},
  352. local_borg_version='1.2.3',
  353. global_arguments=flexmock(log_json=False),
  354. )
  355. def test_extract_archive_calls_borg_with_dry_run_parameter():
  356. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  357. insert_execute_command_mock(('borg', 'extract', '--dry-run', 'repo::archive'))
  358. flexmock(module.feature).should_receive('available').and_return(True)
  359. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  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=True,
  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_destination_path():
  376. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  377. insert_execute_command_mock(('borg', 'extract', 'repo::archive'), destination_path='/dest')
  378. flexmock(module.feature).should_receive('available').and_return(True)
  379. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  380. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  381. ('repo::archive',)
  382. )
  383. flexmock(module.borgmatic.config.validate).should_receive(
  384. 'normalize_repository_path'
  385. ).and_return('repo')
  386. module.extract_archive(
  387. dry_run=False,
  388. repository='repo',
  389. archive='archive',
  390. paths=None,
  391. config={},
  392. local_borg_version='1.2.3',
  393. global_arguments=flexmock(log_json=False),
  394. destination_path='/dest',
  395. )
  396. def test_extract_archive_calls_borg_with_strip_components():
  397. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  398. insert_execute_command_mock(('borg', 'extract', '--strip-components', '5', 'repo::archive'))
  399. flexmock(module.feature).should_receive('available').and_return(True)
  400. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  401. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  402. ('repo::archive',)
  403. )
  404. flexmock(module.borgmatic.config.validate).should_receive(
  405. 'normalize_repository_path'
  406. ).and_return('repo')
  407. module.extract_archive(
  408. dry_run=False,
  409. repository='repo',
  410. archive='archive',
  411. paths=None,
  412. config={},
  413. local_borg_version='1.2.3',
  414. global_arguments=flexmock(log_json=False),
  415. strip_components=5,
  416. )
  417. def test_extract_archive_calls_borg_with_strip_components_calculated_from_all():
  418. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  419. insert_execute_command_mock(
  420. (
  421. 'borg',
  422. 'extract',
  423. '--strip-components',
  424. '2',
  425. 'repo::archive',
  426. 'foo/bar/baz.txt',
  427. 'foo/bar.txt',
  428. )
  429. )
  430. flexmock(module.feature).should_receive('available').and_return(True)
  431. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  432. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  433. ('repo::archive',)
  434. )
  435. flexmock(module.borgmatic.config.validate).should_receive(
  436. 'normalize_repository_path'
  437. ).and_return('repo')
  438. module.extract_archive(
  439. dry_run=False,
  440. repository='repo',
  441. archive='archive',
  442. paths=['foo/bar/baz.txt', 'foo/bar.txt'],
  443. config={},
  444. local_borg_version='1.2.3',
  445. global_arguments=flexmock(log_json=False),
  446. strip_components='all',
  447. )
  448. def test_extract_archive_calls_borg_with_strip_components_calculated_from_all_with_leading_slash():
  449. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  450. insert_execute_command_mock(
  451. (
  452. 'borg',
  453. 'extract',
  454. '--strip-components',
  455. '2',
  456. 'repo::archive',
  457. '/foo/bar/baz.txt',
  458. '/foo/bar.txt',
  459. )
  460. )
  461. flexmock(module.feature).should_receive('available').and_return(True)
  462. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  463. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  464. ('repo::archive',)
  465. )
  466. flexmock(module.borgmatic.config.validate).should_receive(
  467. 'normalize_repository_path'
  468. ).and_return('repo')
  469. module.extract_archive(
  470. dry_run=False,
  471. repository='repo',
  472. archive='archive',
  473. paths=['/foo/bar/baz.txt', '/foo/bar.txt'],
  474. config={},
  475. local_borg_version='1.2.3',
  476. global_arguments=flexmock(log_json=False),
  477. strip_components='all',
  478. )
  479. def test_extract_archive_with_strip_components_all_and_no_paths_raises():
  480. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  481. flexmock(module.feature).should_receive('available').and_return(True)
  482. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  483. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  484. ('repo::archive',)
  485. )
  486. flexmock(module.borgmatic.config.validate).should_receive(
  487. 'normalize_repository_path'
  488. ).and_return('repo')
  489. flexmock(module).should_receive('execute_command').never()
  490. with pytest.raises(ValueError):
  491. module.extract_archive(
  492. dry_run=False,
  493. repository='repo',
  494. archive='archive',
  495. paths=None,
  496. config={},
  497. local_borg_version='1.2.3',
  498. global_arguments=flexmock(log_json=False),
  499. strip_components='all',
  500. )
  501. def test_extract_archive_calls_borg_with_progress_parameter():
  502. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  503. flexmock(module.environment).should_receive('make_environment')
  504. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  505. flexmock(module).should_receive('execute_command').with_args(
  506. ('borg', 'extract', '--progress', 'repo::archive'),
  507. output_file=module.DO_NOT_CAPTURE,
  508. extra_environment=None,
  509. working_directory=None,
  510. borg_local_path='borg',
  511. borg_exit_codes=None,
  512. ).once()
  513. flexmock(module.feature).should_receive('available').and_return(True)
  514. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  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. module.extract_archive(
  522. dry_run=False,
  523. repository='repo',
  524. archive='archive',
  525. paths=None,
  526. config={},
  527. local_borg_version='1.2.3',
  528. global_arguments=flexmock(log_json=False),
  529. progress=True,
  530. )
  531. def test_extract_archive_with_progress_and_extract_to_stdout_raises():
  532. flexmock(module).should_receive('execute_command').never()
  533. with pytest.raises(ValueError):
  534. module.extract_archive(
  535. dry_run=False,
  536. repository='repo',
  537. archive='archive',
  538. paths=None,
  539. config={},
  540. local_borg_version='1.2.3',
  541. global_arguments=flexmock(log_json=False),
  542. progress=True,
  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. extra_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(log_json=False),
  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. extra_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(log_json=False),
  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'), destination_path='/working/dir/dest'
  612. )
  613. flexmock(module.feature).should_receive('available').and_return(True)
  614. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  615. ('/working/dir/repo::archive',)
  616. )
  617. flexmock(module.borgmatic.config.validate).should_receive(
  618. 'normalize_repository_path'
  619. ).with_args('/working/dir/repo').and_return('/working/dir/repo').once()
  620. module.extract_archive(
  621. dry_run=False,
  622. repository='repo',
  623. archive='archive',
  624. paths=None,
  625. config={'working_directory': '/working/dir'},
  626. local_borg_version='1.2.3',
  627. global_arguments=flexmock(log_json=False),
  628. destination_path='dest',
  629. )
  630. def test_extract_archive_uses_configured_working_directory_in_repo_path_when_destination_path_is_not_set():
  631. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  632. insert_execute_command_mock(('borg', 'extract', '/working/dir/repo::archive'))
  633. flexmock(module.feature).should_receive('available').and_return(True)
  634. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  635. ('/working/dir/repo::archive',)
  636. )
  637. flexmock(module.borgmatic.config.validate).should_receive(
  638. 'normalize_repository_path'
  639. ).with_args('/working/dir/repo').and_return('/working/dir/repo').once()
  640. module.extract_archive(
  641. dry_run=False,
  642. repository='repo',
  643. archive='archive',
  644. paths=None,
  645. config={'working_directory': '/working/dir'},
  646. local_borg_version='1.2.3',
  647. global_arguments=flexmock(log_json=False),
  648. )