test_list.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. import argparse
  2. import logging
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic.borg import list as module
  6. from ..test_verbosity import insert_logging_mock
  7. def test_make_list_command_includes_log_info():
  8. insert_logging_mock(logging.INFO)
  9. flexmock(module.flags).should_receive('make_flags').and_return(())
  10. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  11. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  12. command = module.make_list_command(
  13. repository='repo',
  14. storage_config={},
  15. local_borg_version='1.2.3',
  16. list_arguments=flexmock(archive=None, paths=None, json=False),
  17. )
  18. assert command == ('borg', 'list', '--info', 'repo')
  19. def test_make_list_command_includes_json_but_not_info():
  20. insert_logging_mock(logging.INFO)
  21. flexmock(module.flags).should_receive('make_flags').and_return(())
  22. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  23. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  24. command = module.make_list_command(
  25. repository='repo',
  26. storage_config={},
  27. local_borg_version='1.2.3',
  28. list_arguments=flexmock(archive=None, paths=None, json=True),
  29. )
  30. assert command == ('borg', 'list', '--json', 'repo')
  31. def test_make_list_command_includes_log_debug():
  32. insert_logging_mock(logging.DEBUG)
  33. flexmock(module.flags).should_receive('make_flags').and_return(())
  34. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  35. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  36. command = module.make_list_command(
  37. repository='repo',
  38. storage_config={},
  39. local_borg_version='1.2.3',
  40. list_arguments=flexmock(archive=None, paths=None, json=False),
  41. )
  42. assert command == ('borg', 'list', '--debug', '--show-rc', 'repo')
  43. def test_make_list_command_includes_json_but_not_debug():
  44. insert_logging_mock(logging.DEBUG)
  45. flexmock(module.flags).should_receive('make_flags').and_return(())
  46. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  47. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  48. command = module.make_list_command(
  49. repository='repo',
  50. storage_config={},
  51. local_borg_version='1.2.3',
  52. list_arguments=flexmock(archive=None, paths=None, json=True),
  53. )
  54. assert command == ('borg', 'list', '--json', 'repo')
  55. def test_make_list_command_includes_json():
  56. flexmock(module.flags).should_receive('make_flags').and_return(())
  57. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  58. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  59. command = module.make_list_command(
  60. repository='repo',
  61. storage_config={},
  62. local_borg_version='1.2.3',
  63. list_arguments=flexmock(archive=None, paths=None, json=True),
  64. )
  65. assert command == ('borg', 'list', '--json', 'repo')
  66. def test_make_list_command_includes_lock_wait():
  67. flexmock(module.flags).should_receive('make_flags').and_return(()).and_return(
  68. ('--lock-wait', '5')
  69. )
  70. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  71. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  72. command = module.make_list_command(
  73. repository='repo',
  74. storage_config={'lock_wait': 5},
  75. local_borg_version='1.2.3',
  76. list_arguments=flexmock(archive=None, paths=None, json=False),
  77. )
  78. assert command == ('borg', 'list', '--lock-wait', '5', 'repo')
  79. def test_make_list_command_includes_archive():
  80. flexmock(module.flags).should_receive('make_flags').and_return(())
  81. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  82. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  83. ('repo::archive',)
  84. )
  85. command = module.make_list_command(
  86. repository='repo',
  87. storage_config={},
  88. local_borg_version='1.2.3',
  89. list_arguments=flexmock(archive='archive', paths=None, json=False),
  90. )
  91. assert command == ('borg', 'list', 'repo::archive')
  92. def test_make_list_command_includes_archive_and_path():
  93. flexmock(module.flags).should_receive('make_flags').and_return(())
  94. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  95. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  96. ('repo::archive',)
  97. )
  98. command = module.make_list_command(
  99. repository='repo',
  100. storage_config={},
  101. local_borg_version='1.2.3',
  102. list_arguments=flexmock(archive='archive', paths=['var/lib'], json=False),
  103. )
  104. assert command == ('borg', 'list', 'repo::archive', 'var/lib')
  105. def test_make_list_command_includes_local_path():
  106. flexmock(module.flags).should_receive('make_flags').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_list_command(
  110. repository='repo',
  111. storage_config={},
  112. local_borg_version='1.2.3',
  113. list_arguments=flexmock(archive=None, paths=None, json=False),
  114. local_path='borg2',
  115. )
  116. assert command == ('borg2', 'list', 'repo')
  117. def test_make_list_command_includes_remote_path():
  118. flexmock(module.flags).should_receive('make_flags').and_return(
  119. ('--remote-path', 'borg2')
  120. ).and_return(())
  121. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  122. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  123. command = module.make_list_command(
  124. repository='repo',
  125. storage_config={},
  126. local_borg_version='1.2.3',
  127. list_arguments=flexmock(archive=None, paths=None, json=False),
  128. remote_path='borg2',
  129. )
  130. assert command == ('borg', 'list', '--remote-path', 'borg2', 'repo')
  131. def test_make_list_command_includes_short():
  132. flexmock(module.flags).should_receive('make_flags').and_return(())
  133. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--short',))
  134. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  135. command = module.make_list_command(
  136. repository='repo',
  137. storage_config={},
  138. local_borg_version='1.2.3',
  139. list_arguments=flexmock(archive=None, paths=None, json=False, short=True),
  140. )
  141. assert command == ('borg', 'list', '--short', 'repo')
  142. @pytest.mark.parametrize(
  143. 'argument_name',
  144. (
  145. 'prefix',
  146. 'match_archives',
  147. 'sort_by',
  148. 'first',
  149. 'last',
  150. 'exclude',
  151. 'exclude_from',
  152. 'pattern',
  153. 'patterns_from',
  154. ),
  155. )
  156. def test_make_list_command_includes_additional_flags(argument_name):
  157. flexmock(module.flags).should_receive('make_flags').and_return(())
  158. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  159. (f"--{argument_name.replace('_', '-')}", 'value')
  160. )
  161. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  162. command = module.make_list_command(
  163. repository='repo',
  164. storage_config={},
  165. local_borg_version='1.2.3',
  166. list_arguments=flexmock(
  167. archive=None,
  168. paths=None,
  169. json=False,
  170. find_paths=None,
  171. format=None,
  172. **{argument_name: 'value'},
  173. ),
  174. )
  175. assert command == ('borg', 'list', '--' + argument_name.replace('_', '-'), 'value', 'repo')
  176. def test_make_find_paths_considers_none_as_empty_paths():
  177. assert module.make_find_paths(None) == ()
  178. def test_make_find_paths_passes_through_patterns():
  179. find_paths = (
  180. 'fm:*',
  181. 'sh:**/*.txt',
  182. 're:^.*$',
  183. 'pp:root/somedir',
  184. 'pf:root/foo.txt',
  185. 'R /',
  186. 'r /',
  187. 'p /',
  188. 'P /',
  189. '+ /',
  190. '- /',
  191. '! /',
  192. )
  193. assert module.make_find_paths(find_paths) == find_paths
  194. def test_make_find_paths_adds_globs_to_path_fragments():
  195. assert module.make_find_paths(('foo.txt',)) == ('sh:**/*foo.txt*/**',)
  196. def test_list_archive_calls_borg_with_parameters():
  197. list_arguments = argparse.Namespace(
  198. archive='archive',
  199. paths=None,
  200. json=False,
  201. find_paths=None,
  202. prefix=None,
  203. match_archives=None,
  204. sort_by=None,
  205. first=None,
  206. last=None,
  207. )
  208. flexmock(module.feature).should_receive('available').and_return(False)
  209. flexmock(module).should_receive('make_list_command').with_args(
  210. repository='repo',
  211. storage_config={},
  212. local_borg_version='1.2.3',
  213. list_arguments=list_arguments,
  214. local_path='borg',
  215. remote_path=None,
  216. ).and_return(('borg', 'list', 'repo::archive'))
  217. flexmock(module).should_receive('make_find_paths').and_return(())
  218. flexmock(module.environment).should_receive('make_environment')
  219. flexmock(module).should_receive('execute_command').with_args(
  220. ('borg', 'list', 'repo::archive'),
  221. output_log_level=logging.WARNING,
  222. borg_local_path='borg',
  223. extra_environment=None,
  224. ).once()
  225. module.list_archive(
  226. repository='repo',
  227. storage_config={},
  228. local_borg_version='1.2.3',
  229. list_arguments=list_arguments,
  230. )
  231. def test_list_archive_with_archive_and_json_errors():
  232. list_arguments = argparse.Namespace(archive='archive', paths=None, json=True, find_paths=None)
  233. flexmock(module.feature).should_receive('available').and_return(False)
  234. with pytest.raises(ValueError):
  235. module.list_archive(
  236. repository='repo',
  237. storage_config={},
  238. local_borg_version='1.2.3',
  239. list_arguments=list_arguments,
  240. )
  241. def test_list_archive_calls_borg_with_local_path():
  242. list_arguments = argparse.Namespace(
  243. archive='archive',
  244. paths=None,
  245. json=False,
  246. find_paths=None,
  247. prefix=None,
  248. match_archives=None,
  249. sort_by=None,
  250. first=None,
  251. last=None,
  252. )
  253. flexmock(module.feature).should_receive('available').and_return(False)
  254. flexmock(module).should_receive('make_list_command').with_args(
  255. repository='repo',
  256. storage_config={},
  257. local_borg_version='1.2.3',
  258. list_arguments=list_arguments,
  259. local_path='borg2',
  260. remote_path=None,
  261. ).and_return(('borg2', 'list', 'repo::archive'))
  262. flexmock(module).should_receive('make_find_paths').and_return(())
  263. flexmock(module.environment).should_receive('make_environment')
  264. flexmock(module).should_receive('execute_command').with_args(
  265. ('borg2', 'list', 'repo::archive'),
  266. output_log_level=logging.WARNING,
  267. borg_local_path='borg2',
  268. extra_environment=None,
  269. ).once()
  270. module.list_archive(
  271. repository='repo',
  272. storage_config={},
  273. local_borg_version='1.2.3',
  274. list_arguments=list_arguments,
  275. local_path='borg2',
  276. )
  277. def test_list_archive_calls_borg_multiple_times_with_find_paths():
  278. glob_paths = ('**/*foo.txt*/**',)
  279. list_arguments = argparse.Namespace(
  280. archive=None,
  281. json=False,
  282. find_paths=['foo.txt'],
  283. prefix=None,
  284. match_archives=None,
  285. sort_by=None,
  286. first=None,
  287. last=None,
  288. )
  289. flexmock(module.feature).should_receive('available').and_return(False)
  290. flexmock(module.rlist).should_receive('make_rlist_command').and_return(('borg', 'list', 'repo'))
  291. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  292. ('borg', 'list', 'repo'), extra_environment=None,
  293. ).and_return('archive1\narchive2').once()
  294. flexmock(module).should_receive('make_list_command').and_return(
  295. ('borg', 'list', 'repo::archive1')
  296. ).and_return(('borg', 'list', 'repo::archive2'))
  297. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  298. flexmock(module.environment).should_receive('make_environment')
  299. flexmock(module).should_receive('execute_command').with_args(
  300. ('borg', 'list', 'repo::archive1') + glob_paths,
  301. output_log_level=logging.WARNING,
  302. borg_local_path='borg',
  303. extra_environment=None,
  304. ).once()
  305. flexmock(module).should_receive('execute_command').with_args(
  306. ('borg', 'list', 'repo::archive2') + glob_paths,
  307. output_log_level=logging.WARNING,
  308. borg_local_path='borg',
  309. extra_environment=None,
  310. ).once()
  311. module.list_archive(
  312. repository='repo',
  313. storage_config={},
  314. local_borg_version='1.2.3',
  315. list_arguments=list_arguments,
  316. )
  317. def test_list_archive_calls_borg_with_archive():
  318. list_arguments = argparse.Namespace(
  319. archive='archive',
  320. paths=None,
  321. json=False,
  322. find_paths=None,
  323. prefix=None,
  324. match_archives=None,
  325. sort_by=None,
  326. first=None,
  327. last=None,
  328. )
  329. flexmock(module.feature).should_receive('available').and_return(False)
  330. flexmock(module).should_receive('make_list_command').with_args(
  331. repository='repo',
  332. storage_config={},
  333. local_borg_version='1.2.3',
  334. list_arguments=list_arguments,
  335. local_path='borg',
  336. remote_path=None,
  337. ).and_return(('borg', 'list', 'repo::archive'))
  338. flexmock(module).should_receive('make_find_paths').and_return(())
  339. flexmock(module.environment).should_receive('make_environment')
  340. flexmock(module).should_receive('execute_command').with_args(
  341. ('borg', 'list', 'repo::archive'),
  342. output_log_level=logging.WARNING,
  343. borg_local_path='borg',
  344. extra_environment=None,
  345. ).once()
  346. module.list_archive(
  347. repository='repo',
  348. storage_config={},
  349. local_borg_version='1.2.3',
  350. list_arguments=list_arguments,
  351. )
  352. def test_list_archive_without_archive_delegates_to_list_repository():
  353. list_arguments = argparse.Namespace(
  354. archive=None,
  355. short=None,
  356. format=None,
  357. json=None,
  358. prefix=None,
  359. match_archives=None,
  360. sort_by=None,
  361. first=None,
  362. last=None,
  363. find_paths=None,
  364. )
  365. flexmock(module.feature).should_receive('available').and_return(False)
  366. flexmock(module.rlist).should_receive('list_repository')
  367. flexmock(module.environment).should_receive('make_environment').never()
  368. flexmock(module).should_receive('execute_command').never()
  369. module.list_archive(
  370. repository='repo',
  371. storage_config={},
  372. local_borg_version='1.2.3',
  373. list_arguments=list_arguments,
  374. )
  375. def test_list_archive_with_borg_features_without_archive_delegates_to_list_repository():
  376. list_arguments = argparse.Namespace(
  377. archive=None,
  378. short=None,
  379. format=None,
  380. json=None,
  381. prefix=None,
  382. match_archives=None,
  383. sort_by=None,
  384. first=None,
  385. last=None,
  386. find_paths=None,
  387. )
  388. flexmock(module.feature).should_receive('available').and_return(True)
  389. flexmock(module.rlist).should_receive('list_repository')
  390. flexmock(module.environment).should_receive('make_environment').never()
  391. flexmock(module).should_receive('execute_command').never()
  392. module.list_archive(
  393. repository='repo',
  394. storage_config={},
  395. local_borg_version='1.2.3',
  396. list_arguments=list_arguments,
  397. )
  398. @pytest.mark.parametrize(
  399. 'archive_filter_flag', ('prefix', 'match_archives', 'sort_by', 'first', 'last',),
  400. )
  401. def test_list_archive_with_archive_ignores_archive_filter_flag(archive_filter_flag,):
  402. default_filter_flags = {
  403. 'prefix': None,
  404. 'match_archives': None,
  405. 'sort_by': None,
  406. 'first': None,
  407. 'last': None,
  408. }
  409. altered_filter_flags = {**default_filter_flags, **{archive_filter_flag: 'foo'}}
  410. flexmock(module.feature).should_receive('available').with_args(
  411. module.feature.Feature.RLIST, '1.2.3'
  412. ).and_return(False)
  413. flexmock(module).should_receive('make_list_command').with_args(
  414. repository='repo',
  415. storage_config={},
  416. local_borg_version='1.2.3',
  417. list_arguments=argparse.Namespace(
  418. archive='archive', paths=None, json=False, find_paths=None, **default_filter_flags
  419. ),
  420. local_path='borg',
  421. remote_path=None,
  422. ).and_return(('borg', 'list', 'repo::archive'))
  423. flexmock(module).should_receive('make_find_paths').and_return(())
  424. flexmock(module.environment).should_receive('make_environment')
  425. flexmock(module).should_receive('execute_command').with_args(
  426. ('borg', 'list', 'repo::archive'),
  427. output_log_level=logging.WARNING,
  428. borg_local_path='borg',
  429. extra_environment=None,
  430. ).once()
  431. module.list_archive(
  432. repository='repo',
  433. storage_config={},
  434. local_borg_version='1.2.3',
  435. list_arguments=argparse.Namespace(
  436. archive='archive', paths=None, json=False, find_paths=None, **altered_filter_flags
  437. ),
  438. )
  439. @pytest.mark.parametrize(
  440. 'archive_filter_flag', ('prefix', 'match_archives', 'sort_by', 'first', 'last',),
  441. )
  442. def test_list_archive_with_find_paths_allows_archive_filter_flag_but_only_passes_it_to_rlist(
  443. archive_filter_flag,
  444. ):
  445. default_filter_flags = {
  446. 'prefix': None,
  447. 'match_archives': None,
  448. 'sort_by': None,
  449. 'first': None,
  450. 'last': None,
  451. }
  452. altered_filter_flags = {**default_filter_flags, **{archive_filter_flag: 'foo'}}
  453. glob_paths = ('**/*foo.txt*/**',)
  454. flexmock(module.feature).should_receive('available').and_return(True)
  455. flexmock(module.rlist).should_receive('make_rlist_command').with_args(
  456. repository='repo',
  457. storage_config={},
  458. local_borg_version='1.2.3',
  459. rlist_arguments=argparse.Namespace(
  460. repository='repo', short=True, format=None, json=None, **altered_filter_flags
  461. ),
  462. local_path='borg',
  463. remote_path=None,
  464. ).and_return(('borg', 'rlist', '--repo', 'repo'))
  465. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  466. ('borg', 'rlist', '--repo', 'repo'), extra_environment=None,
  467. ).and_return('archive1\narchive2').once()
  468. flexmock(module).should_receive('make_list_command').with_args(
  469. repository='repo',
  470. storage_config={},
  471. local_borg_version='1.2.3',
  472. list_arguments=argparse.Namespace(
  473. repository='repo',
  474. archive='archive1',
  475. paths=None,
  476. short=True,
  477. format=None,
  478. json=None,
  479. find_paths=['foo.txt'],
  480. **default_filter_flags,
  481. ),
  482. local_path='borg',
  483. remote_path=None,
  484. ).and_return(('borg', 'list', '--repo', 'repo', 'archive1'))
  485. flexmock(module).should_receive('make_list_command').with_args(
  486. repository='repo',
  487. storage_config={},
  488. local_borg_version='1.2.3',
  489. list_arguments=argparse.Namespace(
  490. repository='repo',
  491. archive='archive2',
  492. paths=None,
  493. short=True,
  494. format=None,
  495. json=None,
  496. find_paths=['foo.txt'],
  497. **default_filter_flags,
  498. ),
  499. local_path='borg',
  500. remote_path=None,
  501. ).and_return(('borg', 'list', '--repo', 'repo', 'archive2'))
  502. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  503. flexmock(module.environment).should_receive('make_environment')
  504. flexmock(module).should_receive('execute_command').with_args(
  505. ('borg', 'list', '--repo', 'repo', 'archive1') + glob_paths,
  506. output_log_level=logging.WARNING,
  507. borg_local_path='borg',
  508. extra_environment=None,
  509. ).once()
  510. flexmock(module).should_receive('execute_command').with_args(
  511. ('borg', 'list', '--repo', 'repo', 'archive2') + glob_paths,
  512. output_log_level=logging.WARNING,
  513. borg_local_path='borg',
  514. extra_environment=None,
  515. ).once()
  516. module.list_archive(
  517. repository='repo',
  518. storage_config={},
  519. local_borg_version='1.2.3',
  520. list_arguments=argparse.Namespace(
  521. repository='repo',
  522. archive=None,
  523. paths=None,
  524. short=True,
  525. format=None,
  526. json=None,
  527. find_paths=['foo.txt'],
  528. **altered_filter_flags,
  529. ),
  530. )