2
0

test_info.py 11 KB

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