2
0

test_transfer.py 17 KB

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