test_transfer.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.borg import transfer as module
  5. from ..test_verbosity import insert_logging_mock
  6. def test_transfer_archives_calls_borg_with_flags():
  7. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  8. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  9. flexmock(module.flags).should_receive('make_flags').and_return(())
  10. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  11. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  12. flexmock(module.environment).should_receive('make_environment')
  13. flexmock(module).should_receive('execute_command').with_args(
  14. ('borg', 'transfer', '--repo', 'repo'),
  15. output_log_level=module.borgmatic.logger.ANSWER,
  16. borg_local_path='borg',
  17. extra_environment=None,
  18. )
  19. module.transfer_archives(
  20. dry_run=False,
  21. repository='repo',
  22. storage_config={},
  23. local_borg_version='2.3.4',
  24. transfer_arguments=flexmock(archive=None, match_archives=None, source_repository=None),
  25. )
  26. def test_transfer_archives_with_dry_run_calls_borg_with_dry_run_flag():
  27. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  28. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  29. flexmock(module.flags).should_receive('make_flags').and_return(())
  30. flexmock(module.flags).should_receive('make_flags').with_args('dry-run', True).and_return(
  31. ('--dry-run',)
  32. )
  33. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  34. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  35. flexmock(module.environment).should_receive('make_environment')
  36. flexmock(module).should_receive('execute_command').with_args(
  37. ('borg', 'transfer', '--repo', 'repo', '--dry-run'),
  38. output_log_level=module.borgmatic.logger.ANSWER,
  39. borg_local_path='borg',
  40. extra_environment=None,
  41. )
  42. module.transfer_archives(
  43. dry_run=True,
  44. repository='repo',
  45. storage_config={},
  46. local_borg_version='2.3.4',
  47. transfer_arguments=flexmock(archive=None, match_archives=None, source_repository=None),
  48. )
  49. def test_transfer_archives_with_log_info_calls_borg_with_info_flag():
  50. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  51. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  52. flexmock(module.flags).should_receive('make_flags').and_return(())
  53. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  54. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  55. flexmock(module.environment).should_receive('make_environment')
  56. flexmock(module).should_receive('execute_command').with_args(
  57. ('borg', 'transfer', '--info', '--repo', 'repo'),
  58. output_log_level=module.borgmatic.logger.ANSWER,
  59. borg_local_path='borg',
  60. extra_environment=None,
  61. )
  62. insert_logging_mock(logging.INFO)
  63. module.transfer_archives(
  64. dry_run=False,
  65. repository='repo',
  66. storage_config={},
  67. local_borg_version='2.3.4',
  68. transfer_arguments=flexmock(archive=None, match_archives=None, source_repository=None),
  69. )
  70. def test_transfer_archives_with_log_debug_calls_borg_with_debug_flag():
  71. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  72. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  73. flexmock(module.flags).should_receive('make_flags').and_return(())
  74. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  75. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  76. flexmock(module.environment).should_receive('make_environment')
  77. flexmock(module).should_receive('execute_command').with_args(
  78. ('borg', 'transfer', '--debug', '--show-rc', '--repo', 'repo'),
  79. output_log_level=module.borgmatic.logger.ANSWER,
  80. borg_local_path='borg',
  81. extra_environment=None,
  82. )
  83. insert_logging_mock(logging.DEBUG)
  84. module.transfer_archives(
  85. dry_run=False,
  86. repository='repo',
  87. storage_config={},
  88. local_borg_version='2.3.4',
  89. transfer_arguments=flexmock(archive=None, match_archives=None, source_repository=None),
  90. )
  91. def test_transfer_archives_with_archive_calls_borg_with_match_archives_flag():
  92. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  93. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  94. flexmock(module.flags).should_receive('make_flags').and_return(())
  95. flexmock(module.flags).should_receive('make_flags').with_args(
  96. 'match-archives', 'archive'
  97. ).and_return(('--match-archives', 'archive'))
  98. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  99. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  100. flexmock(module.environment).should_receive('make_environment')
  101. flexmock(module).should_receive('execute_command').with_args(
  102. ('borg', 'transfer', '--match-archives', 'archive', '--repo', 'repo'),
  103. output_log_level=module.borgmatic.logger.ANSWER,
  104. borg_local_path='borg',
  105. extra_environment=None,
  106. )
  107. module.transfer_archives(
  108. dry_run=False,
  109. repository='repo',
  110. storage_config={},
  111. local_borg_version='2.3.4',
  112. transfer_arguments=flexmock(archive='archive', match_archives=None, source_repository=None),
  113. )
  114. def test_transfer_archives_with_match_archives_calls_borg_with_match_archives_flag():
  115. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  116. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  117. flexmock(module.flags).should_receive('make_flags').and_return(())
  118. flexmock(module.flags).should_receive('make_flags').with_args(
  119. 'match-archives', 'sh:foo*'
  120. ).and_return(('--match-archives', 'sh:foo*'))
  121. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  122. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  123. flexmock(module.environment).should_receive('make_environment')
  124. flexmock(module).should_receive('execute_command').with_args(
  125. ('borg', 'transfer', '--match-archives', 'sh:foo*', '--repo', 'repo'),
  126. output_log_level=module.borgmatic.logger.ANSWER,
  127. borg_local_path='borg',
  128. extra_environment=None,
  129. )
  130. module.transfer_archives(
  131. dry_run=False,
  132. repository='repo',
  133. storage_config={},
  134. local_borg_version='2.3.4',
  135. transfer_arguments=flexmock(archive=None, match_archives='sh:foo*', source_repository=None),
  136. )
  137. def test_transfer_archives_with_local_path_calls_borg_via_local_path():
  138. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  139. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  140. flexmock(module.flags).should_receive('make_flags').and_return(())
  141. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  142. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  143. flexmock(module.environment).should_receive('make_environment')
  144. flexmock(module).should_receive('execute_command').with_args(
  145. ('borg2', 'transfer', '--repo', 'repo'),
  146. output_log_level=module.borgmatic.logger.ANSWER,
  147. borg_local_path='borg2',
  148. extra_environment=None,
  149. )
  150. module.transfer_archives(
  151. dry_run=False,
  152. repository='repo',
  153. storage_config={},
  154. local_borg_version='2.3.4',
  155. transfer_arguments=flexmock(archive=None, match_archives=None, source_repository=None),
  156. local_path='borg2',
  157. )
  158. def test_transfer_archives_with_remote_path_calls_borg_with_remote_path_flags():
  159. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  160. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  161. flexmock(module.flags).should_receive('make_flags').and_return(())
  162. flexmock(module.flags).should_receive('make_flags').with_args(
  163. 'remote-path', 'borg2'
  164. ).and_return(('--remote-path', 'borg2'))
  165. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  166. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  167. flexmock(module.environment).should_receive('make_environment')
  168. flexmock(module).should_receive('execute_command').with_args(
  169. ('borg', 'transfer', '--remote-path', 'borg2', '--repo', 'repo'),
  170. output_log_level=module.borgmatic.logger.ANSWER,
  171. borg_local_path='borg',
  172. extra_environment=None,
  173. )
  174. module.transfer_archives(
  175. dry_run=False,
  176. repository='repo',
  177. storage_config={},
  178. local_borg_version='2.3.4',
  179. transfer_arguments=flexmock(archive=None, match_archives=None, source_repository=None),
  180. remote_path='borg2',
  181. )
  182. def test_transfer_archives_with_lock_wait_calls_borg_with_lock_wait_flags():
  183. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  184. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  185. flexmock(module.flags).should_receive('make_flags').and_return(())
  186. flexmock(module.flags).should_receive('make_flags').with_args('lock-wait', 5).and_return(
  187. ('--lock-wait', '5')
  188. )
  189. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  190. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  191. storage_config = {'lock_wait': 5}
  192. flexmock(module.environment).should_receive('make_environment')
  193. flexmock(module).should_receive('execute_command').with_args(
  194. ('borg', 'transfer', '--lock-wait', '5', '--repo', 'repo'),
  195. output_log_level=module.borgmatic.logger.ANSWER,
  196. borg_local_path='borg',
  197. extra_environment=None,
  198. )
  199. module.transfer_archives(
  200. dry_run=False,
  201. repository='repo',
  202. storage_config=storage_config,
  203. local_borg_version='2.3.4',
  204. transfer_arguments=flexmock(archive=None, match_archives=None, source_repository=None),
  205. )
  206. @pytest.mark.parametrize('argument_name', ('upgrader', 'sort_by', 'first', 'last'))
  207. def test_transfer_archives_passes_through_arguments_to_borg(argument_name):
  208. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  209. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  210. flag_name = f"--{argument_name.replace('_', ' ')}"
  211. flexmock(module.flags).should_receive('make_flags').and_return(())
  212. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  213. (flag_name, 'value')
  214. )
  215. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  216. flexmock(module.environment).should_receive('make_environment')
  217. flexmock(module).should_receive('execute_command').with_args(
  218. ('borg', 'transfer', flag_name, 'value', '--repo', 'repo'),
  219. output_log_level=module.borgmatic.logger.ANSWER,
  220. borg_local_path='borg',
  221. extra_environment=None,
  222. )
  223. module.transfer_archives(
  224. dry_run=False,
  225. repository='repo',
  226. storage_config={},
  227. local_borg_version='2.3.4',
  228. transfer_arguments=flexmock(
  229. archive=None, match_archives=None, source_repository=None, **{argument_name: 'value'}
  230. ),
  231. )
  232. def test_transfer_archives_with_source_repository_calls_borg_with_other_repo_flags():
  233. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  234. flexmock(module.flags).should_receive('make_flags').and_return(())
  235. flexmock(module.flags).should_receive('make_flags').with_args('other-repo', 'other').and_return(
  236. ('--other-repo', 'other')
  237. )
  238. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  239. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  240. flexmock(module.environment).should_receive('make_environment')
  241. flexmock(module).should_receive('execute_command').with_args(
  242. ('borg', 'transfer', '--repo', 'repo', '--other-repo', 'other'),
  243. output_log_level=module.borgmatic.logger.ANSWER,
  244. borg_local_path='borg',
  245. extra_environment=None,
  246. )
  247. module.transfer_archives(
  248. dry_run=False,
  249. repository='repo',
  250. storage_config={},
  251. local_borg_version='2.3.4',
  252. transfer_arguments=flexmock(archive=None, match_archives=None, source_repository='other'),
  253. )