test_check.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. @pytest.mark.parametrize(
  84. 'checks',
  85. (
  86. ('repository',),
  87. ('archives',),
  88. ('repository', 'archives'),
  89. ('repository', 'archives', 'other'),
  90. ),
  91. )
  92. def test_check_archives_calls_borg_with_parameters(checks):
  93. check_last = flexmock()
  94. consistency_config = {'check_last': check_last}
  95. flexmock(module).should_receive('_parse_checks').and_return(checks)
  96. flexmock(module).should_receive('_make_check_flags').with_args(
  97. checks, check_last, module.DEFAULT_PREFIX
  98. ).and_return(())
  99. insert_execute_command_mock(('borg', 'check', 'repo'))
  100. module.check_archives(
  101. repository='repo', storage_config={}, consistency_config=consistency_config
  102. )
  103. def test_check_archives_with_extract_check_calls_extract_only():
  104. checks = ('extract',)
  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').never()
  109. flexmock(module.extract).should_receive('extract_last_archive_dry_run').once()
  110. insert_execute_command_never()
  111. module.check_archives(
  112. repository='repo', storage_config={}, consistency_config=consistency_config
  113. )
  114. def test_check_archives_with_log_info_calls_borg_with_info_parameter():
  115. checks = ('repository',)
  116. consistency_config = {'check_last': None}
  117. flexmock(module).should_receive('_parse_checks').and_return(checks)
  118. flexmock(module).should_receive('_make_check_flags').and_return(())
  119. insert_logging_mock(logging.INFO)
  120. insert_execute_command_mock(('borg', 'check', '--info', 'repo'))
  121. module.check_archives(
  122. repository='repo', storage_config={}, consistency_config=consistency_config
  123. )
  124. def test_check_archives_with_log_debug_calls_borg_with_debug_parameter():
  125. checks = ('repository',)
  126. consistency_config = {'check_last': None}
  127. flexmock(module).should_receive('_parse_checks').and_return(checks)
  128. flexmock(module).should_receive('_make_check_flags').and_return(())
  129. insert_logging_mock(logging.DEBUG)
  130. insert_execute_command_mock(('borg', 'check', '--debug', '--show-rc', 'repo'))
  131. module.check_archives(
  132. repository='repo', storage_config={}, consistency_config=consistency_config
  133. )
  134. def test_check_archives_without_any_checks_bails():
  135. consistency_config = {'check_last': None}
  136. flexmock(module).should_receive('_parse_checks').and_return(())
  137. insert_execute_command_never()
  138. module.check_archives(
  139. repository='repo', storage_config={}, consistency_config=consistency_config
  140. )
  141. def test_check_archives_with_local_path_calls_borg_via_local_path():
  142. checks = ('repository',)
  143. check_last = flexmock()
  144. consistency_config = {'check_last': check_last}
  145. flexmock(module).should_receive('_parse_checks').and_return(checks)
  146. flexmock(module).should_receive('_make_check_flags').with_args(
  147. checks, check_last, module.DEFAULT_PREFIX
  148. ).and_return(())
  149. insert_execute_command_mock(('borg1', 'check', 'repo'))
  150. module.check_archives(
  151. repository='repo',
  152. storage_config={},
  153. consistency_config=consistency_config,
  154. local_path='borg1',
  155. )
  156. def test_check_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  157. checks = ('repository',)
  158. check_last = flexmock()
  159. consistency_config = {'check_last': check_last}
  160. flexmock(module).should_receive('_parse_checks').and_return(checks)
  161. flexmock(module).should_receive('_make_check_flags').with_args(
  162. checks, check_last, module.DEFAULT_PREFIX
  163. ).and_return(())
  164. insert_execute_command_mock(('borg', 'check', '--remote-path', 'borg1', 'repo'))
  165. module.check_archives(
  166. repository='repo',
  167. storage_config={},
  168. consistency_config=consistency_config,
  169. remote_path='borg1',
  170. )
  171. def test_check_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  172. checks = ('repository',)
  173. check_last = flexmock()
  174. consistency_config = {'check_last': check_last}
  175. flexmock(module).should_receive('_parse_checks').and_return(checks)
  176. flexmock(module).should_receive('_make_check_flags').with_args(
  177. checks, check_last, module.DEFAULT_PREFIX
  178. ).and_return(())
  179. insert_execute_command_mock(('borg', 'check', '--lock-wait', '5', 'repo'))
  180. module.check_archives(
  181. repository='repo', storage_config={'lock_wait': 5}, consistency_config=consistency_config
  182. )
  183. def test_check_archives_with_retention_prefix():
  184. checks = ('repository',)
  185. check_last = flexmock()
  186. prefix = 'foo-'
  187. consistency_config = {'check_last': check_last, 'prefix': prefix}
  188. flexmock(module).should_receive('_parse_checks').and_return(checks)
  189. flexmock(module).should_receive('_make_check_flags').with_args(
  190. checks, check_last, prefix
  191. ).and_return(())
  192. insert_execute_command_mock(('borg', 'check', 'repo'))
  193. module.check_archives(
  194. repository='repo', storage_config={}, consistency_config=consistency_config
  195. )