test_info.py 14 KB

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