test_list.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. 'glob_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. glob_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. glob_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. glob_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').with_args(
  292. ('borg', 'list', 'repo'),
  293. output_log_level=None,
  294. borg_local_path='borg',
  295. extra_environment=None,
  296. ).and_return('archive1\narchive2').once()
  297. flexmock(module).should_receive('make_list_command').and_return(
  298. ('borg', 'list', 'repo::archive1')
  299. ).and_return(('borg', 'list', 'repo::archive2'))
  300. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  301. flexmock(module.environment).should_receive('make_environment')
  302. flexmock(module).should_receive('execute_command').with_args(
  303. ('borg', 'list', 'repo::archive1') + glob_paths,
  304. output_log_level=logging.WARNING,
  305. borg_local_path='borg',
  306. extra_environment=None,
  307. ).once()
  308. flexmock(module).should_receive('execute_command').with_args(
  309. ('borg', 'list', 'repo::archive2') + glob_paths,
  310. output_log_level=logging.WARNING,
  311. borg_local_path='borg',
  312. extra_environment=None,
  313. ).once()
  314. module.list_archive(
  315. repository='repo',
  316. storage_config={},
  317. local_borg_version='1.2.3',
  318. list_arguments=list_arguments,
  319. )
  320. def test_list_archive_calls_borg_with_archive():
  321. list_arguments = argparse.Namespace(
  322. archive='archive',
  323. paths=None,
  324. json=False,
  325. find_paths=None,
  326. prefix=None,
  327. glob_archives=None,
  328. sort_by=None,
  329. first=None,
  330. last=None,
  331. )
  332. flexmock(module.feature).should_receive('available').and_return(False)
  333. flexmock(module).should_receive('make_list_command').with_args(
  334. repository='repo',
  335. storage_config={},
  336. local_borg_version='1.2.3',
  337. list_arguments=list_arguments,
  338. local_path='borg',
  339. remote_path=None,
  340. ).and_return(('borg', 'list', 'repo::archive'))
  341. flexmock(module).should_receive('make_find_paths').and_return(())
  342. flexmock(module.environment).should_receive('make_environment')
  343. flexmock(module).should_receive('execute_command').with_args(
  344. ('borg', 'list', 'repo::archive'),
  345. output_log_level=logging.WARNING,
  346. borg_local_path='borg',
  347. extra_environment=None,
  348. ).once()
  349. module.list_archive(
  350. repository='repo',
  351. storage_config={},
  352. local_borg_version='1.2.3',
  353. list_arguments=list_arguments,
  354. )
  355. def test_list_archive_without_archive_delegates_to_list_repository():
  356. list_arguments = argparse.Namespace(
  357. archive=None,
  358. short=None,
  359. format=None,
  360. json=None,
  361. prefix=None,
  362. glob_archives=None,
  363. sort_by=None,
  364. first=None,
  365. last=None,
  366. find_paths=None,
  367. )
  368. flexmock(module.feature).should_receive('available').and_return(False)
  369. flexmock(module.rlist).should_receive('list_repository')
  370. flexmock(module.environment).should_receive('make_environment').never()
  371. flexmock(module).should_receive('execute_command').never()
  372. module.list_archive(
  373. repository='repo',
  374. storage_config={},
  375. local_borg_version='1.2.3',
  376. list_arguments=list_arguments,
  377. )
  378. def test_list_archive_with_borg_features_without_archive_delegates_to_list_repository():
  379. list_arguments = argparse.Namespace(
  380. archive=None,
  381. short=None,
  382. format=None,
  383. json=None,
  384. prefix=None,
  385. glob_archives=None,
  386. sort_by=None,
  387. first=None,
  388. last=None,
  389. find_paths=None,
  390. )
  391. flexmock(module.feature).should_receive('available').and_return(True)
  392. flexmock(module.rlist).should_receive('list_repository')
  393. flexmock(module.environment).should_receive('make_environment').never()
  394. flexmock(module).should_receive('execute_command').never()
  395. module.list_archive(
  396. repository='repo',
  397. storage_config={},
  398. local_borg_version='1.2.3',
  399. list_arguments=list_arguments,
  400. )
  401. @pytest.mark.parametrize(
  402. 'archive_filter_flag', ('prefix', 'glob_archives', 'sort_by', 'first', 'last',),
  403. )
  404. def test_list_archive_with_archive_ignores_archive_filter_flag(archive_filter_flag,):
  405. default_filter_flags = {
  406. 'prefix': None,
  407. 'glob_archives': None,
  408. 'sort_by': None,
  409. 'first': None,
  410. 'last': None,
  411. }
  412. altered_filter_flags = {**default_filter_flags, **{archive_filter_flag: 'foo'}}
  413. flexmock(module.feature).should_receive('available').with_args(
  414. module.feature.Feature.RLIST, '1.2.3'
  415. ).and_return(False)
  416. flexmock(module).should_receive('make_list_command').with_args(
  417. repository='repo',
  418. storage_config={},
  419. local_borg_version='1.2.3',
  420. list_arguments=argparse.Namespace(
  421. archive='archive', paths=None, json=False, find_paths=None, **default_filter_flags
  422. ),
  423. local_path='borg',
  424. remote_path=None,
  425. ).and_return(('borg', 'list', 'repo::archive'))
  426. flexmock(module).should_receive('make_find_paths').and_return(())
  427. flexmock(module.environment).should_receive('make_environment')
  428. flexmock(module).should_receive('execute_command').with_args(
  429. ('borg', 'list', 'repo::archive'),
  430. output_log_level=logging.WARNING,
  431. borg_local_path='borg',
  432. extra_environment=None,
  433. ).once()
  434. module.list_archive(
  435. repository='repo',
  436. storage_config={},
  437. local_borg_version='1.2.3',
  438. list_arguments=argparse.Namespace(
  439. archive='archive', paths=None, json=False, find_paths=None, **altered_filter_flags
  440. ),
  441. )
  442. @pytest.mark.parametrize(
  443. 'archive_filter_flag', ('prefix', 'glob_archives', 'sort_by', 'first', 'last',),
  444. )
  445. def test_list_archive_with_find_paths_allows_archive_filter_flag_but_only_passes_it_to_rlist(
  446. archive_filter_flag,
  447. ):
  448. default_filter_flags = {
  449. 'prefix': None,
  450. 'glob_archives': None,
  451. 'sort_by': None,
  452. 'first': None,
  453. 'last': None,
  454. }
  455. altered_filter_flags = {**default_filter_flags, **{archive_filter_flag: 'foo'}}
  456. glob_paths = ('**/*foo.txt*/**',)
  457. flexmock(module.feature).should_receive('available').and_return(True)
  458. flexmock(module.rlist).should_receive('make_rlist_command').with_args(
  459. repository='repo',
  460. storage_config={},
  461. local_borg_version='1.2.3',
  462. rlist_arguments=argparse.Namespace(
  463. repository='repo', short=True, format=None, json=None, **altered_filter_flags
  464. ),
  465. local_path='borg',
  466. remote_path=None,
  467. ).and_return(('borg', 'rlist', '--repo', 'repo'))
  468. flexmock(module).should_receive('execute_command').with_args(
  469. ('borg', 'rlist', '--repo', 'repo'),
  470. output_log_level=None,
  471. borg_local_path='borg',
  472. extra_environment=None,
  473. ).and_return('archive1\narchive2').once()
  474. flexmock(module).should_receive('make_list_command').with_args(
  475. repository='repo',
  476. storage_config={},
  477. local_borg_version='1.2.3',
  478. list_arguments=argparse.Namespace(
  479. repository='repo',
  480. archive='archive1',
  481. paths=None,
  482. short=True,
  483. format=None,
  484. json=None,
  485. find_paths=['foo.txt'],
  486. **default_filter_flags,
  487. ),
  488. local_path='borg',
  489. remote_path=None,
  490. ).and_return(('borg', 'list', '--repo', 'repo', 'archive1'))
  491. flexmock(module).should_receive('make_list_command').with_args(
  492. repository='repo',
  493. storage_config={},
  494. local_borg_version='1.2.3',
  495. list_arguments=argparse.Namespace(
  496. repository='repo',
  497. archive='archive2',
  498. paths=None,
  499. short=True,
  500. format=None,
  501. json=None,
  502. find_paths=['foo.txt'],
  503. **default_filter_flags,
  504. ),
  505. local_path='borg',
  506. remote_path=None,
  507. ).and_return(('borg', 'list', '--repo', 'repo', 'archive2'))
  508. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  509. flexmock(module.environment).should_receive('make_environment')
  510. flexmock(module).should_receive('execute_command').with_args(
  511. ('borg', 'list', '--repo', 'repo', 'archive1') + glob_paths,
  512. output_log_level=logging.WARNING,
  513. borg_local_path='borg',
  514. extra_environment=None,
  515. ).once()
  516. flexmock(module).should_receive('execute_command').with_args(
  517. ('borg', 'list', '--repo', 'repo', 'archive2') + glob_paths,
  518. output_log_level=logging.WARNING,
  519. borg_local_path='borg',
  520. extra_environment=None,
  521. ).once()
  522. module.list_archive(
  523. repository='repo',
  524. storage_config={},
  525. local_borg_version='1.2.3',
  526. list_arguments=argparse.Namespace(
  527. repository='repo',
  528. archive=None,
  529. paths=None,
  530. short=True,
  531. format=None,
  532. json=None,
  533. find_paths=['foo.txt'],
  534. **altered_filter_flags,
  535. ),
  536. )