test_check.py 11 KB

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