test_validate.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_guard_configuration_contains_repository_does_not_raise_when_repository_in_config():
  59. module.guard_configuration_contains_repository(
  60. repository='repo', configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
  61. )
  62. def test_guard_configuration_contains_repository_does_not_raise_when_repository_not_given():
  63. module.guard_configuration_contains_repository(
  64. repository=None, configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
  65. )
  66. def test_guard_configuration_contains_repository_errors_when_repository_assumed_to_match_config_twice():
  67. with pytest.raises(ValueError):
  68. module.guard_configuration_contains_repository(
  69. repository=None,
  70. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  71. )
  72. def test_guard_configuration_contains_repository_errors_when_repository_missing_from_config():
  73. with pytest.raises(ValueError):
  74. module.guard_configuration_contains_repository(
  75. repository='nope',
  76. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  77. )
  78. def test_guard_configuration_contains_repository_errors_when_repository_matches_config_twice():
  79. with pytest.raises(ValueError):
  80. module.guard_configuration_contains_repository(
  81. repository='repo',
  82. configurations={
  83. 'config.yaml': {'location': {'repositories': ['repo', 'repo2']}},
  84. 'other.yaml': {'location': {'repositories': ['repo']}},
  85. },
  86. )