test_info.py 11 KB

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