test_rinfo.py 16 KB

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