2
0

test_validate.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.config import validate as module
  4. def test_schema_filename_finds_schema_path():
  5. schema_path = '/var/borgmatic/config/schema.yaml'
  6. flexmock(module.importlib_metadata).should_receive('files').and_return(
  7. flexmock(match=lambda path: False, locate=lambda: None),
  8. flexmock(match=lambda path: True, locate=lambda: schema_path),
  9. flexmock(match=lambda path: False, locate=lambda: None),
  10. )
  11. assert module.schema_filename() == schema_path
  12. def test_schema_filename_with_missing_schema_path_raises():
  13. flexmock(module.importlib_metadata).should_receive('files').and_return(
  14. flexmock(match=lambda path: False, locate=lambda: None),
  15. flexmock(match=lambda path: False, locate=lambda: None),
  16. )
  17. with pytest.raises(FileNotFoundError):
  18. assert module.schema_filename()
  19. def test_format_json_error_path_element_formats_array_index():
  20. module.format_json_error_path_element(3) == '[3]'
  21. def test_format_json_error_path_element_formats_property():
  22. module.format_json_error_path_element('foo') == '.foo'
  23. def test_format_json_error_formats_error_including_path():
  24. flexmock(module).format_json_error_path_element = lambda element: f'.{element}'
  25. error = flexmock(message='oops', path=['foo', 'bar'])
  26. assert module.format_json_error(error) == "At 'foo.bar': oops"
  27. def test_format_json_error_formats_error_without_path():
  28. flexmock(module).should_receive('format_json_error_path_element').never()
  29. error = flexmock(message='oops', path=[])
  30. assert module.format_json_error(error) == 'At the top level: oops'
  31. def test_validation_error_string_contains_errors():
  32. flexmock(module).format_json_error = lambda error: error.message
  33. error = module.Validation_error('config.yaml', ('oops', 'uh oh'))
  34. result = str(error)
  35. assert 'config.yaml' in result
  36. assert 'oops' in result
  37. assert 'uh oh' in result
  38. def test_apply_logical_validation_raises_if_unknown_repository_in_check_repositories():
  39. flexmock(module).format_json_error = lambda error: error.message
  40. with pytest.raises(module.Validation_error):
  41. module.apply_logical_validation(
  42. 'config.yaml',
  43. {
  44. 'location': {'repositories': ['repo.borg', 'other.borg']},
  45. 'retention': {'keep_secondly': 1000},
  46. 'consistency': {'check_repositories': ['repo.borg', 'unknown.borg']},
  47. },
  48. )
  49. def test_apply_logical_validation_does_not_raise_if_known_repository_path_in_check_repositories():
  50. module.apply_logical_validation(
  51. 'config.yaml',
  52. {
  53. 'location': {'repositories': [{'path': 'repo.borg'}, {'path': 'other.borg'}]},
  54. 'retention': {'keep_secondly': 1000},
  55. 'consistency': {'check_repositories': ['repo.borg']},
  56. },
  57. )
  58. def test_apply_logical_validation_does_not_raise_if_known_repository_label_in_check_repositories():
  59. module.apply_logical_validation(
  60. 'config.yaml',
  61. {
  62. 'location': {
  63. 'repositories': [
  64. {'path': 'repo.borg', 'label': 'my_repo'},
  65. {'path': 'other.borg', 'label': 'other_repo'},
  66. ]
  67. },
  68. 'retention': {'keep_secondly': 1000},
  69. 'consistency': {'check_repositories': ['my_repo']},
  70. },
  71. )
  72. def test_apply_logical_validation_does_not_raise_if_archive_name_format_and_prefix_present():
  73. module.apply_logical_validation(
  74. 'config.yaml',
  75. {
  76. 'storage': {'archive_name_format': '{hostname}-{now}'}, # noqa: FS003
  77. 'retention': {'prefix': '{hostname}-'}, # noqa: FS003
  78. 'consistency': {'prefix': '{hostname}-'}, # noqa: FS003
  79. },
  80. )
  81. def test_apply_logical_validation_does_not_raise_otherwise():
  82. module.apply_logical_validation('config.yaml', {'retention': {'keep_secondly': 1000}})
  83. def test_normalize_repository_path_passes_through_remote_repository():
  84. repository = 'example.org:test.borg'
  85. module.normalize_repository_path(repository) == repository
  86. def test_normalize_repository_path_passes_through_file_repository():
  87. repository = 'file:///foo/bar/test.borg'
  88. flexmock(module.os.path).should_receive('abspath').and_return('/foo/bar/test.borg')
  89. module.normalize_repository_path(repository) == '/foo/bar/test.borg'
  90. def test_normalize_repository_path_passes_through_absolute_repository():
  91. repository = '/foo/bar/test.borg'
  92. flexmock(module.os.path).should_receive('abspath').and_return(repository)
  93. module.normalize_repository_path(repository) == repository
  94. def test_normalize_repository_path_resolves_relative_repository():
  95. repository = 'test.borg'
  96. absolute = '/foo/bar/test.borg'
  97. flexmock(module.os.path).should_receive('abspath').and_return(absolute)
  98. module.normalize_repository_path(repository) == absolute
  99. def test_repositories_match_does_not_raise():
  100. flexmock(module).should_receive('normalize_repository_path')
  101. module.repositories_match('foo', 'bar')
  102. def test_guard_configuration_contains_repository_does_not_raise_when_repository_in_config():
  103. flexmock(module).should_receive('repositories_match').replace_with(
  104. lambda first, second: first == second
  105. )
  106. module.guard_configuration_contains_repository(
  107. repository='repo', configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
  108. )
  109. def test_guard_configuration_contains_repository_does_not_raise_when_repository_label_in_config():
  110. module.guard_configuration_contains_repository(
  111. repository='repo',
  112. configurations={
  113. 'config.yaml': {'location': {'repositories': [{'path': 'foo/bar', 'label': 'repo'}]}}
  114. },
  115. )
  116. def test_guard_configuration_contains_repository_does_not_raise_when_repository_not_given():
  117. module.guard_configuration_contains_repository(
  118. repository=None, configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
  119. )
  120. def test_guard_configuration_contains_repository_errors_when_repository_missing_from_config():
  121. flexmock(module).should_receive('repositories_match').replace_with(
  122. lambda first, second: first == second
  123. )
  124. with pytest.raises(ValueError):
  125. module.guard_configuration_contains_repository(
  126. repository='nope',
  127. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  128. )
  129. def test_guard_configuration_contains_repository_errors_when_repository_matches_config_twice():
  130. flexmock(module).should_receive('repositories_match').replace_with(
  131. lambda first, second: first == second
  132. )
  133. with pytest.raises(ValueError):
  134. module.guard_configuration_contains_repository(
  135. repository='repo',
  136. configurations={
  137. 'config.yaml': {'location': {'repositories': ['repo', 'repo2']}},
  138. 'other.yaml': {'location': {'repositories': ['repo']}},
  139. },
  140. )
  141. def test_guard_single_repository_selected_raises_when_multiple_repositories_configured_and_none_selected():
  142. with pytest.raises(ValueError):
  143. module.guard_single_repository_selected(
  144. repository=None,
  145. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  146. )
  147. def test_guard_single_repository_selected_does_not_raise_when_single_repository_configured_and_none_selected():
  148. module.guard_single_repository_selected(
  149. repository=None,
  150. configurations={'config.yaml': {'location': {'repositories': ['repo']}}},
  151. )
  152. def test_guard_single_repository_selected_does_not_raise_when_no_repositories_configured_and_one_selected():
  153. module.guard_single_repository_selected(
  154. repository='repo',
  155. configurations={'config.yaml': {'location': {'repositories': []}}},
  156. )
  157. def test_guard_single_repository_selected_does_not_raise_when_repositories_configured_and_one_selected():
  158. module.guard_single_repository_selected(
  159. repository='repo',
  160. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  161. )