test_list.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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_path='repo',
  14. config={},
  15. local_borg_version='1.2.3',
  16. list_arguments=flexmock(archive=None, paths=None, json=False),
  17. global_arguments=flexmock(),
  18. )
  19. assert command == ('borg', 'list', '--info', 'repo')
  20. def test_make_list_command_includes_json_but_not_info():
  21. insert_logging_mock(logging.INFO)
  22. flexmock(module.flags).should_receive('make_flags').and_return(())
  23. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  24. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  25. command = module.make_list_command(
  26. repository_path='repo',
  27. config={},
  28. local_borg_version='1.2.3',
  29. list_arguments=flexmock(archive=None, paths=None, json=True),
  30. global_arguments=flexmock(),
  31. )
  32. assert command == ('borg', 'list', '--json', 'repo')
  33. def test_make_list_command_includes_log_debug():
  34. insert_logging_mock(logging.DEBUG)
  35. flexmock(module.flags).should_receive('make_flags').and_return(())
  36. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  37. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  38. command = module.make_list_command(
  39. repository_path='repo',
  40. config={},
  41. local_borg_version='1.2.3',
  42. list_arguments=flexmock(archive=None, paths=None, json=False),
  43. global_arguments=flexmock(),
  44. )
  45. assert command == ('borg', 'list', '--debug', '--show-rc', 'repo')
  46. def test_make_list_command_includes_json_but_not_debug():
  47. insert_logging_mock(logging.DEBUG)
  48. flexmock(module.flags).should_receive('make_flags').and_return(())
  49. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  50. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  51. command = module.make_list_command(
  52. repository_path='repo',
  53. config={},
  54. local_borg_version='1.2.3',
  55. list_arguments=flexmock(archive=None, paths=None, json=True),
  56. global_arguments=flexmock(),
  57. )
  58. assert command == ('borg', 'list', '--json', 'repo')
  59. def test_make_list_command_includes_json():
  60. flexmock(module.flags).should_receive('make_flags').and_return(())
  61. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  62. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  63. command = module.make_list_command(
  64. repository_path='repo',
  65. config={},
  66. local_borg_version='1.2.3',
  67. list_arguments=flexmock(archive=None, paths=None, json=True),
  68. global_arguments=flexmock(),
  69. )
  70. assert command == ('borg', 'list', '--json', 'repo')
  71. def test_make_list_command_includes_log_json():
  72. flexmock(module.flags).should_receive('make_flags').and_return(()).and_return(()).and_return(
  73. ('--log-json',),
  74. )
  75. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  76. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  77. command = module.make_list_command(
  78. repository_path='repo',
  79. config={'log_json': True},
  80. local_borg_version='1.2.3',
  81. list_arguments=flexmock(archive=None, paths=None, json=False),
  82. global_arguments=flexmock(),
  83. )
  84. assert command == ('borg', 'list', '--log-json', 'repo')
  85. def test_make_list_command_includes_lock_wait():
  86. flexmock(module.flags).should_receive('make_flags').and_return(()).and_return(()).and_return(
  87. ('--lock-wait', '5'),
  88. )
  89. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  90. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  91. command = module.make_list_command(
  92. repository_path='repo',
  93. config={'lock_wait': 5},
  94. local_borg_version='1.2.3',
  95. list_arguments=flexmock(archive=None, paths=None, json=False),
  96. global_arguments=flexmock(),
  97. )
  98. assert command == ('borg', 'list', '--lock-wait', '5', 'repo')
  99. def test_make_list_command_includes_archive():
  100. flexmock(module.flags).should_receive('make_flags').and_return(())
  101. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  102. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  103. ('repo::archive',),
  104. )
  105. command = module.make_list_command(
  106. repository_path='repo',
  107. config={},
  108. local_borg_version='1.2.3',
  109. list_arguments=flexmock(archive='archive', paths=None, json=False),
  110. global_arguments=flexmock(),
  111. )
  112. assert command == ('borg', 'list', 'repo::archive')
  113. def test_make_list_command_includes_archive_and_path():
  114. flexmock(module.flags).should_receive('make_flags').and_return(())
  115. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  116. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  117. ('repo::archive',),
  118. )
  119. command = module.make_list_command(
  120. repository_path='repo',
  121. config={},
  122. local_borg_version='1.2.3',
  123. list_arguments=flexmock(archive='archive', paths=['var/lib'], json=False),
  124. global_arguments=flexmock(),
  125. )
  126. assert command == ('borg', 'list', 'repo::archive', 'var/lib')
  127. def test_make_list_command_includes_local_path():
  128. flexmock(module.flags).should_receive('make_flags').and_return(())
  129. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  130. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  131. command = module.make_list_command(
  132. repository_path='repo',
  133. config={},
  134. local_borg_version='1.2.3',
  135. list_arguments=flexmock(archive=None, paths=None, json=False),
  136. global_arguments=flexmock(),
  137. local_path='borg2',
  138. )
  139. assert command == ('borg2', 'list', 'repo')
  140. def test_make_list_command_includes_remote_path():
  141. flexmock(module.flags).should_receive('make_flags').and_return(())
  142. flexmock(module.flags).should_receive('make_flags').with_args(
  143. 'remote-path',
  144. 'borg2',
  145. ).and_return(('--remote-path', 'borg2'))
  146. flexmock(module.flags).should_receive('make_flags').with_args('log-json', True).and_return(
  147. ('--log-json'),
  148. )
  149. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  150. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  151. command = module.make_list_command(
  152. repository_path='repo',
  153. config={},
  154. local_borg_version='1.2.3',
  155. list_arguments=flexmock(archive=None, paths=None, json=False),
  156. global_arguments=flexmock(),
  157. remote_path='borg2',
  158. )
  159. assert command == ('borg', 'list', '--remote-path', 'borg2', 'repo')
  160. def test_make_list_command_includes_umask():
  161. flexmock(module.flags).should_receive('make_flags').replace_with(
  162. lambda name, value: (f'--{name}', value) if value else (),
  163. )
  164. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  165. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  166. command = module.make_list_command(
  167. repository_path='repo',
  168. config={'umask': '077'},
  169. local_borg_version='1.2.3',
  170. list_arguments=flexmock(archive=None, paths=None, json=False),
  171. global_arguments=flexmock(),
  172. )
  173. assert command == ('borg', 'list', '--umask', '077', 'repo')
  174. def test_make_list_command_includes_short():
  175. flexmock(module.flags).should_receive('make_flags').and_return(())
  176. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--short',))
  177. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  178. command = module.make_list_command(
  179. repository_path='repo',
  180. config={},
  181. local_borg_version='1.2.3',
  182. list_arguments=flexmock(archive=None, paths=None, json=False, short=True),
  183. global_arguments=flexmock(),
  184. )
  185. assert command == ('borg', 'list', '--short', 'repo')
  186. @pytest.mark.parametrize(
  187. 'argument_name',
  188. (
  189. 'prefix',
  190. 'match_archives',
  191. 'sort_by',
  192. 'first',
  193. 'last',
  194. 'exclude',
  195. 'exclude_from',
  196. 'pattern',
  197. 'patterns_from',
  198. ),
  199. )
  200. def test_make_list_command_includes_additional_flags(argument_name):
  201. flexmock(module.flags).should_receive('make_flags').and_return(())
  202. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  203. (f"--{argument_name.replace('_', '-')}", 'value'),
  204. )
  205. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  206. command = module.make_list_command(
  207. repository_path='repo',
  208. config={},
  209. local_borg_version='1.2.3',
  210. list_arguments=flexmock(
  211. archive=None,
  212. paths=None,
  213. json=False,
  214. find_paths=None,
  215. format=None,
  216. **{argument_name: 'value'},
  217. ),
  218. global_arguments=flexmock(),
  219. )
  220. assert command == ('borg', 'list', '--' + argument_name.replace('_', '-'), 'value', 'repo')
  221. def test_make_find_paths_considers_none_as_empty_paths():
  222. assert module.make_find_paths(None) == ()
  223. def test_make_find_paths_passes_through_patterns():
  224. find_paths = (
  225. 'fm:*',
  226. 'sh:**/*.txt',
  227. 're:^.*$',
  228. 'pp:root/somedir',
  229. 'pf:root/foo.txt',
  230. 'R /',
  231. 'r /',
  232. 'p /',
  233. 'P /',
  234. '+ /',
  235. '- /',
  236. '! /',
  237. )
  238. assert module.make_find_paths(find_paths) == find_paths
  239. def test_make_find_paths_adds_globs_to_path_fragments():
  240. assert module.make_find_paths(('foo.txt',)) == ('sh:**/*foo.txt*/**',)
  241. def test_capture_archive_listing_does_not_raise():
  242. flexmock(module.environment).should_receive('make_environment')
  243. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  244. flexmock(module).should_receive('execute_command_and_capture_output').and_return('')
  245. flexmock(module).should_receive('make_list_command')
  246. module.capture_archive_listing(
  247. repository_path='repo',
  248. archive='archive',
  249. config={},
  250. local_borg_version=flexmock(),
  251. global_arguments=flexmock(),
  252. )
  253. def test_list_archive_calls_borg_with_flags():
  254. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  255. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  256. flexmock(module.logger).answer = lambda message: None
  257. list_arguments = argparse.Namespace(
  258. archive='archive',
  259. paths=None,
  260. json=False,
  261. find_paths=None,
  262. prefix=None,
  263. match_archives=None,
  264. sort_by=None,
  265. first=None,
  266. last=None,
  267. )
  268. global_arguments = flexmock()
  269. flexmock(module.feature).should_receive('available').and_return(False)
  270. flexmock(module).should_receive('make_list_command').with_args(
  271. repository_path='repo',
  272. config={},
  273. local_borg_version='1.2.3',
  274. list_arguments=list_arguments,
  275. global_arguments=global_arguments,
  276. local_path='borg',
  277. remote_path=None,
  278. ).and_return(('borg', 'list', 'repo::archive'))
  279. flexmock(module).should_receive('make_find_paths').and_return(())
  280. flexmock(module.environment).should_receive('make_environment')
  281. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  282. flexmock(module).should_receive('execute_command').with_args(
  283. ('borg', 'list', 'repo::archive'),
  284. output_log_level=module.borgmatic.logger.ANSWER,
  285. environment=None,
  286. working_directory=None,
  287. borg_local_path='borg',
  288. borg_exit_codes=None,
  289. ).once()
  290. module.list_archive(
  291. repository_path='repo',
  292. config={},
  293. local_borg_version='1.2.3',
  294. list_arguments=list_arguments,
  295. global_arguments=global_arguments,
  296. )
  297. def test_list_archive_with_archive_and_json_errors():
  298. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  299. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  300. flexmock(module.logger).answer = lambda message: None
  301. list_arguments = argparse.Namespace(archive='archive', paths=None, json=True, find_paths=None)
  302. flexmock(module.feature).should_receive('available').and_return(False)
  303. with pytest.raises(ValueError):
  304. module.list_archive(
  305. repository_path='repo',
  306. config={},
  307. local_borg_version='1.2.3',
  308. list_arguments=list_arguments,
  309. global_arguments=flexmock(),
  310. )
  311. def test_list_archive_calls_borg_with_local_path():
  312. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  313. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  314. flexmock(module.logger).answer = lambda message: None
  315. list_arguments = argparse.Namespace(
  316. archive='archive',
  317. paths=None,
  318. json=False,
  319. find_paths=None,
  320. prefix=None,
  321. match_archives=None,
  322. sort_by=None,
  323. first=None,
  324. last=None,
  325. )
  326. global_arguments = flexmock()
  327. flexmock(module.feature).should_receive('available').and_return(False)
  328. flexmock(module).should_receive('make_list_command').with_args(
  329. repository_path='repo',
  330. config={},
  331. local_borg_version='1.2.3',
  332. list_arguments=list_arguments,
  333. global_arguments=global_arguments,
  334. local_path='borg2',
  335. remote_path=None,
  336. ).and_return(('borg2', 'list', 'repo::archive'))
  337. flexmock(module).should_receive('make_find_paths').and_return(())
  338. flexmock(module.environment).should_receive('make_environment')
  339. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  340. flexmock(module).should_receive('execute_command').with_args(
  341. ('borg2', 'list', 'repo::archive'),
  342. output_log_level=module.borgmatic.logger.ANSWER,
  343. environment=None,
  344. working_directory=None,
  345. borg_local_path='borg2',
  346. borg_exit_codes=None,
  347. ).once()
  348. module.list_archive(
  349. repository_path='repo',
  350. config={},
  351. local_borg_version='1.2.3',
  352. list_arguments=list_arguments,
  353. global_arguments=global_arguments,
  354. local_path='borg2',
  355. )
  356. def test_list_archive_calls_borg_using_exit_codes():
  357. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  358. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  359. flexmock(module.logger).answer = lambda message: None
  360. list_arguments = argparse.Namespace(
  361. archive='archive',
  362. paths=None,
  363. json=False,
  364. find_paths=None,
  365. prefix=None,
  366. match_archives=None,
  367. sort_by=None,
  368. first=None,
  369. last=None,
  370. )
  371. global_arguments = flexmock()
  372. flexmock(module.feature).should_receive('available').and_return(False)
  373. borg_exit_codes = flexmock()
  374. flexmock(module).should_receive('make_list_command').with_args(
  375. repository_path='repo',
  376. config={'borg_exit_codes': borg_exit_codes},
  377. local_borg_version='1.2.3',
  378. list_arguments=list_arguments,
  379. global_arguments=global_arguments,
  380. local_path='borg',
  381. remote_path=None,
  382. ).and_return(('borg', 'list', 'repo::archive'))
  383. flexmock(module).should_receive('make_find_paths').and_return(())
  384. flexmock(module.environment).should_receive('make_environment')
  385. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  386. flexmock(module).should_receive('execute_command').with_args(
  387. ('borg', 'list', 'repo::archive'),
  388. output_log_level=module.borgmatic.logger.ANSWER,
  389. environment=None,
  390. working_directory=None,
  391. borg_local_path='borg',
  392. borg_exit_codes=borg_exit_codes,
  393. ).once()
  394. module.list_archive(
  395. repository_path='repo',
  396. config={'borg_exit_codes': borg_exit_codes},
  397. local_borg_version='1.2.3',
  398. list_arguments=list_arguments,
  399. global_arguments=global_arguments,
  400. )
  401. def test_list_archive_calls_borg_multiple_times_with_find_paths():
  402. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  403. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  404. flexmock(module.logger).answer = lambda message: None
  405. glob_paths = ('**/*foo.txt*/**',)
  406. list_arguments = argparse.Namespace(
  407. archive=None,
  408. json=False,
  409. find_paths=['foo.txt'],
  410. prefix=None,
  411. match_archives=None,
  412. sort_by=None,
  413. first=None,
  414. last=None,
  415. )
  416. flexmock(module.feature).should_receive('available').and_return(False)
  417. flexmock(module.repo_list).should_receive('make_repo_list_command').and_return(
  418. ('borg', 'list', 'repo'),
  419. )
  420. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  421. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  422. ('borg', 'list', 'repo'),
  423. environment=None,
  424. working_directory=None,
  425. borg_local_path='borg',
  426. borg_exit_codes=None,
  427. ).and_return('archive1\narchive2').once()
  428. flexmock(module).should_receive('make_list_command').and_return(
  429. ('borg', 'list', 'repo::archive1'),
  430. ).and_return(('borg', 'list', 'repo::archive2'))
  431. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  432. flexmock(module.environment).should_receive('make_environment')
  433. flexmock(module).should_receive('execute_command').with_args(
  434. ('borg', 'list', 'repo::archive1', *glob_paths),
  435. output_log_level=module.borgmatic.logger.ANSWER,
  436. environment=None,
  437. working_directory=None,
  438. borg_local_path='borg',
  439. borg_exit_codes=None,
  440. ).once()
  441. flexmock(module).should_receive('execute_command').with_args(
  442. ('borg', 'list', 'repo::archive2', *glob_paths),
  443. output_log_level=module.borgmatic.logger.ANSWER,
  444. environment=None,
  445. working_directory=None,
  446. borg_local_path='borg',
  447. borg_exit_codes=None,
  448. ).once()
  449. module.list_archive(
  450. repository_path='repo',
  451. config={},
  452. local_borg_version='1.2.3',
  453. list_arguments=list_arguments,
  454. global_arguments=flexmock(),
  455. )
  456. def test_list_archive_calls_borg_with_archive():
  457. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  458. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  459. flexmock(module.logger).answer = lambda message: None
  460. list_arguments = argparse.Namespace(
  461. archive='archive',
  462. paths=None,
  463. json=False,
  464. find_paths=None,
  465. prefix=None,
  466. match_archives=None,
  467. sort_by=None,
  468. first=None,
  469. last=None,
  470. )
  471. global_arguments = flexmock()
  472. flexmock(module.feature).should_receive('available').and_return(False)
  473. flexmock(module).should_receive('make_list_command').with_args(
  474. repository_path='repo',
  475. config={},
  476. local_borg_version='1.2.3',
  477. list_arguments=list_arguments,
  478. global_arguments=global_arguments,
  479. local_path='borg',
  480. remote_path=None,
  481. ).and_return(('borg', 'list', 'repo::archive'))
  482. flexmock(module).should_receive('make_find_paths').and_return(())
  483. flexmock(module.environment).should_receive('make_environment')
  484. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  485. flexmock(module).should_receive('execute_command').with_args(
  486. ('borg', 'list', 'repo::archive'),
  487. output_log_level=module.borgmatic.logger.ANSWER,
  488. environment=None,
  489. working_directory=None,
  490. borg_local_path='borg',
  491. borg_exit_codes=None,
  492. ).once()
  493. module.list_archive(
  494. repository_path='repo',
  495. config={},
  496. local_borg_version='1.2.3',
  497. list_arguments=list_arguments,
  498. global_arguments=global_arguments,
  499. )
  500. def test_list_archive_without_archive_delegates_to_list_repository():
  501. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  502. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  503. flexmock(module.logger).answer = lambda message: None
  504. list_arguments = argparse.Namespace(
  505. archive=None,
  506. short=None,
  507. format=None,
  508. json=None,
  509. prefix=None,
  510. match_archives=None,
  511. sort_by=None,
  512. first=None,
  513. last=None,
  514. find_paths=None,
  515. )
  516. flexmock(module.feature).should_receive('available').and_return(False)
  517. flexmock(module.repo_list).should_receive('list_repository')
  518. flexmock(module.environment).should_receive('make_environment').never()
  519. flexmock(module).should_receive('execute_command').never()
  520. module.list_archive(
  521. repository_path='repo',
  522. config={},
  523. local_borg_version='1.2.3',
  524. list_arguments=list_arguments,
  525. global_arguments=flexmock(),
  526. )
  527. def test_list_archive_with_borg_features_without_archive_delegates_to_list_repository():
  528. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  529. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  530. flexmock(module.logger).answer = lambda message: None
  531. list_arguments = argparse.Namespace(
  532. archive=None,
  533. short=None,
  534. format=None,
  535. json=None,
  536. prefix=None,
  537. match_archives=None,
  538. sort_by=None,
  539. first=None,
  540. last=None,
  541. find_paths=None,
  542. )
  543. flexmock(module.feature).should_receive('available').and_return(True)
  544. flexmock(module.repo_list).should_receive('list_repository')
  545. flexmock(module.environment).should_receive('make_environment').never()
  546. flexmock(module).should_receive('execute_command').never()
  547. module.list_archive(
  548. repository_path='repo',
  549. config={},
  550. local_borg_version='1.2.3',
  551. list_arguments=list_arguments,
  552. global_arguments=flexmock(),
  553. )
  554. @pytest.mark.parametrize(
  555. 'archive_filter_flag',
  556. (
  557. 'prefix',
  558. 'match_archives',
  559. 'sort_by',
  560. 'first',
  561. 'last',
  562. ),
  563. )
  564. def test_list_archive_with_archive_ignores_archive_filter_flag(
  565. archive_filter_flag,
  566. ):
  567. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  568. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  569. flexmock(module.logger).answer = lambda message: None
  570. global_arguments = flexmock()
  571. default_filter_flags = {
  572. 'prefix': None,
  573. 'match_archives': None,
  574. 'sort_by': None,
  575. 'first': None,
  576. 'last': None,
  577. }
  578. altered_filter_flags = {**default_filter_flags, **{archive_filter_flag: 'foo'}} # noqa: PIE800
  579. flexmock(module.feature).should_receive('available').with_args(
  580. module.feature.Feature.REPO_LIST,
  581. '1.2.3',
  582. ).and_return(False)
  583. flexmock(module).should_receive('make_list_command').with_args(
  584. repository_path='repo',
  585. config={},
  586. local_borg_version='1.2.3',
  587. list_arguments=argparse.Namespace(
  588. archive='archive',
  589. paths=None,
  590. json=False,
  591. find_paths=None,
  592. **default_filter_flags,
  593. ),
  594. global_arguments=global_arguments,
  595. local_path='borg',
  596. remote_path=None,
  597. ).and_return(('borg', 'list', 'repo::archive'))
  598. flexmock(module).should_receive('make_find_paths').and_return(())
  599. flexmock(module.environment).should_receive('make_environment')
  600. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  601. flexmock(module).should_receive('execute_command').with_args(
  602. ('borg', 'list', 'repo::archive'),
  603. output_log_level=module.borgmatic.logger.ANSWER,
  604. environment=None,
  605. working_directory=None,
  606. borg_local_path='borg',
  607. borg_exit_codes=None,
  608. ).once()
  609. module.list_archive(
  610. repository_path='repo',
  611. config={},
  612. local_borg_version='1.2.3',
  613. list_arguments=argparse.Namespace(
  614. archive='archive',
  615. paths=None,
  616. json=False,
  617. find_paths=None,
  618. **altered_filter_flags,
  619. ),
  620. global_arguments=global_arguments,
  621. )
  622. @pytest.mark.parametrize(
  623. 'archive_filter_flag',
  624. (
  625. 'prefix',
  626. 'match_archives',
  627. 'sort_by',
  628. 'first',
  629. 'last',
  630. ),
  631. )
  632. def test_list_archive_with_find_paths_allows_archive_filter_flag_but_only_passes_it_to_repo_list(
  633. archive_filter_flag,
  634. ):
  635. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  636. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  637. flexmock(module.logger).answer = lambda message: None
  638. default_filter_flags = {
  639. 'prefix': None,
  640. 'match_archives': None,
  641. 'sort_by': None,
  642. 'first': None,
  643. 'last': None,
  644. }
  645. altered_filter_flags = {**default_filter_flags, **{archive_filter_flag: 'foo'}} # noqa: PIE800
  646. glob_paths = ('**/*foo.txt*/**',)
  647. global_arguments = flexmock()
  648. flexmock(module.feature).should_receive('available').and_return(True)
  649. flexmock(module.repo_list).should_receive('make_repo_list_command').with_args(
  650. repository_path='repo',
  651. config={},
  652. local_borg_version='1.2.3',
  653. repo_list_arguments=argparse.Namespace(
  654. repository='repo',
  655. short=True,
  656. format=None,
  657. json=None,
  658. **altered_filter_flags,
  659. ),
  660. global_arguments=global_arguments,
  661. local_path='borg',
  662. remote_path=None,
  663. ).and_return(('borg', 'repo-list', '--repo', 'repo'))
  664. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  665. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  666. ('borg', 'repo-list', '--repo', 'repo'),
  667. environment=None,
  668. working_directory=None,
  669. borg_local_path='borg',
  670. borg_exit_codes=None,
  671. ).and_return('archive1\narchive2').once()
  672. flexmock(module).should_receive('make_list_command').with_args(
  673. repository_path='repo',
  674. config={},
  675. local_borg_version='1.2.3',
  676. list_arguments=argparse.Namespace(
  677. repository='repo',
  678. archive='archive1',
  679. paths=None,
  680. short=True,
  681. format=None,
  682. json=None,
  683. find_paths=['foo.txt'],
  684. **default_filter_flags,
  685. ),
  686. global_arguments=global_arguments,
  687. local_path='borg',
  688. remote_path=None,
  689. ).and_return(('borg', 'list', '--repo', 'repo', 'archive1'))
  690. flexmock(module).should_receive('make_list_command').with_args(
  691. repository_path='repo',
  692. config={},
  693. local_borg_version='1.2.3',
  694. list_arguments=argparse.Namespace(
  695. repository='repo',
  696. archive='archive2',
  697. paths=None,
  698. short=True,
  699. format=None,
  700. json=None,
  701. find_paths=['foo.txt'],
  702. **default_filter_flags,
  703. ),
  704. global_arguments=global_arguments,
  705. local_path='borg',
  706. remote_path=None,
  707. ).and_return(('borg', 'list', '--repo', 'repo', 'archive2'))
  708. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  709. flexmock(module.environment).should_receive('make_environment')
  710. flexmock(module).should_receive('execute_command').with_args(
  711. ('borg', 'list', '--repo', 'repo', 'archive1', *glob_paths),
  712. output_log_level=module.borgmatic.logger.ANSWER,
  713. environment=None,
  714. working_directory=None,
  715. borg_local_path='borg',
  716. borg_exit_codes=None,
  717. ).once()
  718. flexmock(module).should_receive('execute_command').with_args(
  719. ('borg', 'list', '--repo', 'repo', 'archive2', *glob_paths),
  720. output_log_level=module.borgmatic.logger.ANSWER,
  721. environment=None,
  722. working_directory=None,
  723. borg_local_path='borg',
  724. borg_exit_codes=None,
  725. ).once()
  726. module.list_archive(
  727. repository_path='repo',
  728. config={},
  729. local_borg_version='1.2.3',
  730. list_arguments=argparse.Namespace(
  731. repository='repo',
  732. archive=None,
  733. paths=None,
  734. short=True,
  735. format=None,
  736. json=None,
  737. find_paths=['foo.txt'],
  738. **altered_filter_flags,
  739. ),
  740. global_arguments=global_arguments,
  741. )
  742. def test_list_archive_calls_borg_with_working_directory():
  743. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  744. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  745. flexmock(module.logger).answer = lambda message: None
  746. list_arguments = argparse.Namespace(
  747. archive='archive',
  748. paths=None,
  749. json=False,
  750. find_paths=None,
  751. prefix=None,
  752. match_archives=None,
  753. sort_by=None,
  754. first=None,
  755. last=None,
  756. )
  757. global_arguments = flexmock()
  758. flexmock(module.feature).should_receive('available').and_return(False)
  759. flexmock(module).should_receive('make_list_command').with_args(
  760. repository_path='repo',
  761. config={'working_directory': '/working/dir'},
  762. local_borg_version='1.2.3',
  763. list_arguments=list_arguments,
  764. global_arguments=global_arguments,
  765. local_path='borg',
  766. remote_path=None,
  767. ).and_return(('borg', 'list', 'repo::archive'))
  768. flexmock(module).should_receive('make_find_paths').and_return(())
  769. flexmock(module.environment).should_receive('make_environment')
  770. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  771. '/working/dir',
  772. )
  773. flexmock(module).should_receive('execute_command').with_args(
  774. ('borg', 'list', 'repo::archive'),
  775. output_log_level=module.borgmatic.logger.ANSWER,
  776. environment=None,
  777. working_directory='/working/dir',
  778. borg_local_path='borg',
  779. borg_exit_codes=None,
  780. ).once()
  781. module.list_archive(
  782. repository_path='repo',
  783. config={'working_directory': '/working/dir'},
  784. local_borg_version='1.2.3',
  785. list_arguments=list_arguments,
  786. global_arguments=global_arguments,
  787. )