test_check.py 9.4 KB

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