test_transfer.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. output_file=None,
  17. borg_local_path='borg',
  18. extra_environment=None,
  19. )
  20. module.transfer_archives(
  21. dry_run=False,
  22. repository_path='repo',
  23. storage_config={},
  24. local_borg_version='2.3.4',
  25. transfer_arguments=flexmock(
  26. archive=None, progress=None, match_archives=None, source_repository=None
  27. ),
  28. )
  29. def test_transfer_archives_with_dry_run_calls_borg_with_dry_run_flag():
  30. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  31. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  32. flexmock(module.flags).should_receive('make_flags').and_return(())
  33. flexmock(module.flags).should_receive('make_flags').with_args('dry-run', True).and_return(
  34. ('--dry-run',)
  35. )
  36. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  37. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  38. flexmock(module.environment).should_receive('make_environment')
  39. flexmock(module).should_receive('execute_command').with_args(
  40. ('borg', 'transfer', '--repo', 'repo', '--dry-run'),
  41. output_log_level=module.borgmatic.logger.ANSWER,
  42. output_file=None,
  43. borg_local_path='borg',
  44. extra_environment=None,
  45. )
  46. module.transfer_archives(
  47. dry_run=True,
  48. repository_path='repo',
  49. storage_config={},
  50. local_borg_version='2.3.4',
  51. transfer_arguments=flexmock(
  52. archive=None, progress=None, match_archives=None, source_repository=None
  53. ),
  54. )
  55. def test_transfer_archives_with_log_info_calls_borg_with_info_flag():
  56. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  57. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  58. flexmock(module.flags).should_receive('make_flags').and_return(())
  59. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  60. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  61. flexmock(module.environment).should_receive('make_environment')
  62. flexmock(module).should_receive('execute_command').with_args(
  63. ('borg', 'transfer', '--info', '--repo', 'repo'),
  64. output_log_level=module.borgmatic.logger.ANSWER,
  65. output_file=None,
  66. borg_local_path='borg',
  67. extra_environment=None,
  68. )
  69. insert_logging_mock(logging.INFO)
  70. module.transfer_archives(
  71. dry_run=False,
  72. repository_path='repo',
  73. storage_config={},
  74. local_borg_version='2.3.4',
  75. transfer_arguments=flexmock(
  76. archive=None, progress=None, match_archives=None, source_repository=None
  77. ),
  78. )
  79. def test_transfer_archives_with_log_debug_calls_borg_with_debug_flag():
  80. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  81. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  82. flexmock(module.flags).should_receive('make_flags').and_return(())
  83. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  84. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  85. flexmock(module.environment).should_receive('make_environment')
  86. flexmock(module).should_receive('execute_command').with_args(
  87. ('borg', 'transfer', '--debug', '--show-rc', '--repo', 'repo'),
  88. output_log_level=module.borgmatic.logger.ANSWER,
  89. output_file=None,
  90. borg_local_path='borg',
  91. extra_environment=None,
  92. )
  93. insert_logging_mock(logging.DEBUG)
  94. module.transfer_archives(
  95. dry_run=False,
  96. repository_path='repo',
  97. storage_config={},
  98. local_borg_version='2.3.4',
  99. transfer_arguments=flexmock(
  100. archive=None, progress=None, match_archives=None, source_repository=None
  101. ),
  102. )
  103. def test_transfer_archives_with_archive_calls_borg_with_match_archives_flag():
  104. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  105. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  106. flexmock(module.flags).should_receive('make_flags').and_return(())
  107. flexmock(module.flags).should_receive('make_flags').with_args(
  108. 'match-archives', 'archive'
  109. ).and_return(('--match-archives', 'archive'))
  110. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  111. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  112. flexmock(module.environment).should_receive('make_environment')
  113. flexmock(module).should_receive('execute_command').with_args(
  114. ('borg', 'transfer', '--match-archives', 'archive', '--repo', 'repo'),
  115. output_log_level=module.borgmatic.logger.ANSWER,
  116. output_file=None,
  117. borg_local_path='borg',
  118. extra_environment=None,
  119. )
  120. module.transfer_archives(
  121. dry_run=False,
  122. repository_path='repo',
  123. storage_config={},
  124. local_borg_version='2.3.4',
  125. transfer_arguments=flexmock(
  126. archive='archive', progress=None, match_archives=None, source_repository=None
  127. ),
  128. )
  129. def test_transfer_archives_with_match_archives_calls_borg_with_match_archives_flag():
  130. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  131. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  132. flexmock(module.flags).should_receive('make_flags').and_return(())
  133. flexmock(module.flags).should_receive('make_flags').with_args(
  134. 'match-archives', 'sh:foo*'
  135. ).and_return(('--match-archives', 'sh:foo*'))
  136. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  137. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  138. flexmock(module.environment).should_receive('make_environment')
  139. flexmock(module).should_receive('execute_command').with_args(
  140. ('borg', 'transfer', '--match-archives', 'sh:foo*', '--repo', 'repo'),
  141. output_log_level=module.borgmatic.logger.ANSWER,
  142. output_file=None,
  143. borg_local_path='borg',
  144. extra_environment=None,
  145. )
  146. module.transfer_archives(
  147. dry_run=False,
  148. repository_path='repo',
  149. storage_config={},
  150. local_borg_version='2.3.4',
  151. transfer_arguments=flexmock(
  152. archive=None, progress=None, match_archives='sh:foo*', source_repository=None
  153. ),
  154. )
  155. def test_transfer_archives_with_local_path_calls_borg_via_local_path():
  156. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  157. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  158. flexmock(module.flags).should_receive('make_flags').and_return(())
  159. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  160. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  161. flexmock(module.environment).should_receive('make_environment')
  162. flexmock(module).should_receive('execute_command').with_args(
  163. ('borg2', 'transfer', '--repo', 'repo'),
  164. output_log_level=module.borgmatic.logger.ANSWER,
  165. output_file=None,
  166. borg_local_path='borg2',
  167. extra_environment=None,
  168. )
  169. module.transfer_archives(
  170. dry_run=False,
  171. repository_path='repo',
  172. storage_config={},
  173. local_borg_version='2.3.4',
  174. transfer_arguments=flexmock(
  175. archive=None, progress=None, match_archives=None, source_repository=None
  176. ),
  177. local_path='borg2',
  178. )
  179. def test_transfer_archives_with_remote_path_calls_borg_with_remote_path_flags():
  180. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  181. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  182. flexmock(module.flags).should_receive('make_flags').and_return(())
  183. flexmock(module.flags).should_receive('make_flags').with_args(
  184. 'remote-path', 'borg2'
  185. ).and_return(('--remote-path', 'borg2'))
  186. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  187. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  188. flexmock(module.environment).should_receive('make_environment')
  189. flexmock(module).should_receive('execute_command').with_args(
  190. ('borg', 'transfer', '--remote-path', 'borg2', '--repo', 'repo'),
  191. output_log_level=module.borgmatic.logger.ANSWER,
  192. output_file=None,
  193. borg_local_path='borg',
  194. extra_environment=None,
  195. )
  196. module.transfer_archives(
  197. dry_run=False,
  198. repository_path='repo',
  199. storage_config={},
  200. local_borg_version='2.3.4',
  201. transfer_arguments=flexmock(
  202. archive=None, progress=None, match_archives=None, source_repository=None
  203. ),
  204. remote_path='borg2',
  205. )
  206. def test_transfer_archives_with_lock_wait_calls_borg_with_lock_wait_flags():
  207. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  208. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  209. flexmock(module.flags).should_receive('make_flags').and_return(())
  210. flexmock(module.flags).should_receive('make_flags').with_args('lock-wait', 5).and_return(
  211. ('--lock-wait', '5')
  212. )
  213. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  214. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  215. storage_config = {'lock_wait': 5}
  216. flexmock(module.environment).should_receive('make_environment')
  217. flexmock(module).should_receive('execute_command').with_args(
  218. ('borg', 'transfer', '--lock-wait', '5', '--repo', 'repo'),
  219. output_log_level=module.borgmatic.logger.ANSWER,
  220. output_file=None,
  221. borg_local_path='borg',
  222. extra_environment=None,
  223. )
  224. module.transfer_archives(
  225. dry_run=False,
  226. repository_path='repo',
  227. storage_config=storage_config,
  228. local_borg_version='2.3.4',
  229. transfer_arguments=flexmock(
  230. archive=None, progress=None, match_archives=None, source_repository=None
  231. ),
  232. )
  233. def test_transfer_archives_with_progress_calls_borg_with_progress_flag():
  234. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  235. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  236. flexmock(module.flags).should_receive('make_flags').and_return(())
  237. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  238. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  239. flexmock(module.environment).should_receive('make_environment')
  240. flexmock(module).should_receive('execute_command').with_args(
  241. ('borg', 'transfer', '--progress', '--repo', 'repo'),
  242. output_log_level=module.borgmatic.logger.ANSWER,
  243. output_file=module.DO_NOT_CAPTURE,
  244. borg_local_path='borg',
  245. extra_environment=None,
  246. )
  247. module.transfer_archives(
  248. dry_run=False,
  249. repository_path='repo',
  250. storage_config={},
  251. local_borg_version='2.3.4',
  252. transfer_arguments=flexmock(
  253. archive=None, progress=True, match_archives=None, source_repository=None
  254. ),
  255. )
  256. @pytest.mark.parametrize('argument_name', ('upgrader', 'sort_by', 'first', 'last'))
  257. def test_transfer_archives_passes_through_arguments_to_borg(argument_name):
  258. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  259. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  260. flag_name = f"--{argument_name.replace('_', ' ')}"
  261. flexmock(module.flags).should_receive('make_flags').and_return(())
  262. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  263. (flag_name, 'value')
  264. )
  265. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  266. flexmock(module.environment).should_receive('make_environment')
  267. flexmock(module).should_receive('execute_command').with_args(
  268. ('borg', 'transfer', flag_name, 'value', '--repo', 'repo'),
  269. output_log_level=module.borgmatic.logger.ANSWER,
  270. output_file=None,
  271. borg_local_path='borg',
  272. extra_environment=None,
  273. )
  274. module.transfer_archives(
  275. dry_run=False,
  276. repository_path='repo',
  277. storage_config={},
  278. local_borg_version='2.3.4',
  279. transfer_arguments=flexmock(
  280. archive=None,
  281. progress=None,
  282. match_archives=None,
  283. source_repository=None,
  284. **{argument_name: 'value'},
  285. ),
  286. )
  287. def test_transfer_archives_with_source_repository_calls_borg_with_other_repo_flags():
  288. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  289. flexmock(module.flags).should_receive('make_flags').and_return(())
  290. flexmock(module.flags).should_receive('make_flags').with_args('other-repo', 'other').and_return(
  291. ('--other-repo', 'other')
  292. )
  293. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  294. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  295. flexmock(module.environment).should_receive('make_environment')
  296. flexmock(module).should_receive('execute_command').with_args(
  297. ('borg', 'transfer', '--repo', 'repo', '--other-repo', 'other'),
  298. output_log_level=module.borgmatic.logger.ANSWER,
  299. output_file=None,
  300. borg_local_path='borg',
  301. extra_environment=None,
  302. )
  303. module.transfer_archives(
  304. dry_run=False,
  305. repository_path='repo',
  306. storage_config={},
  307. local_borg_version='2.3.4',
  308. transfer_arguments=flexmock(
  309. archive=None, progress=None, match_archives=None, source_repository='other'
  310. ),
  311. )