test_rinfo.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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_parameters():
  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. )
  28. def test_display_repository_info_without_borg_features_calls_borg_with_info_sub_command():
  29. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  30. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  31. flexmock(module.feature).should_receive('available').and_return(False)
  32. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  33. flexmock(module.environment).should_receive('make_environment')
  34. flexmock(module).should_receive('execute_command').with_args(
  35. ('borg', 'info', 'repo'),
  36. output_log_level=module.borgmatic.logger.ANSWER,
  37. borg_local_path='borg',
  38. extra_environment=None,
  39. )
  40. module.display_repository_info(
  41. repository_path='repo',
  42. storage_config={},
  43. local_borg_version='2.3.4',
  44. rinfo_arguments=flexmock(json=False),
  45. )
  46. def test_display_repository_info_with_log_info_calls_borg_with_info_parameter():
  47. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  48. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  49. flexmock(module.feature).should_receive('available').and_return(True)
  50. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  51. (
  52. '--repo',
  53. 'repo',
  54. )
  55. )
  56. flexmock(module.environment).should_receive('make_environment')
  57. flexmock(module).should_receive('execute_command').with_args(
  58. ('borg', 'rinfo', '--info', '--repo', 'repo'),
  59. output_log_level=module.borgmatic.logger.ANSWER,
  60. borg_local_path='borg',
  61. extra_environment=None,
  62. )
  63. insert_logging_mock(logging.INFO)
  64. module.display_repository_info(
  65. repository_path='repo',
  66. storage_config={},
  67. local_borg_version='2.3.4',
  68. rinfo_arguments=flexmock(json=False),
  69. )
  70. def test_display_repository_info_with_log_info_and_json_suppresses_most_borg_output():
  71. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  72. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  73. flexmock(module.feature).should_receive('available').and_return(True)
  74. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  75. (
  76. '--repo',
  77. 'repo',
  78. )
  79. )
  80. flexmock(module.environment).should_receive('make_environment')
  81. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  82. ('borg', 'rinfo', '--json', '--repo', 'repo'),
  83. extra_environment=None,
  84. ).and_return('[]')
  85. insert_logging_mock(logging.INFO)
  86. json_output = module.display_repository_info(
  87. repository_path='repo',
  88. storage_config={},
  89. local_borg_version='2.3.4',
  90. rinfo_arguments=flexmock(json=True),
  91. )
  92. assert json_output == '[]'
  93. def test_display_repository_info_with_log_debug_calls_borg_with_debug_parameter():
  94. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  95. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  96. flexmock(module.feature).should_receive('available').and_return(True)
  97. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  98. (
  99. '--repo',
  100. 'repo',
  101. )
  102. )
  103. flexmock(module.environment).should_receive('make_environment')
  104. flexmock(module).should_receive('execute_command').with_args(
  105. ('borg', 'rinfo', '--debug', '--show-rc', '--repo', 'repo'),
  106. output_log_level=module.borgmatic.logger.ANSWER,
  107. borg_local_path='borg',
  108. extra_environment=None,
  109. )
  110. insert_logging_mock(logging.DEBUG)
  111. module.display_repository_info(
  112. repository_path='repo',
  113. storage_config={},
  114. local_borg_version='2.3.4',
  115. rinfo_arguments=flexmock(json=False),
  116. )
  117. def test_display_repository_info_with_log_debug_and_json_suppresses_most_borg_output():
  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', '--json', '--repo', 'repo'),
  130. extra_environment=None,
  131. ).and_return('[]')
  132. insert_logging_mock(logging.DEBUG)
  133. json_output = module.display_repository_info(
  134. repository_path='repo',
  135. storage_config={},
  136. local_borg_version='2.3.4',
  137. rinfo_arguments=flexmock(json=True),
  138. )
  139. assert json_output == '[]'
  140. def test_display_repository_info_with_json_calls_borg_with_json_parameter():
  141. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  142. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  143. flexmock(module.feature).should_receive('available').and_return(True)
  144. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  145. (
  146. '--repo',
  147. 'repo',
  148. )
  149. )
  150. flexmock(module.environment).should_receive('make_environment')
  151. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  152. ('borg', 'rinfo', '--json', '--repo', 'repo'),
  153. extra_environment=None,
  154. ).and_return('[]')
  155. json_output = module.display_repository_info(
  156. repository_path='repo',
  157. storage_config={},
  158. local_borg_version='2.3.4',
  159. rinfo_arguments=flexmock(json=True),
  160. )
  161. assert json_output == '[]'
  162. def test_display_repository_info_with_local_path_calls_borg_via_local_path():
  163. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  164. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  165. flexmock(module.feature).should_receive('available').and_return(True)
  166. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  167. (
  168. '--repo',
  169. 'repo',
  170. )
  171. )
  172. flexmock(module.environment).should_receive('make_environment')
  173. flexmock(module).should_receive('execute_command').with_args(
  174. ('borg1', 'rinfo', '--repo', 'repo'),
  175. output_log_level=module.borgmatic.logger.ANSWER,
  176. borg_local_path='borg1',
  177. extra_environment=None,
  178. )
  179. module.display_repository_info(
  180. repository_path='repo',
  181. storage_config={},
  182. local_borg_version='2.3.4',
  183. rinfo_arguments=flexmock(json=False),
  184. local_path='borg1',
  185. )
  186. def test_display_repository_info_with_remote_path_calls_borg_with_remote_path_parameters():
  187. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  188. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  189. flexmock(module.feature).should_receive('available').and_return(True)
  190. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  191. (
  192. '--repo',
  193. 'repo',
  194. )
  195. )
  196. flexmock(module.environment).should_receive('make_environment')
  197. flexmock(module).should_receive('execute_command').with_args(
  198. ('borg', 'rinfo', '--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_repository_info(
  204. repository_path='repo',
  205. storage_config={},
  206. local_borg_version='2.3.4',
  207. rinfo_arguments=flexmock(json=False),
  208. remote_path='borg1',
  209. )
  210. def test_display_repository_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. storage_config = {'lock_wait': 5}
  214. flexmock(module.feature).should_receive('available').and_return(True)
  215. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  216. (
  217. '--repo',
  218. 'repo',
  219. )
  220. )
  221. flexmock(module.environment).should_receive('make_environment')
  222. flexmock(module).should_receive('execute_command').with_args(
  223. ('borg', 'rinfo', '--lock-wait', '5', '--repo', 'repo'),
  224. output_log_level=module.borgmatic.logger.ANSWER,
  225. borg_local_path='borg',
  226. extra_environment=None,
  227. )
  228. module.display_repository_info(
  229. repository_path='repo',
  230. storage_config=storage_config,
  231. local_borg_version='2.3.4',
  232. rinfo_arguments=flexmock(json=False),
  233. )