test_list.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. BORG_LIST_LATEST_ARGUMENTS = (
  8. '--last',
  9. '1',
  10. '--short',
  11. 'repo',
  12. )
  13. def test_resolve_archive_name_passes_through_non_latest_archive_name():
  14. archive = 'myhost-2030-01-01T14:41:17.647620'
  15. assert module.resolve_archive_name('repo', archive, storage_config={}) == archive
  16. def test_resolve_archive_name_calls_borg_with_parameters():
  17. expected_archive = 'archive-name'
  18. flexmock(module).should_receive('execute_command').with_args(
  19. ('borg', 'list') + BORG_LIST_LATEST_ARGUMENTS, output_log_level=None, borg_local_path='borg'
  20. ).and_return(expected_archive + '\n')
  21. assert module.resolve_archive_name('repo', 'latest', storage_config={}) == expected_archive
  22. def test_resolve_archive_name_with_log_info_calls_borg_with_info_parameter():
  23. expected_archive = 'archive-name'
  24. flexmock(module).should_receive('execute_command').with_args(
  25. ('borg', 'list', '--info') + BORG_LIST_LATEST_ARGUMENTS,
  26. output_log_level=None,
  27. borg_local_path='borg',
  28. ).and_return(expected_archive + '\n')
  29. insert_logging_mock(logging.INFO)
  30. assert module.resolve_archive_name('repo', 'latest', storage_config={}) == expected_archive
  31. def test_resolve_archive_name_with_log_debug_calls_borg_with_debug_parameter():
  32. expected_archive = 'archive-name'
  33. flexmock(module).should_receive('execute_command').with_args(
  34. ('borg', 'list', '--debug', '--show-rc') + BORG_LIST_LATEST_ARGUMENTS,
  35. output_log_level=None,
  36. borg_local_path='borg',
  37. ).and_return(expected_archive + '\n')
  38. insert_logging_mock(logging.DEBUG)
  39. assert module.resolve_archive_name('repo', 'latest', storage_config={}) == expected_archive
  40. def test_resolve_archive_name_with_local_path_calls_borg_via_local_path():
  41. expected_archive = 'archive-name'
  42. flexmock(module).should_receive('execute_command').with_args(
  43. ('borg1', 'list') + BORG_LIST_LATEST_ARGUMENTS,
  44. output_log_level=None,
  45. borg_local_path='borg1',
  46. ).and_return(expected_archive + '\n')
  47. assert (
  48. module.resolve_archive_name('repo', 'latest', storage_config={}, local_path='borg1')
  49. == expected_archive
  50. )
  51. def test_resolve_archive_name_with_remote_path_calls_borg_with_remote_path_parameters():
  52. expected_archive = 'archive-name'
  53. flexmock(module).should_receive('execute_command').with_args(
  54. ('borg', 'list', '--remote-path', 'borg1') + BORG_LIST_LATEST_ARGUMENTS,
  55. output_log_level=None,
  56. borg_local_path='borg',
  57. ).and_return(expected_archive + '\n')
  58. assert (
  59. module.resolve_archive_name('repo', 'latest', storage_config={}, remote_path='borg1')
  60. == expected_archive
  61. )
  62. def test_resolve_archive_name_without_archives_raises():
  63. flexmock(module).should_receive('execute_command').with_args(
  64. ('borg', 'list') + BORG_LIST_LATEST_ARGUMENTS, output_log_level=None, borg_local_path='borg'
  65. ).and_return('')
  66. with pytest.raises(ValueError):
  67. module.resolve_archive_name('repo', 'latest', storage_config={})
  68. def test_resolve_archive_name_with_lock_wait_calls_borg_with_lock_wait_parameters():
  69. expected_archive = 'archive-name'
  70. flexmock(module).should_receive('execute_command').with_args(
  71. ('borg', 'list', '--lock-wait', 'okay') + BORG_LIST_LATEST_ARGUMENTS,
  72. output_log_level=None,
  73. borg_local_path='borg',
  74. ).and_return(expected_archive + '\n')
  75. assert (
  76. module.resolve_archive_name('repo', 'latest', storage_config={'lock_wait': 'okay'})
  77. == expected_archive
  78. )
  79. def test_make_list_command_includes_log_info():
  80. insert_logging_mock(logging.INFO)
  81. command = module.make_list_command(
  82. repository='repo',
  83. storage_config={},
  84. list_arguments=flexmock(archive=None, paths=None, json=False),
  85. )
  86. assert command == ('borg', 'list', '--info', 'repo')
  87. def test_make_list_command_includes_json_but_not_info():
  88. insert_logging_mock(logging.INFO)
  89. command = module.make_list_command(
  90. repository='repo',
  91. storage_config={},
  92. list_arguments=flexmock(archive=None, paths=None, json=True),
  93. )
  94. assert command == ('borg', 'list', '--json', 'repo')
  95. def test_make_list_command_includes_log_debug():
  96. insert_logging_mock(logging.DEBUG)
  97. command = module.make_list_command(
  98. repository='repo',
  99. storage_config={},
  100. list_arguments=flexmock(archive=None, paths=None, json=False),
  101. )
  102. assert command == ('borg', 'list', '--debug', '--show-rc', 'repo')
  103. def test_make_list_command_includes_json_but_not_debug():
  104. insert_logging_mock(logging.DEBUG)
  105. command = module.make_list_command(
  106. repository='repo',
  107. storage_config={},
  108. list_arguments=flexmock(archive=None, paths=None, json=True),
  109. )
  110. assert command == ('borg', 'list', '--json', 'repo')
  111. def test_make_list_command_includes_json():
  112. command = module.make_list_command(
  113. repository='repo',
  114. storage_config={},
  115. list_arguments=flexmock(archive=None, paths=None, json=True),
  116. )
  117. assert command == ('borg', 'list', '--json', 'repo')
  118. def test_make_list_command_includes_lock_wait():
  119. command = module.make_list_command(
  120. repository='repo',
  121. storage_config={'lock_wait': 5},
  122. list_arguments=flexmock(archive=None, paths=None, json=False),
  123. )
  124. assert command == ('borg', 'list', '--lock-wait', '5', 'repo')
  125. def test_make_list_command_includes_archive():
  126. command = module.make_list_command(
  127. repository='repo',
  128. storage_config={},
  129. list_arguments=flexmock(archive='archive', paths=None, json=False),
  130. )
  131. assert command == ('borg', 'list', 'repo::archive')
  132. def test_make_list_command_includes_archive_and_path():
  133. command = module.make_list_command(
  134. repository='repo',
  135. storage_config={},
  136. list_arguments=flexmock(archive='archive', paths=['var/lib'], json=False),
  137. )
  138. assert command == ('borg', 'list', 'repo::archive', 'var/lib')
  139. def test_make_list_command_includes_local_path():
  140. command = module.make_list_command(
  141. repository='repo',
  142. storage_config={},
  143. list_arguments=flexmock(archive=None, paths=None, json=False),
  144. local_path='borg2',
  145. )
  146. assert command == ('borg2', 'list', 'repo')
  147. def test_make_list_command_includes_remote_path():
  148. command = module.make_list_command(
  149. repository='repo',
  150. storage_config={},
  151. list_arguments=flexmock(archive=None, paths=None, json=False),
  152. remote_path='borg2',
  153. )
  154. assert command == ('borg', 'list', '--remote-path', 'borg2', 'repo')
  155. def test_make_list_command_includes_short():
  156. command = module.make_list_command(
  157. repository='repo',
  158. storage_config={},
  159. list_arguments=flexmock(archive=None, paths=None, json=False, short=True),
  160. )
  161. assert command == ('borg', 'list', '--short', 'repo')
  162. @pytest.mark.parametrize(
  163. 'argument_name',
  164. (
  165. 'prefix',
  166. 'glob_archives',
  167. 'sort_by',
  168. 'first',
  169. 'last',
  170. 'exclude',
  171. 'exclude_from',
  172. 'pattern',
  173. 'patterns_from',
  174. ),
  175. )
  176. def test_make_list_command_includes_additional_flags(argument_name):
  177. command = module.make_list_command(
  178. repository='repo',
  179. storage_config={},
  180. list_arguments=flexmock(
  181. archive=None,
  182. paths=None,
  183. json=False,
  184. find_paths=None,
  185. format=None,
  186. **{argument_name: 'value'}
  187. ),
  188. )
  189. assert command == ('borg', 'list', '--' + argument_name.replace('_', '-'), 'value', 'repo')
  190. def test_make_find_paths_considers_none_as_empty_paths():
  191. assert module.make_find_paths(None) == ()
  192. def test_make_find_paths_passes_through_patterns():
  193. find_paths = (
  194. 'fm:*',
  195. 'sh:**/*.txt',
  196. 're:^.*$',
  197. 'pp:root/somedir',
  198. 'pf:root/foo.txt',
  199. 'R /',
  200. 'r /',
  201. 'p /',
  202. 'P /',
  203. '+ /',
  204. '- /',
  205. '! /',
  206. )
  207. assert module.make_find_paths(find_paths) == find_paths
  208. def test_make_find_paths_adds_globs_to_path_fragments():
  209. assert module.make_find_paths(('foo.txt',)) == ('sh:**/*foo.txt*/**',)
  210. def test_list_archives_calls_borg_with_parameters():
  211. list_arguments = argparse.Namespace(archive=None, paths=None, json=False, find_paths=None)
  212. flexmock(module).should_receive('make_list_command').with_args(
  213. repository='repo',
  214. storage_config={},
  215. list_arguments=list_arguments,
  216. local_path='borg',
  217. remote_path=None,
  218. ).and_return(('borg', 'list', 'repo'))
  219. flexmock(module).should_receive('make_find_paths').and_return(())
  220. flexmock(module).should_receive('execute_command').with_args(
  221. ('borg', 'list', 'repo'), output_log_level=logging.WARNING, borg_local_path='borg'
  222. ).once()
  223. module.list_archives(
  224. repository='repo', storage_config={}, list_arguments=list_arguments,
  225. )
  226. def test_list_archives_with_json_suppresses_most_borg_output():
  227. list_arguments = argparse.Namespace(archive=None, paths=None, json=True, find_paths=None)
  228. flexmock(module).should_receive('make_list_command').with_args(
  229. repository='repo',
  230. storage_config={},
  231. list_arguments=list_arguments,
  232. local_path='borg',
  233. remote_path=None,
  234. ).and_return(('borg', 'list', 'repo'))
  235. flexmock(module).should_receive('make_find_paths').and_return(())
  236. flexmock(module).should_receive('execute_command').with_args(
  237. ('borg', 'list', 'repo'), output_log_level=None, borg_local_path='borg'
  238. ).once()
  239. module.list_archives(
  240. repository='repo', storage_config={}, list_arguments=list_arguments,
  241. )
  242. def test_list_archives_calls_borg_with_local_path():
  243. list_arguments = argparse.Namespace(archive=None, paths=None, json=False, find_paths=None)
  244. flexmock(module).should_receive('make_list_command').with_args(
  245. repository='repo',
  246. storage_config={},
  247. list_arguments=list_arguments,
  248. local_path='borg2',
  249. remote_path=None,
  250. ).and_return(('borg2', 'list', 'repo'))
  251. flexmock(module).should_receive('make_find_paths').and_return(())
  252. flexmock(module).should_receive('execute_command').with_args(
  253. ('borg2', 'list', 'repo'), output_log_level=logging.WARNING, borg_local_path='borg2'
  254. ).once()
  255. module.list_archives(
  256. repository='repo', storage_config={}, list_arguments=list_arguments, local_path='borg2',
  257. )
  258. def test_list_archives_calls_borg_multiple_times_with_find_paths():
  259. glob_paths = ('**/*foo.txt*/**',)
  260. list_arguments = argparse.Namespace(
  261. archive=None, paths=None, json=False, find_paths=['foo.txt'], format=None
  262. )
  263. flexmock(module).should_receive('make_list_command').and_return(
  264. ('borg', 'list', 'repo')
  265. ).and_return(('borg', 'list', 'repo::archive1')).and_return(('borg', 'list', 'repo::archive2'))
  266. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  267. flexmock(module).should_receive('execute_command').with_args(
  268. ('borg', 'list', 'repo'), output_log_level=None, borg_local_path='borg'
  269. ).and_return(
  270. 'archive1 Sun, 2022-05-29 15:27:04 [abc]\narchive2 Mon, 2022-05-30 19:47:15 [xyz]'
  271. ).once()
  272. flexmock(module).should_receive('execute_command').with_args(
  273. ('borg', 'list', 'repo::archive1') + glob_paths,
  274. output_log_level=logging.WARNING,
  275. borg_local_path='borg',
  276. ).once()
  277. flexmock(module).should_receive('execute_command').with_args(
  278. ('borg', 'list', 'repo::archive2') + glob_paths,
  279. output_log_level=logging.WARNING,
  280. borg_local_path='borg',
  281. ).once()
  282. module.list_archives(
  283. repository='repo', storage_config={}, list_arguments=list_arguments,
  284. )
  285. def test_list_archives_calls_borg_with_archive():
  286. list_arguments = argparse.Namespace(archive='archive', paths=None, json=False, find_paths=None)
  287. flexmock(module).should_receive('make_list_command').with_args(
  288. repository='repo',
  289. storage_config={},
  290. list_arguments=list_arguments,
  291. local_path='borg',
  292. remote_path=None,
  293. ).and_return(('borg', 'list', 'repo::archive'))
  294. flexmock(module).should_receive('make_find_paths').and_return(())
  295. flexmock(module).should_receive('execute_command').with_args(
  296. ('borg', 'list', 'repo::archive'), output_log_level=logging.WARNING, borg_local_path='borg'
  297. ).once()
  298. module.list_archives(
  299. repository='repo', storage_config={}, list_arguments=list_arguments,
  300. )