test_list.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.borg import list as module
  5. from ..test_verbosity import insert_logging_mock
  6. BORG_LIST_LATEST_ARGUMENTS = (
  7. '--glob-archives',
  8. module.BORG_EXCLUDE_CHECKPOINTS_GLOB,
  9. '--last',
  10. '1',
  11. '--short',
  12. 'repo',
  13. )
  14. def test_resolve_archive_name_passes_through_non_latest_archive_name():
  15. archive = 'myhost-2030-01-01T14:41:17.647620'
  16. assert module.resolve_archive_name('repo', archive, storage_config={}) == archive
  17. def test_resolve_archive_name_calls_borg_with_parameters():
  18. expected_archive = 'archive-name'
  19. flexmock(module).should_receive('execute_command').with_args(
  20. ('borg', 'list') + BORG_LIST_LATEST_ARGUMENTS, output_log_level=None, borg_local_path='borg'
  21. ).and_return(expected_archive + '\n')
  22. assert module.resolve_archive_name('repo', 'latest', storage_config={}) == expected_archive
  23. def test_resolve_archive_name_with_log_info_calls_borg_with_info_parameter():
  24. expected_archive = 'archive-name'
  25. flexmock(module).should_receive('execute_command').with_args(
  26. ('borg', 'list', '--info') + BORG_LIST_LATEST_ARGUMENTS,
  27. output_log_level=None,
  28. borg_local_path='borg',
  29. ).and_return(expected_archive + '\n')
  30. insert_logging_mock(logging.INFO)
  31. assert module.resolve_archive_name('repo', 'latest', storage_config={}) == expected_archive
  32. def test_resolve_archive_name_with_log_debug_calls_borg_with_debug_parameter():
  33. expected_archive = 'archive-name'
  34. flexmock(module).should_receive('execute_command').with_args(
  35. ('borg', 'list', '--debug', '--show-rc') + BORG_LIST_LATEST_ARGUMENTS,
  36. output_log_level=None,
  37. borg_local_path='borg',
  38. ).and_return(expected_archive + '\n')
  39. insert_logging_mock(logging.DEBUG)
  40. assert module.resolve_archive_name('repo', 'latest', storage_config={}) == expected_archive
  41. def test_resolve_archive_name_with_local_path_calls_borg_via_local_path():
  42. expected_archive = 'archive-name'
  43. flexmock(module).should_receive('execute_command').with_args(
  44. ('borg1', 'list') + BORG_LIST_LATEST_ARGUMENTS,
  45. output_log_level=None,
  46. borg_local_path='borg1',
  47. ).and_return(expected_archive + '\n')
  48. assert (
  49. module.resolve_archive_name('repo', 'latest', storage_config={}, local_path='borg1')
  50. == expected_archive
  51. )
  52. def test_resolve_archive_name_with_remote_path_calls_borg_with_remote_path_parameters():
  53. expected_archive = 'archive-name'
  54. flexmock(module).should_receive('execute_command').with_args(
  55. ('borg', 'list', '--remote-path', 'borg1') + BORG_LIST_LATEST_ARGUMENTS,
  56. output_log_level=None,
  57. borg_local_path='borg',
  58. ).and_return(expected_archive + '\n')
  59. assert (
  60. module.resolve_archive_name('repo', 'latest', storage_config={}, remote_path='borg1')
  61. == expected_archive
  62. )
  63. def test_resolve_archive_name_without_archives_raises():
  64. flexmock(module).should_receive('execute_command').with_args(
  65. ('borg', 'list') + BORG_LIST_LATEST_ARGUMENTS, output_log_level=None, borg_local_path='borg'
  66. ).and_return('')
  67. with pytest.raises(ValueError):
  68. module.resolve_archive_name('repo', 'latest', storage_config={})
  69. def test_resolve_archive_name_with_lock_wait_calls_borg_with_lock_wait_parameters():
  70. expected_archive = 'archive-name'
  71. flexmock(module).should_receive('execute_command').with_args(
  72. ('borg', 'list', '--lock-wait', 'okay') + BORG_LIST_LATEST_ARGUMENTS,
  73. output_log_level=None,
  74. borg_local_path='borg',
  75. ).and_return(expected_archive + '\n')
  76. assert (
  77. module.resolve_archive_name('repo', 'latest', storage_config={'lock_wait': 'okay'})
  78. == expected_archive
  79. )
  80. def test_list_archives_calls_borg_with_parameters():
  81. flexmock(module).should_receive('execute_command').with_args(
  82. ('borg', 'list', 'repo'), output_log_level=logging.WARNING, borg_local_path='borg'
  83. )
  84. module.list_archives(
  85. repository='repo',
  86. storage_config={},
  87. list_arguments=flexmock(archive=None, paths=None, json=False, successful=False),
  88. )
  89. def test_list_archives_with_log_info_calls_borg_with_info_parameter():
  90. flexmock(module).should_receive('execute_command').with_args(
  91. ('borg', 'list', '--info', 'repo'), output_log_level=logging.WARNING, borg_local_path='borg'
  92. )
  93. insert_logging_mock(logging.INFO)
  94. module.list_archives(
  95. repository='repo',
  96. storage_config={},
  97. list_arguments=flexmock(archive=None, paths=None, json=False, successful=False),
  98. )
  99. def test_list_archives_with_log_info_and_json_suppresses_most_borg_output():
  100. flexmock(module).should_receive('execute_command').with_args(
  101. ('borg', 'list', '--json', 'repo'), output_log_level=None, borg_local_path='borg'
  102. )
  103. insert_logging_mock(logging.INFO)
  104. module.list_archives(
  105. repository='repo',
  106. storage_config={},
  107. list_arguments=flexmock(archive=None, paths=None, json=True, successful=False),
  108. )
  109. def test_list_archives_with_log_debug_calls_borg_with_debug_parameter():
  110. flexmock(module).should_receive('execute_command').with_args(
  111. ('borg', 'list', '--debug', '--show-rc', 'repo'),
  112. output_log_level=logging.WARNING,
  113. borg_local_path='borg',
  114. )
  115. insert_logging_mock(logging.DEBUG)
  116. module.list_archives(
  117. repository='repo',
  118. storage_config={},
  119. list_arguments=flexmock(archive=None, paths=None, json=False, successful=False),
  120. )
  121. def test_list_archives_with_log_debug_and_json_suppresses_most_borg_output():
  122. flexmock(module).should_receive('execute_command').with_args(
  123. ('borg', 'list', '--json', 'repo'), output_log_level=None, borg_local_path='borg'
  124. )
  125. insert_logging_mock(logging.DEBUG)
  126. module.list_archives(
  127. repository='repo',
  128. storage_config={},
  129. list_arguments=flexmock(archive=None, paths=None, json=True, successful=False),
  130. )
  131. def test_list_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  132. storage_config = {'lock_wait': 5}
  133. flexmock(module).should_receive('execute_command').with_args(
  134. ('borg', 'list', '--lock-wait', '5', 'repo'),
  135. output_log_level=logging.WARNING,
  136. borg_local_path='borg',
  137. )
  138. module.list_archives(
  139. repository='repo',
  140. storage_config=storage_config,
  141. list_arguments=flexmock(archive=None, paths=None, json=False, successful=False),
  142. )
  143. def test_list_archives_with_archive_calls_borg_with_archive_parameter():
  144. storage_config = {}
  145. flexmock(module).should_receive('execute_command').with_args(
  146. ('borg', 'list', 'repo::archive'), output_log_level=logging.WARNING, borg_local_path='borg'
  147. )
  148. module.list_archives(
  149. repository='repo',
  150. storage_config=storage_config,
  151. list_arguments=flexmock(archive='archive', paths=None, json=False, successful=False),
  152. )
  153. def test_list_archives_with_path_calls_borg_with_path_parameter():
  154. storage_config = {}
  155. flexmock(module).should_receive('execute_command').with_args(
  156. ('borg', 'list', 'repo::archive', 'var/lib'),
  157. output_log_level=logging.WARNING,
  158. borg_local_path='borg',
  159. )
  160. module.list_archives(
  161. repository='repo',
  162. storage_config=storage_config,
  163. list_arguments=flexmock(archive='archive', paths=['var/lib'], json=False, successful=False),
  164. )
  165. def test_list_archives_with_local_path_calls_borg_via_local_path():
  166. flexmock(module).should_receive('execute_command').with_args(
  167. ('borg1', 'list', 'repo'), output_log_level=logging.WARNING, borg_local_path='borg1'
  168. )
  169. module.list_archives(
  170. repository='repo',
  171. storage_config={},
  172. list_arguments=flexmock(archive=None, paths=None, json=False, successful=False),
  173. local_path='borg1',
  174. )
  175. def test_list_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  176. flexmock(module).should_receive('execute_command').with_args(
  177. ('borg', 'list', '--remote-path', 'borg1', 'repo'),
  178. output_log_level=logging.WARNING,
  179. borg_local_path='borg',
  180. )
  181. module.list_archives(
  182. repository='repo',
  183. storage_config={},
  184. list_arguments=flexmock(archive=None, paths=None, json=False, successful=False),
  185. remote_path='borg1',
  186. )
  187. def test_list_archives_with_short_calls_borg_with_short_parameter():
  188. flexmock(module).should_receive('execute_command').with_args(
  189. ('borg', 'list', '--short', 'repo'),
  190. output_log_level=logging.WARNING,
  191. borg_local_path='borg',
  192. ).and_return('[]')
  193. module.list_archives(
  194. repository='repo',
  195. storage_config={},
  196. list_arguments=flexmock(archive=None, paths=None, json=False, successful=False, short=True),
  197. )
  198. @pytest.mark.parametrize(
  199. 'argument_name',
  200. (
  201. 'prefix',
  202. 'glob_archives',
  203. 'sort_by',
  204. 'first',
  205. 'last',
  206. 'exclude',
  207. 'exclude_from',
  208. 'pattern',
  209. 'patterns_from',
  210. ),
  211. )
  212. def test_list_archives_passes_through_arguments_to_borg(argument_name):
  213. flexmock(module).should_receive('execute_command').with_args(
  214. ('borg', 'list', '--' + argument_name.replace('_', '-'), 'value', 'repo'),
  215. output_log_level=logging.WARNING,
  216. borg_local_path='borg',
  217. ).and_return('[]')
  218. module.list_archives(
  219. repository='repo',
  220. storage_config={},
  221. list_arguments=flexmock(
  222. archive=None, paths=None, json=False, successful=False, **{argument_name: 'value'}
  223. ),
  224. )
  225. def test_list_archives_with_successful_calls_borg_to_exclude_checkpoints():
  226. flexmock(module).should_receive('execute_command').with_args(
  227. ('borg', 'list', '--glob-archives', module.BORG_EXCLUDE_CHECKPOINTS_GLOB, 'repo'),
  228. output_log_level=logging.WARNING,
  229. borg_local_path='borg',
  230. ).and_return('[]')
  231. module.list_archives(
  232. repository='repo',
  233. storage_config={},
  234. list_arguments=flexmock(archive=None, paths=None, json=False, successful=True),
  235. )
  236. def test_list_archives_with_json_calls_borg_with_json_parameter():
  237. flexmock(module).should_receive('execute_command').with_args(
  238. ('borg', 'list', '--json', 'repo'), output_log_level=None, borg_local_path='borg'
  239. ).and_return('[]')
  240. json_output = module.list_archives(
  241. repository='repo',
  242. storage_config={},
  243. list_arguments=flexmock(archive=None, paths=None, json=True, successful=False),
  244. )
  245. assert json_output == '[]'