test_validate.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import io
  2. import string
  3. import sys
  4. import os
  5. from flexmock import flexmock
  6. import pytest
  7. from borgmatic.config import validate as module
  8. def test_schema_filename_returns_plausable_path():
  9. schema_path = module.schema_filename()
  10. assert schema_path.endswith('/schema.yaml')
  11. def mock_config_and_schema(config_yaml):
  12. '''
  13. Set up mocks for the config config YAML string and the default schema so that the code under
  14. test consumes them when parsing the configuration.
  15. '''
  16. config_stream = io.StringIO(config_yaml)
  17. schema_stream = open(module.schema_filename())
  18. builtins = flexmock(sys.modules['builtins'])
  19. builtins.should_receive('open').with_args('config.yaml').and_return(config_stream)
  20. builtins.should_receive('open').with_args('schema.yaml').and_return(schema_stream)
  21. def test_parse_configuration_transforms_file_into_mapping():
  22. mock_config_and_schema(
  23. '''
  24. location:
  25. source_directories:
  26. - /home
  27. - /etc
  28. repository: hostname.borg
  29. retention:
  30. keep_daily: 7
  31. consistency:
  32. checks:
  33. - repository
  34. - archives
  35. '''
  36. )
  37. result = module.parse_configuration('config.yaml', 'schema.yaml')
  38. assert result == {
  39. 'location': {'source_directories': ['/home', '/etc'], 'repository': 'hostname.borg'},
  40. 'retention': {'keep_daily': 7},
  41. 'consistency': {'checks': ['repository', 'archives']},
  42. }
  43. def test_parse_configuration_passes_through_quoted_punctuation():
  44. escaped_punctuation = string.punctuation.replace('\\', r'\\').replace('"', r'\"')
  45. mock_config_and_schema(
  46. '''
  47. location:
  48. source_directories:
  49. - /home
  50. repository: "{}.borg"
  51. '''.format(escaped_punctuation)
  52. )
  53. result = module.parse_configuration('config.yaml', 'schema.yaml')
  54. assert result == {
  55. 'location': {
  56. 'source_directories': ['/home'],
  57. 'repository': '{}.borg'.format(string.punctuation),
  58. },
  59. }
  60. def test_parse_configuration_raises_for_missing_config_file():
  61. with pytest.raises(FileNotFoundError):
  62. module.parse_configuration('config.yaml', 'schema.yaml')
  63. def test_parse_configuration_raises_for_missing_schema_file():
  64. mock_config_and_schema('')
  65. builtins = flexmock(sys.modules['builtins'])
  66. builtins.should_receive('open').with_args('schema.yaml').and_raise(FileNotFoundError)
  67. with pytest.raises(FileNotFoundError):
  68. module.parse_configuration('config.yaml', 'schema.yaml')
  69. def test_parse_configuration_raises_for_syntax_error():
  70. mock_config_and_schema('foo:\nbar')
  71. with pytest.raises(ValueError):
  72. module.parse_configuration('config.yaml', 'schema.yaml')
  73. def test_parse_configuration_raises_for_validation_error():
  74. mock_config_and_schema(
  75. '''
  76. location:
  77. source_directories: yes
  78. repository: hostname.borg
  79. '''
  80. )
  81. with pytest.raises(module.Validation_error):
  82. module.parse_configuration('config.yaml', 'schema.yaml')
  83. def test_display_validation_error_does_not_raise():
  84. flexmock(sys.modules['builtins']).should_receive('print')
  85. error = module.Validation_error('config.yaml', ('oops', 'uh oh'))
  86. module.display_validation_error(error)