test_validate.py 8.1 KB

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