test_list.py 9.6 KB

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