2
0

test_extract.py 12 KB

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