test_list.py 30 KB

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