test_list.py 31 KB

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