test_check.py 8.2 KB

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