test_rlist.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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,
  24. extra_environment=None,
  25. ).and_return(expected_archive + '\n')
  26. assert (
  27. module.resolve_archive_name('repo', 'latest', storage_config={}, local_borg_version='1.2.3')
  28. == expected_archive
  29. )
  30. def test_resolve_archive_name_with_log_info_calls_borg_without_info_parameter():
  31. expected_archive = 'archive-name'
  32. flexmock(module.environment).should_receive('make_environment')
  33. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  34. ('borg', 'list') + BORG_LIST_LATEST_ARGUMENTS,
  35. extra_environment=None,
  36. ).and_return(expected_archive + '\n')
  37. insert_logging_mock(logging.INFO)
  38. assert (
  39. module.resolve_archive_name('repo', 'latest', storage_config={}, local_borg_version='1.2.3')
  40. == expected_archive
  41. )
  42. def test_resolve_archive_name_with_log_debug_calls_borg_without_debug_parameter():
  43. expected_archive = 'archive-name'
  44. flexmock(module.environment).should_receive('make_environment')
  45. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  46. ('borg', 'list') + BORG_LIST_LATEST_ARGUMENTS,
  47. extra_environment=None,
  48. ).and_return(expected_archive + '\n')
  49. insert_logging_mock(logging.DEBUG)
  50. assert (
  51. module.resolve_archive_name('repo', 'latest', storage_config={}, local_borg_version='1.2.3')
  52. == expected_archive
  53. )
  54. def test_resolve_archive_name_with_local_path_calls_borg_via_local_path():
  55. expected_archive = 'archive-name'
  56. flexmock(module.environment).should_receive('make_environment')
  57. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  58. ('borg1', 'list') + BORG_LIST_LATEST_ARGUMENTS,
  59. extra_environment=None,
  60. ).and_return(expected_archive + '\n')
  61. assert (
  62. module.resolve_archive_name(
  63. 'repo', 'latest', storage_config={}, local_borg_version='1.2.3', local_path='borg1'
  64. )
  65. == expected_archive
  66. )
  67. def test_resolve_archive_name_with_remote_path_calls_borg_with_remote_path_parameters():
  68. expected_archive = 'archive-name'
  69. flexmock(module.environment).should_receive('make_environment')
  70. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  71. ('borg', 'list', '--remote-path', 'borg1') + BORG_LIST_LATEST_ARGUMENTS,
  72. extra_environment=None,
  73. ).and_return(expected_archive + '\n')
  74. assert (
  75. module.resolve_archive_name(
  76. 'repo', 'latest', storage_config={}, local_borg_version='1.2.3', remote_path='borg1'
  77. )
  78. == expected_archive
  79. )
  80. def test_resolve_archive_name_without_archives_raises():
  81. flexmock(module.environment).should_receive('make_environment')
  82. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  83. ('borg', 'list') + BORG_LIST_LATEST_ARGUMENTS,
  84. extra_environment=None,
  85. ).and_return('')
  86. with pytest.raises(ValueError):
  87. module.resolve_archive_name('repo', 'latest', storage_config={}, local_borg_version='1.2.3')
  88. def test_resolve_archive_name_with_lock_wait_calls_borg_with_lock_wait_parameters():
  89. expected_archive = 'archive-name'
  90. flexmock(module.environment).should_receive('make_environment')
  91. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  92. ('borg', 'list', '--lock-wait', 'okay') + BORG_LIST_LATEST_ARGUMENTS,
  93. extra_environment=None,
  94. ).and_return(expected_archive + '\n')
  95. assert (
  96. module.resolve_archive_name(
  97. 'repo', 'latest', storage_config={'lock_wait': 'okay'}, local_borg_version='1.2.3'
  98. )
  99. == expected_archive
  100. )
  101. def test_make_rlist_command_includes_log_info():
  102. insert_logging_mock(logging.INFO)
  103. flexmock(module.flags).should_receive('make_flags').and_return(())
  104. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  105. None, None, '1.2.3'
  106. ).and_return(())
  107. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  108. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  109. command = module.make_rlist_command(
  110. repository_path='repo',
  111. storage_config={},
  112. local_borg_version='1.2.3',
  113. rlist_arguments=flexmock(
  114. archive=None, paths=None, json=False, prefix=None, match_archives=None
  115. ),
  116. )
  117. assert command == ('borg', 'list', '--info', 'repo')
  118. def test_make_rlist_command_includes_json_but_not_info():
  119. insert_logging_mock(logging.INFO)
  120. flexmock(module.flags).should_receive('make_flags').and_return(())
  121. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  122. None, None, '1.2.3'
  123. ).and_return(())
  124. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  125. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  126. command = module.make_rlist_command(
  127. repository_path='repo',
  128. storage_config={},
  129. local_borg_version='1.2.3',
  130. rlist_arguments=flexmock(
  131. archive=None, paths=None, json=True, prefix=None, match_archives=None
  132. ),
  133. )
  134. assert command == ('borg', 'list', '--json', 'repo')
  135. def test_make_rlist_command_includes_log_debug():
  136. insert_logging_mock(logging.DEBUG)
  137. flexmock(module.flags).should_receive('make_flags').and_return(())
  138. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  139. None, None, '1.2.3'
  140. ).and_return(())
  141. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  142. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  143. command = module.make_rlist_command(
  144. repository_path='repo',
  145. storage_config={},
  146. local_borg_version='1.2.3',
  147. rlist_arguments=flexmock(
  148. archive=None, paths=None, json=False, prefix=None, match_archives=None
  149. ),
  150. )
  151. assert command == ('borg', 'list', '--debug', '--show-rc', 'repo')
  152. def test_make_rlist_command_includes_json_but_not_debug():
  153. insert_logging_mock(logging.DEBUG)
  154. flexmock(module.flags).should_receive('make_flags').and_return(())
  155. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  156. None, None, '1.2.3'
  157. ).and_return(())
  158. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  159. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  160. command = module.make_rlist_command(
  161. repository_path='repo',
  162. storage_config={},
  163. local_borg_version='1.2.3',
  164. rlist_arguments=flexmock(
  165. archive=None, paths=None, json=True, prefix=None, match_archives=None
  166. ),
  167. )
  168. assert command == ('borg', 'list', '--json', 'repo')
  169. def test_make_rlist_command_includes_json():
  170. flexmock(module.flags).should_receive('make_flags').and_return(())
  171. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  172. None, None, '1.2.3'
  173. ).and_return(())
  174. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  175. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  176. command = module.make_rlist_command(
  177. repository_path='repo',
  178. storage_config={},
  179. local_borg_version='1.2.3',
  180. rlist_arguments=flexmock(
  181. archive=None, paths=None, json=True, prefix=None, match_archives=None
  182. ),
  183. )
  184. assert command == ('borg', 'list', '--json', 'repo')
  185. def test_make_rlist_command_includes_lock_wait():
  186. flexmock(module.flags).should_receive('make_flags').and_return(()).and_return(
  187. ('--lock-wait', '5')
  188. ).and_return(())
  189. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  190. None, None, '1.2.3'
  191. ).and_return(())
  192. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  193. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  194. command = module.make_rlist_command(
  195. repository_path='repo',
  196. storage_config={'lock_wait': 5},
  197. local_borg_version='1.2.3',
  198. rlist_arguments=flexmock(
  199. archive=None, paths=None, json=False, prefix=None, match_archives=None
  200. ),
  201. )
  202. assert command == ('borg', 'list', '--lock-wait', '5', 'repo')
  203. def test_make_rlist_command_includes_local_path():
  204. flexmock(module.flags).should_receive('make_flags').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(
  215. archive=None, paths=None, json=False, prefix=None, match_archives=None
  216. ),
  217. local_path='borg2',
  218. )
  219. assert command == ('borg2', 'list', 'repo')
  220. def test_make_rlist_command_includes_remote_path():
  221. flexmock(module.flags).should_receive('make_flags').and_return(
  222. ('--remote-path', 'borg2')
  223. ).and_return(()).and_return(())
  224. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  225. None, None, '1.2.3'
  226. ).and_return(())
  227. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  228. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  229. command = module.make_rlist_command(
  230. repository_path='repo',
  231. storage_config={},
  232. local_borg_version='1.2.3',
  233. rlist_arguments=flexmock(
  234. archive=None, paths=None, json=False, prefix=None, match_archives=None
  235. ),
  236. remote_path='borg2',
  237. )
  238. assert command == ('borg', 'list', '--remote-path', 'borg2', 'repo')
  239. def test_make_rlist_command_transforms_prefix_into_match_archives():
  240. flexmock(module.flags).should_receive('make_flags').and_return(()).and_return(()).and_return(
  241. ('--match-archives', 'sh:foo*')
  242. )
  243. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  244. None, None, '1.2.3'
  245. ).and_return(())
  246. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  247. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  248. command = module.make_rlist_command(
  249. repository_path='repo',
  250. storage_config={},
  251. local_borg_version='1.2.3',
  252. rlist_arguments=flexmock(archive=None, paths=None, json=False, prefix='foo'),
  253. )
  254. assert command == ('borg', 'list', '--match-archives', 'sh:foo*', 'repo')
  255. def test_make_rlist_command_prefers_prefix_over_archive_name_format():
  256. flexmock(module.flags).should_receive('make_flags').and_return(()).and_return(()).and_return(
  257. ('--match-archives', 'sh:foo*')
  258. )
  259. flexmock(module.flags).should_receive('make_match_archives_flags').never()
  260. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  261. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  262. command = module.make_rlist_command(
  263. repository_path='repo',
  264. storage_config={'archive_name_format': 'bar-{now}'}, # noqa: FS003
  265. local_borg_version='1.2.3',
  266. rlist_arguments=flexmock(archive=None, paths=None, json=False, prefix='foo'),
  267. )
  268. assert command == ('borg', 'list', '--match-archives', 'sh:foo*', 'repo')
  269. def test_make_rlist_command_transforms_archive_name_format_into_match_archives():
  270. flexmock(module.flags).should_receive('make_flags').and_return(())
  271. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  272. None, 'bar-{now}', '1.2.3' # noqa: FS003
  273. ).and_return(('--match-archives', 'sh:bar-*'))
  274. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  275. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  276. command = module.make_rlist_command(
  277. repository_path='repo',
  278. storage_config={'archive_name_format': 'bar-{now}'}, # noqa: FS003
  279. local_borg_version='1.2.3',
  280. rlist_arguments=flexmock(
  281. archive=None, paths=None, json=False, prefix=None, match_archives=None
  282. ),
  283. )
  284. assert command == ('borg', 'list', '--match-archives', 'sh:bar-*', 'repo')
  285. def test_make_rlist_command_includes_short():
  286. flexmock(module.flags).should_receive('make_flags').and_return(())
  287. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  288. None, None, '1.2.3'
  289. ).and_return(())
  290. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--short',))
  291. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  292. command = module.make_rlist_command(
  293. repository_path='repo',
  294. storage_config={},
  295. local_borg_version='1.2.3',
  296. rlist_arguments=flexmock(
  297. archive=None, paths=None, json=False, prefix=None, match_archives=None, short=True
  298. ),
  299. )
  300. assert command == ('borg', 'list', '--short', 'repo')
  301. @pytest.mark.parametrize(
  302. 'argument_name',
  303. (
  304. 'sort_by',
  305. 'first',
  306. 'last',
  307. 'exclude',
  308. 'exclude_from',
  309. 'pattern',
  310. 'patterns_from',
  311. ),
  312. )
  313. def test_make_rlist_command_includes_additional_flags(argument_name):
  314. flexmock(module.flags).should_receive('make_flags').and_return(())
  315. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  316. None, None, '1.2.3'
  317. ).and_return(())
  318. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  319. (f"--{argument_name.replace('_', '-')}", 'value')
  320. )
  321. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  322. command = module.make_rlist_command(
  323. repository_path='repo',
  324. storage_config={},
  325. local_borg_version='1.2.3',
  326. rlist_arguments=flexmock(
  327. archive=None,
  328. paths=None,
  329. json=False,
  330. prefix=None,
  331. match_archives=None,
  332. find_paths=None,
  333. format=None,
  334. **{argument_name: 'value'},
  335. ),
  336. )
  337. assert command == ('borg', 'list', '--' + argument_name.replace('_', '-'), 'value', 'repo')
  338. def test_make_rlist_command_with_match_archives_calls_borg_with_match_archives_parameters():
  339. flexmock(module.flags).should_receive('make_flags').and_return(())
  340. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  341. None, None, '1.2.3'
  342. ).and_return(())
  343. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  344. 'foo-*',
  345. None,
  346. '1.2.3',
  347. ).and_return(('--match-archives', 'foo-*'))
  348. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  349. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  350. command = module.make_rlist_command(
  351. repository_path='repo',
  352. storage_config={},
  353. local_borg_version='1.2.3',
  354. rlist_arguments=flexmock(
  355. archive=None,
  356. paths=None,
  357. json=False,
  358. prefix=None,
  359. match_archives='foo-*',
  360. find_paths=None,
  361. format=None,
  362. ),
  363. )
  364. assert command == ('borg', 'list', '--match-archives', 'foo-*', 'repo')
  365. def test_list_repository_calls_borg_with_parameters():
  366. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  367. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  368. rlist_arguments = argparse.Namespace(json=False)
  369. flexmock(module.feature).should_receive('available').and_return(False)
  370. flexmock(module).should_receive('make_rlist_command').with_args(
  371. repository_path='repo',
  372. storage_config={},
  373. local_borg_version='1.2.3',
  374. rlist_arguments=rlist_arguments,
  375. local_path='borg',
  376. remote_path=None,
  377. ).and_return(('borg', 'rlist', 'repo'))
  378. flexmock(module.environment).should_receive('make_environment')
  379. flexmock(module).should_receive('execute_command').with_args(
  380. ('borg', 'rlist', 'repo'),
  381. output_log_level=module.borgmatic.logger.ANSWER,
  382. borg_local_path='borg',
  383. extra_environment=None,
  384. ).once()
  385. module.list_repository(
  386. repository_path='repo',
  387. storage_config={},
  388. local_borg_version='1.2.3',
  389. rlist_arguments=rlist_arguments,
  390. )
  391. def test_list_repository_with_json_returns_borg_output():
  392. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  393. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  394. rlist_arguments = argparse.Namespace(json=True)
  395. json_output = flexmock()
  396. flexmock(module.feature).should_receive('available').and_return(False)
  397. flexmock(module).should_receive('make_rlist_command').with_args(
  398. repository_path='repo',
  399. storage_config={},
  400. local_borg_version='1.2.3',
  401. rlist_arguments=rlist_arguments,
  402. local_path='borg',
  403. remote_path=None,
  404. ).and_return(('borg', 'rlist', 'repo'))
  405. flexmock(module.environment).should_receive('make_environment')
  406. flexmock(module).should_receive('execute_command_and_capture_output').and_return(json_output)
  407. assert (
  408. module.list_repository(
  409. repository_path='repo',
  410. storage_config={},
  411. local_borg_version='1.2.3',
  412. rlist_arguments=rlist_arguments,
  413. )
  414. == json_output
  415. )