test_check.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.borg import check as module
  5. from ..test_verbosity import insert_logging_mock
  6. def insert_execute_command_mock(command):
  7. flexmock(module).should_receive('execute_command').with_args(
  8. command, error_on_warnings=True
  9. ).once()
  10. def insert_execute_command_never():
  11. flexmock(module).should_receive('execute_command').never()
  12. def test_parse_checks_returns_them_as_tuple():
  13. checks = module._parse_checks({'checks': ['foo', 'disabled', 'bar']})
  14. assert checks == ('foo', 'bar')
  15. def test_parse_checks_with_missing_value_returns_defaults():
  16. checks = module._parse_checks({})
  17. assert checks == module.DEFAULT_CHECKS
  18. def test_parse_checks_with_blank_value_returns_defaults():
  19. checks = module._parse_checks({'checks': []})
  20. assert checks == module.DEFAULT_CHECKS
  21. def test_parse_checks_with_none_value_returns_defaults():
  22. checks = module._parse_checks({'checks': None})
  23. assert checks == module.DEFAULT_CHECKS
  24. def test_parse_checks_with_disabled_returns_no_checks():
  25. checks = module._parse_checks({'checks': ['disabled']})
  26. assert checks == ()
  27. def test_parse_checks_with_data_check_also_injects_archives():
  28. checks = module._parse_checks({'checks': ['data']})
  29. assert checks == ('data', 'archives')
  30. def test_parse_checks_with_data_check_passes_through_archives():
  31. checks = module._parse_checks({'checks': ['data', 'archives']})
  32. assert checks == ('data', 'archives')
  33. def test_parse_checks_prefers_override_checks_to_configured_checks():
  34. checks = module._parse_checks({'checks': ['archives']}, only_checks=['repository', 'extract'])
  35. assert checks == ('repository', 'extract')
  36. def test_parse_checks_with_override_data_check_also_injects_archives():
  37. checks = module._parse_checks({'checks': ['extract']}, only_checks=['data'])
  38. assert checks == ('data', 'archives')
  39. def test_make_check_flags_with_repository_check_returns_flag():
  40. flags = module._make_check_flags(('repository',))
  41. assert flags == ('--repository-only',)
  42. def test_make_check_flags_with_archives_check_returns_flag():
  43. flags = module._make_check_flags(('archives',))
  44. assert flags == ('--archives-only',)
  45. def test_make_check_flags_with_data_check_returns_flag():
  46. flags = module._make_check_flags(('data',))
  47. assert flags == ('--verify-data',)
  48. def test_make_check_flags_with_extract_omits_extract_flag():
  49. flags = module._make_check_flags(('extract',))
  50. assert flags == ()
  51. def test_make_check_flags_with_default_checks_and_default_prefix_returns_default_flags():
  52. flags = module._make_check_flags(module.DEFAULT_CHECKS, prefix=module.DEFAULT_PREFIX)
  53. assert flags == ('--prefix', module.DEFAULT_PREFIX)
  54. def test_make_check_flags_with_all_checks_and_default_prefix_returns_default_flags():
  55. flags = module._make_check_flags(
  56. module.DEFAULT_CHECKS + ('extract',), prefix=module.DEFAULT_PREFIX
  57. )
  58. assert flags == ('--prefix', module.DEFAULT_PREFIX)
  59. def test_make_check_flags_with_archives_check_and_last_includes_last_flag():
  60. flags = module._make_check_flags(('archives',), check_last=3)
  61. assert flags == ('--archives-only', '--last', '3')
  62. def test_make_check_flags_with_repository_check_and_last_omits_last_flag():
  63. flags = module._make_check_flags(('repository',), check_last=3)
  64. assert flags == ('--repository-only',)
  65. def test_make_check_flags_with_default_checks_and_last_includes_last_flag():
  66. flags = module._make_check_flags(module.DEFAULT_CHECKS, check_last=3)
  67. assert flags == ('--last', '3')
  68. def test_make_check_flags_with_archives_check_and_prefix_includes_prefix_flag():
  69. flags = module._make_check_flags(('archives',), prefix='foo-')
  70. assert flags == ('--archives-only', '--prefix', 'foo-')
  71. def test_make_check_flags_with_archives_check_and_empty_prefix_omits_prefix_flag():
  72. flags = module._make_check_flags(('archives',), prefix='')
  73. assert flags == ('--archives-only',)
  74. def test_make_check_flags_with_archives_check_and_none_prefix_omits_prefix_flag():
  75. flags = module._make_check_flags(('archives',), prefix=None)
  76. assert flags == ('--archives-only',)
  77. def test_make_check_flags_with_repository_check_and_prefix_omits_prefix_flag():
  78. flags = module._make_check_flags(('repository',), prefix='foo-')
  79. assert flags == ('--repository-only',)
  80. def test_make_check_flags_with_default_checks_and_prefix_includes_prefix_flag():
  81. flags = module._make_check_flags(module.DEFAULT_CHECKS, prefix='foo-')
  82. assert flags == ('--prefix', 'foo-')
  83. def test_check_archives_with_progress_calls_borg_with_progress_parameter():
  84. checks = ('repository',)
  85. consistency_config = {'check_last': None}
  86. flexmock(module).should_receive('_parse_checks').and_return(checks)
  87. flexmock(module).should_receive('_make_check_flags').and_return(())
  88. flexmock(module).should_receive('execute_command').never()
  89. flexmock(module).should_receive('execute_command').with_args(
  90. ('borg', 'check', '--progress', 'repo'),
  91. output_file=module.DO_NOT_CAPTURE,
  92. error_on_warnings=True,
  93. ).once()
  94. module.check_archives(
  95. repository='repo', storage_config={}, consistency_config=consistency_config, progress=True
  96. )
  97. def test_check_archives_with_repair_calls_borg_with_repair_parameter():
  98. checks = ('repository',)
  99. consistency_config = {'check_last': None}
  100. flexmock(module).should_receive('_parse_checks').and_return(checks)
  101. flexmock(module).should_receive('_make_check_flags').and_return(())
  102. flexmock(module).should_receive('execute_command').never()
  103. flexmock(module).should_receive('execute_command').with_args(
  104. ('borg', 'check', '--repair', 'repo'),
  105. output_file=module.DO_NOT_CAPTURE,
  106. error_on_warnings=True,
  107. ).once()
  108. module.check_archives(
  109. repository='repo', storage_config={}, consistency_config=consistency_config, repair=True
  110. )
  111. @pytest.mark.parametrize(
  112. 'checks',
  113. (
  114. ('repository',),
  115. ('archives',),
  116. ('repository', 'archives'),
  117. ('repository', 'archives', 'other'),
  118. ),
  119. )
  120. def test_check_archives_calls_borg_with_parameters(checks):
  121. check_last = flexmock()
  122. consistency_config = {'check_last': check_last}
  123. flexmock(module).should_receive('_parse_checks').and_return(checks)
  124. flexmock(module).should_receive('_make_check_flags').with_args(
  125. checks, check_last, module.DEFAULT_PREFIX
  126. ).and_return(())
  127. insert_execute_command_mock(('borg', 'check', 'repo'))
  128. module.check_archives(
  129. repository='repo', storage_config={}, consistency_config=consistency_config
  130. )
  131. def test_check_archives_with_extract_check_calls_extract_only():
  132. checks = ('extract',)
  133. check_last = flexmock()
  134. consistency_config = {'check_last': check_last}
  135. flexmock(module).should_receive('_parse_checks').and_return(checks)
  136. flexmock(module).should_receive('_make_check_flags').never()
  137. flexmock(module.extract).should_receive('extract_last_archive_dry_run').once()
  138. insert_execute_command_never()
  139. module.check_archives(
  140. repository='repo', storage_config={}, consistency_config=consistency_config
  141. )
  142. def test_check_archives_with_log_info_calls_borg_with_info_parameter():
  143. checks = ('repository',)
  144. consistency_config = {'check_last': None}
  145. flexmock(module).should_receive('_parse_checks').and_return(checks)
  146. flexmock(module).should_receive('_make_check_flags').and_return(())
  147. insert_logging_mock(logging.INFO)
  148. insert_execute_command_mock(('borg', 'check', '--info', 'repo'))
  149. module.check_archives(
  150. repository='repo', storage_config={}, consistency_config=consistency_config
  151. )
  152. def test_check_archives_with_log_debug_calls_borg_with_debug_parameter():
  153. checks = ('repository',)
  154. consistency_config = {'check_last': None}
  155. flexmock(module).should_receive('_parse_checks').and_return(checks)
  156. flexmock(module).should_receive('_make_check_flags').and_return(())
  157. insert_logging_mock(logging.DEBUG)
  158. insert_execute_command_mock(('borg', 'check', '--debug', '--show-rc', 'repo'))
  159. module.check_archives(
  160. repository='repo', storage_config={}, consistency_config=consistency_config
  161. )
  162. def test_check_archives_without_any_checks_bails():
  163. consistency_config = {'check_last': None}
  164. flexmock(module).should_receive('_parse_checks').and_return(())
  165. insert_execute_command_never()
  166. module.check_archives(
  167. repository='repo', storage_config={}, consistency_config=consistency_config
  168. )
  169. def test_check_archives_with_local_path_calls_borg_via_local_path():
  170. checks = ('repository',)
  171. check_last = flexmock()
  172. consistency_config = {'check_last': check_last}
  173. flexmock(module).should_receive('_parse_checks').and_return(checks)
  174. flexmock(module).should_receive('_make_check_flags').with_args(
  175. checks, check_last, module.DEFAULT_PREFIX
  176. ).and_return(())
  177. insert_execute_command_mock(('borg1', 'check', 'repo'))
  178. module.check_archives(
  179. repository='repo',
  180. storage_config={},
  181. consistency_config=consistency_config,
  182. local_path='borg1',
  183. )
  184. def test_check_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  185. checks = ('repository',)
  186. check_last = flexmock()
  187. consistency_config = {'check_last': check_last}
  188. flexmock(module).should_receive('_parse_checks').and_return(checks)
  189. flexmock(module).should_receive('_make_check_flags').with_args(
  190. checks, check_last, module.DEFAULT_PREFIX
  191. ).and_return(())
  192. insert_execute_command_mock(('borg', 'check', '--remote-path', 'borg1', 'repo'))
  193. module.check_archives(
  194. repository='repo',
  195. storage_config={},
  196. consistency_config=consistency_config,
  197. remote_path='borg1',
  198. )
  199. def test_check_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  200. checks = ('repository',)
  201. check_last = flexmock()
  202. consistency_config = {'check_last': check_last}
  203. flexmock(module).should_receive('_parse_checks').and_return(checks)
  204. flexmock(module).should_receive('_make_check_flags').with_args(
  205. checks, check_last, module.DEFAULT_PREFIX
  206. ).and_return(())
  207. insert_execute_command_mock(('borg', 'check', '--lock-wait', '5', 'repo'))
  208. module.check_archives(
  209. repository='repo', storage_config={'lock_wait': 5}, consistency_config=consistency_config
  210. )
  211. def test_check_archives_with_retention_prefix():
  212. checks = ('repository',)
  213. check_last = flexmock()
  214. prefix = 'foo-'
  215. consistency_config = {'check_last': check_last, 'prefix': prefix}
  216. flexmock(module).should_receive('_parse_checks').and_return(checks)
  217. flexmock(module).should_receive('_make_check_flags').with_args(
  218. checks, check_last, prefix
  219. ).and_return(())
  220. insert_execute_command_mock(('borg', 'check', 'repo'))
  221. module.check_archives(
  222. repository='repo', storage_config={}, consistency_config=consistency_config
  223. )
  224. def test_check_archives_with_extra_borg_options_calls_borg_with_extra_options():
  225. checks = ('repository',)
  226. consistency_config = {'check_last': None}
  227. flexmock(module).should_receive('_parse_checks').and_return(checks)
  228. flexmock(module).should_receive('_make_check_flags').and_return(())
  229. insert_execute_command_mock(('borg', 'check', '--extra', '--options', 'repo'))
  230. module.check_archives(
  231. repository='repo',
  232. storage_config={'extra_borg_options': {'check': '--extra --options'}},
  233. consistency_config=consistency_config,
  234. )