test_rlist.py 18 KB

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