test_info.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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),
  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),
  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),
  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),
  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),
  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),
  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_flags').with_args(
  141. 'match-archives', 'archive'
  142. ).and_return(('--match-archives', 'archive'))
  143. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  144. None, None, '2.3.4'
  145. ).and_return(())
  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', '--repo', 'repo', '--match-archives', 'archive'),
  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),
  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),
  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),
  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),
  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),
  307. )
  308. @pytest.mark.parametrize('argument_name', ('match_archives', 'sort_by', 'first', 'last'))
  309. def test_display_archives_info_passes_through_arguments_to_borg(argument_name):
  310. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  311. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  312. flag_name = f"--{argument_name.replace('_', ' ')}"
  313. flexmock(module.flags).should_receive('make_flags').and_return(())
  314. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  315. None, None, '2.3.4'
  316. ).and_return(())
  317. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  318. (flag_name, 'value')
  319. )
  320. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  321. flexmock(module.environment).should_receive('make_environment')
  322. flexmock(module).should_receive('execute_command').with_args(
  323. ('borg', 'info', flag_name, 'value', '--repo', 'repo'),
  324. output_log_level=module.borgmatic.logger.ANSWER,
  325. borg_local_path='borg',
  326. extra_environment=None,
  327. )
  328. module.display_archives_info(
  329. repository_path='repo',
  330. storage_config={},
  331. local_borg_version='2.3.4',
  332. info_arguments=flexmock(archive=None, json=False, prefix=None, **{argument_name: 'value'}),
  333. )