test_rinfo.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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').with_args(
  17. ('borg', 'rinfo', '--repo', 'repo'),
  18. output_log_level=module.borgmatic.logger.ANSWER,
  19. borg_local_path='borg',
  20. extra_environment=None,
  21. )
  22. module.display_repository_info(
  23. repository_path='repo',
  24. config={},
  25. local_borg_version='2.3.4',
  26. rinfo_arguments=flexmock(json=False),
  27. global_arguments=flexmock(log_json=False),
  28. )
  29. def test_display_repository_info_without_borg_features_calls_borg_with_info_sub_command():
  30. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  31. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  32. flexmock(module.feature).should_receive('available').and_return(False)
  33. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  34. flexmock(module.environment).should_receive('make_environment')
  35. flexmock(module).should_receive('execute_command').with_args(
  36. ('borg', 'info', 'repo'),
  37. output_log_level=module.borgmatic.logger.ANSWER,
  38. borg_local_path='borg',
  39. extra_environment=None,
  40. )
  41. module.display_repository_info(
  42. repository_path='repo',
  43. config={},
  44. local_borg_version='2.3.4',
  45. rinfo_arguments=flexmock(json=False),
  46. global_arguments=flexmock(log_json=False),
  47. )
  48. def test_display_repository_info_with_log_info_calls_borg_with_info_flag():
  49. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  50. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  51. flexmock(module.feature).should_receive('available').and_return(True)
  52. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  53. (
  54. '--repo',
  55. 'repo',
  56. )
  57. )
  58. flexmock(module.environment).should_receive('make_environment')
  59. flexmock(module).should_receive('execute_command').with_args(
  60. ('borg', 'rinfo', '--info', '--repo', 'repo'),
  61. output_log_level=module.borgmatic.logger.ANSWER,
  62. borg_local_path='borg',
  63. extra_environment=None,
  64. )
  65. insert_logging_mock(logging.INFO)
  66. module.display_repository_info(
  67. repository_path='repo',
  68. config={},
  69. local_borg_version='2.3.4',
  70. rinfo_arguments=flexmock(json=False),
  71. global_arguments=flexmock(log_json=False),
  72. )
  73. def test_display_repository_info_with_log_info_and_json_suppresses_most_borg_output():
  74. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  75. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  76. flexmock(module.feature).should_receive('available').and_return(True)
  77. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  78. (
  79. '--repo',
  80. 'repo',
  81. )
  82. )
  83. flexmock(module.environment).should_receive('make_environment')
  84. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  85. ('borg', 'rinfo', '--json', '--repo', 'repo'),
  86. extra_environment=None,
  87. borg_local_path='borg',
  88. ).and_return('[]')
  89. insert_logging_mock(logging.INFO)
  90. json_output = module.display_repository_info(
  91. repository_path='repo',
  92. config={},
  93. local_borg_version='2.3.4',
  94. rinfo_arguments=flexmock(json=True),
  95. global_arguments=flexmock(log_json=False),
  96. )
  97. assert json_output == '[]'
  98. def test_display_repository_info_with_log_debug_calls_borg_with_debug_flag():
  99. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  100. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  101. flexmock(module.feature).should_receive('available').and_return(True)
  102. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  103. (
  104. '--repo',
  105. 'repo',
  106. )
  107. )
  108. flexmock(module.environment).should_receive('make_environment')
  109. flexmock(module).should_receive('execute_command').with_args(
  110. ('borg', 'rinfo', '--debug', '--show-rc', '--repo', 'repo'),
  111. output_log_level=module.borgmatic.logger.ANSWER,
  112. borg_local_path='borg',
  113. extra_environment=None,
  114. )
  115. insert_logging_mock(logging.DEBUG)
  116. module.display_repository_info(
  117. repository_path='repo',
  118. config={},
  119. local_borg_version='2.3.4',
  120. rinfo_arguments=flexmock(json=False),
  121. global_arguments=flexmock(log_json=False),
  122. )
  123. def test_display_repository_info_with_log_debug_and_json_suppresses_most_borg_output():
  124. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  125. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  126. flexmock(module.feature).should_receive('available').and_return(True)
  127. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  128. (
  129. '--repo',
  130. 'repo',
  131. )
  132. )
  133. flexmock(module.environment).should_receive('make_environment')
  134. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  135. ('borg', 'rinfo', '--json', '--repo', 'repo'),
  136. extra_environment=None,
  137. borg_local_path='borg',
  138. ).and_return('[]')
  139. insert_logging_mock(logging.DEBUG)
  140. json_output = module.display_repository_info(
  141. repository_path='repo',
  142. config={},
  143. local_borg_version='2.3.4',
  144. rinfo_arguments=flexmock(json=True),
  145. global_arguments=flexmock(log_json=False),
  146. )
  147. assert json_output == '[]'
  148. def test_display_repository_info_with_json_calls_borg_with_json_flag():
  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. json_output = module.display_repository_info(
  165. repository_path='repo',
  166. config={},
  167. local_borg_version='2.3.4',
  168. rinfo_arguments=flexmock(json=True),
  169. global_arguments=flexmock(log_json=False),
  170. )
  171. assert json_output == '[]'
  172. def test_display_repository_info_with_local_path_calls_borg_via_local_path():
  173. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  174. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  175. flexmock(module.feature).should_receive('available').and_return(True)
  176. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  177. (
  178. '--repo',
  179. 'repo',
  180. )
  181. )
  182. flexmock(module.environment).should_receive('make_environment')
  183. flexmock(module).should_receive('execute_command').with_args(
  184. ('borg1', 'rinfo', '--repo', 'repo'),
  185. output_log_level=module.borgmatic.logger.ANSWER,
  186. borg_local_path='borg1',
  187. extra_environment=None,
  188. )
  189. module.display_repository_info(
  190. repository_path='repo',
  191. config={},
  192. local_borg_version='2.3.4',
  193. rinfo_arguments=flexmock(json=False),
  194. global_arguments=flexmock(log_json=False),
  195. local_path='borg1',
  196. )
  197. def test_display_repository_info_with_remote_path_calls_borg_with_remote_path_flags():
  198. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  199. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  200. flexmock(module.feature).should_receive('available').and_return(True)
  201. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  202. (
  203. '--repo',
  204. 'repo',
  205. )
  206. )
  207. flexmock(module.environment).should_receive('make_environment')
  208. flexmock(module).should_receive('execute_command').with_args(
  209. ('borg', 'rinfo', '--remote-path', 'borg1', '--repo', 'repo'),
  210. output_log_level=module.borgmatic.logger.ANSWER,
  211. borg_local_path='borg',
  212. extra_environment=None,
  213. )
  214. module.display_repository_info(
  215. repository_path='repo',
  216. config={},
  217. local_borg_version='2.3.4',
  218. rinfo_arguments=flexmock(json=False),
  219. global_arguments=flexmock(log_json=False),
  220. remote_path='borg1',
  221. )
  222. def test_display_repository_info_with_log_json_calls_borg_with_log_json_flags():
  223. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  224. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  225. flexmock(module.feature).should_receive('available').and_return(True)
  226. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  227. (
  228. '--repo',
  229. 'repo',
  230. )
  231. )
  232. flexmock(module.environment).should_receive('make_environment')
  233. flexmock(module).should_receive('execute_command').with_args(
  234. ('borg', 'rinfo', '--log-json', '--repo', 'repo'),
  235. output_log_level=module.borgmatic.logger.ANSWER,
  236. borg_local_path='borg',
  237. extra_environment=None,
  238. )
  239. module.display_repository_info(
  240. repository_path='repo',
  241. config={},
  242. local_borg_version='2.3.4',
  243. rinfo_arguments=flexmock(json=False),
  244. global_arguments=flexmock(log_json=True),
  245. )
  246. def test_display_repository_info_with_lock_wait_calls_borg_with_lock_wait_flags():
  247. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  248. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  249. config = {'lock_wait': 5}
  250. flexmock(module.feature).should_receive('available').and_return(True)
  251. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  252. (
  253. '--repo',
  254. 'repo',
  255. )
  256. )
  257. flexmock(module.environment).should_receive('make_environment')
  258. flexmock(module).should_receive('execute_command').with_args(
  259. ('borg', 'rinfo', '--lock-wait', '5', '--repo', 'repo'),
  260. output_log_level=module.borgmatic.logger.ANSWER,
  261. borg_local_path='borg',
  262. extra_environment=None,
  263. )
  264. module.display_repository_info(
  265. repository_path='repo',
  266. config=config,
  267. local_borg_version='2.3.4',
  268. rinfo_arguments=flexmock(json=False),
  269. global_arguments=flexmock(log_json=False),
  270. )