2014-12-07 03:35:20 +01:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
2014-11-18 06:57:44 +01:00
|
|
|
from flexmock import flexmock
|
2016-04-11 00:59:36 +02:00
|
|
|
import pytest
|
2014-11-18 06:57:44 +01:00
|
|
|
|
2017-07-05 01:52:24 +02:00
|
|
|
from borgmatic.config import legacy as module
|
2014-11-18 06:57:44 +01:00
|
|
|
|
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
def test_option_should_create_config_option():
|
|
|
|
option = module.option('name', bool, required=False)
|
2014-11-18 06:57:44 +01:00
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
assert option == module.Config_option('name', bool, False)
|
2014-11-18 06:57:44 +01:00
|
|
|
|
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
def test_option_should_create_config_option_with_defaults():
|
|
|
|
option = module.option('name')
|
2014-11-18 06:57:44 +01:00
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
assert option == module.Config_option('name', str, True)
|
2014-11-18 06:57:44 +01:00
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
|
|
|
|
def test_validate_configuration_format_with_valid_config_should_not_raise():
|
|
|
|
parser = flexmock()
|
|
|
|
parser.should_receive('sections').and_return(('section', 'other'))
|
|
|
|
parser.should_receive('options').with_args('section').and_return(('stuff',))
|
|
|
|
parser.should_receive('options').with_args('other').and_return(('such',))
|
|
|
|
config_format = (
|
|
|
|
module.Section_format(
|
2018-09-30 07:45:00 +02:00
|
|
|
'section', options=(module.Config_option('stuff', str, required=True),)
|
2014-12-07 03:35:20 +01:00
|
|
|
),
|
2018-09-30 07:45:00 +02:00
|
|
|
module.Section_format('other', options=(module.Config_option('such', str, required=True),)),
|
2014-11-18 06:57:44 +01:00
|
|
|
)
|
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
module.validate_configuration_format(parser, config_format)
|
2014-11-18 06:57:44 +01:00
|
|
|
|
|
|
|
|
2015-05-11 07:00:31 +02:00
|
|
|
def test_validate_configuration_format_with_missing_required_section_should_raise():
|
2014-12-07 03:35:20 +01:00
|
|
|
parser = flexmock()
|
|
|
|
parser.should_receive('sections').and_return(('section',))
|
|
|
|
config_format = (
|
2015-05-11 07:00:31 +02:00
|
|
|
module.Section_format(
|
2018-09-30 07:45:00 +02:00
|
|
|
'section', options=(module.Config_option('stuff', str, required=True),)
|
2015-05-11 07:00:31 +02:00
|
|
|
),
|
|
|
|
# At least one option in this section is required, so the section is required.
|
|
|
|
module.Section_format(
|
|
|
|
'missing',
|
|
|
|
options=(
|
|
|
|
module.Config_option('such', str, required=False),
|
|
|
|
module.Config_option('things', str, required=True),
|
|
|
|
),
|
|
|
|
),
|
2014-12-07 03:35:20 +01:00
|
|
|
)
|
|
|
|
|
2016-04-11 00:59:36 +02:00
|
|
|
with pytest.raises(ValueError):
|
2014-12-07 03:35:20 +01:00
|
|
|
module.validate_configuration_format(parser, config_format)
|
2014-11-18 06:57:44 +01:00
|
|
|
|
|
|
|
|
2015-05-11 07:00:31 +02:00
|
|
|
def test_validate_configuration_format_with_missing_optional_section_should_not_raise():
|
|
|
|
parser = flexmock()
|
|
|
|
parser.should_receive('sections').and_return(('section',))
|
|
|
|
parser.should_receive('options').with_args('section').and_return(('stuff',))
|
|
|
|
config_format = (
|
|
|
|
module.Section_format(
|
2018-09-30 07:45:00 +02:00
|
|
|
'section', options=(module.Config_option('stuff', str, required=True),)
|
2015-05-11 07:00:31 +02:00
|
|
|
),
|
|
|
|
# No options in the section are required, so the section is optional.
|
|
|
|
module.Section_format(
|
|
|
|
'missing',
|
|
|
|
options=(
|
|
|
|
module.Config_option('such', str, required=False),
|
|
|
|
module.Config_option('things', str, required=False),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
module.validate_configuration_format(parser, config_format)
|
|
|
|
|
|
|
|
|
|
|
|
def test_validate_configuration_format_with_unknown_section_should_raise():
|
2014-12-07 03:35:20 +01:00
|
|
|
parser = flexmock()
|
|
|
|
parser.should_receive('sections').and_return(('section', 'extra'))
|
2018-09-30 07:45:00 +02:00
|
|
|
config_format = (module.Section_format('section', options=()),)
|
2014-11-18 06:57:44 +01:00
|
|
|
|
2016-04-11 00:59:36 +02:00
|
|
|
with pytest.raises(ValueError):
|
2014-12-07 03:35:20 +01:00
|
|
|
module.validate_configuration_format(parser, config_format)
|
2014-11-18 06:57:44 +01:00
|
|
|
|
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
def test_validate_configuration_format_with_missing_required_option_should_raise():
|
|
|
|
parser = flexmock()
|
|
|
|
parser.should_receive('sections').and_return(('section',))
|
|
|
|
parser.should_receive('options').with_args('section').and_return(('option',))
|
|
|
|
config_format = (
|
|
|
|
module.Section_format(
|
|
|
|
'section',
|
|
|
|
options=(
|
|
|
|
module.Config_option('option', str, required=True),
|
|
|
|
module.Config_option('missing', str, required=True),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2014-11-18 06:57:44 +01:00
|
|
|
|
2016-04-11 00:59:36 +02:00
|
|
|
with pytest.raises(ValueError):
|
2014-12-07 03:35:20 +01:00
|
|
|
module.validate_configuration_format(parser, config_format)
|
2014-11-18 06:57:44 +01:00
|
|
|
|
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
def test_validate_configuration_format_with_missing_optional_option_should_not_raise():
|
|
|
|
parser = flexmock()
|
|
|
|
parser.should_receive('sections').and_return(('section',))
|
|
|
|
parser.should_receive('options').with_args('section').and_return(('option',))
|
|
|
|
config_format = (
|
|
|
|
module.Section_format(
|
|
|
|
'section',
|
|
|
|
options=(
|
|
|
|
module.Config_option('option', str, required=True),
|
|
|
|
module.Config_option('missing', str, required=False),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2014-11-18 06:57:44 +01:00
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
module.validate_configuration_format(parser, config_format)
|
|
|
|
|
|
|
|
|
|
|
|
def test_validate_configuration_format_with_extra_option_should_raise():
|
|
|
|
parser = flexmock()
|
|
|
|
parser.should_receive('sections').and_return(('section',))
|
|
|
|
parser.should_receive('options').with_args('section').and_return(('option', 'extra'))
|
|
|
|
config_format = (
|
|
|
|
module.Section_format(
|
2018-09-30 07:45:00 +02:00
|
|
|
'section', options=(module.Config_option('option', str, required=True),)
|
2014-12-07 03:35:20 +01:00
|
|
|
),
|
|
|
|
)
|
2014-11-18 06:57:44 +01:00
|
|
|
|
2016-04-11 00:59:36 +02:00
|
|
|
with pytest.raises(ValueError):
|
2014-12-07 03:35:20 +01:00
|
|
|
module.validate_configuration_format(parser, config_format)
|
2014-11-18 06:57:44 +01:00
|
|
|
|
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
def test_parse_section_options_should_return_section_options():
|
|
|
|
parser = flexmock()
|
|
|
|
parser.should_receive('get').with_args('section', 'foo').and_return('value')
|
|
|
|
parser.should_receive('getint').with_args('section', 'bar').and_return(1)
|
2016-02-13 19:43:31 +01:00
|
|
|
parser.should_receive('getboolean').never()
|
2014-12-07 03:35:20 +01:00
|
|
|
parser.should_receive('has_option').with_args('section', 'foo').and_return(True)
|
|
|
|
parser.should_receive('has_option').with_args('section', 'bar').and_return(True)
|
|
|
|
|
|
|
|
section_format = module.Section_format(
|
|
|
|
'section',
|
|
|
|
(
|
|
|
|
module.Config_option('foo', str, required=True),
|
|
|
|
module.Config_option('bar', int, required=True),
|
|
|
|
),
|
|
|
|
)
|
2014-11-18 06:57:44 +01:00
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
config = module.parse_section_options(parser, section_format)
|
|
|
|
|
2018-09-30 07:45:00 +02:00
|
|
|
assert config == OrderedDict((('foo', 'value'), ('bar', 1)))
|
2014-11-18 06:57:44 +01:00
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
|
2015-05-11 07:00:31 +02:00
|
|
|
def test_parse_section_options_for_missing_section_should_return_empty_dict():
|
|
|
|
parser = flexmock()
|
|
|
|
parser.should_receive('get').never()
|
|
|
|
parser.should_receive('getint').never()
|
2016-02-13 19:43:31 +01:00
|
|
|
parser.should_receive('getboolean').never()
|
2015-05-11 07:00:31 +02:00
|
|
|
parser.should_receive('has_option').with_args('section', 'foo').and_return(False)
|
|
|
|
parser.should_receive('has_option').with_args('section', 'bar').and_return(False)
|
|
|
|
|
|
|
|
section_format = module.Section_format(
|
|
|
|
'section',
|
|
|
|
(
|
|
|
|
module.Config_option('foo', str, required=False),
|
|
|
|
module.Config_option('bar', int, required=False),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
config = module.parse_section_options(parser, section_format)
|
|
|
|
|
|
|
|
assert config == OrderedDict()
|
|
|
|
|
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
def insert_mock_parser():
|
|
|
|
parser = flexmock()
|
2015-09-07 00:55:14 +02:00
|
|
|
parser.should_receive('read').and_return([flexmock()])
|
2015-09-07 00:33:56 +02:00
|
|
|
module.RawConfigParser = lambda: parser
|
2014-12-07 03:35:20 +01:00
|
|
|
|
|
|
|
return parser
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_configuration_should_return_section_configs():
|
|
|
|
parser = insert_mock_parser()
|
2015-07-28 06:47:52 +02:00
|
|
|
config_format = (flexmock(name='items'), flexmock(name='things'))
|
2014-12-07 03:35:20 +01:00
|
|
|
mock_module = flexmock(module)
|
|
|
|
mock_module.should_receive('validate_configuration_format').with_args(
|
2018-09-30 07:45:00 +02:00
|
|
|
parser, config_format
|
2014-12-07 03:35:20 +01:00
|
|
|
).once()
|
2015-07-28 06:47:52 +02:00
|
|
|
mock_section_configs = (flexmock(), flexmock())
|
2014-12-07 03:35:20 +01:00
|
|
|
|
2015-07-28 06:47:52 +02:00
|
|
|
for section_format, section_config in zip(config_format, mock_section_configs):
|
2014-12-07 03:35:20 +01:00
|
|
|
mock_module.should_receive('parse_section_options').with_args(
|
2018-09-30 07:45:00 +02:00
|
|
|
parser, section_format
|
2014-12-07 03:35:20 +01:00
|
|
|
).and_return(section_config).once()
|
|
|
|
|
2015-07-28 06:47:52 +02:00
|
|
|
parsed_config = module.parse_configuration('filename', config_format)
|
2014-12-07 03:35:20 +01:00
|
|
|
|
2015-07-28 06:47:52 +02:00
|
|
|
assert parsed_config == type(parsed_config)(*mock_section_configs)
|
2015-09-07 00:55:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_parse_configuration_with_file_open_error_should_raise():
|
|
|
|
parser = insert_mock_parser()
|
|
|
|
parser.should_receive('read').and_return([])
|
|
|
|
|
2016-04-11 00:59:36 +02:00
|
|
|
with pytest.raises(ValueError):
|
2015-09-07 00:55:14 +02:00
|
|
|
module.parse_configuration('filename', config_format=flexmock())
|