test_list.py 17 KB

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