test_rlist.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import argparse
  2. import logging
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic.borg import rlist as module
  6. from ..test_verbosity import insert_logging_mock
  7. BORG_LIST_LATEST_ARGUMENTS = (
  8. '--last',
  9. '1',
  10. '--short',
  11. 'repo',
  12. )
  13. def test_resolve_archive_name_passes_through_non_latest_archive_name():
  14. archive = 'myhost-2030-01-01T14:41:17.647620'
  15. assert (
  16. module.resolve_archive_name('repo', archive, storage_config={}, local_borg_version='1.2.3')
  17. == archive
  18. )
  19. def test_resolve_archive_name_calls_borg_with_parameters():
  20. expected_archive = 'archive-name'
  21. flexmock(module.environment).should_receive('make_environment')
  22. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  23. ('borg', 'list') + BORG_LIST_LATEST_ARGUMENTS, extra_environment=None,
  24. ).and_return(expected_archive + '\n')
  25. assert (
  26. module.resolve_archive_name('repo', 'latest', storage_config={}, local_borg_version='1.2.3')
  27. == expected_archive
  28. )
  29. def test_resolve_archive_name_with_log_info_calls_borg_without_info_parameter():
  30. expected_archive = 'archive-name'
  31. flexmock(module.environment).should_receive('make_environment')
  32. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  33. ('borg', 'list') + BORG_LIST_LATEST_ARGUMENTS, extra_environment=None,
  34. ).and_return(expected_archive + '\n')
  35. insert_logging_mock(logging.INFO)
  36. assert (
  37. module.resolve_archive_name('repo', 'latest', storage_config={}, local_borg_version='1.2.3')
  38. == expected_archive
  39. )
  40. def test_resolve_archive_name_with_log_debug_calls_borg_without_debug_parameter():
  41. expected_archive = 'archive-name'
  42. flexmock(module.environment).should_receive('make_environment')
  43. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  44. ('borg', 'list') + BORG_LIST_LATEST_ARGUMENTS, extra_environment=None,
  45. ).and_return(expected_archive + '\n')
  46. insert_logging_mock(logging.DEBUG)
  47. assert (
  48. module.resolve_archive_name('repo', 'latest', storage_config={}, local_borg_version='1.2.3')
  49. == expected_archive
  50. )
  51. def test_resolve_archive_name_with_local_path_calls_borg_via_local_path():
  52. expected_archive = 'archive-name'
  53. flexmock(module.environment).should_receive('make_environment')
  54. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  55. ('borg1', 'list') + BORG_LIST_LATEST_ARGUMENTS, extra_environment=None,
  56. ).and_return(expected_archive + '\n')
  57. assert (
  58. module.resolve_archive_name(
  59. 'repo', 'latest', storage_config={}, local_borg_version='1.2.3', local_path='borg1'
  60. )
  61. == expected_archive
  62. )
  63. def test_resolve_archive_name_with_remote_path_calls_borg_with_remote_path_parameters():
  64. expected_archive = 'archive-name'
  65. flexmock(module.environment).should_receive('make_environment')
  66. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  67. ('borg', 'list', '--remote-path', 'borg1') + BORG_LIST_LATEST_ARGUMENTS,
  68. extra_environment=None,
  69. ).and_return(expected_archive + '\n')
  70. assert (
  71. module.resolve_archive_name(
  72. 'repo', 'latest', storage_config={}, local_borg_version='1.2.3', remote_path='borg1'
  73. )
  74. == expected_archive
  75. )
  76. def test_resolve_archive_name_without_archives_raises():
  77. flexmock(module.environment).should_receive('make_environment')
  78. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  79. ('borg', 'list') + BORG_LIST_LATEST_ARGUMENTS, extra_environment=None,
  80. ).and_return('')
  81. with pytest.raises(ValueError):
  82. module.resolve_archive_name('repo', 'latest', storage_config={}, local_borg_version='1.2.3')
  83. def test_resolve_archive_name_with_lock_wait_calls_borg_with_lock_wait_parameters():
  84. expected_archive = 'archive-name'
  85. flexmock(module.environment).should_receive('make_environment')
  86. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  87. ('borg', 'list', '--lock-wait', 'okay') + BORG_LIST_LATEST_ARGUMENTS,
  88. extra_environment=None,
  89. ).and_return(expected_archive + '\n')
  90. assert (
  91. module.resolve_archive_name(
  92. 'repo', 'latest', storage_config={'lock_wait': 'okay'}, local_borg_version='1.2.3'
  93. )
  94. == expected_archive
  95. )
  96. def test_make_rlist_command_includes_log_info():
  97. insert_logging_mock(logging.INFO)
  98. flexmock(module.flags).should_receive('make_flags').and_return(())
  99. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  100. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  101. command = module.make_rlist_command(
  102. repository_path='repo',
  103. storage_config={},
  104. local_borg_version='1.2.3',
  105. rlist_arguments=flexmock(archive=None, paths=None, json=False, prefix=None),
  106. )
  107. assert command == ('borg', 'list', '--info', 'repo')
  108. def test_make_rlist_command_includes_json_but_not_info():
  109. insert_logging_mock(logging.INFO)
  110. flexmock(module.flags).should_receive('make_flags').and_return(())
  111. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  112. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  113. command = module.make_rlist_command(
  114. repository_path='repo',
  115. storage_config={},
  116. local_borg_version='1.2.3',
  117. rlist_arguments=flexmock(archive=None, paths=None, json=True, prefix=None),
  118. )
  119. assert command == ('borg', 'list', '--json', 'repo')
  120. def test_make_rlist_command_includes_log_debug():
  121. insert_logging_mock(logging.DEBUG)
  122. flexmock(module.flags).should_receive('make_flags').and_return(())
  123. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  124. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  125. command = module.make_rlist_command(
  126. repository_path='repo',
  127. storage_config={},
  128. local_borg_version='1.2.3',
  129. rlist_arguments=flexmock(archive=None, paths=None, json=False, prefix=None),
  130. )
  131. assert command == ('borg', 'list', '--debug', '--show-rc', 'repo')
  132. def test_make_rlist_command_includes_json_but_not_debug():
  133. insert_logging_mock(logging.DEBUG)
  134. flexmock(module.flags).should_receive('make_flags').and_return(())
  135. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  136. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  137. command = module.make_rlist_command(
  138. repository_path='repo',
  139. storage_config={},
  140. local_borg_version='1.2.3',
  141. rlist_arguments=flexmock(archive=None, paths=None, json=True, prefix=None),
  142. )
  143. assert command == ('borg', 'list', '--json', 'repo')
  144. def test_make_rlist_command_includes_json():
  145. flexmock(module.flags).should_receive('make_flags').and_return(())
  146. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  147. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  148. command = module.make_rlist_command(
  149. repository_path='repo',
  150. storage_config={},
  151. local_borg_version='1.2.3',
  152. rlist_arguments=flexmock(archive=None, paths=None, json=True, prefix=None),
  153. )
  154. assert command == ('borg', 'list', '--json', 'repo')
  155. def test_make_rlist_command_includes_lock_wait():
  156. flexmock(module.flags).should_receive('make_flags').and_return(()).and_return(
  157. ('--lock-wait', '5')
  158. ).and_return(())
  159. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  160. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  161. command = module.make_rlist_command(
  162. repository_path='repo',
  163. storage_config={'lock_wait': 5},
  164. local_borg_version='1.2.3',
  165. rlist_arguments=flexmock(archive=None, paths=None, json=False, prefix=None),
  166. )
  167. assert command == ('borg', 'list', '--lock-wait', '5', 'repo')
  168. def test_make_rlist_command_includes_local_path():
  169. flexmock(module.flags).should_receive('make_flags').and_return(())
  170. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  171. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  172. command = module.make_rlist_command(
  173. repository_path='repo',
  174. storage_config={},
  175. local_borg_version='1.2.3',
  176. rlist_arguments=flexmock(archive=None, paths=None, json=False, prefix=None),
  177. local_path='borg2',
  178. )
  179. assert command == ('borg2', 'list', 'repo')
  180. def test_make_rlist_command_includes_remote_path():
  181. flexmock(module.flags).should_receive('make_flags').and_return(
  182. ('--remote-path', 'borg2')
  183. ).and_return(()).and_return(())
  184. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  185. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  186. command = module.make_rlist_command(
  187. repository_path='repo',
  188. storage_config={},
  189. local_borg_version='1.2.3',
  190. rlist_arguments=flexmock(archive=None, paths=None, json=False, prefix=None),
  191. remote_path='borg2',
  192. )
  193. assert command == ('borg', 'list', '--remote-path', 'borg2', 'repo')
  194. def test_make_rlist_command_transforms_prefix_into_match_archives():
  195. flexmock(module.flags).should_receive('make_flags').and_return(()).and_return(()).and_return(
  196. ('--match-archives', 'sh:foo*')
  197. )
  198. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  199. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  200. command = module.make_rlist_command(
  201. repository_path='repo',
  202. storage_config={},
  203. local_borg_version='1.2.3',
  204. rlist_arguments=flexmock(archive=None, paths=None, json=False, prefix='foo'),
  205. )
  206. assert command == ('borg', 'list', '--match-archives', 'sh:foo*', 'repo')
  207. def test_make_rlist_command_includes_short():
  208. flexmock(module.flags).should_receive('make_flags').and_return(())
  209. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--short',))
  210. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  211. command = module.make_rlist_command(
  212. repository_path='repo',
  213. storage_config={},
  214. local_borg_version='1.2.3',
  215. rlist_arguments=flexmock(archive=None, paths=None, json=False, prefix=None, short=True),
  216. )
  217. assert command == ('borg', 'list', '--short', 'repo')
  218. @pytest.mark.parametrize(
  219. 'argument_name',
  220. (
  221. 'match_archives',
  222. 'sort_by',
  223. 'first',
  224. 'last',
  225. 'exclude',
  226. 'exclude_from',
  227. 'pattern',
  228. 'patterns_from',
  229. ),
  230. )
  231. def test_make_rlist_command_includes_additional_flags(argument_name):
  232. flexmock(module.flags).should_receive('make_flags').and_return(())
  233. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  234. (f"--{argument_name.replace('_', '-')}", 'value')
  235. )
  236. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  237. command = module.make_rlist_command(
  238. repository_path='repo',
  239. storage_config={},
  240. local_borg_version='1.2.3',
  241. rlist_arguments=flexmock(
  242. archive=None,
  243. paths=None,
  244. json=False,
  245. prefix=None,
  246. find_paths=None,
  247. format=None,
  248. **{argument_name: 'value'},
  249. ),
  250. )
  251. assert command == ('borg', 'list', '--' + argument_name.replace('_', '-'), 'value', 'repo')
  252. def test_list_repository_calls_borg_with_parameters():
  253. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  254. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  255. rlist_arguments = argparse.Namespace(json=False)
  256. flexmock(module.feature).should_receive('available').and_return(False)
  257. flexmock(module).should_receive('make_rlist_command').with_args(
  258. repository_path='repo',
  259. storage_config={},
  260. local_borg_version='1.2.3',
  261. rlist_arguments=rlist_arguments,
  262. local_path='borg',
  263. remote_path=None,
  264. ).and_return(('borg', 'rlist', 'repo'))
  265. flexmock(module.environment).should_receive('make_environment')
  266. flexmock(module).should_receive('execute_command').with_args(
  267. ('borg', 'rlist', 'repo'),
  268. output_log_level=module.borgmatic.logger.ANSWER,
  269. borg_local_path='borg',
  270. extra_environment=None,
  271. ).once()
  272. module.list_repository(
  273. repository_path='repo',
  274. storage_config={},
  275. local_borg_version='1.2.3',
  276. rlist_arguments=rlist_arguments,
  277. )
  278. def test_list_repository_with_json_returns_borg_output():
  279. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  280. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  281. rlist_arguments = argparse.Namespace(json=True)
  282. json_output = flexmock()
  283. flexmock(module.feature).should_receive('available').and_return(False)
  284. flexmock(module).should_receive('make_rlist_command').with_args(
  285. repository_path='repo',
  286. storage_config={},
  287. local_borg_version='1.2.3',
  288. rlist_arguments=rlist_arguments,
  289. local_path='borg',
  290. remote_path=None,
  291. ).and_return(('borg', 'rlist', 'repo'))
  292. flexmock(module.environment).should_receive('make_environment')
  293. flexmock(module).should_receive('execute_command_and_capture_output').and_return(json_output)
  294. assert (
  295. module.list_repository(
  296. repository_path='repo',
  297. storage_config={},
  298. local_borg_version='1.2.3',
  299. rlist_arguments=rlist_arguments,
  300. )
  301. == json_output
  302. )