2
0

test_rlist.py 14 KB

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