test_check.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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_make_check_flags_with_repository_check_returns_flag():
  26. flags = module._make_check_flags(('repository',))
  27. assert flags == ('--repository-only',)
  28. def test_make_check_flags_with_extract_omits_extract_flag():
  29. flags = module._make_check_flags(('extract',))
  30. assert flags == ()
  31. def test_make_check_flags_with_default_checks_and_default_prefix_returns_default_flags():
  32. flags = module._make_check_flags(module.DEFAULT_CHECKS, prefix=module.DEFAULT_PREFIX)
  33. assert flags == ('--prefix', module.DEFAULT_PREFIX)
  34. def test_make_check_flags_with_all_checks_and_default_prefix_returns_default_flags():
  35. flags = module._make_check_flags(
  36. module.DEFAULT_CHECKS + ('extract',), prefix=module.DEFAULT_PREFIX
  37. )
  38. assert flags == ('--prefix', module.DEFAULT_PREFIX)
  39. def test_make_check_flags_with_archives_check_and_last_includes_last_flag():
  40. flags = module._make_check_flags(('archives',), check_last=3)
  41. assert flags == ('--archives-only', '--last', '3')
  42. def test_make_check_flags_with_repository_check_and_last_omits_last_flag():
  43. flags = module._make_check_flags(('repository',), check_last=3)
  44. assert flags == ('--repository-only',)
  45. def test_make_check_flags_with_default_checks_and_last_includes_last_flag():
  46. flags = module._make_check_flags(module.DEFAULT_CHECKS, check_last=3)
  47. assert flags == ('--last', '3')
  48. def test_make_check_flags_with_archives_check_and_prefix_includes_prefix_flag():
  49. flags = module._make_check_flags(('archives',), prefix='foo-')
  50. assert flags == ('--archives-only', '--prefix', 'foo-')
  51. def test_make_check_flags_with_archives_check_and_empty_prefix_omits_prefix_flag():
  52. flags = module._make_check_flags(('archives',), prefix='')
  53. assert flags == ('--archives-only',)
  54. def test_make_check_flags_with_archives_check_and_none_prefix_omits_prefix_flag():
  55. flags = module._make_check_flags(('archives',), prefix=None)
  56. assert flags == ('--archives-only',)
  57. def test_make_check_flags_with_repository_check_and_prefix_omits_prefix_flag():
  58. flags = module._make_check_flags(('repository',), prefix='foo-')
  59. assert flags == ('--repository-only',)
  60. def test_make_check_flags_with_default_checks_and_prefix_includes_prefix_flag():
  61. flags = module._make_check_flags(module.DEFAULT_CHECKS, prefix='foo-')
  62. assert flags == ('--prefix', 'foo-')
  63. @pytest.mark.parametrize(
  64. 'checks',
  65. (
  66. ('repository',),
  67. ('archives',),
  68. ('repository', 'archives'),
  69. ('repository', 'archives', 'other'),
  70. ),
  71. )
  72. def test_check_archives_calls_borg_with_parameters(checks):
  73. check_last = flexmock()
  74. consistency_config = {'check_last': check_last}
  75. flexmock(module).should_receive('_parse_checks').and_return(checks)
  76. flexmock(module).should_receive('_make_check_flags').with_args(
  77. checks, check_last, module.DEFAULT_PREFIX
  78. ).and_return(())
  79. insert_execute_command_mock(('borg', 'check', 'repo'))
  80. module.check_archives(
  81. repository='repo', storage_config={}, consistency_config=consistency_config
  82. )
  83. def test_check_archives_with_extract_check_calls_extract_only():
  84. checks = ('extract',)
  85. check_last = flexmock()
  86. consistency_config = {'check_last': check_last}
  87. flexmock(module).should_receive('_parse_checks').and_return(checks)
  88. flexmock(module).should_receive('_make_check_flags').never()
  89. flexmock(module.extract).should_receive('extract_last_archive_dry_run').once()
  90. insert_execute_command_never()
  91. module.check_archives(
  92. repository='repo', storage_config={}, consistency_config=consistency_config
  93. )
  94. def test_check_archives_with_log_info_calls_borg_with_info_parameter():
  95. checks = ('repository',)
  96. consistency_config = {'check_last': None}
  97. flexmock(module).should_receive('_parse_checks').and_return(checks)
  98. flexmock(module).should_receive('_make_check_flags').and_return(())
  99. insert_logging_mock(logging.INFO)
  100. insert_execute_command_mock(('borg', 'check', 'repo', '--info'))
  101. module.check_archives(
  102. repository='repo', storage_config={}, consistency_config=consistency_config
  103. )
  104. def test_check_archives_with_log_debug_calls_borg_with_debug_parameter():
  105. checks = ('repository',)
  106. consistency_config = {'check_last': None}
  107. flexmock(module).should_receive('_parse_checks').and_return(checks)
  108. flexmock(module).should_receive('_make_check_flags').and_return(())
  109. insert_logging_mock(logging.DEBUG)
  110. insert_execute_command_mock(('borg', 'check', 'repo', '--debug', '--show-rc'))
  111. module.check_archives(
  112. repository='repo', storage_config={}, consistency_config=consistency_config
  113. )
  114. def test_check_archives_without_any_checks_bails():
  115. consistency_config = {'check_last': None}
  116. flexmock(module).should_receive('_parse_checks').and_return(())
  117. insert_execute_command_never()
  118. module.check_archives(
  119. repository='repo', storage_config={}, consistency_config=consistency_config
  120. )
  121. def test_check_archives_with_local_path_calls_borg_via_local_path():
  122. checks = ('repository',)
  123. check_last = flexmock()
  124. consistency_config = {'check_last': check_last}
  125. flexmock(module).should_receive('_parse_checks').and_return(checks)
  126. flexmock(module).should_receive('_make_check_flags').with_args(
  127. checks, check_last, module.DEFAULT_PREFIX
  128. ).and_return(())
  129. insert_execute_command_mock(('borg1', 'check', 'repo'))
  130. module.check_archives(
  131. repository='repo',
  132. storage_config={},
  133. consistency_config=consistency_config,
  134. local_path='borg1',
  135. )
  136. def test_check_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  137. checks = ('repository',)
  138. check_last = flexmock()
  139. consistency_config = {'check_last': check_last}
  140. flexmock(module).should_receive('_parse_checks').and_return(checks)
  141. flexmock(module).should_receive('_make_check_flags').with_args(
  142. checks, check_last, module.DEFAULT_PREFIX
  143. ).and_return(())
  144. insert_execute_command_mock(('borg', 'check', 'repo', '--remote-path', 'borg1'))
  145. module.check_archives(
  146. repository='repo',
  147. storage_config={},
  148. consistency_config=consistency_config,
  149. remote_path='borg1',
  150. )
  151. def test_check_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  152. checks = ('repository',)
  153. check_last = flexmock()
  154. consistency_config = {'check_last': check_last}
  155. flexmock(module).should_receive('_parse_checks').and_return(checks)
  156. flexmock(module).should_receive('_make_check_flags').with_args(
  157. checks, check_last, module.DEFAULT_PREFIX
  158. ).and_return(())
  159. insert_execute_command_mock(('borg', 'check', 'repo', '--lock-wait', '5'))
  160. module.check_archives(
  161. repository='repo', storage_config={'lock_wait': 5}, consistency_config=consistency_config
  162. )
  163. def test_check_archives_with_retention_prefix():
  164. checks = ('repository',)
  165. check_last = flexmock()
  166. prefix = 'foo-'
  167. consistency_config = {'check_last': check_last, 'prefix': prefix}
  168. flexmock(module).should_receive('_parse_checks').and_return(checks)
  169. flexmock(module).should_receive('_make_check_flags').with_args(
  170. checks, check_last, prefix
  171. ).and_return(())
  172. insert_execute_command_mock(('borg', 'check', 'repo'))
  173. module.check_archives(
  174. repository='repo', storage_config={}, consistency_config=consistency_config
  175. )