2017-07-05 01:32:37 +00:00
|
|
|
import io
|
|
|
|
import string
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
|
|
|
from flexmock import flexmock
|
|
|
|
import pytest
|
|
|
|
|
2017-07-09 05:33:51 +00:00
|
|
|
from borgmatic.config import validate as module
|
2017-07-05 01:32:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_schema_filename_returns_plausable_path():
|
|
|
|
schema_path = module.schema_filename()
|
|
|
|
|
|
|
|
assert schema_path.endswith('/schema.yaml')
|
|
|
|
|
|
|
|
|
|
|
|
def mock_config_and_schema(config_yaml):
|
|
|
|
'''
|
2017-07-09 06:01:41 +00:00
|
|
|
Set up mocks for the config config YAML string and the default schema so that the code under
|
|
|
|
test consumes them when parsing the configuration.
|
2017-07-05 01:32:37 +00:00
|
|
|
'''
|
2017-07-09 05:33:51 +00:00
|
|
|
config_stream = io.StringIO(config_yaml)
|
2017-07-09 06:01:41 +00:00
|
|
|
schema_stream = open(module.schema_filename())
|
|
|
|
builtins = flexmock(sys.modules['builtins'])
|
|
|
|
builtins.should_receive('open').with_args('config.yaml').and_return(config_stream)
|
|
|
|
builtins.should_receive('open').with_args('schema.yaml').and_return(schema_stream)
|
2017-07-05 01:32:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_parse_configuration_transforms_file_into_mapping():
|
|
|
|
mock_config_and_schema(
|
|
|
|
'''
|
|
|
|
location:
|
|
|
|
source_directories:
|
|
|
|
- /home
|
|
|
|
- /etc
|
|
|
|
|
2017-07-23 05:56:46 +00:00
|
|
|
repositories:
|
|
|
|
- hostname.borg
|
2017-07-05 01:32:37 +00:00
|
|
|
|
|
|
|
retention:
|
|
|
|
keep_daily: 7
|
|
|
|
|
|
|
|
consistency:
|
|
|
|
checks:
|
|
|
|
- repository
|
|
|
|
- archives
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
|
|
|
|
result = module.parse_configuration('config.yaml', 'schema.yaml')
|
|
|
|
|
|
|
|
assert result == {
|
2017-07-23 05:56:46 +00:00
|
|
|
'location': {'source_directories': ['/home', '/etc'], 'repositories': ['hostname.borg']},
|
2017-07-05 01:32:37 +00:00
|
|
|
'retention': {'keep_daily': 7},
|
|
|
|
'consistency': {'checks': ['repository', 'archives']},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_configuration_passes_through_quoted_punctuation():
|
|
|
|
escaped_punctuation = string.punctuation.replace('\\', r'\\').replace('"', r'\"')
|
|
|
|
|
|
|
|
mock_config_and_schema(
|
|
|
|
'''
|
|
|
|
location:
|
|
|
|
source_directories:
|
|
|
|
- /home
|
|
|
|
|
2017-07-23 05:56:46 +00:00
|
|
|
repositories:
|
|
|
|
- "{}.borg"
|
2017-07-05 01:32:37 +00:00
|
|
|
'''.format(escaped_punctuation)
|
|
|
|
)
|
|
|
|
|
|
|
|
result = module.parse_configuration('config.yaml', 'schema.yaml')
|
|
|
|
|
|
|
|
assert result == {
|
|
|
|
'location': {
|
|
|
|
'source_directories': ['/home'],
|
2017-07-23 05:56:46 +00:00
|
|
|
'repositories': ['{}.borg'.format(string.punctuation)],
|
2017-07-05 01:32:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_configuration_raises_for_missing_config_file():
|
|
|
|
with pytest.raises(FileNotFoundError):
|
|
|
|
module.parse_configuration('config.yaml', 'schema.yaml')
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_configuration_raises_for_missing_schema_file():
|
|
|
|
mock_config_and_schema('')
|
2017-07-09 05:33:51 +00:00
|
|
|
builtins = flexmock(sys.modules['builtins'])
|
|
|
|
builtins.should_receive('open').with_args('schema.yaml').and_raise(FileNotFoundError)
|
2017-07-05 01:32:37 +00:00
|
|
|
|
|
|
|
with pytest.raises(FileNotFoundError):
|
|
|
|
module.parse_configuration('config.yaml', 'schema.yaml')
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_configuration_raises_for_syntax_error():
|
2017-07-09 06:01:41 +00:00
|
|
|
mock_config_and_schema('foo:\nbar')
|
2017-07-05 01:32:37 +00:00
|
|
|
|
2017-07-09 06:01:41 +00:00
|
|
|
with pytest.raises(ValueError):
|
2017-07-05 01:32:37 +00:00
|
|
|
module.parse_configuration('config.yaml', 'schema.yaml')
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_configuration_raises_for_validation_error():
|
|
|
|
mock_config_and_schema(
|
|
|
|
'''
|
|
|
|
location:
|
|
|
|
source_directories: yes
|
2017-07-23 05:56:46 +00:00
|
|
|
repositories:
|
|
|
|
- hostname.borg
|
2017-07-05 01:32:37 +00:00
|
|
|
'''
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(module.Validation_error):
|
|
|
|
module.parse_configuration('config.yaml', 'schema.yaml')
|
2017-07-09 17:27:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_display_validation_error_does_not_raise():
|
|
|
|
flexmock(sys.modules['builtins']).should_receive('print')
|
|
|
|
error = module.Validation_error('config.yaml', ('oops', 'uh oh'))
|
|
|
|
|
|
|
|
module.display_validation_error(error)
|