2
0

test_check.py 11 KB

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