test_validate.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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: '.{}'.format(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_logical_validation_raises_if_archive_name_format_present_without_prefix():
  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. 'storage': {'archive_name_format': '{hostname}-{now}'},
  30. 'retention': {'keep_daily': 7},
  31. },
  32. )
  33. def test_apply_logical_validation_raises_if_archive_name_format_present_without_retention_prefix():
  34. flexmock(module).format_json_error = lambda error: error.message
  35. with pytest.raises(module.Validation_error):
  36. module.apply_logical_validation(
  37. 'config.yaml',
  38. {
  39. 'storage': {'archive_name_format': '{hostname}-{now}'},
  40. 'retention': {'keep_daily': 7},
  41. 'consistency': {'prefix': '{hostname}-'},
  42. },
  43. )
  44. def test_apply_locical_validation_raises_if_unknown_repository_in_check_repositories():
  45. flexmock(module).format_json_error = lambda error: error.message
  46. with pytest.raises(module.Validation_error):
  47. module.apply_logical_validation(
  48. 'config.yaml',
  49. {
  50. 'location': {'repositories': ['repo.borg', 'other.borg']},
  51. 'retention': {'keep_secondly': 1000},
  52. 'consistency': {'check_repositories': ['repo.borg', 'unknown.borg']},
  53. },
  54. )
  55. def test_apply_locical_validation_does_not_raise_if_known_repository_in_check_repositories():
  56. module.apply_logical_validation(
  57. 'config.yaml',
  58. {
  59. 'location': {'repositories': ['repo.borg', 'other.borg']},
  60. 'retention': {'keep_secondly': 1000},
  61. 'consistency': {'check_repositories': ['repo.borg']},
  62. },
  63. )
  64. def test_apply_logical_validation_does_not_raise_if_archive_name_format_and_prefix_present():
  65. module.apply_logical_validation(
  66. 'config.yaml',
  67. {
  68. 'storage': {'archive_name_format': '{hostname}-{now}'},
  69. 'retention': {'prefix': '{hostname}-'},
  70. 'consistency': {'prefix': '{hostname}-'},
  71. },
  72. )
  73. def test_apply_logical_validation_does_not_raise_otherwise():
  74. module.apply_logical_validation('config.yaml', {'retention': {'keep_secondly': 1000}})
  75. def test_normalize_repository_path_passes_through_remote_repository():
  76. repository = 'example.org:test.borg'
  77. module.normalize_repository_path(repository) == repository
  78. def test_normalize_repository_path_passes_through_absolute_repository():
  79. repository = '/foo/bar/test.borg'
  80. flexmock(module.os.path).should_receive('abspath').and_return(repository)
  81. module.normalize_repository_path(repository) == repository
  82. def test_normalize_repository_path_resolves_relative_repository():
  83. repository = 'test.borg'
  84. absolute = '/foo/bar/test.borg'
  85. flexmock(module.os.path).should_receive('abspath').and_return(absolute)
  86. module.normalize_repository_path(repository) == absolute
  87. def test_repositories_match_does_not_raise():
  88. flexmock(module).should_receive('normalize_repository_path')
  89. module.repositories_match('foo', 'bar')
  90. def test_guard_configuration_contains_repository_does_not_raise_when_repository_in_config():
  91. flexmock(module).should_receive('repositories_match').replace_with(
  92. lambda first, second: first == second
  93. )
  94. module.guard_configuration_contains_repository(
  95. repository='repo', configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
  96. )
  97. def test_guard_configuration_contains_repository_does_not_raise_when_repository_not_given():
  98. module.guard_configuration_contains_repository(
  99. repository=None, configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
  100. )
  101. def test_guard_configuration_contains_repository_errors_when_repository_assumed_to_match_config_twice():
  102. with pytest.raises(ValueError):
  103. module.guard_configuration_contains_repository(
  104. repository=None,
  105. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  106. )
  107. def test_guard_configuration_contains_repository_errors_when_repository_missing_from_config():
  108. flexmock(module).should_receive('repositories_match').replace_with(
  109. lambda first, second: first == second
  110. )
  111. with pytest.raises(ValueError):
  112. module.guard_configuration_contains_repository(
  113. repository='nope',
  114. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  115. )
  116. def test_guard_configuration_contains_repository_errors_when_repository_matches_config_twice():
  117. flexmock(module).should_receive('repositories_match').replace_with(
  118. lambda first, second: first == second
  119. )
  120. with pytest.raises(ValueError):
  121. module.guard_configuration_contains_repository(
  122. repository='repo',
  123. configurations={
  124. 'config.yaml': {'location': {'repositories': ['repo', 'repo2']}},
  125. 'other.yaml': {'location': {'repositories': ['repo']}},
  126. },
  127. )