2
0

test_validate.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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).format_json_error = lambda error: error.message
  41. with pytest.raises(module.Validation_error):
  42. module.apply_logical_validation(
  43. 'config.yaml',
  44. {
  45. 'location': {'repositories': ['repo.borg', 'other.borg']},
  46. 'retention': {'keep_secondly': 1000},
  47. 'consistency': {'check_repositories': ['repo.borg', 'unknown.borg']},
  48. },
  49. )
  50. def test_apply_logical_validation_does_not_raise_if_known_repository_path_in_check_repositories():
  51. module.apply_logical_validation(
  52. 'config.yaml',
  53. {
  54. 'location': {'repositories': [{'path': 'repo.borg'}, {'path': 'other.borg'}]},
  55. 'retention': {'keep_secondly': 1000},
  56. 'consistency': {'check_repositories': ['repo.borg']},
  57. },
  58. )
  59. def test_apply_logical_validation_does_not_raise_if_known_repository_label_in_check_repositories():
  60. module.apply_logical_validation(
  61. 'config.yaml',
  62. {
  63. 'location': {
  64. 'repositories': [
  65. {'path': 'repo.borg', 'label': 'my_repo'},
  66. {'path': 'other.borg', 'label': 'other_repo'},
  67. ]
  68. },
  69. 'retention': {'keep_secondly': 1000},
  70. 'consistency': {'check_repositories': ['my_repo']},
  71. },
  72. )
  73. def test_apply_logical_validation_does_not_raise_if_archive_name_format_and_prefix_present():
  74. module.apply_logical_validation(
  75. 'config.yaml',
  76. {
  77. 'storage': {'archive_name_format': '{hostname}-{now}'}, # noqa: FS003
  78. 'retention': {'prefix': '{hostname}-'}, # noqa: FS003
  79. 'consistency': {'prefix': '{hostname}-'}, # noqa: FS003
  80. },
  81. )
  82. def test_apply_logical_validation_does_not_raise_otherwise():
  83. module.apply_logical_validation('config.yaml', {'retention': {'keep_secondly': 1000}})
  84. def test_normalize_repository_path_passes_through_remote_repository():
  85. repository = 'example.org:test.borg'
  86. module.normalize_repository_path(repository) == repository
  87. def test_normalize_repository_path_passes_through_file_repository():
  88. repository = 'file:///foo/bar/test.borg'
  89. flexmock(module.os.path).should_receive('abspath').and_return('/foo/bar/test.borg')
  90. module.normalize_repository_path(repository) == '/foo/bar/test.borg'
  91. def test_normalize_repository_path_passes_through_absolute_repository():
  92. repository = '/foo/bar/test.borg'
  93. flexmock(module.os.path).should_receive('abspath').and_return(repository)
  94. module.normalize_repository_path(repository) == 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').and_return(absolute)
  99. module.normalize_repository_path(repository) == absolute
  100. def test_repositories_match_does_not_raise():
  101. flexmock(module).should_receive('normalize_repository_path')
  102. module.repositories_match('foo', 'bar')
  103. def test_guard_configuration_contains_repository_does_not_raise_when_repository_in_config():
  104. flexmock(module).should_receive('repositories_match').replace_with(
  105. lambda first, second: first == second
  106. )
  107. module.guard_configuration_contains_repository(
  108. repository='repo', configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
  109. )
  110. def test_guard_configuration_contains_repository_does_not_raise_when_repository_label_in_config():
  111. module.guard_configuration_contains_repository(
  112. repository='repo',
  113. configurations={
  114. 'config.yaml': {'location': {'repositories': [{'path': 'foo/bar', 'label': 'repo'}]}}
  115. },
  116. )
  117. def test_guard_configuration_contains_repository_does_not_raise_when_repository_not_given():
  118. module.guard_configuration_contains_repository(
  119. repository=None, configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
  120. )
  121. def test_guard_configuration_contains_repository_errors_when_repository_missing_from_config():
  122. flexmock(module).should_receive('repositories_match').replace_with(
  123. lambda first, second: first == second
  124. )
  125. with pytest.raises(ValueError):
  126. module.guard_configuration_contains_repository(
  127. repository='nope',
  128. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  129. )
  130. def test_guard_configuration_contains_repository_errors_when_repository_matches_config_twice():
  131. flexmock(module).should_receive('repositories_match').replace_with(
  132. lambda first, second: first == second
  133. )
  134. with pytest.raises(ValueError):
  135. module.guard_configuration_contains_repository(
  136. repository='repo',
  137. configurations={
  138. 'config.yaml': {'location': {'repositories': ['repo', 'repo2']}},
  139. 'other.yaml': {'location': {'repositories': ['repo']}},
  140. },
  141. )
  142. def test_guard_single_repository_selected_raises_when_multiple_repositories_configured_and_none_selected():
  143. with pytest.raises(ValueError):
  144. module.guard_single_repository_selected(
  145. repository=None,
  146. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  147. )
  148. def test_guard_single_repository_selected_does_not_raise_when_single_repository_configured_and_none_selected():
  149. module.guard_single_repository_selected(
  150. repository=None,
  151. configurations={'config.yaml': {'location': {'repositories': ['repo']}}},
  152. )
  153. def test_guard_single_repository_selected_does_not_raise_when_no_repositories_configured_and_one_selected():
  154. module.guard_single_repository_selected(
  155. repository='repo',
  156. configurations={'config.yaml': {'location': {'repositories': []}}},
  157. )
  158. def test_guard_single_repository_selected_does_not_raise_when_repositories_configured_and_one_selected():
  159. module.guard_single_repository_selected(
  160. repository='repo',
  161. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  162. )