test_validate.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import os
  2. import sys
  3. from io import StringIO
  4. import pytest
  5. from flexmock import flexmock
  6. from borgmatic.config import validate as module
  7. def test_schema_filename_finds_schema_path():
  8. schema_path = '/var/borgmatic/config/schema.yaml'
  9. flexmock(os.path).should_receive('dirname').and_return('/var/borgmatic/config')
  10. builtins = flexmock(sys.modules['builtins'])
  11. builtins.should_receive('open').with_args(schema_path).and_return(StringIO())
  12. assert module.schema_filename() == schema_path
  13. def test_schema_filename_raises_filenotfounderror():
  14. schema_path = '/var/borgmatic/config/schema.yaml'
  15. flexmock(os.path).should_receive('dirname').and_return('/var/borgmatic/config')
  16. builtins = flexmock(sys.modules['builtins'])
  17. builtins.should_receive('open').with_args(schema_path).and_raise(FileNotFoundError)
  18. with pytest.raises(FileNotFoundError):
  19. module.schema_filename()
  20. def test_format_json_error_path_element_formats_array_index():
  21. module.format_json_error_path_element(3) == '[3]'
  22. def test_format_json_error_path_element_formats_property():
  23. module.format_json_error_path_element('foo') == '.foo'
  24. def test_format_json_error_formats_error_including_path():
  25. flexmock(module).format_json_error_path_element = lambda element: f'.{element}'
  26. error = flexmock(message='oops', path=['foo', 'bar'])
  27. assert module.format_json_error(error) == "At 'foo.bar': oops"
  28. def test_format_json_error_formats_error_without_path():
  29. flexmock(module).should_receive('format_json_error_path_element').never()
  30. error = flexmock(message='oops', path=[])
  31. assert module.format_json_error(error) == 'At the top level: oops'
  32. def test_validation_error_string_contains_errors():
  33. flexmock(module).format_json_error = lambda error: error.message
  34. error = module.Validation_error('config.yaml', ('oops', 'uh oh'))
  35. result = str(error)
  36. assert 'config.yaml' in result
  37. assert 'oops' in result
  38. assert 'uh oh' in result
  39. def test_apply_logical_validation_raises_if_unknown_repository_in_check_repositories():
  40. flexmock(module).should_receive('repositories_match').and_return(False)
  41. with pytest.raises(module.Validation_error):
  42. module.apply_logical_validation(
  43. 'config.yaml',
  44. {
  45. 'repositories': ['repo.borg', 'other.borg'],
  46. 'keep_secondly': 1000,
  47. 'check_repositories': ['repo.borg', 'unknown.borg'],
  48. },
  49. )
  50. def test_apply_logical_validation_does_not_raise_if_known_repository_in_check_repositories():
  51. flexmock(module).should_receive('repositories_match').and_return(True)
  52. module.apply_logical_validation(
  53. 'config.yaml',
  54. {
  55. 'repositories': [{'path': 'repo.borg'}, {'path': 'other.borg'}],
  56. 'keep_secondly': 1000,
  57. 'check_repositories': ['repo.borg'],
  58. },
  59. )
  60. def test_normalize_repository_path_passes_through_remote_repository():
  61. repository = 'example.org:test.borg'
  62. module.normalize_repository_path(repository) == repository
  63. def test_normalize_repository_path_passes_through_remote_repository_with_base_dir():
  64. repository = 'example.org:test.borg'
  65. flexmock(module.os.path).should_receive('abspath').never()
  66. module.normalize_repository_path(repository, '/working') == repository
  67. def test_normalize_repository_path_passes_through_file_repository():
  68. repository = 'file:///foo/bar/test.borg'
  69. flexmock(module.os.path).should_receive('abspath').with_args('/foo/bar/test.borg').and_return(
  70. '/foo/bar/test.borg'
  71. )
  72. module.normalize_repository_path(repository) == '/foo/bar/test.borg'
  73. def test_normalize_repository_path_passes_through_absolute_file_repository_with_base_dir():
  74. repository = 'file:///foo/bar/test.borg'
  75. flexmock(module.os.path).should_receive('abspath').with_args('/foo/bar/test.borg').and_return(
  76. '/foo/bar/test.borg'
  77. )
  78. module.normalize_repository_path(repository, '/working') == '/foo/bar/test.borg'
  79. def test_normalize_repository_path_resolves_relative_file_repository_with_base_dir():
  80. repository = 'file://foo/bar/test.borg'
  81. flexmock(module.os.path).should_receive('abspath').with_args(
  82. '/working/foo/bar/test.borg'
  83. ).and_return('/working/foo/bar/test.borg')
  84. module.normalize_repository_path(repository, '/working') == '/working/foo/bar/test.borg'
  85. def test_normalize_repository_path_passes_through_absolute_repository():
  86. repository = '/foo/bar/test.borg'
  87. flexmock(module.os.path).should_receive('abspath').and_return(repository)
  88. module.normalize_repository_path(repository) == repository
  89. def test_normalize_repository_path_passes_through_absolute_repository_with_base_dir():
  90. repository = '/foo/bar/test.borg'
  91. flexmock(module.os.path).should_receive('abspath').and_return(repository)
  92. module.normalize_repository_path(repository, '/working') == repository
  93. def test_normalize_repository_path_resolves_relative_repository():
  94. repository = 'test.borg'
  95. absolute = '/foo/bar/test.borg'
  96. flexmock(module.os.path).should_receive('abspath').with_args(repository).and_return(absolute)
  97. module.normalize_repository_path(repository) == absolute
  98. def test_normalize_repository_path_resolves_relative_repository_with_base_dir():
  99. repository = 'test.borg'
  100. base = '/working'
  101. absolute = '/working/test.borg'
  102. flexmock(module.os.path).should_receive('abspath').with_args('/working/test.borg').and_return(
  103. absolute
  104. )
  105. module.normalize_repository_path(repository, base) == absolute
  106. @pytest.mark.parametrize(
  107. 'first,second,expected_result',
  108. (
  109. (None, None, False),
  110. ('foo', None, False),
  111. (None, 'bar', False),
  112. ('foo', 'foo', True),
  113. ('foo', 'bar', False),
  114. ('foo*', 'foof', True),
  115. ('barf', 'bar*', True),
  116. ('foo*', 'bar*', False),
  117. ),
  118. )
  119. def test_glob_match_matches_globs(first, second, expected_result):
  120. assert module.glob_match(first=first, second=second) is expected_result
  121. def test_repositories_match_matches_on_path():
  122. flexmock(module).should_receive('normalize_repository_path')
  123. flexmock(module).should_receive('glob_match').replace_with(
  124. lambda first, second: first == second
  125. )
  126. module.repositories_match(
  127. {'path': 'foo', 'label': 'my repo'}, {'path': 'foo', 'label': 'other repo'}
  128. ) is True
  129. def test_repositories_match_matches_on_label():
  130. flexmock(module).should_receive('normalize_repository_path')
  131. flexmock(module).should_receive('glob_match').replace_with(
  132. lambda first, second: first == second
  133. )
  134. module.repositories_match(
  135. {'path': 'foo', 'label': 'my repo'}, {'path': 'bar', 'label': 'my repo'}
  136. ) is True
  137. def test_repositories_match_with_different_paths_and_labels_does_not_match():
  138. flexmock(module).should_receive('normalize_repository_path')
  139. flexmock(module).should_receive('glob_match').replace_with(
  140. lambda first, second: first == second
  141. )
  142. module.repositories_match(
  143. {'path': 'foo', 'label': 'my repo'}, {'path': 'bar', 'label': 'other repo'}
  144. ) is False
  145. def test_repositories_match_matches_on_string_repository():
  146. flexmock(module).should_receive('normalize_repository_path')
  147. flexmock(module).should_receive('glob_match').replace_with(
  148. lambda first, second: first == second
  149. )
  150. module.repositories_match('foo', 'foo') is True
  151. def test_repositories_match_with_different_string_repositories_does_not_match():
  152. flexmock(module).should_receive('normalize_repository_path')
  153. flexmock(module).should_receive('glob_match').replace_with(
  154. lambda first, second: first == second
  155. )
  156. module.repositories_match('foo', 'bar') is False
  157. def test_repositories_match_supports_mixed_repositories():
  158. flexmock(module).should_receive('normalize_repository_path')
  159. flexmock(module).should_receive('glob_match').replace_with(
  160. lambda first, second: first == second
  161. )
  162. module.repositories_match({'path': 'foo', 'label': 'my foo'}, 'bar') is False
  163. def test_guard_configuration_contains_repository_does_not_raise_when_repository_matches():
  164. flexmock(module).should_receive('repositories_match').and_return(True)
  165. module.guard_configuration_contains_repository(
  166. repository='repo',
  167. configurations={'config.yaml': {'repositories': [{'path': 'foo/bar', 'label': 'repo'}]}},
  168. )
  169. def test_guard_configuration_contains_repository_does_not_raise_when_repository_is_none():
  170. flexmock(module).should_receive('repositories_match').never()
  171. module.guard_configuration_contains_repository(
  172. repository=None,
  173. configurations={'config.yaml': {'repositories': [{'path': 'foo/bar', 'label': 'repo'}]}},
  174. )
  175. def test_guard_configuration_contains_repository_errors_when_repository_does_not_match():
  176. flexmock(module).should_receive('repositories_match').and_return(False)
  177. with pytest.raises(ValueError):
  178. module.guard_configuration_contains_repository(
  179. repository='nope',
  180. configurations={'config.yaml': {'repositories': ['repo', 'repo2']}},
  181. )