test_extract.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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, working_directory=None):
  7. flexmock(module.environment).should_receive('make_environment')
  8. flexmock(module).should_receive('execute_command').with_args(
  9. command, working_directory=working_directory, extra_environment=None,
  10. ).once()
  11. def insert_execute_command_output_mock(command, result):
  12. flexmock(module.environment).should_receive('make_environment')
  13. flexmock(module).should_receive('execute_command').with_args(
  14. command, output_log_level=None, borg_local_path=command[0], extra_environment=None,
  15. ).and_return(result).once()
  16. def test_extract_last_archive_dry_run_calls_borg_with_last_archive():
  17. insert_execute_command_output_mock(
  18. ('borg', 'list', '--short', 'repo'), result='archive1\narchive2\n'
  19. )
  20. insert_execute_command_mock(('borg', 'extract', '--dry-run', 'repo::archive2'))
  21. flexmock(module.feature).should_receive('available').and_return(True)
  22. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  23. ('repo::archive2',)
  24. )
  25. module.extract_last_archive_dry_run(storage_config={}, repository='repo', lock_wait=None)
  26. def test_extract_last_archive_dry_run_without_any_archives_should_not_raise():
  27. insert_execute_command_output_mock(('borg', 'list', '--short', 'repo'), result='\n')
  28. flexmock(module.feature).should_receive('available').and_return(True)
  29. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(('repo',))
  30. module.extract_last_archive_dry_run(storage_config={}, repository='repo', lock_wait=None)
  31. def test_extract_last_archive_dry_run_with_log_info_calls_borg_with_info_parameter():
  32. insert_execute_command_output_mock(
  33. ('borg', 'list', '--short', '--info', 'repo'), result='archive1\narchive2\n'
  34. )
  35. insert_execute_command_mock(('borg', 'extract', '--dry-run', '--info', 'repo::archive2'))
  36. insert_logging_mock(logging.INFO)
  37. flexmock(module.feature).should_receive('available').and_return(True)
  38. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  39. ('repo::archive2',)
  40. )
  41. module.extract_last_archive_dry_run(storage_config={}, repository='repo', lock_wait=None)
  42. def test_extract_last_archive_dry_run_with_log_debug_calls_borg_with_debug_parameter():
  43. insert_execute_command_output_mock(
  44. ('borg', 'list', '--short', '--debug', '--show-rc', 'repo'), result='archive1\narchive2\n'
  45. )
  46. insert_execute_command_mock(
  47. ('borg', 'extract', '--dry-run', '--debug', '--show-rc', '--list', 'repo::archive2')
  48. )
  49. insert_logging_mock(logging.DEBUG)
  50. flexmock(module.feature).should_receive('available').and_return(True)
  51. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  52. ('repo::archive2',)
  53. )
  54. module.extract_last_archive_dry_run(storage_config={}, repository='repo', lock_wait=None)
  55. def test_extract_last_archive_dry_run_calls_borg_via_local_path():
  56. insert_execute_command_output_mock(
  57. ('borg1', 'list', '--short', 'repo'), result='archive1\narchive2\n'
  58. )
  59. insert_execute_command_mock(('borg1', 'extract', '--dry-run', 'repo::archive2'))
  60. flexmock(module.feature).should_receive('available').and_return(True)
  61. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  62. ('repo::archive2',)
  63. )
  64. module.extract_last_archive_dry_run(
  65. storage_config={}, repository='repo', lock_wait=None, local_path='borg1'
  66. )
  67. def test_extract_last_archive_dry_run_calls_borg_with_remote_path_parameters():
  68. insert_execute_command_output_mock(
  69. ('borg', 'list', '--short', '--remote-path', 'borg1', 'repo'), result='archive1\narchive2\n'
  70. )
  71. insert_execute_command_mock(
  72. ('borg', 'extract', '--dry-run', '--remote-path', 'borg1', 'repo::archive2')
  73. )
  74. flexmock(module.feature).should_receive('available').and_return(True)
  75. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  76. ('repo::archive2',)
  77. )
  78. module.extract_last_archive_dry_run(
  79. storage_config={}, repository='repo', lock_wait=None, remote_path='borg1'
  80. )
  81. def test_extract_last_archive_dry_run_calls_borg_with_lock_wait_parameters():
  82. insert_execute_command_output_mock(
  83. ('borg', 'list', '--short', '--lock-wait', '5', 'repo'), result='archive1\narchive2\n'
  84. )
  85. insert_execute_command_mock(
  86. ('borg', 'extract', '--dry-run', '--lock-wait', '5', 'repo::archive2')
  87. )
  88. flexmock(module.feature).should_receive('available').and_return(True)
  89. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  90. ('repo::archive2',)
  91. )
  92. module.extract_last_archive_dry_run(storage_config={}, repository='repo', lock_wait=5)
  93. def test_extract_archive_calls_borg_with_path_parameters():
  94. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  95. insert_execute_command_mock(('borg', 'extract', 'repo::archive', 'path1', 'path2'))
  96. flexmock(module.feature).should_receive('available').and_return(True)
  97. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  98. ('repo::archive',)
  99. )
  100. module.extract_archive(
  101. dry_run=False,
  102. repository='repo',
  103. archive='archive',
  104. paths=['path1', 'path2'],
  105. location_config={},
  106. storage_config={},
  107. local_borg_version='1.2.3',
  108. )
  109. def test_extract_archive_calls_borg_with_remote_path_parameters():
  110. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  111. insert_execute_command_mock(('borg', 'extract', '--remote-path', 'borg1', 'repo::archive'))
  112. flexmock(module.feature).should_receive('available').and_return(True)
  113. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  114. ('repo::archive',)
  115. )
  116. module.extract_archive(
  117. dry_run=False,
  118. repository='repo',
  119. archive='archive',
  120. paths=None,
  121. location_config={},
  122. storage_config={},
  123. local_borg_version='1.2.3',
  124. remote_path='borg1',
  125. )
  126. @pytest.mark.parametrize(
  127. 'feature_available,option_flag', ((True, '--numeric-ids'), (False, '--numeric-owner'),),
  128. )
  129. def test_extract_archive_calls_borg_with_numeric_ids_parameter(feature_available, option_flag):
  130. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  131. insert_execute_command_mock(('borg', 'extract', option_flag, 'repo::archive'))
  132. flexmock(module.feature).should_receive('available').and_return(feature_available)
  133. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  134. ('repo::archive',)
  135. )
  136. module.extract_archive(
  137. dry_run=False,
  138. repository='repo',
  139. archive='archive',
  140. paths=None,
  141. location_config={'numeric_owner': True},
  142. storage_config={},
  143. local_borg_version='1.2.3',
  144. )
  145. def test_extract_archive_calls_borg_with_umask_parameters():
  146. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  147. insert_execute_command_mock(('borg', 'extract', '--umask', '0770', 'repo::archive'))
  148. flexmock(module.feature).should_receive('available').and_return(True)
  149. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  150. ('repo::archive',)
  151. )
  152. module.extract_archive(
  153. dry_run=False,
  154. repository='repo',
  155. archive='archive',
  156. paths=None,
  157. location_config={},
  158. storage_config={'umask': '0770'},
  159. local_borg_version='1.2.3',
  160. )
  161. def test_extract_archive_calls_borg_with_lock_wait_parameters():
  162. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  163. insert_execute_command_mock(('borg', 'extract', '--lock-wait', '5', 'repo::archive'))
  164. flexmock(module.feature).should_receive('available').and_return(True)
  165. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  166. ('repo::archive',)
  167. )
  168. module.extract_archive(
  169. dry_run=False,
  170. repository='repo',
  171. archive='archive',
  172. paths=None,
  173. location_config={},
  174. storage_config={'lock_wait': '5'},
  175. local_borg_version='1.2.3',
  176. )
  177. def test_extract_archive_with_log_info_calls_borg_with_info_parameter():
  178. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  179. insert_execute_command_mock(('borg', 'extract', '--info', 'repo::archive'))
  180. insert_logging_mock(logging.INFO)
  181. flexmock(module.feature).should_receive('available').and_return(True)
  182. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  183. ('repo::archive',)
  184. )
  185. module.extract_archive(
  186. dry_run=False,
  187. repository='repo',
  188. archive='archive',
  189. paths=None,
  190. location_config={},
  191. storage_config={},
  192. local_borg_version='1.2.3',
  193. )
  194. def test_extract_archive_with_log_debug_calls_borg_with_debug_parameters():
  195. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  196. insert_execute_command_mock(
  197. ('borg', 'extract', '--debug', '--list', '--show-rc', 'repo::archive')
  198. )
  199. insert_logging_mock(logging.DEBUG)
  200. flexmock(module.feature).should_receive('available').and_return(True)
  201. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  202. ('repo::archive',)
  203. )
  204. module.extract_archive(
  205. dry_run=False,
  206. repository='repo',
  207. archive='archive',
  208. paths=None,
  209. location_config={},
  210. storage_config={},
  211. local_borg_version='1.2.3',
  212. )
  213. def test_extract_archive_calls_borg_with_dry_run_parameter():
  214. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  215. insert_execute_command_mock(('borg', 'extract', '--dry-run', 'repo::archive'))
  216. flexmock(module.feature).should_receive('available').and_return(True)
  217. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  218. ('repo::archive',)
  219. )
  220. module.extract_archive(
  221. dry_run=True,
  222. repository='repo',
  223. archive='archive',
  224. paths=None,
  225. location_config={},
  226. storage_config={},
  227. local_borg_version='1.2.3',
  228. )
  229. def test_extract_archive_calls_borg_with_destination_path():
  230. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  231. insert_execute_command_mock(('borg', 'extract', 'repo::archive'), working_directory='/dest')
  232. flexmock(module.feature).should_receive('available').and_return(True)
  233. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  234. ('repo::archive',)
  235. )
  236. module.extract_archive(
  237. dry_run=False,
  238. repository='repo',
  239. archive='archive',
  240. paths=None,
  241. location_config={},
  242. storage_config={},
  243. local_borg_version='1.2.3',
  244. destination_path='/dest',
  245. )
  246. def test_extract_archive_calls_borg_with_strip_components():
  247. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  248. insert_execute_command_mock(('borg', 'extract', '--strip-components', '5', 'repo::archive'))
  249. flexmock(module.feature).should_receive('available').and_return(True)
  250. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  251. ('repo::archive',)
  252. )
  253. module.extract_archive(
  254. dry_run=False,
  255. repository='repo',
  256. archive='archive',
  257. paths=None,
  258. location_config={},
  259. storage_config={},
  260. local_borg_version='1.2.3',
  261. strip_components=5,
  262. )
  263. def test_extract_archive_calls_borg_with_progress_parameter():
  264. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  265. flexmock(module.environment).should_receive('make_environment')
  266. flexmock(module).should_receive('execute_command').with_args(
  267. ('borg', 'extract', '--progress', 'repo::archive'),
  268. output_file=module.DO_NOT_CAPTURE,
  269. working_directory=None,
  270. extra_environment=None,
  271. ).once()
  272. flexmock(module.feature).should_receive('available').and_return(True)
  273. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  274. ('repo::archive',)
  275. )
  276. module.extract_archive(
  277. dry_run=False,
  278. repository='repo',
  279. archive='archive',
  280. paths=None,
  281. location_config={},
  282. storage_config={},
  283. local_borg_version='1.2.3',
  284. progress=True,
  285. )
  286. def test_extract_archive_with_progress_and_extract_to_stdout_raises():
  287. flexmock(module).should_receive('execute_command').never()
  288. with pytest.raises(ValueError):
  289. module.extract_archive(
  290. dry_run=False,
  291. repository='repo',
  292. archive='archive',
  293. paths=None,
  294. location_config={},
  295. storage_config={},
  296. local_borg_version='1.2.3',
  297. progress=True,
  298. extract_to_stdout=True,
  299. )
  300. def test_extract_archive_calls_borg_with_stdout_parameter_and_returns_process():
  301. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  302. process = flexmock()
  303. flexmock(module.environment).should_receive('make_environment')
  304. flexmock(module).should_receive('execute_command').with_args(
  305. ('borg', 'extract', '--stdout', 'repo::archive'),
  306. output_file=module.subprocess.PIPE,
  307. working_directory=None,
  308. run_to_completion=False,
  309. extra_environment=None,
  310. ).and_return(process).once()
  311. flexmock(module.feature).should_receive('available').and_return(True)
  312. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  313. ('repo::archive',)
  314. )
  315. assert (
  316. module.extract_archive(
  317. dry_run=False,
  318. repository='repo',
  319. archive='archive',
  320. paths=None,
  321. location_config={},
  322. storage_config={},
  323. local_borg_version='1.2.3',
  324. extract_to_stdout=True,
  325. )
  326. == process
  327. )
  328. def test_extract_archive_skips_abspath_for_remote_repository():
  329. flexmock(module.os.path).should_receive('abspath').never()
  330. flexmock(module.environment).should_receive('make_environment')
  331. flexmock(module).should_receive('execute_command').with_args(
  332. ('borg', 'extract', 'server:repo::archive'), working_directory=None, extra_environment=None,
  333. ).once()
  334. flexmock(module.feature).should_receive('available').and_return(True)
  335. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  336. ('server:repo::archive',)
  337. )
  338. module.extract_archive(
  339. dry_run=False,
  340. repository='server:repo',
  341. archive='archive',
  342. paths=None,
  343. location_config={},
  344. storage_config={},
  345. local_borg_version='1.2.3',
  346. )