test_rinfo.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. storage_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. storage_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. storage_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. ).and_return('[]')
  88. insert_logging_mock(logging.INFO)
  89. json_output = module.display_repository_info(
  90. repository_path='repo',
  91. storage_config={},
  92. local_borg_version='2.3.4',
  93. rinfo_arguments=flexmock(json=True),
  94. global_arguments=flexmock(log_json=False),
  95. )
  96. assert json_output == '[]'
  97. def test_display_repository_info_with_log_debug_calls_borg_with_debug_flag():
  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').with_args(
  109. ('borg', 'rinfo', '--debug', '--show-rc', '--repo', 'repo'),
  110. output_log_level=module.borgmatic.logger.ANSWER,
  111. borg_local_path='borg',
  112. extra_environment=None,
  113. )
  114. insert_logging_mock(logging.DEBUG)
  115. module.display_repository_info(
  116. repository_path='repo',
  117. storage_config={},
  118. local_borg_version='2.3.4',
  119. rinfo_arguments=flexmock(json=False),
  120. global_arguments=flexmock(log_json=False),
  121. )
  122. def test_display_repository_info_with_log_debug_and_json_suppresses_most_borg_output():
  123. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  124. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  125. flexmock(module.feature).should_receive('available').and_return(True)
  126. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  127. (
  128. '--repo',
  129. 'repo',
  130. )
  131. )
  132. flexmock(module.environment).should_receive('make_environment')
  133. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  134. ('borg', 'rinfo', '--json', '--repo', 'repo'),
  135. extra_environment=None,
  136. ).and_return('[]')
  137. insert_logging_mock(logging.DEBUG)
  138. json_output = module.display_repository_info(
  139. repository_path='repo',
  140. storage_config={},
  141. local_borg_version='2.3.4',
  142. rinfo_arguments=flexmock(json=True),
  143. global_arguments=flexmock(log_json=False),
  144. )
  145. assert json_output == '[]'
  146. def test_display_repository_info_with_json_calls_borg_with_json_flag():
  147. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  148. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  149. flexmock(module.feature).should_receive('available').and_return(True)
  150. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  151. (
  152. '--repo',
  153. 'repo',
  154. )
  155. )
  156. flexmock(module.environment).should_receive('make_environment')
  157. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  158. ('borg', 'rinfo', '--json', '--repo', 'repo'),
  159. extra_environment=None,
  160. ).and_return('[]')
  161. json_output = module.display_repository_info(
  162. repository_path='repo',
  163. storage_config={},
  164. local_borg_version='2.3.4',
  165. rinfo_arguments=flexmock(json=True),
  166. global_arguments=flexmock(log_json=False),
  167. )
  168. assert json_output == '[]'
  169. def test_display_repository_info_with_local_path_calls_borg_via_local_path():
  170. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  171. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  172. flexmock(module.feature).should_receive('available').and_return(True)
  173. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  174. (
  175. '--repo',
  176. 'repo',
  177. )
  178. )
  179. flexmock(module.environment).should_receive('make_environment')
  180. flexmock(module).should_receive('execute_command').with_args(
  181. ('borg1', 'rinfo', '--repo', 'repo'),
  182. output_log_level=module.borgmatic.logger.ANSWER,
  183. borg_local_path='borg1',
  184. extra_environment=None,
  185. )
  186. module.display_repository_info(
  187. repository_path='repo',
  188. storage_config={},
  189. local_borg_version='2.3.4',
  190. rinfo_arguments=flexmock(json=False),
  191. global_arguments=flexmock(log_json=False),
  192. local_path='borg1',
  193. )
  194. def test_display_repository_info_with_remote_path_calls_borg_with_remote_path_flags():
  195. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  196. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  197. flexmock(module.feature).should_receive('available').and_return(True)
  198. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  199. (
  200. '--repo',
  201. 'repo',
  202. )
  203. )
  204. flexmock(module.environment).should_receive('make_environment')
  205. flexmock(module).should_receive('execute_command').with_args(
  206. ('borg', 'rinfo', '--remote-path', 'borg1', '--repo', 'repo'),
  207. output_log_level=module.borgmatic.logger.ANSWER,
  208. borg_local_path='borg',
  209. extra_environment=None,
  210. )
  211. module.display_repository_info(
  212. repository_path='repo',
  213. storage_config={},
  214. local_borg_version='2.3.4',
  215. rinfo_arguments=flexmock(json=False),
  216. global_arguments=flexmock(log_json=False),
  217. remote_path='borg1',
  218. )
  219. def test_display_repository_info_with_log_json_calls_borg_with_log_json_flags():
  220. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  221. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  222. flexmock(module.feature).should_receive('available').and_return(True)
  223. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  224. (
  225. '--repo',
  226. 'repo',
  227. )
  228. )
  229. flexmock(module.environment).should_receive('make_environment')
  230. flexmock(module).should_receive('execute_command').with_args(
  231. ('borg', 'rinfo', '--log-json', '--repo', 'repo'),
  232. output_log_level=module.borgmatic.logger.ANSWER,
  233. borg_local_path='borg',
  234. extra_environment=None,
  235. )
  236. module.display_repository_info(
  237. repository_path='repo',
  238. storage_config={},
  239. local_borg_version='2.3.4',
  240. rinfo_arguments=flexmock(json=False),
  241. global_arguments=flexmock(log_json=True),
  242. )
  243. def test_display_repository_info_with_lock_wait_calls_borg_with_lock_wait_flags():
  244. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  245. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  246. storage_config = {'lock_wait': 5}
  247. flexmock(module.feature).should_receive('available').and_return(True)
  248. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  249. (
  250. '--repo',
  251. 'repo',
  252. )
  253. )
  254. flexmock(module.environment).should_receive('make_environment')
  255. flexmock(module).should_receive('execute_command').with_args(
  256. ('borg', 'rinfo', '--lock-wait', '5', '--repo', 'repo'),
  257. output_log_level=module.borgmatic.logger.ANSWER,
  258. borg_local_path='borg',
  259. extra_environment=None,
  260. )
  261. module.display_repository_info(
  262. repository_path='repo',
  263. storage_config=storage_config,
  264. local_borg_version='2.3.4',
  265. rinfo_arguments=flexmock(json=False),
  266. global_arguments=flexmock(log_json=False),
  267. )