test_info.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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'),
  63. extra_environment=None,
  64. ).and_return('[]')
  65. insert_logging_mock(logging.INFO)
  66. json_output = module.display_archives_info(
  67. repository_path='repo',
  68. storage_config={},
  69. local_borg_version='2.3.4',
  70. info_arguments=flexmock(archive=None, json=True, prefix=None, match_archives=None),
  71. )
  72. assert json_output == '[]'
  73. def test_display_archives_info_with_log_debug_calls_borg_with_debug_parameter():
  74. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  75. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  76. flexmock(module.flags).should_receive('make_flags').and_return(())
  77. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  78. None, None, '2.3.4'
  79. ).and_return(())
  80. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  81. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  82. flexmock(module.environment).should_receive('make_environment')
  83. flexmock(module).should_receive('execute_command').with_args(
  84. ('borg', 'info', '--debug', '--show-rc', '--repo', 'repo'),
  85. output_log_level=module.borgmatic.logger.ANSWER,
  86. borg_local_path='borg',
  87. extra_environment=None,
  88. )
  89. insert_logging_mock(logging.DEBUG)
  90. module.display_archives_info(
  91. repository_path='repo',
  92. storage_config={},
  93. local_borg_version='2.3.4',
  94. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  95. )
  96. def test_display_archives_info_with_log_debug_and_json_suppresses_most_borg_output():
  97. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  98. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  99. flexmock(module.flags).should_receive('make_flags').and_return(())
  100. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  101. None, None, '2.3.4'
  102. ).and_return(())
  103. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  104. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  105. flexmock(module.environment).should_receive('make_environment')
  106. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  107. ('borg', 'info', '--json', '--repo', 'repo'),
  108. extra_environment=None,
  109. ).and_return('[]')
  110. insert_logging_mock(logging.DEBUG)
  111. json_output = module.display_archives_info(
  112. repository_path='repo',
  113. storage_config={},
  114. local_borg_version='2.3.4',
  115. info_arguments=flexmock(archive=None, json=True, prefix=None, match_archives=None),
  116. )
  117. assert json_output == '[]'
  118. def test_display_archives_info_with_json_calls_borg_with_json_parameter():
  119. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  120. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  121. flexmock(module.flags).should_receive('make_flags').and_return(())
  122. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  123. None, None, '2.3.4'
  124. ).and_return(())
  125. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  126. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  127. flexmock(module.environment).should_receive('make_environment')
  128. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  129. ('borg', 'info', '--json', '--repo', 'repo'),
  130. extra_environment=None,
  131. ).and_return('[]')
  132. json_output = module.display_archives_info(
  133. repository_path='repo',
  134. storage_config={},
  135. local_borg_version='2.3.4',
  136. info_arguments=flexmock(archive=None, json=True, prefix=None, match_archives=None),
  137. )
  138. assert json_output == '[]'
  139. def test_display_archives_info_with_archive_calls_borg_with_match_archives_parameter():
  140. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  141. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  142. flexmock(module.flags).should_receive('make_flags').and_return(())
  143. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  144. 'archive', None, '2.3.4'
  145. ).and_return(('--match-archives', 'archive'))
  146. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  147. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  148. flexmock(module.environment).should_receive('make_environment')
  149. flexmock(module).should_receive('execute_command').with_args(
  150. ('borg', 'info', '--match-archives', 'archive', '--repo', 'repo'),
  151. output_log_level=module.borgmatic.logger.ANSWER,
  152. borg_local_path='borg',
  153. extra_environment=None,
  154. )
  155. module.display_archives_info(
  156. repository_path='repo',
  157. storage_config={},
  158. local_borg_version='2.3.4',
  159. info_arguments=flexmock(archive='archive', json=False, prefix=None, match_archives=None),
  160. )
  161. def test_display_archives_info_with_local_path_calls_borg_via_local_path():
  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, None, '2.3.4'
  167. ).and_return(())
  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. ('borg1', 'info', '--repo', 'repo'),
  173. output_log_level=module.borgmatic.logger.ANSWER,
  174. borg_local_path='borg1',
  175. extra_environment=None,
  176. )
  177. module.display_archives_info(
  178. repository_path='repo',
  179. storage_config={},
  180. local_borg_version='2.3.4',
  181. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  182. local_path='borg1',
  183. )
  184. def test_display_archives_info_with_remote_path_calls_borg_with_remote_path_parameters():
  185. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  186. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  187. flexmock(module.flags).should_receive('make_flags').and_return(())
  188. flexmock(module.flags).should_receive('make_flags').with_args(
  189. 'remote-path', 'borg1'
  190. ).and_return(('--remote-path', 'borg1'))
  191. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  192. None, None, '2.3.4'
  193. ).and_return(())
  194. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  195. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  196. flexmock(module.environment).should_receive('make_environment')
  197. flexmock(module).should_receive('execute_command').with_args(
  198. ('borg', 'info', '--remote-path', 'borg1', '--repo', 'repo'),
  199. output_log_level=module.borgmatic.logger.ANSWER,
  200. borg_local_path='borg',
  201. extra_environment=None,
  202. )
  203. module.display_archives_info(
  204. repository_path='repo',
  205. storage_config={},
  206. local_borg_version='2.3.4',
  207. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  208. remote_path='borg1',
  209. )
  210. def test_display_archives_info_with_lock_wait_calls_borg_with_lock_wait_parameters():
  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('lock-wait', 5).and_return(
  215. ('--lock-wait', '5')
  216. )
  217. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  218. None, None, '2.3.4'
  219. ).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. storage_config = {'lock_wait': 5}
  223. flexmock(module.environment).should_receive('make_environment')
  224. flexmock(module).should_receive('execute_command').with_args(
  225. ('borg', 'info', '--lock-wait', '5', '--repo', 'repo'),
  226. output_log_level=module.borgmatic.logger.ANSWER,
  227. borg_local_path='borg',
  228. extra_environment=None,
  229. )
  230. module.display_archives_info(
  231. repository_path='repo',
  232. storage_config=storage_config,
  233. local_borg_version='2.3.4',
  234. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  235. )
  236. def test_display_archives_info_transforms_prefix_into_match_archives_parameters():
  237. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  238. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  239. flexmock(module.flags).should_receive('make_flags').and_return(())
  240. flexmock(module.flags).should_receive('make_flags').with_args(
  241. 'match-archives', 'sh:foo*'
  242. ).and_return(('--match-archives', 'sh:foo*'))
  243. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  244. None, None, '2.3.4'
  245. ).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. flexmock(module.environment).should_receive('make_environment')
  249. flexmock(module).should_receive('execute_command').with_args(
  250. ('borg', 'info', '--match-archives', 'sh:foo*', '--repo', 'repo'),
  251. output_log_level=module.borgmatic.logger.ANSWER,
  252. borg_local_path='borg',
  253. extra_environment=None,
  254. )
  255. module.display_archives_info(
  256. repository_path='repo',
  257. storage_config={},
  258. local_borg_version='2.3.4',
  259. info_arguments=flexmock(archive=None, json=False, prefix='foo'),
  260. )
  261. def test_display_archives_info_prefers_prefix_over_archive_name_format():
  262. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  263. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  264. flexmock(module.flags).should_receive('make_flags').and_return(())
  265. flexmock(module.flags).should_receive('make_flags').with_args(
  266. 'match-archives', 'sh:foo*'
  267. ).and_return(('--match-archives', 'sh:foo*'))
  268. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  269. None, None, '2.3.4'
  270. ).and_return(())
  271. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  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', 'info', '--match-archives', 'sh:foo*', '--repo', 'repo'),
  276. output_log_level=module.borgmatic.logger.ANSWER,
  277. borg_local_path='borg',
  278. extra_environment=None,
  279. )
  280. module.display_archives_info(
  281. repository_path='repo',
  282. storage_config={'archive_name_format': 'bar-{now}'}, # noqa: FS003
  283. local_borg_version='2.3.4',
  284. info_arguments=flexmock(archive=None, json=False, prefix='foo'),
  285. )
  286. def test_display_archives_info_transforms_archive_name_format_into_match_archives_parameters():
  287. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  288. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  289. flexmock(module.flags).should_receive('make_flags').and_return(())
  290. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  291. None, 'bar-{now}', '2.3.4' # noqa: FS003
  292. ).and_return(('--match-archives', 'sh:bar-*'))
  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', 'info', '--match-archives', 'sh:bar-*', '--repo', 'repo'),
  298. output_log_level=module.borgmatic.logger.ANSWER,
  299. borg_local_path='borg',
  300. extra_environment=None,
  301. )
  302. module.display_archives_info(
  303. repository_path='repo',
  304. storage_config={'archive_name_format': 'bar-{now}'}, # noqa: FS003
  305. local_borg_version='2.3.4',
  306. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  307. )
  308. def test_display_archives_with_match_archives_option_calls_borg_with_match_archives_parameter():
  309. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  310. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  311. flexmock(module.flags).should_receive('make_flags').and_return(())
  312. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  313. 'sh:foo-*', 'bar-{now}', '2.3.4' # noqa: FS003
  314. ).and_return(('--match-archives', 'sh:foo-*'))
  315. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  316. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  317. flexmock(module.environment).should_receive('make_environment')
  318. flexmock(module).should_receive('execute_command').with_args(
  319. ('borg', 'info', '--match-archives', 'sh:foo-*', '--repo', 'repo'),
  320. output_log_level=module.borgmatic.logger.ANSWER,
  321. borg_local_path='borg',
  322. extra_environment=None,
  323. )
  324. module.display_archives_info(
  325. repository_path='repo',
  326. storage_config={
  327. 'archive_name_format': 'bar-{now}', # noqa: FS003
  328. 'match_archives': 'sh:foo-*',
  329. },
  330. local_borg_version='2.3.4',
  331. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  332. )
  333. def test_display_archives_with_match_archives_flag_calls_borg_with_match_archives_parameter():
  334. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  335. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  336. flexmock(module.flags).should_receive('make_flags').and_return(())
  337. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  338. 'sh:foo-*', 'bar-{now}', '2.3.4' # noqa: FS003
  339. ).and_return(('--match-archives', 'sh:foo-*'))
  340. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  341. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  342. flexmock(module.environment).should_receive('make_environment')
  343. flexmock(module).should_receive('execute_command').with_args(
  344. ('borg', 'info', '--match-archives', 'sh:foo-*', '--repo', 'repo'),
  345. output_log_level=module.borgmatic.logger.ANSWER,
  346. borg_local_path='borg',
  347. extra_environment=None,
  348. )
  349. module.display_archives_info(
  350. repository_path='repo',
  351. storage_config={'archive_name_format': 'bar-{now}'}, # noqa: FS003
  352. local_borg_version='2.3.4',
  353. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives='sh:foo-*'),
  354. )
  355. @pytest.mark.parametrize('argument_name', ('sort_by', 'first', 'last'))
  356. def test_display_archives_info_passes_through_arguments_to_borg(argument_name):
  357. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  358. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  359. flag_name = f"--{argument_name.replace('_', ' ')}"
  360. flexmock(module.flags).should_receive('make_flags').and_return(())
  361. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  362. None, None, '2.3.4'
  363. ).and_return(())
  364. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  365. (flag_name, 'value')
  366. )
  367. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  368. flexmock(module.environment).should_receive('make_environment')
  369. flexmock(module).should_receive('execute_command').with_args(
  370. ('borg', 'info', flag_name, 'value', '--repo', 'repo'),
  371. output_log_level=module.borgmatic.logger.ANSWER,
  372. borg_local_path='borg',
  373. extra_environment=None,
  374. )
  375. module.display_archives_info(
  376. repository_path='repo',
  377. storage_config={},
  378. local_borg_version='2.3.4',
  379. info_arguments=flexmock(
  380. archive=None, json=False, prefix=None, match_archives=None, **{argument_name: 'value'}
  381. ),
  382. )