test_check.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. from subprocess import STDOUT
  2. import sys
  3. from flexmock import flexmock
  4. import pytest
  5. from borgmatic.borg import check as module
  6. from borgmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS
  7. def insert_subprocess_mock(check_call_command, **kwargs):
  8. subprocess = flexmock(module.subprocess)
  9. subprocess.should_receive('check_call').with_args(check_call_command, **kwargs).once()
  10. def insert_subprocess_never():
  11. subprocess = flexmock(module.subprocess)
  12. subprocess.should_receive('check_call').never()
  13. def test_parse_checks_returns_them_as_tuple():
  14. checks = module._parse_checks({'checks': ['foo', 'disabled', 'bar']})
  15. assert checks == ('foo', 'bar')
  16. def test_parse_checks_with_missing_value_returns_defaults():
  17. checks = module._parse_checks({})
  18. assert checks == module.DEFAULT_CHECKS
  19. def test_parse_checks_with_blank_value_returns_defaults():
  20. checks = module._parse_checks({'checks': []})
  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_checks_returns_flags():
  26. flags = module._make_check_flags(('repository',))
  27. assert flags == ('--repository-only',)
  28. def test_make_check_flags_with_extract_check_does_not_make_extract_flag():
  29. flags = module._make_check_flags(('extract',))
  30. assert flags == ()
  31. def test_make_check_flags_with_default_checks_returns_no_flags():
  32. flags = module._make_check_flags(module.DEFAULT_CHECKS)
  33. assert flags == ()
  34. def test_make_check_flags_with_all_checks_returns_no_flags():
  35. flags = module._make_check_flags(module.DEFAULT_CHECKS + ('extract',))
  36. assert flags == ()
  37. def test_make_check_flags_with_checks_and_last_returns_flags_including_last():
  38. flags = module._make_check_flags(('repository',), check_last=3)
  39. assert flags == ('--repository-only', '--last', '3')
  40. def test_make_check_flags_with_default_checks_and_last_returns_last_flag():
  41. flags = module._make_check_flags(module.DEFAULT_CHECKS, check_last=3)
  42. assert flags == ('--last', '3')
  43. @pytest.mark.parametrize(
  44. 'checks',
  45. (
  46. ('repository',),
  47. ('archives',),
  48. ('repository', 'archives'),
  49. ('repository', 'archives', 'other'),
  50. ),
  51. )
  52. def test_check_archives_calls_borg_with_parameters(checks):
  53. check_last = flexmock()
  54. consistency_config = flexmock().should_receive('get').and_return(check_last).mock
  55. flexmock(module).should_receive('_parse_checks').and_return(checks)
  56. flexmock(module).should_receive('_make_check_flags').with_args(checks, check_last).and_return(())
  57. stdout = flexmock()
  58. insert_subprocess_mock(
  59. ('borg', 'check', 'repo'),
  60. stdout=stdout, stderr=STDOUT,
  61. )
  62. flexmock(sys.modules['builtins']).should_receive('open').and_return(stdout)
  63. flexmock(module.os).should_receive('devnull')
  64. module.check_archives(
  65. verbosity=None,
  66. repository='repo',
  67. consistency_config=consistency_config,
  68. )
  69. def test_check_archives_with_extract_check_calls_extract_only():
  70. checks = ('extract',)
  71. check_last = flexmock()
  72. consistency_config = flexmock().should_receive('get').and_return(check_last).mock
  73. flexmock(module).should_receive('_parse_checks').and_return(checks)
  74. flexmock(module).should_receive('_make_check_flags').never()
  75. flexmock(module.extract).should_receive('extract_last_archive_dry_run').once()
  76. insert_subprocess_never()
  77. module.check_archives(
  78. verbosity=None,
  79. repository='repo',
  80. consistency_config=consistency_config,
  81. )
  82. def test_check_archives_with_verbosity_some_calls_borg_with_info_parameter():
  83. checks = ('repository',)
  84. consistency_config = flexmock().should_receive('get').and_return(None).mock
  85. flexmock(module).should_receive('_parse_checks').and_return(checks)
  86. flexmock(module).should_receive('_make_check_flags').and_return(())
  87. insert_subprocess_mock(
  88. ('borg', 'check', 'repo', '--info'),
  89. stdout=None, stderr=STDOUT,
  90. )
  91. module.check_archives(
  92. verbosity=VERBOSITY_SOME,
  93. repository='repo',
  94. consistency_config=consistency_config,
  95. )
  96. def test_check_archives_with_verbosity_lots_calls_borg_with_debug_parameter():
  97. checks = ('repository',)
  98. consistency_config = flexmock().should_receive('get').and_return(None).mock
  99. flexmock(module).should_receive('_parse_checks').and_return(checks)
  100. flexmock(module).should_receive('_make_check_flags').and_return(())
  101. insert_subprocess_mock(
  102. ('borg', 'check', 'repo', '--debug'),
  103. stdout=None, stderr=STDOUT,
  104. )
  105. module.check_archives(
  106. verbosity=VERBOSITY_LOTS,
  107. repository='repo',
  108. consistency_config=consistency_config,
  109. )
  110. def test_check_archives_without_any_checks_bails():
  111. consistency_config = flexmock().should_receive('get').and_return(None).mock
  112. flexmock(module).should_receive('_parse_checks').and_return(())
  113. insert_subprocess_never()
  114. module.check_archives(
  115. verbosity=None,
  116. repository='repo',
  117. consistency_config=consistency_config,
  118. )
  119. def test_check_archives_with_local_path_calls_borg_via_local_path():
  120. checks = ('repository',)
  121. check_last = flexmock()
  122. consistency_config = flexmock().should_receive('get').and_return(check_last).mock
  123. flexmock(module).should_receive('_parse_checks').and_return(checks)
  124. flexmock(module).should_receive('_make_check_flags').with_args(checks, check_last).and_return(())
  125. stdout = flexmock()
  126. insert_subprocess_mock(
  127. ('borg1', 'check', 'repo'),
  128. stdout=stdout, stderr=STDOUT,
  129. )
  130. flexmock(sys.modules['builtins']).should_receive('open').and_return(stdout)
  131. flexmock(module.os).should_receive('devnull')
  132. module.check_archives(
  133. verbosity=None,
  134. repository='repo',
  135. consistency_config=consistency_config,
  136. local_path='borg1',
  137. )
  138. def test_check_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  139. checks = ('repository',)
  140. check_last = flexmock()
  141. consistency_config = flexmock().should_receive('get').and_return(check_last).mock
  142. flexmock(module).should_receive('_parse_checks').and_return(checks)
  143. flexmock(module).should_receive('_make_check_flags').with_args(checks, check_last).and_return(())
  144. stdout = flexmock()
  145. insert_subprocess_mock(
  146. ('borg', 'check', 'repo', '--remote-path', 'borg1'),
  147. stdout=stdout, stderr=STDOUT,
  148. )
  149. flexmock(sys.modules['builtins']).should_receive('open').and_return(stdout)
  150. flexmock(module.os).should_receive('devnull')
  151. module.check_archives(
  152. verbosity=None,
  153. repository='repo',
  154. consistency_config=consistency_config,
  155. remote_path='borg1',
  156. )