test_extract.py 28 KB

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