test_validate.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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_logical_validation_does_not_raise_or_warn_if_archive_name_format_and_prefix_present():
  41. logger = flexmock(module.logger)
  42. logger.should_receive('warning').never()
  43. module.apply_logical_validation(
  44. 'config.yaml',
  45. {
  46. 'storage': {'archive_name_format': '{hostname}-{now}'},
  47. 'retention': {'prefix': '{hostname}-'},
  48. 'consistency': {'prefix': '{hostname}-'},
  49. },
  50. )
  51. def test_apply_logical_validation_does_not_raise_otherwise():
  52. module.apply_logical_validation('config.yaml', {'retention': {'keep_secondly': 1000}})