test_check.py 7.8 KB

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