test_transfer.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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_flags').with_args(
  112. 'match-archives', 'archive'
  113. ).and_return(('--match-archives', 'archive'))
  114. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  115. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  116. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  117. flexmock(module.environment).should_receive('make_environment')
  118. flexmock(module).should_receive('execute_command').with_args(
  119. ('borg', 'transfer', '--match-archives', 'archive', '--repo', 'repo'),
  120. output_log_level=module.borgmatic.logger.ANSWER,
  121. output_file=None,
  122. borg_local_path='borg',
  123. extra_environment=None,
  124. )
  125. module.transfer_archives(
  126. dry_run=False,
  127. repository_path='repo',
  128. storage_config={'archive_name_format': 'bar-{now}'}, # noqa: FS003
  129. local_borg_version='2.3.4',
  130. transfer_arguments=flexmock(
  131. archive='archive', progress=None, match_archives=None, source_repository=None
  132. ),
  133. )
  134. def test_transfer_archives_with_match_archives_calls_borg_with_match_archives_flag():
  135. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  136. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  137. flexmock(module.flags).should_receive('make_flags').and_return(())
  138. flexmock(module.flags).should_receive('make_flags').with_args(
  139. 'match-archives', 'sh:foo*'
  140. ).and_return(('--match-archives', 'sh:foo*'))
  141. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  142. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  143. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  144. flexmock(module.environment).should_receive('make_environment')
  145. flexmock(module).should_receive('execute_command').with_args(
  146. ('borg', 'transfer', '--match-archives', 'sh:foo*', '--repo', 'repo'),
  147. output_log_level=module.borgmatic.logger.ANSWER,
  148. output_file=None,
  149. borg_local_path='borg',
  150. extra_environment=None,
  151. )
  152. module.transfer_archives(
  153. dry_run=False,
  154. repository_path='repo',
  155. storage_config={'archive_name_format': 'bar-{now}'}, # noqa: FS003
  156. local_borg_version='2.3.4',
  157. transfer_arguments=flexmock(
  158. archive=None, progress=None, match_archives='sh:foo*', source_repository=None
  159. ),
  160. )
  161. def test_transfer_archives_with_archive_name_format_calls_borg_with_match_archives_flag():
  162. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  163. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  164. flexmock(module.flags).should_receive('make_flags').and_return(())
  165. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  166. None, 'bar-{now}', '2.3.4' # noqa: FS003
  167. ).and_return(('--match-archives', 'sh:bar-*'))
  168. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  169. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  170. flexmock(module.environment).should_receive('make_environment')
  171. flexmock(module).should_receive('execute_command').with_args(
  172. ('borg', 'transfer', '--match-archives', 'sh:bar-*', '--repo', 'repo'),
  173. output_log_level=module.borgmatic.logger.ANSWER,
  174. output_file=None,
  175. borg_local_path='borg',
  176. extra_environment=None,
  177. )
  178. module.transfer_archives(
  179. dry_run=False,
  180. repository_path='repo',
  181. storage_config={'archive_name_format': 'bar-{now}'}, # noqa: FS003
  182. local_borg_version='2.3.4',
  183. transfer_arguments=flexmock(
  184. archive=None, progress=None, match_archives=None, source_repository=None
  185. ),
  186. )
  187. def test_transfer_archives_with_local_path_calls_borg_via_local_path():
  188. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  189. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  190. flexmock(module.flags).should_receive('make_flags').and_return(())
  191. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  192. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  193. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  194. flexmock(module.environment).should_receive('make_environment')
  195. flexmock(module).should_receive('execute_command').with_args(
  196. ('borg2', 'transfer', '--repo', 'repo'),
  197. output_log_level=module.borgmatic.logger.ANSWER,
  198. output_file=None,
  199. borg_local_path='borg2',
  200. extra_environment=None,
  201. )
  202. module.transfer_archives(
  203. dry_run=False,
  204. repository_path='repo',
  205. storage_config={},
  206. local_borg_version='2.3.4',
  207. transfer_arguments=flexmock(
  208. archive=None, progress=None, match_archives=None, source_repository=None
  209. ),
  210. local_path='borg2',
  211. )
  212. def test_transfer_archives_with_remote_path_calls_borg_with_remote_path_flags():
  213. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  214. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  215. flexmock(module.flags).should_receive('make_flags').and_return(())
  216. flexmock(module.flags).should_receive('make_flags').with_args(
  217. 'remote-path', 'borg2'
  218. ).and_return(('--remote-path', 'borg2'))
  219. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  220. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  221. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  222. flexmock(module.environment).should_receive('make_environment')
  223. flexmock(module).should_receive('execute_command').with_args(
  224. ('borg', 'transfer', '--remote-path', 'borg2', '--repo', 'repo'),
  225. output_log_level=module.borgmatic.logger.ANSWER,
  226. output_file=None,
  227. borg_local_path='borg',
  228. extra_environment=None,
  229. )
  230. module.transfer_archives(
  231. dry_run=False,
  232. repository_path='repo',
  233. storage_config={},
  234. local_borg_version='2.3.4',
  235. transfer_arguments=flexmock(
  236. archive=None, progress=None, match_archives=None, source_repository=None
  237. ),
  238. remote_path='borg2',
  239. )
  240. def test_transfer_archives_with_lock_wait_calls_borg_with_lock_wait_flags():
  241. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  242. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  243. flexmock(module.flags).should_receive('make_flags').and_return(())
  244. flexmock(module.flags).should_receive('make_flags').with_args('lock-wait', 5).and_return(
  245. ('--lock-wait', '5')
  246. )
  247. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  248. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  249. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  250. storage_config = {'lock_wait': 5}
  251. flexmock(module.environment).should_receive('make_environment')
  252. flexmock(module).should_receive('execute_command').with_args(
  253. ('borg', 'transfer', '--lock-wait', '5', '--repo', 'repo'),
  254. output_log_level=module.borgmatic.logger.ANSWER,
  255. output_file=None,
  256. borg_local_path='borg',
  257. extra_environment=None,
  258. )
  259. module.transfer_archives(
  260. dry_run=False,
  261. repository_path='repo',
  262. storage_config=storage_config,
  263. local_borg_version='2.3.4',
  264. transfer_arguments=flexmock(
  265. archive=None, progress=None, match_archives=None, source_repository=None
  266. ),
  267. )
  268. def test_transfer_archives_with_progress_calls_borg_with_progress_flag():
  269. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  270. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  271. flexmock(module.flags).should_receive('make_flags').and_return(())
  272. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  273. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  274. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  275. flexmock(module.environment).should_receive('make_environment')
  276. flexmock(module).should_receive('execute_command').with_args(
  277. ('borg', 'transfer', '--progress', '--repo', 'repo'),
  278. output_log_level=module.borgmatic.logger.ANSWER,
  279. output_file=module.DO_NOT_CAPTURE,
  280. borg_local_path='borg',
  281. extra_environment=None,
  282. )
  283. module.transfer_archives(
  284. dry_run=False,
  285. repository_path='repo',
  286. storage_config={},
  287. local_borg_version='2.3.4',
  288. transfer_arguments=flexmock(
  289. archive=None, progress=True, match_archives=None, source_repository=None
  290. ),
  291. )
  292. @pytest.mark.parametrize('argument_name', ('upgrader', 'sort_by', 'first', 'last'))
  293. def test_transfer_archives_passes_through_arguments_to_borg(argument_name):
  294. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  295. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  296. flag_name = f"--{argument_name.replace('_', ' ')}"
  297. flexmock(module.flags).should_receive('make_flags').and_return(())
  298. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  299. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  300. (flag_name, 'value')
  301. )
  302. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  303. flexmock(module.environment).should_receive('make_environment')
  304. flexmock(module).should_receive('execute_command').with_args(
  305. ('borg', 'transfer', flag_name, 'value', '--repo', 'repo'),
  306. output_log_level=module.borgmatic.logger.ANSWER,
  307. output_file=None,
  308. borg_local_path='borg',
  309. extra_environment=None,
  310. )
  311. module.transfer_archives(
  312. dry_run=False,
  313. repository_path='repo',
  314. storage_config={},
  315. local_borg_version='2.3.4',
  316. transfer_arguments=flexmock(
  317. archive=None,
  318. progress=None,
  319. match_archives=None,
  320. source_repository=None,
  321. **{argument_name: 'value'},
  322. ),
  323. )
  324. def test_transfer_archives_with_source_repository_calls_borg_with_other_repo_flags():
  325. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  326. flexmock(module.flags).should_receive('make_flags').and_return(())
  327. flexmock(module.flags).should_receive('make_flags').with_args('other-repo', 'other').and_return(
  328. ('--other-repo', 'other')
  329. )
  330. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  331. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  332. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  333. flexmock(module.environment).should_receive('make_environment')
  334. flexmock(module).should_receive('execute_command').with_args(
  335. ('borg', 'transfer', '--repo', 'repo', '--other-repo', 'other'),
  336. output_log_level=module.borgmatic.logger.ANSWER,
  337. output_file=None,
  338. borg_local_path='borg',
  339. extra_environment=None,
  340. )
  341. module.transfer_archives(
  342. dry_run=False,
  343. repository_path='repo',
  344. storage_config={},
  345. local_borg_version='2.3.4',
  346. transfer_arguments=flexmock(
  347. archive=None, progress=None, match_archives=None, source_repository='other'
  348. ),
  349. )