test_extract.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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).should_receive('execute_command').with_args(
  8. command, working_directory=working_directory
  9. ).once()
  10. def insert_execute_command_output_mock(command, result):
  11. flexmock(module).should_receive('execute_command').with_args(
  12. command, output_log_level=None, borg_local_path=command[0]
  13. ).and_return(result).once()
  14. def test_extract_last_archive_dry_run_calls_borg_with_last_archive():
  15. insert_execute_command_output_mock(
  16. ('borg', 'list', '--short', 'repo'), result='archive1\narchive2\n'
  17. )
  18. insert_execute_command_mock(('borg', 'extract', '--dry-run', 'repo::archive2'))
  19. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  20. def test_extract_last_archive_dry_run_without_any_archives_should_not_raise():
  21. insert_execute_command_output_mock(('borg', 'list', '--short', 'repo'), result='\n')
  22. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  23. def test_extract_last_archive_dry_run_with_log_info_calls_borg_with_info_parameter():
  24. insert_execute_command_output_mock(
  25. ('borg', 'list', '--short', '--info', 'repo'), result='archive1\narchive2\n'
  26. )
  27. insert_execute_command_mock(('borg', 'extract', '--dry-run', '--info', 'repo::archive2'))
  28. insert_logging_mock(logging.INFO)
  29. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  30. def test_extract_last_archive_dry_run_with_log_debug_calls_borg_with_debug_parameter():
  31. insert_execute_command_output_mock(
  32. ('borg', 'list', '--short', '--debug', '--show-rc', 'repo'), result='archive1\narchive2\n'
  33. )
  34. insert_execute_command_mock(
  35. ('borg', 'extract', '--dry-run', '--debug', '--show-rc', '--list', 'repo::archive2')
  36. )
  37. insert_logging_mock(logging.DEBUG)
  38. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  39. def test_extract_last_archive_dry_run_calls_borg_via_local_path():
  40. insert_execute_command_output_mock(
  41. ('borg1', 'list', '--short', 'repo'), result='archive1\narchive2\n'
  42. )
  43. insert_execute_command_mock(('borg1', 'extract', '--dry-run', 'repo::archive2'))
  44. module.extract_last_archive_dry_run(repository='repo', lock_wait=None, local_path='borg1')
  45. def test_extract_last_archive_dry_run_calls_borg_with_remote_path_parameters():
  46. insert_execute_command_output_mock(
  47. ('borg', 'list', '--short', '--remote-path', 'borg1', 'repo'), result='archive1\narchive2\n'
  48. )
  49. insert_execute_command_mock(
  50. ('borg', 'extract', '--dry-run', '--remote-path', 'borg1', 'repo::archive2')
  51. )
  52. module.extract_last_archive_dry_run(repository='repo', lock_wait=None, remote_path='borg1')
  53. def test_extract_last_archive_dry_run_calls_borg_with_lock_wait_parameters():
  54. insert_execute_command_output_mock(
  55. ('borg', 'list', '--short', '--lock-wait', '5', 'repo'), result='archive1\narchive2\n'
  56. )
  57. insert_execute_command_mock(
  58. ('borg', 'extract', '--dry-run', '--lock-wait', '5', 'repo::archive2')
  59. )
  60. module.extract_last_archive_dry_run(repository='repo', lock_wait=5)
  61. def test_extract_archive_calls_borg_with_path_parameters():
  62. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  63. insert_execute_command_mock(('borg', 'extract', 'repo::archive', 'path1', 'path2'))
  64. module.extract_archive(
  65. dry_run=False,
  66. repository='repo',
  67. archive='archive',
  68. paths=['path1', 'path2'],
  69. location_config={},
  70. storage_config={},
  71. )
  72. def test_extract_archive_calls_borg_with_remote_path_parameters():
  73. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  74. insert_execute_command_mock(('borg', 'extract', '--remote-path', 'borg1', 'repo::archive'))
  75. module.extract_archive(
  76. dry_run=False,
  77. repository='repo',
  78. archive='archive',
  79. paths=None,
  80. location_config={},
  81. storage_config={},
  82. remote_path='borg1',
  83. )
  84. def test_extract_archive_calls_borg_with_numeric_owner_parameter():
  85. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  86. insert_execute_command_mock(('borg', 'extract', '--numeric-owner', 'repo::archive'))
  87. module.extract_archive(
  88. dry_run=False,
  89. repository='repo',
  90. archive='archive',
  91. paths=None,
  92. location_config={'numeric_owner': True},
  93. storage_config={},
  94. )
  95. def test_extract_archive_calls_borg_with_umask_parameters():
  96. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  97. insert_execute_command_mock(('borg', 'extract', '--umask', '0770', 'repo::archive'))
  98. module.extract_archive(
  99. dry_run=False,
  100. repository='repo',
  101. archive='archive',
  102. paths=None,
  103. location_config={},
  104. storage_config={'umask': '0770'},
  105. )
  106. def test_extract_archive_calls_borg_with_lock_wait_parameters():
  107. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  108. insert_execute_command_mock(('borg', 'extract', '--lock-wait', '5', 'repo::archive'))
  109. module.extract_archive(
  110. dry_run=False,
  111. repository='repo',
  112. archive='archive',
  113. paths=None,
  114. location_config={},
  115. storage_config={'lock_wait': '5'},
  116. )
  117. def test_extract_archive_with_log_info_calls_borg_with_info_parameter():
  118. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  119. insert_execute_command_mock(('borg', 'extract', '--info', 'repo::archive'))
  120. insert_logging_mock(logging.INFO)
  121. module.extract_archive(
  122. dry_run=False,
  123. repository='repo',
  124. archive='archive',
  125. paths=None,
  126. location_config={},
  127. storage_config={},
  128. )
  129. def test_extract_archive_with_log_debug_calls_borg_with_debug_parameters():
  130. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  131. insert_execute_command_mock(
  132. ('borg', 'extract', '--debug', '--list', '--show-rc', 'repo::archive')
  133. )
  134. insert_logging_mock(logging.DEBUG)
  135. module.extract_archive(
  136. dry_run=False,
  137. repository='repo',
  138. archive='archive',
  139. paths=None,
  140. location_config={},
  141. storage_config={},
  142. )
  143. def test_extract_archive_calls_borg_with_dry_run_parameter():
  144. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  145. insert_execute_command_mock(('borg', 'extract', '--dry-run', 'repo::archive'))
  146. module.extract_archive(
  147. dry_run=True,
  148. repository='repo',
  149. archive='archive',
  150. paths=None,
  151. location_config={},
  152. storage_config={},
  153. )
  154. def test_extract_archive_calls_borg_with_destination_path():
  155. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  156. insert_execute_command_mock(('borg', 'extract', 'repo::archive'), working_directory='/dest')
  157. module.extract_archive(
  158. dry_run=False,
  159. repository='repo',
  160. archive='archive',
  161. paths=None,
  162. location_config={},
  163. storage_config={},
  164. destination_path='/dest',
  165. )
  166. def test_extract_archive_calls_borg_with_strip_components():
  167. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  168. insert_execute_command_mock(('borg', 'extract', '--strip-components', '5', 'repo::archive'))
  169. module.extract_archive(
  170. dry_run=False,
  171. repository='repo',
  172. archive='archive',
  173. paths=None,
  174. location_config={},
  175. storage_config={},
  176. strip_components=5,
  177. )
  178. def test_extract_archive_calls_borg_with_progress_parameter():
  179. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  180. flexmock(module).should_receive('execute_command').with_args(
  181. ('borg', 'extract', '--progress', 'repo::archive'),
  182. output_file=module.DO_NOT_CAPTURE,
  183. working_directory=None,
  184. ).once()
  185. module.extract_archive(
  186. dry_run=False,
  187. repository='repo',
  188. archive='archive',
  189. paths=None,
  190. location_config={},
  191. storage_config={},
  192. progress=True,
  193. )
  194. def test_extract_archive_with_progress_and_extract_to_stdout_raises():
  195. flexmock(module).should_receive('execute_command').never()
  196. with pytest.raises(ValueError):
  197. module.extract_archive(
  198. dry_run=False,
  199. repository='repo',
  200. archive='archive',
  201. paths=None,
  202. location_config={},
  203. storage_config={},
  204. progress=True,
  205. extract_to_stdout=True,
  206. )
  207. def test_extract_archive_calls_borg_with_stdout_parameter_and_returns_process():
  208. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  209. process = flexmock()
  210. flexmock(module).should_receive('execute_command').with_args(
  211. ('borg', 'extract', '--stdout', 'repo::archive'),
  212. output_file=module.subprocess.PIPE,
  213. working_directory=None,
  214. run_to_completion=False,
  215. ).and_return(process).once()
  216. assert (
  217. module.extract_archive(
  218. dry_run=False,
  219. repository='repo',
  220. archive='archive',
  221. paths=None,
  222. location_config={},
  223. storage_config={},
  224. extract_to_stdout=True,
  225. )
  226. == process
  227. )
  228. def test_extract_archive_skips_abspath_for_remote_repository():
  229. flexmock(module.os.path).should_receive('abspath').never()
  230. flexmock(module).should_receive('execute_command').with_args(
  231. ('borg', 'extract', 'server:repo::archive'), working_directory=None
  232. ).once()
  233. module.extract_archive(
  234. dry_run=False,
  235. repository='server:repo',
  236. archive='archive',
  237. paths=None,
  238. location_config={},
  239. storage_config={},
  240. )