several corrections about dependency

This commit is contained in:
2017-09-17 15:55:32 +02:00
parent 635b71d291
commit 3567e18256
12 changed files with 225 additions and 78 deletions

View File

@ -15,6 +15,18 @@ from time import sleep, time
from py.test import raises
global incr
incr = 0
def return_incr():
global incr
incr += 1
return incr
def return_value(val):
return val
def make_description():
u1 = IntOption('u1', '', multi=True)
u2 = IntOption('u2', '')
@ -513,8 +525,7 @@ def test_cache_master_and_slaves_master():
assert cfg.cfgimpl_get_values()._p_.get_cached(cfg) == {'val1.val1': {None: ([], None)}, 'val1.val2': {None: ([], None)}}
cfg.val1.val1.append()
assert cfg.cfgimpl_get_settings()._p_.get_cached(cfg) == {None: {None: (set(['cache', 'disabled', 'frozen', 'hidden', 'validator', 'warnings']), None)},
'val1': {None: (set([]), None)},
'val1.val1': {None: (set(['empty']), None)}}
'val1': {None: (set([]), None)}}
assert cfg.cfgimpl_get_values()._p_.get_cached(cfg) == {}
cfg.cfgimpl_get_values().force_cache()
assert cfg.cfgimpl_get_settings()._p_.get_cached(cfg) == {None: {None: (set(['cache', 'disabled', 'frozen', 'hidden', 'validator', 'warnings']), None)},
@ -527,10 +538,8 @@ def test_cache_master_and_slaves_master():
cfg.cfgimpl_get_values().force_cache()
cfg.val1.val2[1] = 'oui'
assert cfg.cfgimpl_get_settings()._p_.get_cached(cfg) == {None: {None: (set(['cache', 'disabled', 'frozen', 'hidden', 'validator', 'warnings']), None)},
'val1': {None: (set([]), None)},
'val1.val1': {None: (set(['empty']), None)},
'val1.val2': {None: (set([]), None), 0: (set([]), None), 1: (set([]), None)}}
assert cfg.cfgimpl_get_values()._p_.get_cached(cfg) == {'val1.val1': {None: ([None, None], None)}}
'val1': {None: (set([]), None)}}
assert cfg.cfgimpl_get_values()._p_.get_cached(cfg) == {}
cfg.cfgimpl_get_values().force_cache()
assert cfg.cfgimpl_get_settings()._p_.get_cached(cfg) == {None: {None: (set(['cache', 'disabled', 'frozen', 'hidden', 'validator', 'warnings']), None)},
'val1': {None: (set([]), None)},
@ -557,8 +566,7 @@ def test_cache_master_callback():
assert cfg.cfgimpl_get_values()._p_.get_cached(cfg) == {'val1.val1': {None: ([], None)}, 'val1.val2': {None: ([], None)}}
cfg.val1.val1.append()
assert cfg.cfgimpl_get_settings()._p_.get_cached(cfg) == {None: {None: (set(['cache', 'disabled', 'frozen', 'hidden', 'validator', 'warnings']), None)},
'val1': {None: (set([]), None)},
'val1.val1': {None: (set(['empty']), None)}}
'val1': {None: (set([]), None)}}
assert cfg.cfgimpl_get_values()._p_.get_cached(cfg) == {}
cfg.cfgimpl_get_values().force_cache()
assert cfg.cfgimpl_get_settings()._p_.get_cached(cfg) == {None: {None: (set(['cache', 'disabled', 'frozen', 'hidden', 'validator', 'warnings']), None)},
@ -644,3 +652,17 @@ def test_cache_global_properties():
assert c.cfgimpl_get_settings()._p_.get_cached(c) == {None: {None: (set(['cache', 'frozen', 'hidden', 'validator', 'warnings', 'test']), None)},
'activate_service': {None: (set([]), None)},
'ip_address_service': {None: (set([]), None)}}
def test_callback_value_incr():
val1 = IntOption('val1', "", callback=return_incr)
val2 = IntOption('val2', "", callback=return_value, callback_params={'value': ((val1, False),)})
maconfig = OptionDescription('rootconfig', '', [val1, val2])
cfg = Config(maconfig)
cfg.read_write()
assert cfg.val1 == 1
sleep(1)
assert cfg.val2 == 1
sleep(1)
assert cfg.val1 == 2
#assert cfg.val2 == 2

View File

@ -53,6 +53,10 @@ def is_config(config, **kwargs):
return 'no'
def ret_from_config(config):
return config.val1
def return_raise(*arg):
raise Exception('test')
@ -273,6 +277,19 @@ def test_callback_with_error():
assert cfg.val1 == 'no'
def test_callback_with_context_value():
val1 = StrOption("val1", "")
val2 = StrOption("val2", "", callback=ret_from_config, callback_params={'': ((None,),)})
maconfig = OptionDescription('rootconfig', '', [val1, val2])
cfg = Config(maconfig)
cfg.val1 = 'yes'
assert cfg.val1 == 'yes'
assert cfg.val2 == 'yes'
cfg.val1 = 'no'
assert cfg.val1 == 'no'
assert cfg.val2 == 'no'
def test_callback_value():
val1 = StrOption('val1', "", 'val')
val2 = StrOption('val2', "", callback=return_value, callback_params={'': ((val1, False),)})

View File

@ -807,3 +807,27 @@ def test_consistency_network_netmask_mandatory():
c.read_only()
c.cfgimpl_get_settings().remove('mandatory')
c.make_dict()
def test_consistency_has_dependency():
a = IPOption('a', '')
b = NetmaskOption('b', '')
od = OptionDescription('od', '', [a, b])
b.impl_add_consistency('ip_netmask', a)
c = Config(od)
assert c.cfgimpl_get_description().a.impl_has_dependency() is True
assert c.cfgimpl_get_description().b.impl_has_dependency() is True
assert c.cfgimpl_get_description().a.impl_has_dependency(False) is True
assert c.cfgimpl_get_description().b.impl_has_dependency(False) is True
def test_consistency_not_equal_has_dependency():
a = IntOption('a', '')
b = IntOption('b', '')
od = OptionDescription('od', '', [a, b])
a.impl_add_consistency('not_equal', b)
c = Config(od)
assert c.cfgimpl_get_description().a.impl_has_dependency() is False
assert c.cfgimpl_get_description().b.impl_has_dependency() is False
assert c.cfgimpl_get_description().a.impl_has_dependency(False) is True
assert c.cfgimpl_get_description().b.impl_has_dependency(False) is True

View File

@ -87,6 +87,11 @@ def value_empty(value, empty, values):
raise ValueError('error')
def valid_from_config(value, config):
if config.opt1 != u'yes':
raise ValueError("c'est une erreur")
def test_validator():
opt1 = StrOption('opt1', '', validator=return_true, default='val')
raises(ValueError, "StrOption('opt2', '', validator=return_false, default='val')")
@ -284,6 +289,17 @@ def test_validator_params_context():
assert 'validator' in cfg.cfgimpl_get_settings()
def test_validator_params_context_value():
opt1 = StrOption('opt1', '', 'yes')
opt2 = StrOption('opt2', '', validator=valid_from_config, validator_params={'': ((None,),)}, default='val')
root = OptionDescription('root', '', [opt1, opt2])
cfg = Config(root)
assert cfg.opt1 == 'yes'
assert cfg.opt2 == 'val'
cfg.opt1 = 'no'
raises(ValueError, "cfg.opt2")
def test_validator_params_key():
opt1 = StrOption('opt1', '', validator=return_true, validator_params={'param': ('yes',)}, default='val')
raises(ConfigError, "StrOption('opt2', '', validator=return_true, validator_params={'param_unknown': ('yes',)}, default='val')")
@ -447,3 +463,21 @@ def test_validator_slave_param():
cfg.ip_admin_eth0.netmask_admin_eth0 = ['val']
cfg.ip_admin_eth0.ip_admin_eth0 = ['yes', 'yes']
cfg.ip_admin_eth0.netmask_admin_eth0[1] = 'val'
def test_validator_dependencies():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip reseau autorise")
netmask_admin_eth0 = StrOption('netmask_admin_eth0',
"masque du sous-reseau",
validator=return_true,
validator_params={'param': ((ip_admin_eth0, False),)})
opt2 = StrOption('opt2', '', validator=return_false)
root = OptionDescription('root', '', [ip_admin_eth0, netmask_admin_eth0, opt2])
cfg = Config(root)
assert cfg.cfgimpl_get_description().ip_admin_eth0.impl_has_dependency() is False
assert cfg.cfgimpl_get_description().netmask_admin_eth0.impl_has_dependency() is True
assert cfg.cfgimpl_get_description().opt2.impl_has_dependency() is False
#
assert cfg.cfgimpl_get_description().ip_admin_eth0.impl_has_dependency(False) is True
assert cfg.cfgimpl_get_description().netmask_admin_eth0.impl_has_dependency(False) is False
assert cfg.cfgimpl_get_description().opt2.impl_has_dependency(False) is False

View File

@ -145,3 +145,16 @@ def test_symlink_slaves():
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
interface1
raises(ValueError, 'interface1.impl_set_group_type(groups.master)')
#____________________________________________________________
def test_symlink_dependency():
boolopt = BoolOption("b", "", default=False)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
config = Config(descr)
assert config.cfgimpl_get_description().s1.b.impl_has_dependency() is False
assert config.cfgimpl_get_description().c.impl_has_dependency() is True
assert config.cfgimpl_get_description().s1.b.impl_has_dependency(False) is True
assert config.cfgimpl_get_description().c.impl_has_dependency(False) is False