test_validate.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import pytest
  2. from borgmatic.config import validate as module
  3. def test_validation_error_str_contains_error_messages_and_config_filename():
  4. error = module.Validation_error('config.yaml', ('oops', 'uh oh'))
  5. result = str(error)
  6. assert 'config.yaml' in result
  7. assert 'oops' in result
  8. assert 'uh oh' in result
  9. def test_apply_logical_validation_raises_if_archive_name_format_present_without_prefix():
  10. with pytest.raises(module.Validation_error):
  11. module.apply_logical_validation(
  12. 'config.yaml',
  13. {
  14. 'storage': {'archive_name_format': '{hostname}-{now}'},
  15. 'retention': {'keep_daily': 7},
  16. },
  17. )
  18. def test_apply_logical_validation_raises_if_archive_name_format_present_without_retention_prefix():
  19. with pytest.raises(module.Validation_error):
  20. module.apply_logical_validation(
  21. 'config.yaml',
  22. {
  23. 'storage': {'archive_name_format': '{hostname}-{now}'},
  24. 'retention': {'keep_daily': 7},
  25. 'consistency': {'prefix': '{hostname}-'},
  26. },
  27. )
  28. def test_apply_locical_validation_raises_if_unknown_repository_in_check_repositories():
  29. with pytest.raises(module.Validation_error):
  30. module.apply_logical_validation(
  31. 'config.yaml',
  32. {
  33. 'location': {'repositories': ['repo.borg', 'other.borg']},
  34. 'retention': {'keep_secondly': 1000},
  35. 'consistency': {'check_repositories': ['repo.borg', 'unknown.borg']},
  36. },
  37. )
  38. def test_apply_locical_validation_does_not_raise_if_known_repository_in_check_repositories():
  39. module.apply_logical_validation(
  40. 'config.yaml',
  41. {
  42. 'location': {'repositories': ['repo.borg', 'other.borg']},
  43. 'retention': {'keep_secondly': 1000},
  44. 'consistency': {'check_repositories': ['repo.borg']},
  45. },
  46. )
  47. def test_apply_logical_validation_does_not_raise_if_archive_name_format_and_prefix_present():
  48. module.apply_logical_validation(
  49. 'config.yaml',
  50. {
  51. 'storage': {'archive_name_format': '{hostname}-{now}'},
  52. 'retention': {'prefix': '{hostname}-'},
  53. 'consistency': {'prefix': '{hostname}-'},
  54. },
  55. )
  56. def test_apply_logical_validation_does_not_raise_otherwise():
  57. module.apply_logical_validation('config.yaml', {'retention': {'keep_secondly': 1000}})
  58. def test_remove_examples_strips_examples_from_map():
  59. schema = {
  60. 'map': {
  61. 'foo': {'desc': 'thing1', 'example': 'bar'},
  62. 'baz': {'desc': 'thing2', 'example': 'quux'},
  63. }
  64. }
  65. module.remove_examples(schema)
  66. assert schema == {'map': {'foo': {'desc': 'thing1'}, 'baz': {'desc': 'thing2'}}}
  67. def test_remove_examples_strips_examples_from_sequence_of_maps():
  68. schema = {'seq': [{'map': {'foo': {'desc': 'thing', 'example': 'bar'}}, 'example': 'stuff'}]}
  69. module.remove_examples(schema)
  70. assert schema == {'seq': [{'map': {'foo': {'desc': 'thing'}}}]}
  71. def test_guard_configuration_contains_repository_does_not_raise_when_repository_in_config():
  72. module.guard_configuration_contains_repository(
  73. repository='repo', configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
  74. )
  75. def test_guard_configuration_contains_repository_does_not_raise_when_repository_not_given():
  76. module.guard_configuration_contains_repository(
  77. repository=None, configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
  78. )
  79. def test_guard_configuration_contains_repository_errors_when_repository_assumed_to_match_config_twice():
  80. with pytest.raises(ValueError):
  81. module.guard_configuration_contains_repository(
  82. repository=None,
  83. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  84. )
  85. def test_guard_configuration_contains_repository_errors_when_repository_missing_from_config():
  86. with pytest.raises(ValueError):
  87. module.guard_configuration_contains_repository(
  88. repository='nope',
  89. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  90. )
  91. def test_guard_configuration_contains_repository_errors_when_repository_matches_config_twice():
  92. with pytest.raises(ValueError):
  93. module.guard_configuration_contains_repository(
  94. repository='repo',
  95. configurations={
  96. 'config.yaml': {'location': {'repositories': ['repo', 'repo2']}},
  97. 'other.yaml': {'location': {'repositories': ['repo']}},
  98. },
  99. )