test_info.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.borg import info as module
  5. from ..test_verbosity import insert_logging_mock
  6. def test_display_archives_info_calls_borg_with_parameters():
  7. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  8. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  9. flexmock(module.flags).should_receive('make_flags').and_return(())
  10. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  11. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  12. flexmock(module.environment).should_receive('make_environment')
  13. flexmock(module).should_receive('execute_command').with_args(
  14. ('borg', 'info', '--repo', 'repo'),
  15. output_log_level=module.borgmatic.logger.ANSWER,
  16. borg_local_path='borg',
  17. extra_environment=None,
  18. )
  19. module.display_archives_info(
  20. repository_path='repo',
  21. storage_config={},
  22. local_borg_version='2.3.4',
  23. info_arguments=flexmock(archive=None, json=False, prefix=None),
  24. )
  25. def test_display_archives_info_with_log_info_calls_borg_with_info_parameter():
  26. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  27. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  28. flexmock(module.flags).should_receive('make_flags').and_return(())
  29. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  30. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  31. flexmock(module.environment).should_receive('make_environment')
  32. flexmock(module).should_receive('execute_command').with_args(
  33. ('borg', 'info', '--info', '--repo', 'repo'),
  34. output_log_level=module.borgmatic.logger.ANSWER,
  35. borg_local_path='borg',
  36. extra_environment=None,
  37. )
  38. insert_logging_mock(logging.INFO)
  39. module.display_archives_info(
  40. repository_path='repo',
  41. storage_config={},
  42. local_borg_version='2.3.4',
  43. info_arguments=flexmock(archive=None, json=False, prefix=None),
  44. )
  45. def test_display_archives_info_with_log_info_and_json_suppresses_most_borg_output():
  46. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  47. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  48. flexmock(module.flags).should_receive('make_flags').and_return(())
  49. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  50. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  51. flexmock(module.environment).should_receive('make_environment')
  52. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  53. ('borg', 'info', '--json', '--repo', 'repo'), extra_environment=None,
  54. ).and_return('[]')
  55. insert_logging_mock(logging.INFO)
  56. json_output = module.display_archives_info(
  57. repository_path='repo',
  58. storage_config={},
  59. local_borg_version='2.3.4',
  60. info_arguments=flexmock(archive=None, json=True, prefix=None),
  61. )
  62. assert json_output == '[]'
  63. def test_display_archives_info_with_log_debug_calls_borg_with_debug_parameter():
  64. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  65. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  66. flexmock(module.flags).should_receive('make_flags').and_return(())
  67. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  68. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  69. flexmock(module.environment).should_receive('make_environment')
  70. flexmock(module).should_receive('execute_command').with_args(
  71. ('borg', 'info', '--debug', '--show-rc', '--repo', 'repo'),
  72. output_log_level=module.borgmatic.logger.ANSWER,
  73. borg_local_path='borg',
  74. extra_environment=None,
  75. )
  76. insert_logging_mock(logging.DEBUG)
  77. module.display_archives_info(
  78. repository_path='repo',
  79. storage_config={},
  80. local_borg_version='2.3.4',
  81. info_arguments=flexmock(archive=None, json=False, prefix=None),
  82. )
  83. def test_display_archives_info_with_log_debug_and_json_suppresses_most_borg_output():
  84. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  85. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  86. flexmock(module.flags).should_receive('make_flags').and_return(())
  87. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  88. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  89. flexmock(module.environment).should_receive('make_environment')
  90. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  91. ('borg', 'info', '--json', '--repo', 'repo'), extra_environment=None,
  92. ).and_return('[]')
  93. insert_logging_mock(logging.DEBUG)
  94. json_output = module.display_archives_info(
  95. repository_path='repo',
  96. storage_config={},
  97. local_borg_version='2.3.4',
  98. info_arguments=flexmock(archive=None, json=True, prefix=None),
  99. )
  100. assert json_output == '[]'
  101. def test_display_archives_info_with_json_calls_borg_with_json_parameter():
  102. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  103. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  104. flexmock(module.flags).should_receive('make_flags').and_return(())
  105. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  106. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  107. flexmock(module.environment).should_receive('make_environment')
  108. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  109. ('borg', 'info', '--json', '--repo', 'repo'), extra_environment=None,
  110. ).and_return('[]')
  111. json_output = module.display_archives_info(
  112. repository_path='repo',
  113. storage_config={},
  114. local_borg_version='2.3.4',
  115. info_arguments=flexmock(archive=None, json=True, prefix=None),
  116. )
  117. assert json_output == '[]'
  118. def test_display_archives_info_with_archive_calls_borg_with_match_archives_parameter():
  119. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  120. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  121. flexmock(module.flags).should_receive('make_flags').and_return(())
  122. flexmock(module.flags).should_receive('make_flags').with_args(
  123. 'match-archives', 'archive'
  124. ).and_return(('--match-archives', 'archive'))
  125. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  126. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  127. flexmock(module.environment).should_receive('make_environment')
  128. flexmock(module).should_receive('execute_command').with_args(
  129. ('borg', 'info', '--repo', 'repo', '--match-archives', 'archive'),
  130. output_log_level=module.borgmatic.logger.ANSWER,
  131. borg_local_path='borg',
  132. extra_environment=None,
  133. )
  134. module.display_archives_info(
  135. repository_path='repo',
  136. storage_config={},
  137. local_borg_version='2.3.4',
  138. info_arguments=flexmock(archive='archive', json=False, prefix=None),
  139. )
  140. def test_display_archives_info_with_local_path_calls_borg_via_local_path():
  141. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  142. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  143. flexmock(module.flags).should_receive('make_flags').and_return(())
  144. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  145. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  146. flexmock(module.environment).should_receive('make_environment')
  147. flexmock(module).should_receive('execute_command').with_args(
  148. ('borg1', 'info', '--repo', 'repo'),
  149. output_log_level=module.borgmatic.logger.ANSWER,
  150. borg_local_path='borg1',
  151. extra_environment=None,
  152. )
  153. module.display_archives_info(
  154. repository_path='repo',
  155. storage_config={},
  156. local_borg_version='2.3.4',
  157. info_arguments=flexmock(archive=None, json=False, prefix=None),
  158. local_path='borg1',
  159. )
  160. def test_display_archives_info_with_remote_path_calls_borg_with_remote_path_parameters():
  161. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  162. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  163. flexmock(module.flags).should_receive('make_flags').and_return(())
  164. flexmock(module.flags).should_receive('make_flags').with_args(
  165. 'remote-path', 'borg1'
  166. ).and_return(('--remote-path', 'borg1'))
  167. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  168. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  169. flexmock(module.environment).should_receive('make_environment')
  170. flexmock(module).should_receive('execute_command').with_args(
  171. ('borg', 'info', '--remote-path', 'borg1', '--repo', 'repo'),
  172. output_log_level=module.borgmatic.logger.ANSWER,
  173. borg_local_path='borg',
  174. extra_environment=None,
  175. )
  176. module.display_archives_info(
  177. repository_path='repo',
  178. storage_config={},
  179. local_borg_version='2.3.4',
  180. info_arguments=flexmock(archive=None, json=False, prefix=None),
  181. remote_path='borg1',
  182. )
  183. def test_display_archives_info_with_lock_wait_calls_borg_with_lock_wait_parameters():
  184. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  185. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  186. flexmock(module.flags).should_receive('make_flags').and_return(())
  187. flexmock(module.flags).should_receive('make_flags').with_args('lock-wait', 5).and_return(
  188. ('--lock-wait', '5')
  189. )
  190. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  191. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  192. storage_config = {'lock_wait': 5}
  193. flexmock(module.environment).should_receive('make_environment')
  194. flexmock(module).should_receive('execute_command').with_args(
  195. ('borg', 'info', '--lock-wait', '5', '--repo', 'repo'),
  196. output_log_level=module.borgmatic.logger.ANSWER,
  197. borg_local_path='borg',
  198. extra_environment=None,
  199. )
  200. module.display_archives_info(
  201. repository_path='repo',
  202. storage_config=storage_config,
  203. local_borg_version='2.3.4',
  204. info_arguments=flexmock(archive=None, json=False, prefix=None),
  205. )
  206. def test_display_archives_info_with_prefix_calls_borg_with_match_archives_parameters():
  207. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  208. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  209. flexmock(module.flags).should_receive('make_flags').and_return(())
  210. flexmock(module.flags).should_receive('make_flags').with_args(
  211. 'match-archives', 'sh:foo*'
  212. ).and_return(('--match-archives', 'sh:foo*'))
  213. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  214. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  215. flexmock(module.environment).should_receive('make_environment')
  216. flexmock(module).should_receive('execute_command').with_args(
  217. ('borg', 'info', '--match-archives', 'sh:foo*', '--repo', 'repo'),
  218. output_log_level=module.borgmatic.logger.ANSWER,
  219. borg_local_path='borg',
  220. extra_environment=None,
  221. )
  222. module.display_archives_info(
  223. repository_path='repo',
  224. storage_config={},
  225. local_borg_version='2.3.4',
  226. info_arguments=flexmock(archive=None, json=False, prefix='foo'),
  227. )
  228. @pytest.mark.parametrize('argument_name', ('match_archives', 'sort_by', 'first', 'last'))
  229. def test_display_archives_info_passes_through_arguments_to_borg(argument_name):
  230. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  231. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  232. flag_name = f"--{argument_name.replace('_', ' ')}"
  233. flexmock(module.flags).should_receive('make_flags').and_return(())
  234. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  235. (flag_name, 'value')
  236. )
  237. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  238. flexmock(module.environment).should_receive('make_environment')
  239. flexmock(module).should_receive('execute_command').with_args(
  240. ('borg', 'info', flag_name, 'value', '--repo', 'repo'),
  241. output_log_level=module.borgmatic.logger.ANSWER,
  242. borg_local_path='borg',
  243. extra_environment=None,
  244. )
  245. module.display_archives_info(
  246. repository_path='repo',
  247. storage_config={},
  248. local_borg_version='2.3.4',
  249. info_arguments=flexmock(archive=None, json=False, prefix=None, **{argument_name: 'value'}),
  250. )