test_info.py 9.0 KB

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