test_check.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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_without_capture').with_args(
  90. ('borg', 'check', '--progress', 'repo'), error_on_warnings=True
  91. ).once()
  92. module.check_archives(
  93. repository='repo', storage_config={}, consistency_config=consistency_config, progress=True
  94. )
  95. def test_check_archives_with_repair_calls_borg_with_repair_parameter():
  96. checks = ('repository',)
  97. consistency_config = {'check_last': None}
  98. flexmock(module).should_receive('_parse_checks').and_return(checks)
  99. flexmock(module).should_receive('_make_check_flags').and_return(())
  100. flexmock(module).should_receive('execute_command').never()
  101. flexmock(module).should_receive('execute_command_without_capture').with_args(
  102. ('borg', 'check', '--repair', 'repo'), error_on_warnings=True
  103. ).once()
  104. module.check_archives(
  105. repository='repo', storage_config={}, consistency_config=consistency_config, repair=True
  106. )
  107. @pytest.mark.parametrize(
  108. 'checks',
  109. (
  110. ('repository',),
  111. ('archives',),
  112. ('repository', 'archives'),
  113. ('repository', 'archives', 'other'),
  114. ),
  115. )
  116. def test_check_archives_calls_borg_with_parameters(checks):
  117. check_last = flexmock()
  118. consistency_config = {'check_last': check_last}
  119. flexmock(module).should_receive('_parse_checks').and_return(checks)
  120. flexmock(module).should_receive('_make_check_flags').with_args(
  121. checks, check_last, module.DEFAULT_PREFIX
  122. ).and_return(())
  123. insert_execute_command_mock(('borg', 'check', 'repo'))
  124. module.check_archives(
  125. repository='repo', storage_config={}, consistency_config=consistency_config
  126. )
  127. def test_check_archives_with_extract_check_calls_extract_only():
  128. checks = ('extract',)
  129. check_last = flexmock()
  130. consistency_config = {'check_last': check_last}
  131. flexmock(module).should_receive('_parse_checks').and_return(checks)
  132. flexmock(module).should_receive('_make_check_flags').never()
  133. flexmock(module.extract).should_receive('extract_last_archive_dry_run').once()
  134. insert_execute_command_never()
  135. module.check_archives(
  136. repository='repo', storage_config={}, consistency_config=consistency_config
  137. )
  138. def test_check_archives_with_log_info_calls_borg_with_info_parameter():
  139. checks = ('repository',)
  140. consistency_config = {'check_last': None}
  141. flexmock(module).should_receive('_parse_checks').and_return(checks)
  142. flexmock(module).should_receive('_make_check_flags').and_return(())
  143. insert_logging_mock(logging.INFO)
  144. insert_execute_command_mock(('borg', 'check', '--info', 'repo'))
  145. module.check_archives(
  146. repository='repo', storage_config={}, consistency_config=consistency_config
  147. )
  148. def test_check_archives_with_log_debug_calls_borg_with_debug_parameter():
  149. checks = ('repository',)
  150. consistency_config = {'check_last': None}
  151. flexmock(module).should_receive('_parse_checks').and_return(checks)
  152. flexmock(module).should_receive('_make_check_flags').and_return(())
  153. insert_logging_mock(logging.DEBUG)
  154. insert_execute_command_mock(('borg', 'check', '--debug', '--show-rc', 'repo'))
  155. module.check_archives(
  156. repository='repo', storage_config={}, consistency_config=consistency_config
  157. )
  158. def test_check_archives_without_any_checks_bails():
  159. consistency_config = {'check_last': None}
  160. flexmock(module).should_receive('_parse_checks').and_return(())
  161. insert_execute_command_never()
  162. module.check_archives(
  163. repository='repo', storage_config={}, consistency_config=consistency_config
  164. )
  165. def test_check_archives_with_local_path_calls_borg_via_local_path():
  166. checks = ('repository',)
  167. check_last = flexmock()
  168. consistency_config = {'check_last': check_last}
  169. flexmock(module).should_receive('_parse_checks').and_return(checks)
  170. flexmock(module).should_receive('_make_check_flags').with_args(
  171. checks, check_last, module.DEFAULT_PREFIX
  172. ).and_return(())
  173. insert_execute_command_mock(('borg1', 'check', 'repo'))
  174. module.check_archives(
  175. repository='repo',
  176. storage_config={},
  177. consistency_config=consistency_config,
  178. local_path='borg1',
  179. )
  180. def test_check_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  181. checks = ('repository',)
  182. check_last = flexmock()
  183. consistency_config = {'check_last': check_last}
  184. flexmock(module).should_receive('_parse_checks').and_return(checks)
  185. flexmock(module).should_receive('_make_check_flags').with_args(
  186. checks, check_last, module.DEFAULT_PREFIX
  187. ).and_return(())
  188. insert_execute_command_mock(('borg', 'check', '--remote-path', 'borg1', 'repo'))
  189. module.check_archives(
  190. repository='repo',
  191. storage_config={},
  192. consistency_config=consistency_config,
  193. remote_path='borg1',
  194. )
  195. def test_check_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  196. checks = ('repository',)
  197. check_last = flexmock()
  198. consistency_config = {'check_last': check_last}
  199. flexmock(module).should_receive('_parse_checks').and_return(checks)
  200. flexmock(module).should_receive('_make_check_flags').with_args(
  201. checks, check_last, module.DEFAULT_PREFIX
  202. ).and_return(())
  203. insert_execute_command_mock(('borg', 'check', '--lock-wait', '5', 'repo'))
  204. module.check_archives(
  205. repository='repo', storage_config={'lock_wait': 5}, consistency_config=consistency_config
  206. )
  207. def test_check_archives_with_retention_prefix():
  208. checks = ('repository',)
  209. check_last = flexmock()
  210. prefix = 'foo-'
  211. consistency_config = {'check_last': check_last, 'prefix': prefix}
  212. flexmock(module).should_receive('_parse_checks').and_return(checks)
  213. flexmock(module).should_receive('_make_check_flags').with_args(
  214. checks, check_last, prefix
  215. ).and_return(())
  216. insert_execute_command_mock(('borg', 'check', 'repo'))
  217. module.check_archives(
  218. repository='repo', storage_config={}, consistency_config=consistency_config
  219. )
  220. def test_check_archives_with_extra_borg_options_calls_borg_with_extra_options():
  221. checks = ('repository',)
  222. consistency_config = {'check_last': None}
  223. flexmock(module).should_receive('_parse_checks').and_return(checks)
  224. flexmock(module).should_receive('_make_check_flags').and_return(())
  225. insert_execute_command_mock(('borg', 'check', '--extra', '--options', 'repo'))
  226. module.check_archives(
  227. repository='repo',
  228. storage_config={'extra_borg_options': {'check': '--extra --options'}},
  229. consistency_config=consistency_config,
  230. )