test_validate.py 9.5 KB

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