test_info.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.borg import info as module
  5. from ..test_verbosity import insert_logging_mock
  6. def test_display_archives_info_calls_borg_with_parameters():
  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').with_args(
  11. None, None, '2.3.4'
  12. ).and_return(())
  13. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  14. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  15. flexmock(module.environment).should_receive('make_environment')
  16. flexmock(module).should_receive('execute_command').with_args(
  17. ('borg', 'info', '--repo', 'repo'),
  18. output_log_level=module.borgmatic.logger.ANSWER,
  19. borg_local_path='borg',
  20. extra_environment=None,
  21. )
  22. module.display_archives_info(
  23. repository_path='repo',
  24. storage_config={},
  25. local_borg_version='2.3.4',
  26. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  27. )
  28. def test_display_archives_info_with_log_info_calls_borg_with_info_parameter():
  29. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  30. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  31. flexmock(module.flags).should_receive('make_flags').and_return(())
  32. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  33. None, None, '2.3.4'
  34. ).and_return(())
  35. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  36. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  37. flexmock(module.environment).should_receive('make_environment')
  38. flexmock(module).should_receive('execute_command').with_args(
  39. ('borg', 'info', '--info', '--repo', 'repo'),
  40. output_log_level=module.borgmatic.logger.ANSWER,
  41. borg_local_path='borg',
  42. extra_environment=None,
  43. )
  44. insert_logging_mock(logging.INFO)
  45. module.display_archives_info(
  46. repository_path='repo',
  47. storage_config={},
  48. local_borg_version='2.3.4',
  49. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  50. )
  51. def test_display_archives_info_with_log_info_and_json_suppresses_most_borg_output():
  52. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  53. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  54. flexmock(module.flags).should_receive('make_flags').and_return(())
  55. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  56. None, None, '2.3.4'
  57. ).and_return(())
  58. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  59. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  60. flexmock(module.environment).should_receive('make_environment')
  61. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  62. ('borg', 'info', '--json', '--repo', 'repo'), extra_environment=None,
  63. ).and_return('[]')
  64. insert_logging_mock(logging.INFO)
  65. json_output = module.display_archives_info(
  66. repository_path='repo',
  67. storage_config={},
  68. local_borg_version='2.3.4',
  69. info_arguments=flexmock(archive=None, json=True, prefix=None, match_archives=None),
  70. )
  71. assert json_output == '[]'
  72. def test_display_archives_info_with_log_debug_calls_borg_with_debug_parameter():
  73. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  74. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  75. flexmock(module.flags).should_receive('make_flags').and_return(())
  76. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  77. None, None, '2.3.4'
  78. ).and_return(())
  79. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  80. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  81. flexmock(module.environment).should_receive('make_environment')
  82. flexmock(module).should_receive('execute_command').with_args(
  83. ('borg', 'info', '--debug', '--show-rc', '--repo', 'repo'),
  84. output_log_level=module.borgmatic.logger.ANSWER,
  85. borg_local_path='borg',
  86. extra_environment=None,
  87. )
  88. insert_logging_mock(logging.DEBUG)
  89. module.display_archives_info(
  90. repository_path='repo',
  91. storage_config={},
  92. local_borg_version='2.3.4',
  93. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  94. )
  95. def test_display_archives_info_with_log_debug_and_json_suppresses_most_borg_output():
  96. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  97. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  98. flexmock(module.flags).should_receive('make_flags').and_return(())
  99. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  100. None, None, '2.3.4'
  101. ).and_return(())
  102. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  103. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  104. flexmock(module.environment).should_receive('make_environment')
  105. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  106. ('borg', 'info', '--json', '--repo', 'repo'), extra_environment=None,
  107. ).and_return('[]')
  108. insert_logging_mock(logging.DEBUG)
  109. json_output = module.display_archives_info(
  110. repository_path='repo',
  111. storage_config={},
  112. local_borg_version='2.3.4',
  113. info_arguments=flexmock(archive=None, json=True, prefix=None, match_archives=None),
  114. )
  115. assert json_output == '[]'
  116. def test_display_archives_info_with_json_calls_borg_with_json_parameter():
  117. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  118. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  119. flexmock(module.flags).should_receive('make_flags').and_return(())
  120. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  121. None, None, '2.3.4'
  122. ).and_return(())
  123. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  124. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  125. flexmock(module.environment).should_receive('make_environment')
  126. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  127. ('borg', 'info', '--json', '--repo', 'repo'), extra_environment=None,
  128. ).and_return('[]')
  129. json_output = module.display_archives_info(
  130. repository_path='repo',
  131. storage_config={},
  132. local_borg_version='2.3.4',
  133. info_arguments=flexmock(archive=None, json=True, prefix=None, match_archives=None),
  134. )
  135. assert json_output == '[]'
  136. def test_display_archives_info_with_archive_calls_borg_with_match_archives_parameter():
  137. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  138. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  139. flexmock(module.flags).should_receive('make_flags').and_return(())
  140. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  141. 'archive', None, '2.3.4'
  142. ).and_return(('--match-archives', 'archive'))
  143. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  144. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  145. flexmock(module.environment).should_receive('make_environment')
  146. flexmock(module).should_receive('execute_command').with_args(
  147. ('borg', 'info', '--match-archives', 'archive', '--repo', 'repo'),
  148. output_log_level=module.borgmatic.logger.ANSWER,
  149. borg_local_path='borg',
  150. extra_environment=None,
  151. )
  152. module.display_archives_info(
  153. repository_path='repo',
  154. storage_config={},
  155. local_borg_version='2.3.4',
  156. info_arguments=flexmock(archive='archive', json=False, prefix=None, match_archives=None),
  157. )
  158. def test_display_archives_info_with_local_path_calls_borg_via_local_path():
  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_match_archives_flags').with_args(
  163. None, None, '2.3.4'
  164. ).and_return(())
  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. ('borg1', 'info', '--repo', 'repo'),
  170. output_log_level=module.borgmatic.logger.ANSWER,
  171. borg_local_path='borg1',
  172. extra_environment=None,
  173. )
  174. module.display_archives_info(
  175. repository_path='repo',
  176. storage_config={},
  177. local_borg_version='2.3.4',
  178. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  179. local_path='borg1',
  180. )
  181. def test_display_archives_info_with_remote_path_calls_borg_with_remote_path_parameters():
  182. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  183. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  184. flexmock(module.flags).should_receive('make_flags').and_return(())
  185. flexmock(module.flags).should_receive('make_flags').with_args(
  186. 'remote-path', 'borg1'
  187. ).and_return(('--remote-path', 'borg1'))
  188. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  189. None, None, '2.3.4'
  190. ).and_return(())
  191. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  192. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  193. flexmock(module.environment).should_receive('make_environment')
  194. flexmock(module).should_receive('execute_command').with_args(
  195. ('borg', 'info', '--remote-path', 'borg1', '--repo', 'repo'),
  196. output_log_level=module.borgmatic.logger.ANSWER,
  197. borg_local_path='borg',
  198. extra_environment=None,
  199. )
  200. module.display_archives_info(
  201. repository_path='repo',
  202. storage_config={},
  203. local_borg_version='2.3.4',
  204. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  205. remote_path='borg1',
  206. )
  207. def test_display_archives_info_with_lock_wait_calls_borg_with_lock_wait_parameters():
  208. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  209. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  210. flexmock(module.flags).should_receive('make_flags').and_return(())
  211. flexmock(module.flags).should_receive('make_flags').with_args('lock-wait', 5).and_return(
  212. ('--lock-wait', '5')
  213. )
  214. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  215. None, None, '2.3.4'
  216. ).and_return(())
  217. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  218. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  219. storage_config = {'lock_wait': 5}
  220. flexmock(module.environment).should_receive('make_environment')
  221. flexmock(module).should_receive('execute_command').with_args(
  222. ('borg', 'info', '--lock-wait', '5', '--repo', 'repo'),
  223. output_log_level=module.borgmatic.logger.ANSWER,
  224. borg_local_path='borg',
  225. extra_environment=None,
  226. )
  227. module.display_archives_info(
  228. repository_path='repo',
  229. storage_config=storage_config,
  230. local_borg_version='2.3.4',
  231. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  232. )
  233. def test_display_archives_info_transforms_prefix_into_match_archives_parameters():
  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').with_args(
  238. 'match-archives', 'sh:foo*'
  239. ).and_return(('--match-archives', 'sh:foo*'))
  240. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  241. None, None, '2.3.4'
  242. ).and_return(())
  243. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  244. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  245. flexmock(module.environment).should_receive('make_environment')
  246. flexmock(module).should_receive('execute_command').with_args(
  247. ('borg', 'info', '--match-archives', 'sh:foo*', '--repo', 'repo'),
  248. output_log_level=module.borgmatic.logger.ANSWER,
  249. borg_local_path='borg',
  250. extra_environment=None,
  251. )
  252. module.display_archives_info(
  253. repository_path='repo',
  254. storage_config={},
  255. local_borg_version='2.3.4',
  256. info_arguments=flexmock(archive=None, json=False, prefix='foo'),
  257. )
  258. def test_display_archives_info_prefers_prefix_over_archive_name_format():
  259. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  260. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  261. flexmock(module.flags).should_receive('make_flags').and_return(())
  262. flexmock(module.flags).should_receive('make_flags').with_args(
  263. 'match-archives', 'sh:foo*'
  264. ).and_return(('--match-archives', 'sh:foo*'))
  265. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  266. None, None, '2.3.4'
  267. ).and_return(())
  268. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  269. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  270. flexmock(module.environment).should_receive('make_environment')
  271. flexmock(module).should_receive('execute_command').with_args(
  272. ('borg', 'info', '--match-archives', 'sh:foo*', '--repo', 'repo'),
  273. output_log_level=module.borgmatic.logger.ANSWER,
  274. borg_local_path='borg',
  275. extra_environment=None,
  276. )
  277. module.display_archives_info(
  278. repository_path='repo',
  279. storage_config={'archive_name_format': 'bar-{now}'}, # noqa: FS003
  280. local_borg_version='2.3.4',
  281. info_arguments=flexmock(archive=None, json=False, prefix='foo'),
  282. )
  283. def test_display_archives_info_transforms_archive_name_format_into_match_archives_parameters():
  284. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  285. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  286. flexmock(module.flags).should_receive('make_flags').and_return(())
  287. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  288. None, 'bar-{now}', '2.3.4' # noqa: FS003
  289. ).and_return(('--match-archives', 'sh:bar-*'))
  290. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  291. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  292. flexmock(module.environment).should_receive('make_environment')
  293. flexmock(module).should_receive('execute_command').with_args(
  294. ('borg', 'info', '--match-archives', 'sh:bar-*', '--repo', 'repo'),
  295. output_log_level=module.borgmatic.logger.ANSWER,
  296. borg_local_path='borg',
  297. extra_environment=None,
  298. )
  299. module.display_archives_info(
  300. repository_path='repo',
  301. storage_config={'archive_name_format': 'bar-{now}'}, # noqa: FS003
  302. local_borg_version='2.3.4',
  303. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  304. )
  305. def test_display_archives_with_match_archives_option_calls_borg_with_match_archives_parameter():
  306. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  307. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  308. flexmock(module.flags).should_receive('make_flags').and_return(())
  309. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  310. 'sh:foo-*', 'bar-{now}', '2.3.4' # noqa: FS003
  311. ).and_return(('--match-archives', 'sh:foo-*'))
  312. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  313. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  314. flexmock(module.environment).should_receive('make_environment')
  315. flexmock(module).should_receive('execute_command').with_args(
  316. ('borg', 'info', '--match-archives', 'sh:foo-*', '--repo', 'repo'),
  317. output_log_level=module.borgmatic.logger.ANSWER,
  318. borg_local_path='borg',
  319. extra_environment=None,
  320. )
  321. module.display_archives_info(
  322. repository_path='repo',
  323. storage_config={
  324. 'archive_name_format': 'bar-{now}', # noqa: FS003
  325. 'match_archives': 'sh:foo-*',
  326. },
  327. local_borg_version='2.3.4',
  328. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  329. )
  330. def test_display_archives_with_match_archives_flag_calls_borg_with_match_archives_parameter():
  331. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  332. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  333. flexmock(module.flags).should_receive('make_flags').and_return(())
  334. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  335. 'sh:foo-*', 'bar-{now}', '2.3.4' # noqa: FS003
  336. ).and_return(('--match-archives', 'sh:foo-*'))
  337. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  338. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  339. flexmock(module.environment).should_receive('make_environment')
  340. flexmock(module).should_receive('execute_command').with_args(
  341. ('borg', 'info', '--match-archives', 'sh:foo-*', '--repo', 'repo'),
  342. output_log_level=module.borgmatic.logger.ANSWER,
  343. borg_local_path='borg',
  344. extra_environment=None,
  345. )
  346. module.display_archives_info(
  347. repository_path='repo',
  348. storage_config={'archive_name_format': 'bar-{now}'}, # noqa: FS003
  349. local_borg_version='2.3.4',
  350. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives='sh:foo-*'),
  351. )
  352. @pytest.mark.parametrize('argument_name', ('sort_by', 'first', 'last'))
  353. def test_display_archives_info_passes_through_arguments_to_borg(argument_name):
  354. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  355. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  356. flag_name = f"--{argument_name.replace('_', ' ')}"
  357. flexmock(module.flags).should_receive('make_flags').and_return(())
  358. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  359. None, None, '2.3.4'
  360. ).and_return(())
  361. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  362. (flag_name, 'value')
  363. )
  364. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  365. flexmock(module.environment).should_receive('make_environment')
  366. flexmock(module).should_receive('execute_command').with_args(
  367. ('borg', 'info', flag_name, 'value', '--repo', 'repo'),
  368. output_log_level=module.borgmatic.logger.ANSWER,
  369. borg_local_path='borg',
  370. extra_environment=None,
  371. )
  372. module.display_archives_info(
  373. repository_path='repo',
  374. storage_config={},
  375. local_borg_version='2.3.4',
  376. info_arguments=flexmock(
  377. archive=None, json=False, prefix=None, match_archives=None, **{argument_name: 'value'}
  378. ),
  379. )