test_validate.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.config import validate as module
  4. def test_format_json_error_path_element_formats_array_index():
  5. module.format_json_error_path_element(3) == '[3]'
  6. def test_format_json_error_path_element_formats_property():
  7. module.format_json_error_path_element('foo') == '.foo'
  8. def test_format_json_error_formats_error_including_path():
  9. flexmock(module).format_json_error_path_element = lambda element: f'.{element}'
  10. error = flexmock(message='oops', path=['foo', 'bar'])
  11. assert module.format_json_error(error) == "At 'foo.bar': oops"
  12. def test_format_json_error_formats_error_without_path():
  13. flexmock(module).should_receive('format_json_error_path_element').never()
  14. error = flexmock(message='oops', path=[])
  15. assert module.format_json_error(error) == 'At the top level: oops'
  16. def test_validation_error_string_contains_errors():
  17. flexmock(module).format_json_error = lambda error: error.message
  18. error = module.Validation_error('config.yaml', ('oops', 'uh oh'))
  19. result = str(error)
  20. assert 'config.yaml' in result
  21. assert 'oops' in result
  22. assert 'uh oh' in result
  23. def test_apply_locical_validation_raises_if_unknown_repository_in_check_repositories():
  24. flexmock(module).format_json_error = lambda error: error.message
  25. with pytest.raises(module.Validation_error):
  26. module.apply_logical_validation(
  27. 'config.yaml',
  28. {
  29. 'location': {'repositories': ['repo.borg', 'other.borg']},
  30. 'retention': {'keep_secondly': 1000},
  31. 'consistency': {'check_repositories': ['repo.borg', 'unknown.borg']},
  32. },
  33. )
  34. def test_apply_locical_validation_does_not_raise_if_known_repository_in_check_repositories():
  35. module.apply_logical_validation(
  36. 'config.yaml',
  37. {
  38. 'location': {'repositories': ['repo.borg', 'other.borg']},
  39. 'retention': {'keep_secondly': 1000},
  40. 'consistency': {'check_repositories': ['repo.borg']},
  41. },
  42. )
  43. def test_apply_logical_validation_does_not_raise_if_archive_name_format_and_prefix_present():
  44. module.apply_logical_validation(
  45. 'config.yaml',
  46. {
  47. 'storage': {'archive_name_format': '{hostname}-{now}'}, # noqa: FS003
  48. 'retention': {'prefix': '{hostname}-'}, # noqa: FS003
  49. 'consistency': {'prefix': '{hostname}-'}, # noqa: FS003
  50. },
  51. )
  52. def test_apply_logical_validation_does_not_raise_otherwise():
  53. module.apply_logical_validation('config.yaml', {'retention': {'keep_secondly': 1000}})
  54. def test_normalize_repository_path_passes_through_remote_repository():
  55. repository = 'example.org:test.borg'
  56. module.normalize_repository_path(repository) == repository
  57. def test_normalize_repository_path_passes_through_file_repository():
  58. repository = 'file:///foo/bar/test.borg'
  59. flexmock(module.os.path).should_receive('abspath').and_return('/foo/bar/test.borg')
  60. module.normalize_repository_path(repository) == '/foo/bar/test.borg'
  61. def test_normalize_repository_path_passes_through_absolute_repository():
  62. repository = '/foo/bar/test.borg'
  63. flexmock(module.os.path).should_receive('abspath').and_return(repository)
  64. module.normalize_repository_path(repository) == repository
  65. def test_normalize_repository_path_resolves_relative_repository():
  66. repository = 'test.borg'
  67. absolute = '/foo/bar/test.borg'
  68. flexmock(module.os.path).should_receive('abspath').and_return(absolute)
  69. module.normalize_repository_path(repository) == absolute
  70. def test_repositories_match_does_not_raise():
  71. flexmock(module).should_receive('normalize_repository_path')
  72. module.repositories_match('foo', 'bar')
  73. def test_guard_configuration_contains_repository_does_not_raise_when_repository_in_config():
  74. flexmock(module).should_receive('repositories_match').replace_with(
  75. lambda first, second: first == second
  76. )
  77. module.guard_configuration_contains_repository(
  78. repository='repo', configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
  79. )
  80. def test_guard_configuration_contains_repository_does_not_raise_when_repository_not_given():
  81. module.guard_configuration_contains_repository(
  82. repository=None, configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
  83. )
  84. def test_guard_configuration_contains_repository_errors_when_repository_missing_from_config():
  85. flexmock(module).should_receive('repositories_match').replace_with(
  86. lambda first, second: first == second
  87. )
  88. with pytest.raises(ValueError):
  89. module.guard_configuration_contains_repository(
  90. repository='nope',
  91. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  92. )
  93. def test_guard_configuration_contains_repository_errors_when_repository_matches_config_twice():
  94. flexmock(module).should_receive('repositories_match').replace_with(
  95. lambda first, second: first == second
  96. )
  97. with pytest.raises(ValueError):
  98. module.guard_configuration_contains_repository(
  99. repository='repo',
  100. configurations={
  101. 'config.yaml': {'location': {'repositories': ['repo', 'repo2']}},
  102. 'other.yaml': {'location': {'repositories': ['repo']}},
  103. },
  104. )
  105. def test_guard_single_repository_selected_raises_when_multiple_repositories_configured_and_none_selected():
  106. with pytest.raises(ValueError):
  107. module.guard_single_repository_selected(
  108. repository=None,
  109. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  110. )
  111. def test_guard_single_repository_selected_does_not_raise_when_single_repository_configured_and_none_selected():
  112. module.guard_single_repository_selected(
  113. repository=None, configurations={'config.yaml': {'location': {'repositories': ['repo']}}},
  114. )
  115. def test_guard_single_repository_selected_does_not_raise_when_no_repositories_configured_and_one_selected():
  116. module.guard_single_repository_selected(
  117. repository='repo', configurations={'config.yaml': {'location': {'repositories': []}}},
  118. )
  119. def test_guard_single_repository_selected_does_not_raise_when_repositories_configured_and_one_selected():
  120. module.guard_single_repository_selected(
  121. repository='repo',
  122. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  123. )