test_rinfo.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic.borg import rinfo as module
  4. from ..test_verbosity import insert_logging_mock
  5. def test_display_repository_info_calls_borg_with_flags():
  6. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  7. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  8. flexmock(module.feature).should_receive('available').and_return(True)
  9. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  10. (
  11. '--repo',
  12. 'repo',
  13. )
  14. )
  15. flexmock(module.environment).should_receive('make_environment')
  16. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  17. ('borg', 'rinfo', '--json', '--repo', 'repo'),
  18. borg_local_path='borg',
  19. extra_environment=None,
  20. ).and_return('[]')
  21. flexmock(module.flags).should_receive('warn_for_aggressive_archive_flags')
  22. flexmock(module).should_receive('execute_command').with_args(
  23. ('borg', 'rinfo', '--repo', 'repo'),
  24. output_log_level=module.borgmatic.logger.ANSWER,
  25. borg_local_path='borg',
  26. extra_environment=None,
  27. )
  28. module.display_repository_info(
  29. repository_path='repo',
  30. config={},
  31. local_borg_version='2.3.4',
  32. rinfo_arguments=flexmock(json=False),
  33. global_arguments=flexmock(log_json=False),
  34. )
  35. def test_display_repository_info_without_borg_features_calls_borg_with_info_sub_command():
  36. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  37. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  38. flexmock(module.feature).should_receive('available').and_return(False)
  39. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  40. flexmock(module.environment).should_receive('make_environment')
  41. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  42. ('borg', 'rinfo', '--json', 'repo'),
  43. borg_local_path='borg',
  44. extra_environment=None,
  45. ).and_return('[]')
  46. flexmock(module.flags).should_receive('warn_for_aggressive_archive_flags')
  47. flexmock(module).should_receive('execute_command').with_args(
  48. ('borg', 'info', 'repo'),
  49. output_log_level=module.borgmatic.logger.ANSWER,
  50. borg_local_path='borg',
  51. extra_environment=None,
  52. )
  53. module.display_repository_info(
  54. repository_path='repo',
  55. config={},
  56. local_borg_version='2.3.4',
  57. rinfo_arguments=flexmock(json=False),
  58. global_arguments=flexmock(log_json=False),
  59. )
  60. def test_display_repository_info_with_log_info_calls_borg_with_info_flag():
  61. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  62. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  63. flexmock(module.feature).should_receive('available').and_return(True)
  64. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  65. (
  66. '--repo',
  67. 'repo',
  68. )
  69. )
  70. flexmock(module.environment).should_receive('make_environment')
  71. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  72. ('borg', 'rinfo', '--info', '--json', '--repo', 'repo'),
  73. borg_local_path='borg',
  74. extra_environment=None,
  75. ).and_return('[]')
  76. flexmock(module.flags).should_receive('warn_for_aggressive_archive_flags')
  77. flexmock(module).should_receive('execute_command').with_args(
  78. ('borg', 'rinfo', '--info', '--repo', 'repo'),
  79. output_log_level=module.borgmatic.logger.ANSWER,
  80. borg_local_path='borg',
  81. extra_environment=None,
  82. )
  83. insert_logging_mock(logging.INFO)
  84. module.display_repository_info(
  85. repository_path='repo',
  86. config={},
  87. local_borg_version='2.3.4',
  88. rinfo_arguments=flexmock(json=False),
  89. global_arguments=flexmock(log_json=False),
  90. )
  91. def test_display_repository_info_with_log_info_and_json_suppresses_most_borg_output():
  92. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  93. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  94. flexmock(module.feature).should_receive('available').and_return(True)
  95. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  96. (
  97. '--repo',
  98. 'repo',
  99. )
  100. )
  101. flexmock(module.environment).should_receive('make_environment')
  102. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  103. ('borg', 'rinfo', '--json', '--repo', 'repo'),
  104. extra_environment=None,
  105. borg_local_path='borg',
  106. ).and_return('[]')
  107. flexmock(module.flags).should_receive('warn_for_aggressive_archive_flags').never()
  108. insert_logging_mock(logging.INFO)
  109. json_output = module.display_repository_info(
  110. repository_path='repo',
  111. config={},
  112. local_borg_version='2.3.4',
  113. rinfo_arguments=flexmock(json=True),
  114. global_arguments=flexmock(log_json=False),
  115. )
  116. assert json_output == '[]'
  117. def test_display_repository_info_with_log_debug_calls_borg_with_debug_flag():
  118. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  119. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  120. flexmock(module.feature).should_receive('available').and_return(True)
  121. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  122. (
  123. '--repo',
  124. 'repo',
  125. )
  126. )
  127. flexmock(module.environment).should_receive('make_environment')
  128. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  129. ('borg', 'rinfo', '--debug', '--show-rc', '--json', '--repo', 'repo'),
  130. borg_local_path='borg',
  131. extra_environment=None,
  132. ).and_return('[]')
  133. flexmock(module.flags).should_receive('warn_for_aggressive_archive_flags')
  134. flexmock(module).should_receive('execute_command').with_args(
  135. ('borg', 'rinfo', '--debug', '--show-rc', '--repo', 'repo'),
  136. output_log_level=module.borgmatic.logger.ANSWER,
  137. borg_local_path='borg',
  138. extra_environment=None,
  139. )
  140. insert_logging_mock(logging.DEBUG)
  141. module.display_repository_info(
  142. repository_path='repo',
  143. config={},
  144. local_borg_version='2.3.4',
  145. rinfo_arguments=flexmock(json=False),
  146. global_arguments=flexmock(log_json=False),
  147. )
  148. def test_display_repository_info_with_log_debug_and_json_suppresses_most_borg_output():
  149. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  150. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  151. flexmock(module.feature).should_receive('available').and_return(True)
  152. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  153. (
  154. '--repo',
  155. 'repo',
  156. )
  157. )
  158. flexmock(module.environment).should_receive('make_environment')
  159. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  160. ('borg', 'rinfo', '--json', '--repo', 'repo'),
  161. extra_environment=None,
  162. borg_local_path='borg',
  163. ).and_return('[]')
  164. flexmock(module.flags).should_receive('warn_for_aggressive_archive_flags').never()
  165. insert_logging_mock(logging.DEBUG)
  166. json_output = module.display_repository_info(
  167. repository_path='repo',
  168. config={},
  169. local_borg_version='2.3.4',
  170. rinfo_arguments=flexmock(json=True),
  171. global_arguments=flexmock(log_json=False),
  172. )
  173. assert json_output == '[]'
  174. def test_display_repository_info_with_json_calls_borg_with_json_flag():
  175. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  176. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  177. flexmock(module.feature).should_receive('available').and_return(True)
  178. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  179. (
  180. '--repo',
  181. 'repo',
  182. )
  183. )
  184. flexmock(module.environment).should_receive('make_environment')
  185. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  186. ('borg', 'rinfo', '--json', '--repo', 'repo'),
  187. extra_environment=None,
  188. borg_local_path='borg',
  189. ).and_return('[]')
  190. flexmock(module.flags).should_receive('warn_for_aggressive_archive_flags').never()
  191. json_output = module.display_repository_info(
  192. repository_path='repo',
  193. config={},
  194. local_borg_version='2.3.4',
  195. rinfo_arguments=flexmock(json=True),
  196. global_arguments=flexmock(log_json=False),
  197. )
  198. assert json_output == '[]'
  199. def test_display_repository_info_with_local_path_calls_borg_via_local_path():
  200. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  201. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  202. flexmock(module.feature).should_receive('available').and_return(True)
  203. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  204. (
  205. '--repo',
  206. 'repo',
  207. )
  208. )
  209. flexmock(module.environment).should_receive('make_environment')
  210. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  211. ('borg1', 'rinfo', '--json', '--repo', 'repo'),
  212. extra_environment=None,
  213. borg_local_path='borg',
  214. ).and_return('[]')
  215. flexmock(module.flags).should_receive('warn_for_aggressive_archive_flags')
  216. flexmock(module).should_receive('execute_command').with_args(
  217. ('borg1', 'rinfo', '--repo', 'repo'),
  218. output_log_level=module.borgmatic.logger.ANSWER,
  219. borg_local_path='borg1',
  220. extra_environment=None,
  221. )
  222. module.display_repository_info(
  223. repository_path='repo',
  224. config={},
  225. local_borg_version='2.3.4',
  226. rinfo_arguments=flexmock(json=False),
  227. global_arguments=flexmock(log_json=False),
  228. local_path='borg1',
  229. )
  230. def test_display_repository_info_with_remote_path_calls_borg_with_remote_path_flags():
  231. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  232. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  233. flexmock(module.feature).should_receive('available').and_return(True)
  234. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  235. (
  236. '--repo',
  237. 'repo',
  238. )
  239. )
  240. flexmock(module.environment).should_receive('make_environment')
  241. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  242. ('borg', 'rinfo', '--remote-path', 'borg1', '--json', '--repo', 'repo'),
  243. extra_environment=None,
  244. borg_local_path='borg',
  245. ).and_return('[]')
  246. flexmock(module.flags).should_receive('warn_for_aggressive_archive_flags')
  247. flexmock(module).should_receive('execute_command').with_args(
  248. ('borg', 'rinfo', '--remote-path', 'borg1', '--repo', 'repo'),
  249. output_log_level=module.borgmatic.logger.ANSWER,
  250. borg_local_path='borg',
  251. extra_environment=None,
  252. )
  253. module.display_repository_info(
  254. repository_path='repo',
  255. config={},
  256. local_borg_version='2.3.4',
  257. rinfo_arguments=flexmock(json=False),
  258. global_arguments=flexmock(log_json=False),
  259. remote_path='borg1',
  260. )
  261. def test_display_repository_info_with_log_json_calls_borg_with_log_json_flags():
  262. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  263. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  264. flexmock(module.feature).should_receive('available').and_return(True)
  265. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  266. (
  267. '--repo',
  268. 'repo',
  269. )
  270. )
  271. flexmock(module.environment).should_receive('make_environment')
  272. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  273. ('borg', 'rinfo', '--log-json', '--json', '--repo', 'repo'),
  274. extra_environment=None,
  275. borg_local_path='borg',
  276. ).and_return('[]')
  277. flexmock(module.flags).should_receive('warn_for_aggressive_archive_flags')
  278. flexmock(module).should_receive('execute_command').with_args(
  279. ('borg', 'rinfo', '--log-json', '--repo', 'repo'),
  280. output_log_level=module.borgmatic.logger.ANSWER,
  281. borg_local_path='borg',
  282. extra_environment=None,
  283. )
  284. module.display_repository_info(
  285. repository_path='repo',
  286. config={},
  287. local_borg_version='2.3.4',
  288. rinfo_arguments=flexmock(json=False),
  289. global_arguments=flexmock(log_json=True),
  290. )
  291. def test_display_repository_info_with_lock_wait_calls_borg_with_lock_wait_flags():
  292. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  293. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  294. config = {'lock_wait': 5}
  295. flexmock(module.feature).should_receive('available').and_return(True)
  296. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  297. (
  298. '--repo',
  299. 'repo',
  300. )
  301. )
  302. flexmock(module.environment).should_receive('make_environment')
  303. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  304. ('borg', 'rinfo', '--lock-wait', '5', '--json', '--repo', 'repo'),
  305. extra_environment=None,
  306. borg_local_path='borg',
  307. ).and_return('[]')
  308. flexmock(module.flags).should_receive('warn_for_aggressive_archive_flags')
  309. flexmock(module).should_receive('execute_command').with_args(
  310. ('borg', 'rinfo', '--lock-wait', '5', '--repo', 'repo'),
  311. output_log_level=module.borgmatic.logger.ANSWER,
  312. borg_local_path='borg',
  313. extra_environment=None,
  314. )
  315. module.display_repository_info(
  316. repository_path='repo',
  317. config=config,
  318. local_borg_version='2.3.4',
  319. rinfo_arguments=flexmock(json=False),
  320. global_arguments=flexmock(log_json=False),
  321. )