test_validate.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.config import validate as module
  4. def test_validation_error_str_contains_error_messages_and_config_filename():
  5. error = module.Validation_error('config.yaml', ('oops', 'uh oh'))
  6. result = str(error)
  7. assert 'config.yaml' in result
  8. assert 'oops' in result
  9. assert 'uh oh' in result
  10. def test_apply_logical_validation_raises_if_archive_name_format_present_without_prefix():
  11. with pytest.raises(module.Validation_error):
  12. module.apply_logical_validation(
  13. 'config.yaml',
  14. {
  15. 'storage': {'archive_name_format': '{hostname}-{now}'},
  16. 'retention': {'keep_daily': 7},
  17. },
  18. )
  19. def test_apply_logical_validation_raises_if_archive_name_format_present_without_retention_prefix():
  20. with pytest.raises(module.Validation_error):
  21. module.apply_logical_validation(
  22. 'config.yaml',
  23. {
  24. 'storage': {'archive_name_format': '{hostname}-{now}'},
  25. 'retention': {'keep_daily': 7},
  26. 'consistency': {'prefix': '{hostname}-'},
  27. },
  28. )
  29. def test_apply_logical_validation_warns_if_archive_name_format_present_without_consistency_prefix():
  30. logger = flexmock(module.logger)
  31. logger.should_receive('warning').once()
  32. module.apply_logical_validation(
  33. 'config.yaml',
  34. {
  35. 'storage': {'archive_name_format': '{hostname}-{now}'},
  36. 'retention': {'prefix': '{hostname}-'},
  37. 'consistency': {},
  38. },
  39. )
  40. def test_apply_locical_validation_raises_if_unknown_repository_in_check_repositories():
  41. with pytest.raises(module.Validation_error):
  42. module.apply_logical_validation(
  43. 'config.yaml',
  44. {
  45. 'location': {'repositories': ['repo.borg', 'other.borg']},
  46. 'retention': {'keep_secondly': 1000},
  47. 'consistency': {'check_repositories': ['repo.borg', 'unknown.borg']},
  48. },
  49. )
  50. def test_apply_locical_validation_does_not_raise_if_known_repository_in_check_repositories():
  51. module.apply_logical_validation(
  52. 'config.yaml',
  53. {
  54. 'location': {'repositories': ['repo.borg', 'other.borg']},
  55. 'retention': {'keep_secondly': 1000},
  56. 'consistency': {'check_repositories': ['repo.borg']},
  57. },
  58. )
  59. def test_apply_logical_validation_does_not_raise_or_warn_if_archive_name_format_and_prefix_present():
  60. logger = flexmock(module.logger)
  61. logger.should_receive('warning').never()
  62. module.apply_logical_validation(
  63. 'config.yaml',
  64. {
  65. 'storage': {'archive_name_format': '{hostname}-{now}'},
  66. 'retention': {'prefix': '{hostname}-'},
  67. 'consistency': {'prefix': '{hostname}-'},
  68. },
  69. )
  70. def test_apply_logical_validation_does_not_raise_otherwise():
  71. module.apply_logical_validation('config.yaml', {'retention': {'keep_secondly': 1000}})