test_rlist.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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_match_archives_flags').with_args(
  100. None, None, '1.2.3'
  101. ).and_return(())
  102. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  103. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  104. command = module.make_rlist_command(
  105. repository_path='repo',
  106. storage_config={},
  107. local_borg_version='1.2.3',
  108. rlist_arguments=flexmock(archive=None, paths=None, json=False, prefix=None),
  109. )
  110. assert command == ('borg', 'list', '--info', 'repo')
  111. def test_make_rlist_command_includes_json_but_not_info():
  112. insert_logging_mock(logging.INFO)
  113. flexmock(module.flags).should_receive('make_flags').and_return(())
  114. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  115. None, None, '1.2.3'
  116. ).and_return(())
  117. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  118. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  119. command = module.make_rlist_command(
  120. repository_path='repo',
  121. storage_config={},
  122. local_borg_version='1.2.3',
  123. rlist_arguments=flexmock(archive=None, paths=None, json=True, prefix=None),
  124. )
  125. assert command == ('borg', 'list', '--json', 'repo')
  126. def test_make_rlist_command_includes_log_debug():
  127. insert_logging_mock(logging.DEBUG)
  128. flexmock(module.flags).should_receive('make_flags').and_return(())
  129. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  130. None, None, '1.2.3'
  131. ).and_return(())
  132. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  133. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  134. command = module.make_rlist_command(
  135. repository_path='repo',
  136. storage_config={},
  137. local_borg_version='1.2.3',
  138. rlist_arguments=flexmock(archive=None, paths=None, json=False, prefix=None),
  139. )
  140. assert command == ('borg', 'list', '--debug', '--show-rc', 'repo')
  141. def test_make_rlist_command_includes_json_but_not_debug():
  142. insert_logging_mock(logging.DEBUG)
  143. flexmock(module.flags).should_receive('make_flags').and_return(())
  144. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  145. None, None, '1.2.3'
  146. ).and_return(())
  147. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  148. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  149. command = module.make_rlist_command(
  150. repository_path='repo',
  151. storage_config={},
  152. local_borg_version='1.2.3',
  153. rlist_arguments=flexmock(archive=None, paths=None, json=True, prefix=None),
  154. )
  155. assert command == ('borg', 'list', '--json', 'repo')
  156. def test_make_rlist_command_includes_json():
  157. flexmock(module.flags).should_receive('make_flags').and_return(())
  158. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  159. None, None, '1.2.3'
  160. ).and_return(())
  161. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  162. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  163. command = module.make_rlist_command(
  164. repository_path='repo',
  165. storage_config={},
  166. local_borg_version='1.2.3',
  167. rlist_arguments=flexmock(archive=None, paths=None, json=True, prefix=None),
  168. )
  169. assert command == ('borg', 'list', '--json', 'repo')
  170. def test_make_rlist_command_includes_lock_wait():
  171. flexmock(module.flags).should_receive('make_flags').and_return(()).and_return(
  172. ('--lock-wait', '5')
  173. ).and_return(())
  174. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  175. None, None, '1.2.3'
  176. ).and_return(())
  177. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  178. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  179. command = module.make_rlist_command(
  180. repository_path='repo',
  181. storage_config={'lock_wait': 5},
  182. local_borg_version='1.2.3',
  183. rlist_arguments=flexmock(archive=None, paths=None, json=False, prefix=None),
  184. )
  185. assert command == ('borg', 'list', '--lock-wait', '5', 'repo')
  186. def test_make_rlist_command_includes_local_path():
  187. flexmock(module.flags).should_receive('make_flags').and_return(())
  188. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  189. None, None, '1.2.3'
  190. ).and_return(())
  191. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  192. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  193. command = module.make_rlist_command(
  194. repository_path='repo',
  195. storage_config={},
  196. local_borg_version='1.2.3',
  197. rlist_arguments=flexmock(archive=None, paths=None, json=False, prefix=None),
  198. local_path='borg2',
  199. )
  200. assert command == ('borg2', 'list', 'repo')
  201. def test_make_rlist_command_includes_remote_path():
  202. flexmock(module.flags).should_receive('make_flags').and_return(
  203. ('--remote-path', 'borg2')
  204. ).and_return(()).and_return(())
  205. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  206. None, None, '1.2.3'
  207. ).and_return(())
  208. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  209. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  210. command = module.make_rlist_command(
  211. repository_path='repo',
  212. storage_config={},
  213. local_borg_version='1.2.3',
  214. rlist_arguments=flexmock(archive=None, paths=None, json=False, prefix=None),
  215. remote_path='borg2',
  216. )
  217. assert command == ('borg', 'list', '--remote-path', 'borg2', 'repo')
  218. def test_make_rlist_command_transforms_prefix_into_match_archives():
  219. flexmock(module.flags).should_receive('make_flags').and_return(()).and_return(()).and_return(
  220. ('--match-archives', 'sh:foo*')
  221. )
  222. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  223. None, None, '1.2.3'
  224. ).and_return(())
  225. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  226. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  227. command = module.make_rlist_command(
  228. repository_path='repo',
  229. storage_config={},
  230. local_borg_version='1.2.3',
  231. rlist_arguments=flexmock(archive=None, paths=None, json=False, prefix='foo'),
  232. )
  233. assert command == ('borg', 'list', '--match-archives', 'sh:foo*', 'repo')
  234. def test_make_rlist_command_prefers_prefix_over_archive_name_format():
  235. flexmock(module.flags).should_receive('make_flags').and_return(()).and_return(()).and_return(
  236. ('--match-archives', 'sh:foo*')
  237. )
  238. flexmock(module.flags).should_receive('make_match_archives_flags').never()
  239. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  240. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  241. command = module.make_rlist_command(
  242. repository_path='repo',
  243. storage_config={'archive_name_format': 'bar-{now}'}, # noqa: FS003
  244. local_borg_version='1.2.3',
  245. rlist_arguments=flexmock(archive=None, paths=None, json=False, prefix='foo'),
  246. )
  247. assert command == ('borg', 'list', '--match-archives', 'sh:foo*', 'repo')
  248. def test_make_rlist_command_transforms_archive_name_format_into_match_archives():
  249. flexmock(module.flags).should_receive('make_flags').and_return(())
  250. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  251. None, 'bar-{now}', '1.2.3' # noqa: FS003
  252. ).and_return(('--match-archives', 'sh:bar-*'))
  253. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  254. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  255. command = module.make_rlist_command(
  256. repository_path='repo',
  257. storage_config={'archive_name_format': 'bar-{now}'}, # noqa: FS003
  258. local_borg_version='1.2.3',
  259. rlist_arguments=flexmock(archive=None, paths=None, json=False, prefix=None),
  260. )
  261. assert command == ('borg', 'list', '--match-archives', 'sh:bar-*', 'repo')
  262. def test_make_rlist_command_includes_short():
  263. flexmock(module.flags).should_receive('make_flags').and_return(())
  264. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  265. None, None, '1.2.3'
  266. ).and_return(())
  267. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--short',))
  268. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  269. command = module.make_rlist_command(
  270. repository_path='repo',
  271. storage_config={},
  272. local_borg_version='1.2.3',
  273. rlist_arguments=flexmock(archive=None, paths=None, json=False, prefix=None, short=True),
  274. )
  275. assert command == ('borg', 'list', '--short', 'repo')
  276. @pytest.mark.parametrize(
  277. 'argument_name',
  278. (
  279. 'match_archives',
  280. 'sort_by',
  281. 'first',
  282. 'last',
  283. 'exclude',
  284. 'exclude_from',
  285. 'pattern',
  286. 'patterns_from',
  287. ),
  288. )
  289. def test_make_rlist_command_includes_additional_flags(argument_name):
  290. flexmock(module.flags).should_receive('make_flags').and_return(())
  291. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  292. None, None, '1.2.3'
  293. ).and_return(())
  294. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  295. (f"--{argument_name.replace('_', '-')}", 'value')
  296. )
  297. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  298. command = module.make_rlist_command(
  299. repository_path='repo',
  300. storage_config={},
  301. local_borg_version='1.2.3',
  302. rlist_arguments=flexmock(
  303. archive=None,
  304. paths=None,
  305. json=False,
  306. prefix=None,
  307. find_paths=None,
  308. format=None,
  309. **{argument_name: 'value'},
  310. ),
  311. )
  312. assert command == ('borg', 'list', '--' + argument_name.replace('_', '-'), 'value', 'repo')
  313. def test_list_repository_calls_borg_with_parameters():
  314. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  315. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  316. rlist_arguments = argparse.Namespace(json=False)
  317. flexmock(module.feature).should_receive('available').and_return(False)
  318. flexmock(module).should_receive('make_rlist_command').with_args(
  319. repository_path='repo',
  320. storage_config={},
  321. local_borg_version='1.2.3',
  322. rlist_arguments=rlist_arguments,
  323. local_path='borg',
  324. remote_path=None,
  325. ).and_return(('borg', 'rlist', 'repo'))
  326. flexmock(module.environment).should_receive('make_environment')
  327. flexmock(module).should_receive('execute_command').with_args(
  328. ('borg', 'rlist', 'repo'),
  329. output_log_level=module.borgmatic.logger.ANSWER,
  330. borg_local_path='borg',
  331. extra_environment=None,
  332. ).once()
  333. module.list_repository(
  334. repository_path='repo',
  335. storage_config={},
  336. local_borg_version='1.2.3',
  337. rlist_arguments=rlist_arguments,
  338. )
  339. def test_list_repository_with_json_returns_borg_output():
  340. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  341. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  342. rlist_arguments = argparse.Namespace(json=True)
  343. json_output = flexmock()
  344. flexmock(module.feature).should_receive('available').and_return(False)
  345. flexmock(module).should_receive('make_rlist_command').with_args(
  346. repository_path='repo',
  347. storage_config={},
  348. local_borg_version='1.2.3',
  349. rlist_arguments=rlist_arguments,
  350. local_path='borg',
  351. remote_path=None,
  352. ).and_return(('borg', 'rlist', 'repo'))
  353. flexmock(module.environment).should_receive('make_environment')
  354. flexmock(module).should_receive('execute_command_and_capture_output').and_return(json_output)
  355. assert (
  356. module.list_repository(
  357. repository_path='repo',
  358. storage_config={},
  359. local_borg_version='1.2.3',
  360. rlist_arguments=rlist_arguments,
  361. )
  362. == json_output
  363. )