better hidden/display support

This commit is contained in:
Emmanuel Garette 2019-07-04 20:43:47 +02:00
parent 83f05197fb
commit fb1286e50e
14 changed files with 1489 additions and 1195 deletions

View file

@ -370,7 +370,6 @@ def test_callback_value_tuple(config_type):
def test_callback_value_force_permissive2(config_type):
config_type = 'tiramisu-api'
val1 = StrOption('val1', "", 'val', properties=('disabled',))
val2 = StrOption('val2', "", callback=return_value, callback_params=Params(ParamOption(val1)))
val3 = StrOption('val3', "", callback=return_value, callback_params=Params(ParamOption(val1, True)))
@ -578,7 +577,7 @@ def test_callback_leader_and_followers_leader2(config_type):
assert cfg.option('val1.val2', 0).value.get() == 'val2'
def test_callback_leader_and_followers_leader_mandatory(config_type):
def test_callback_leader_and_followers_leader_mandatory1(config_type):
val = StrOption('val', "", default='val')
val1 = StrOption('val1', "", multi=True, callback=return_value2, callback_params=Params(ParamOption(val)), properties=('mandatory',))
val3 = StrOption('val3', "", multi=True, callback=return_index, callback_params=Params(ParamOption(val1)), properties=('mandatory',))
@ -591,9 +590,13 @@ def test_callback_leader_and_followers_leader_mandatory(config_type):
assert cfg.option('val1.val3', 0).value.get() == 'val'
assert cfg.option('val1.val4', 0).value.get() == 'val'
assert cfg.option('val1.val1').value.get() == ['val']
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.property.read_write()
cfg = get_config(cfg_ori, config_type)
cfg.option('val1.val1').value.set([undefined, 'val3'])
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.property.read_only()
cfg = get_config(cfg_ori, config_type)
assert cfg.option('val1.val1').value.get() == ['val', 'val3']

View file

@ -162,7 +162,6 @@ def test_consistency_not_equal(config_type):
def test_consistency_not_equal_many_opts(config_type):
config_type='tiramisu-api'
a = IntOption('a', '')
b = IntOption('b', '')
c = IntOption('c', '')
@ -584,7 +583,6 @@ def test_consistency_ip_netmask_invalid():
def test_consistency_network_netmask(config_type):
config_type = 'tiramisu-api'
a = NetworkOption('a', '')
b = NetmaskOption('b', '')
od = OptionDescription('od', '', [a, b])

View file

@ -2,6 +2,7 @@
"frozen and hidden values"
from .autopath import do_autopath
do_autopath()
from .config import config_type, get_config
from py.test import raises
@ -45,95 +46,122 @@ def make_description():
# ____________________________________________________________
def test_is_hidden():
def test_is_hidden(config_type):
descr = make_description()
api = Config(descr)
api.property.read_write()
assert not 'frozen' in api.forcepermissive.option('gc.dummy').property.get()
cfg = Config(descr)
cfg.property.read_write()
assert not 'frozen' in cfg.forcepermissive.option('gc.dummy').property.get()
cfg = get_config(cfg, config_type)
# setattr
raises(PropertiesOptionError, "api.option('gc.dummy').value.get() == False")
raises(PropertiesOptionError, "cfg.option('gc.dummy').value.get() == False")
# getattr
raises(PropertiesOptionError, "api.option('gc.dummy').value.get()")
raises(PropertiesOptionError, "cfg.option('gc.dummy').value.get()")
def test_group_is_hidden():
def test_group_is_hidden(config_type):
descr = make_description()
api = Config(descr)
api.property.read_write()
api.option('gc').property.add('hidden')
raises(PropertiesOptionError, "api.option('gc.dummy').value.get()")
assert 'hidden' in api.forcepermissive.option('gc').property.get()
raises(PropertiesOptionError, "api.option('gc.float').value.get()")
cfg_ori = Config(descr)
cfg_ori.property.read_write()
cfg_ori.option('gc').property.add('hidden')
cfg = get_config(cfg_ori, config_type)
raises(PropertiesOptionError, "cfg.option('gc.dummy').value.get()")
if config_type == 'tiramisu-api':
cfg.send()
assert 'hidden' in cfg_ori.forcepermissive.option('gc').property.get()
cfg = get_config(cfg_ori, config_type)
raises(PropertiesOptionError, "cfg.option('gc.float').value.get()")
# manually set the subconfigs to "show"
api.forcepermissive.option('gc').property.pop('hidden')
assert not 'hidden' in api.option('gc').property.get()
assert api.option('gc.float').value.get() == 2.3
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.forcepermissive.option('gc').property.pop('hidden')
cfg = get_config(cfg_ori, config_type)
assert not 'hidden' in cfg.option('gc').property.get()
assert cfg.option('gc.float').value.get() == 2.3
#dummy est en hide
prop = []
try:
api.option('gc.dummy').value.set(False)
cfg.option('gc.dummy').value.set(False)
except PropertiesOptionError as err:
prop = err.proptype
if config_type == 'tiramisu-api':
assert 'disabled' in prop
else:
assert 'hidden' in prop
def test_group_is_hidden_multi():
def test_group_is_hidden_multi(config_type):
descr = make_description()
api = Config(descr)
api.property.read_write()
api.option('objspace').property.add('hidden')
raises(PropertiesOptionError, "api.option('objspace').value.get()")
assert 'hidden' in api.forcepermissive.option('objspace').property.get()
cfg_ori = Config(descr)
cfg_ori.property.read_write()
cfg_ori.option('objspace').property.add('hidden')
cfg = get_config(cfg_ori, config_type)
raises(PropertiesOptionError, "cfg.option('objspace').value.get()")
if config_type == 'tiramisu-api':
cfg.send()
assert 'hidden' in cfg_ori.forcepermissive.option('objspace').property.get()
cfg = get_config(cfg_ori, config_type)
prop = []
try:
api.option('objspace').value.set(['std'])
cfg.option('objspace').value.set(['std'])
except PropertiesOptionError as err:
prop = err.proptype
if config_type == 'tiramisu-api':
assert 'disabled' in prop
else:
assert 'hidden' in prop
api.forcepermissive.option('objspace').property.pop('hidden')
assert not 'hidden' in api.option('objspace').property.get()
api.option('objspace').value.set(['std', 'std'])
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.forcepermissive.option('objspace').property.pop('hidden')
cfg = get_config(cfg_ori, config_type)
assert not 'hidden' in cfg.option('objspace').property.get()
cfg.option('objspace').value.set(['std', 'std'])
def test_global_show():
def test_global_show(config_type):
descr = make_description()
api = Config(descr)
api.property.read_write()
api.forcepermissive.option('gc.dummy').property.add('hidden')
assert 'hidden' in api.forcepermissive.option('gc.dummy').property.get()
raises(PropertiesOptionError, "api.option('gc.dummy').value.get() == False")
cfg = Config(descr)
cfg.property.read_write()
cfg.forcepermissive.option('gc.dummy').property.add('hidden')
assert 'hidden' in cfg.forcepermissive.option('gc.dummy').property.get()
cfg = get_config(cfg, config_type)
raises(PropertiesOptionError, "cfg.option('gc.dummy').value.get() == False")
def test_with_many_subgroups():
def test_with_many_subgroups(config_type):
descr = make_description()
api = Config(descr)
cfg_ori = Config(descr)
#booltwo = config.unwrap_from_path('gc.subgroup.booltwo')
#setting = config.cfgimpl_get_settings()
assert not 'hidden' in api.option('gc.subgroup.booltwo').property.get()
assert api.option('gc.subgroup.booltwo').value.get() is False
api.option('gc.subgroup.booltwo').property.add('hidden')
cfg = get_config(cfg_ori, config_type)
assert not 'hidden' in cfg.option('gc.subgroup.booltwo').property.get()
assert cfg.option('gc.subgroup.booltwo').value.get() is False
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.option('gc.subgroup.booltwo').property.add('hidden')
def test_password_option():
def test_password_option(config_type):
o = PasswordOption('o', '')
d = OptionDescription('d', '', [o])
api = Config(d)
cfg = Config(d)
cfg = get_config(cfg, config_type)
api.option('o').value.set('a_valid_password')
raises(ValueError, "api.option('o').value.set(1)")
cfg.option('o').value.set('a_valid_password')
raises(ValueError, "cfg.option('o').value.set(1)")
def test_date_option():
def test_date_option(config_type):
o = DateOption('o', '')
d = OptionDescription('d', '', [o])
api = Config(d)
cfg = Config(d)
cfg = get_config(cfg, config_type)
api.option('o').value.set('2017-02-04')
api.option('o').value.set('2017-2-4')
raises(ValueError, "api.option('o').value.set(1)")
raises(ValueError, "api.option('o').value.set('2017-13-20')")
raises(ValueError, "api.option('o').value.set('2017-11-31')")
raises(ValueError, "api.option('o').value.set('2017-12-32')")
raises(ValueError, "api.option('o').value.set('2017-2-29')")
raises(ValueError, "api.option('o').value.set('2-2-2017')")
raises(ValueError, "api.option('o').value.set('2017/2/2')")
cfg.option('o').value.set('2017-02-04')
cfg.option('o').value.set('2017-2-4')
raises(ValueError, "cfg.option('o').value.set(1)")
raises(ValueError, "cfg.option('o').value.set('2017-13-20')")
raises(ValueError, "cfg.option('o').value.set('2017-11-31')")
raises(ValueError, "cfg.option('o').value.set('2017-12-32')")
raises(ValueError, "cfg.option('o').value.set('2017-2-29')")
raises(ValueError, "cfg.option('o').value.set('2-2-2017')")
raises(ValueError, "cfg.option('o').value.set('2017/2/2')")

View file

@ -1,5 +1,6 @@
from .autopath import do_autopath
do_autopath()
from .config import config_type, get_config
import warnings
from py.test import raises
@ -39,9 +40,9 @@ def return_if_val(value):
def is_context(value, context):
api = Config(context)
api.property.pop('validator')
if not isinstance(api, Config):
cfg = Config(context)
cfg.property.pop('validator')
if not isinstance(cfg, Config):
raise ValueError('not context')
@ -96,114 +97,130 @@ def value_empty(value, empty, values):
def valid_from_config(value, config):
api = Config(config)
if api.option('opt1').value.get() != u'yes':
cfg = Config(config)
if cfg.option('opt1').value.get() != u'yes':
raise ValueError("c'est une erreur")
def test_validator():
def test_validator(config_type):
opt1 = StrOption('opt1', '', validator=return_true, default='val')
raises(ValueError, "StrOption('opt2', '', validator=return_false, default='val')")
opt2 = StrOption('opt2', '', validator=return_false)
root = OptionDescription('root', '', [opt1, opt2])
api = Config(root)
assert api.option('opt1').value.get() == 'val'
raises(ValueError, "api.option('opt2').value.set('val')")
cfg_ori = Config(root)
cfg = get_config(cfg_ori, config_type)
assert cfg.option('opt1').value.get() == 'val'
raises(ValueError, "cfg.option('opt2').value.set('val')")
try:
api.option('opt2').value.set('val')
cfg.option('opt2').value.set('val')
except ValueError as err:
msg = _('"{0}" is an invalid {1} for "{2}"').format('val', _('string'), 'opt2') + ', ' + _('test error return_false')
assert str(err) == msg
api.property.add('demoting_error_warning')
if config_type == 'tiramisu-api':
msg = _('"{0}" is an invalid {1} for "{2}"').format('val', 'string', 'opt2') + ', ' + _('test error return_false')
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.property.add('demoting_error_warning')
cfg = get_config(cfg_ori, config_type)
warnings.simplefilter("always", ValueWarning)
with warnings.catch_warnings(record=True) as w:
api.option('opt2').value.set('val')
cfg.option('opt2').value.set('val')
assert len(w) == 1
assert str(w[0].message) == msg
def test_validator_params():
def test_validator_params(config_type):
opt1 = StrOption('opt1', '', validator=return_true, validator_params=Params(ParamValue('yes')), default='val')
raises(ValueError, "StrOption('opt2', '', validator=return_false, validator_params=Params(ParamValue('yes')), default='val')")
opt2 = StrOption('opt2', '', validator=return_false, validator_params=Params(ParamValue('yes')))
root = OptionDescription('root', '', [opt1, opt2])
api = Config(root)
assert api.option('opt1').value.get() == 'val'
raises(ValueError, "api.option('opt2').value.set('val')")
api.property.add('demoting_error_warning')
cfg_ori = Config(root)
cfg = get_config(cfg_ori, config_type)
assert cfg.option('opt1').value.get() == 'val'
raises(ValueError, "cfg.option('opt2').value.set('val')")
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.property.add('demoting_error_warning')
cfg = get_config(cfg_ori, config_type)
warnings.simplefilter("always", ValueWarning)
with warnings.catch_warnings(record=True) as w:
api.option('opt2').value.set('val')
cfg.option('opt2').value.set('val')
assert len(w) == 1
def test_validator_params_value_values():
def test_validator_params_value_values(config_type):
opt1 = StrOption('opt1', '', validator=value_values, default=['val'], multi=True)
root = OptionDescription('root', '', [opt1])
api = Config(root)
assert api.option('opt1').value.get() == ['val']
api.option('opt1').value.set(['val1', 'val2'])
cfg = Config(root)
cfg = get_config(cfg, config_type)
assert cfg.option('opt1').value.get() == ['val']
cfg.option('opt1').value.set(['val1', 'val2'])
def test_validator_params_value_values_index():
def test_validator_params_value_values_index(config_type):
opt1 = StrOption('opt1', '', validator=value_values_index, default=['val'], multi=True)
root = OptionDescription('root', '', [opt1])
api = Config(root)
assert api.option('opt1').value.get() == ['val']
api.option('opt1').value.set(['val1', 'val2'])
cfg = Config(root)
cfg = get_config(cfg, config_type)
assert cfg.option('opt1').value.get() == ['val']
cfg.option('opt1').value.set(['val1', 'val2'])
def test_validator_params_value_values_leader():
def test_validator_params_value_values_leader(config_type):
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip reseau autorise", multi=True, validator=value_values)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-reseau", multi=True)
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
root = OptionDescription('root', '', [interface1])
api = Config(root)
assert api.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['val1', 'val2'])
cfg = Config(root)
cfg = get_config(cfg, config_type)
assert cfg.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val1', 'val2'])
def test_validator_params_value_values_index_leader():
def test_validator_params_value_values_index_leader(config_type):
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip reseau autorise", multi=True, validator=value_values_index)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-reseau", multi=True)
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
root = OptionDescription('root', '', [interface1])
api = Config(root)
assert api.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['val1', 'val2'])
cfg = Config(root)
cfg = get_config(cfg, config_type)
assert cfg.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val1', 'val2'])
def test_validator_params_value_values_follower():
def test_validator_params_value_values_follower(config_type):
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip reseau autorise", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-reseau", multi=True, validator=value_values)
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
root = OptionDescription('root', '', [interface1])
api = Config(root)
assert api.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['val'])
api.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('val1')
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['val', 'val'])
api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('val2')
cfg = Config(root)
cfg = get_config(cfg, config_type)
assert cfg.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val'])
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('val1')
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val', 'val'])
cfg.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('val2')
def test_validator_params_value_values_index_follower():
def test_validator_params_value_values_index_follower(config_type):
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip reseau autorise", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-reseau", multi=True, validator=value_values_index)
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
root = OptionDescription('root', '', [interface1])
api = Config(root)
assert api.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['val'])
api.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('val1')
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['val', 'val'])
api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('val2')
cfg = Config(root)
cfg = get_config(cfg, config_type)
assert cfg.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val'])
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('val1')
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val', 'val'])
cfg.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('val2')
def test_validator_params_value_values_notmulti():
raises(ConfigError, "opt1 = StrOption('opt1', '', validator=value_values, default='val')")
def test_validator_params_value_values_kwargs_empty():
def test_validator_params_value_values_kwargs_empty(config_type):
v = BoolOption('v', '', default=False)
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip reseau autorise", multi=True, default=["ip"])
netmask_admin_eth0 = StrOption('netmask_admin_eth0',
@ -213,14 +230,15 @@ def test_validator_params_value_values_kwargs_empty():
validator_params=Params(ParamOption(v)))
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
root = OptionDescription('root', '', [v, interface1])
api = Config(root)
assert api.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['ip']
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['ip', 'val'])
cfg = Config(root)
cfg = get_config(cfg, config_type)
assert cfg.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['ip']
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['ip', 'val'])
#cfg.ip_admin_eth0.ip_admin_eth0.append('val')
#cfg.ip_admin_eth0.netmask_admin_eth0[1] = 'val2'
def test_validator_params_value_values_kwargs():
def test_validator_params_value_values_kwargs(config_type):
v = BoolOption('v', '', default=False)
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip reseau autorise", multi=True, default=["ip"])
netmask_admin_eth0 = StrOption('netmask_admin_eth0',
@ -230,14 +248,15 @@ def test_validator_params_value_values_kwargs():
validator_params=Params(kwargs={'auto': ParamOption(v)}))
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
root = OptionDescription('root', '', [v, interface1])
api = Config(root)
assert api.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['ip']
api.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('val1')
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['ip', 'val'])
api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('val2')
cfg = Config(root)
cfg = get_config(cfg, config_type)
assert cfg.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['ip']
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('val1')
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['ip', 'val'])
cfg.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('val2')
def test_validator_params_value_values_kwargs_values():
def test_validator_params_value_values_kwargs_values(config_type):
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip reseau autorise", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0',
"masque du sous-reseau",
@ -246,15 +265,16 @@ def test_validator_params_value_values_kwargs_values():
validator_params=Params(kwargs={'values': ParamOption(ip_admin_eth0)}))
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
root = OptionDescription('root', '', [interface1])
api = Config(root)
assert api.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['val'])
api.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('val1')
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['val', 'val'])
api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('val2')
cfg = Config(root)
cfg = get_config(cfg, config_type)
assert cfg.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val'])
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('val1')
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val', 'val'])
cfg.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('val2')
def test_validator_params_value_values_kwargs2():
def test_validator_params_value_values_kwargs2(config_type):
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip reseau autorise", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0',
"masque du sous-reseau",
@ -263,14 +283,15 @@ def test_validator_params_value_values_kwargs2():
validator_params=Params(ParamValue(['val1']), {'index': ParamOption(ip_admin_eth0)}))
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
root = OptionDescription('root', '', [interface1])
api = Config(root)
assert api.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['val'])
api.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('val1')
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['val', 'val'])
cfg = Config(root)
cfg = get_config(cfg, config_type)
assert cfg.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val'])
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('val1')
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val', 'val'])
def test_validator_params_value_values_kwargs_index():
def test_validator_params_value_values_kwargs_index(config_type):
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip reseau autorise", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0',
"masque du sous-reseau",
@ -279,158 +300,181 @@ def test_validator_params_value_values_kwargs_index():
validator_params=Params(kwargs={'index': ParamOption(ip_admin_eth0)}))
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
root = OptionDescription('root', '', [interface1])
api = Config(root)
assert api.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['val'])
api.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('val1')
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['val', 'val'])
api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('val2')
cfg = Config(root)
cfg = get_config(cfg, config_type)
assert cfg.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val'])
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('val1')
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val', 'val'])
cfg.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('val2')
def test_validator_params_context():
opt1 = StrOption('opt1', '', validator=is_context, validator_params=Params(ParamContext()), default='val')
root = OptionDescription('root', '', [opt1])
api = Config(root)
assert 'validator' in api.property.get()
assert api.option('opt1').value.get() == 'val'
assert 'validator' in api.property.get()
cfg = Config(root)
# cfg = get_config(cfg, config_type) # ParamContext not supported
assert 'validator' in cfg.property.get()
assert cfg.option('opt1').value.get() == 'val'
assert 'validator' in cfg.property.get()
def test_validator_params_context_value():
opt1 = StrOption('opt1', '', 'yes')
opt2 = StrOption('opt2', '', validator=valid_from_config, validator_params=Params(ParamContext()), default='val')
root = OptionDescription('root', '', [opt1, opt2])
api = Config(root)
assert api.option('opt1').value.get() == 'yes'
assert api.option('opt2').value.get() == 'val'
api.option('opt1').value.set('no')
raises(ValueError, "assert api.option('opt2').value.get()")
api.property.add('demoting_error_warning')
cfg = Config(root)
# cfg = get_config(cfg_ori, config_type) # ParamContext not supported
assert cfg.option('opt1').value.get() == 'yes'
assert cfg.option('opt2').value.get() == 'val'
cfg.option('opt1').value.set('no')
raises(ValueError, "assert cfg.option('opt2').value.get()")
if config_type == 'tiramisu-api':
cfg.send()
cfg.property.add('demoting_error_warning')
warnings.simplefilter("always", ValueWarning)
with warnings.catch_warnings(record=True) as w:
api.option('opt2').value.get()
cfg.option('opt2').value.get()
assert len(w) == 1
def test_validator_params_key():
def test_validator_params_key(config_type):
opt1 = StrOption('opt1', '', validator=return_true, validator_params=Params(kwargs={'param': ParamValue('yes')}), default='val')
raises(ConfigError, "StrOption('opt2', '', validator=return_true, validator_params=Params(kwargs={'param_unknown': ParamValue('yes')}), default='val')")
root = OptionDescription('root', '', [opt1])
api = Config(root)
assert api.option('opt1').value.get() == 'val'
cfg = Config(root)
cfg = get_config(cfg, config_type)
assert cfg.option('opt1').value.get() == 'val'
def test_validator_params_option():
def test_validator_params_option(config_type):
opt0 = StrOption('opt0', '', default='yes')
opt1 = StrOption('opt1', '', validator=return_true, validator_params=Params(ParamOption(opt0)), default='val')
r = OptionDescription('root', '', [opt0, opt1])
api = Config(r)
assert api.option('opt1').value.get() == 'val'
api.option('opt0').value.set('val')
raises(ValueError, "api.option('opt1').value.get()")
api.property.add('demoting_error_warning')
cfg_ori = Config(r)
cfg = get_config(cfg_ori, config_type)
assert cfg.option('opt1').value.get() == 'val'
cfg.option('opt0').value.set('val')
raises(ValueError, "cfg.option('opt1').value.get()")
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.property.add('demoting_error_warning')
cfg = get_config(cfg_ori, config_type)
warnings.simplefilter("always", ValueWarning)
with warnings.catch_warnings(record=True) as w:
api.option('opt1').value.get()
cfg.option('opt1').value.get()
assert len(w) == 1
def test_validator_multi():
def test_validator_multi(config_type):
opt1 = StrOption('opt1', '', validator=return_if_val, multi=True)
root = OptionDescription('root', '', [opt1])
api = Config(root)
assert api.option('opt1').value.get() == []
api.option('opt1').value.set(['val'])
assert api.option('opt1').value.get() == ['val']
raises(ValueError, "api.option('opt1').value.set(['val', 'val1'])")
api.property.add('demoting_error_warning')
cfg_ori = Config(root)
cfg = get_config(cfg_ori, config_type)
assert cfg.option('opt1').value.get() == []
cfg.option('opt1').value.set(['val'])
assert cfg.option('opt1').value.get() == ['val']
raises(ValueError, "cfg.option('opt1').value.set(['val', 'val1'])")
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.property.add('demoting_error_warning')
cfg = get_config(cfg_ori, config_type)
warnings.simplefilter("always", ValueWarning)
with warnings.catch_warnings(record=True) as w:
api.option('opt1').value.set(['val', 'val1'])
cfg.option('opt1').value.set(['val', 'val1'])
assert len(w) == 1
def test_validator_warning():
def test_validator_warning(config_type):
opt1 = StrOption('opt1', '', validator=return_true, default='val', warnings_only=True)
opt2 = StrOption('opt2', '', validator=return_false, warnings_only=True)
opt3 = StrOption('opt3', '', validator=return_if_val, multi=True, warnings_only=True)
root = OptionDescription('root', '', [opt1, opt2, opt3])
api = Config(root)
assert api.option('opt1').value.get() == 'val'
cfg = Config(root)
cfg = get_config(cfg, config_type)
assert cfg.option('opt1').value.get() == 'val'
warnings.simplefilter("always", ValueWarning)
with warnings.catch_warnings(record=True) as w:
api.option('opt1').value.set('val')
cfg.option('opt1').value.set('val')
assert w == []
#
with warnings.catch_warnings(record=True) as w:
api.option('opt2').value.set('val')
cfg.option('opt2').value.set('val')
assert len(w) == 1
if config_type != 'tiramisu-api':
assert w[0].message.opt() == opt2
assert str(w[0].message) == msg_err.format('val', opt2._display_name, 'opt2') + ', ' + 'test error return_false'
#
with warnings.catch_warnings(record=True) as w:
api.option('opt3').value.set(['val'])
cfg.option('opt3').value.set(['val'])
assert w == []
#
with warnings.catch_warnings(record=True) as w:
api.option('opt3').value.set(['val', 'val1'])
cfg.option('opt3').value.set(['val', 'val1'])
assert len(w) == 1
if config_type != 'tiramisu-api':
assert w[0].message.opt() == opt3
assert str(w[0].message) == msg_err.format('val1', opt3._display_name, 'opt3') + ', ' + 'test error'
#
with warnings.catch_warnings(record=True) as w:
raises(ValueError, "api.option('opt2').value.set(1)")
raises(ValueError, "cfg.option('opt2').value.set(1)")
assert len(w) == 0
#
with warnings.catch_warnings(record=True) as w:
api.option('opt2').value.set('val')
api.option('opt3').value.set(['val', 'val1', 'val'])
cfg.option('opt2').value.set('val')
cfg.option('opt3').value.set(['val', 'val1', 'val'])
assert len(w) == 2
if config_type != 'tiramisu-api':
assert w[0].message.opt() == opt2
assert str(w[0].message) == msg_err.format('val', opt2._display_name, 'opt2') + ', ' + 'test error return_false'
assert w[1].message.opt() == opt3
assert str(w[1].message) == msg_err.format('val1', opt3._display_name, 'opt3') + ', ' + 'test error'
def test_validator_warning_disabled():
def test_validator_warning_disabled(config_type):
opt1 = StrOption('opt1', '', validator=return_true, default='val', warnings_only=True)
opt2 = StrOption('opt2', '', validator=return_false, warnings_only=True)
opt3 = StrOption('opt3', '', validator=return_if_val, multi=True, warnings_only=True)
root = OptionDescription('root', '', [opt1, opt2, opt3])
api = Config(root)
api.property.pop('warnings')
assert api.option('opt1').value.get() == 'val'
cfg_ori = Config(root)
cfg_ori.property.pop('warnings')
cfg = get_config(cfg_ori, config_type)
assert cfg.option('opt1').value.get() == 'val'
warnings.simplefilter("always", ValueWarning)
with warnings.catch_warnings(record=True) as w:
api.option('opt1').value.set('val')
cfg.option('opt1').value.set('val')
assert w == []
#
with warnings.catch_warnings(record=True) as w:
api.option('opt2').value.set('val')
cfg.option('opt2').value.set('val')
assert w == []
#
with warnings.catch_warnings(record=True) as w:
api.option('opt3').value.set(['val'])
cfg.option('opt3').value.set(['val'])
assert w == []
#
with warnings.catch_warnings(record=True) as w:
api.option('opt3').value.set(['val', 'val1'])
cfg.option('opt3').value.set(['val', 'val1'])
assert w == []
raises(ValueError, "api.option('opt2').value.set(1)")
raises(ValueError, "cfg.option('opt2').value.set(1)")
#
with warnings.catch_warnings(record=True) as w:
api.option('opt2').value.set('val')
api.option('opt3').value.set(['val', 'val1', 'val'])
cfg.option('opt2').value.set('val')
cfg.option('opt3').value.set(['val', 'val1', 'val'])
assert w == []
#
api.property.add('demoting_error_warning')
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.property.add('demoting_error_warning')
cfg = get_config(cfg_ori, config_type)
if config_type != 'tiramisu-api':
warnings.simplefilter("always", ValueWarning)
with warnings.catch_warnings(record=True) as w:
api.option('opt2').value.set(1)
cfg.option('opt2').value.set(1)
assert len(w) == 1
def test_validator_warning_leadership():
def test_validator_warning_leadership(config_type):
display_name_ip = "ip reseau autorise"
display_name_netmask = "masque du sous-reseau"
ip_admin_eth0 = StrOption('ip_admin_eth0', display_name_ip, multi=True, validator=return_false, warnings_only=True)
@ -438,48 +482,55 @@ def test_validator_warning_leadership():
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
assert interface1.impl_get_group_type() == groups.leadership
root = OptionDescription('root', '', [interface1])
api = Config(root)
cfg = Config(root)
cfg = get_config(cfg, config_type)
warnings.simplefilter("always", ValueWarning)
with warnings.catch_warnings(record=True) as w:
api.option('ip_admin_eth0.ip_admin_eth0').value.set([None])
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set([None])
assert w == []
#
with warnings.catch_warnings(record=True) as w:
api.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('val1')
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('val1')
assert len(w) == 1
if config_type != 'tiramisu-api':
assert w[0].message.opt() == netmask_admin_eth0
assert str(w[0].message) == msg_err.format('val1', netmask_admin_eth0._display_name, display_name_netmask) + ', test error'
#
with warnings.catch_warnings(record=True) as w:
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['val'])
assert len(w) == 1
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val'])
if config_type != 'tiramisu-api':
assert w[0].message.opt() == ip_admin_eth0
assert str(w[0].message) == msg_err.format('val', ip_admin_eth0._display_name, display_name_ip) + ', test error return_false'
else:
assert len(w) == 2
#
with warnings.catch_warnings(record=True) as w:
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['val', 'val1', 'val1'])
#FIXME
#assert len(w) == 1
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val', 'val1', 'val1'])
if config_type != 'tiramisu-api':
assert w[0].message.opt() == ip_admin_eth0
assert str(w[0].message) == msg_err.format('val', ip_admin_eth0._display_name, display_name_ip) + ', test error return_false'
else:
assert len(w) == 3
#
with warnings.catch_warnings(record=True) as w:
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['val1', 'val', 'val1'])
#FIXME
#assert len(w) == 1
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val1', 'val', 'val1'])
if config_type != 'tiramisu-api':
assert w[0].message.opt() == ip_admin_eth0
assert str(w[0].message) == msg_err.format('val', ip_admin_eth0._display_name, display_name_ip) + ', test error return_false'
else:
assert len(w) == 3
#
warnings.resetwarnings()
with warnings.catch_warnings(record=True) as w:
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['val1', 'val1', 'val'])
#FIXME
#assert len(w) == 1
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val1', 'val1', 'val'])
if config_type != 'tiramisu-api':
assert w[0].message.opt() == ip_admin_eth0
assert str(w[0].message) == msg_err.format('val', ip_admin_eth0._display_name, display_name_ip) + ', test error return_false'
else:
assert len(w) == 3
def test_validator_follower_param():
def test_validator_follower_param(config_type):
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip reseau autorise", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0',
"masque du sous-reseau",
@ -488,12 +539,13 @@ def test_validator_follower_param():
validator_params=Params(kwargs={'param': ParamOption(ip_admin_eth0)}))
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
root = OptionDescription('root', '', [interface1])
api = Config(root)
assert api.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['yes'])
api.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('val')
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['yes', 'yes'])
api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('val')
cfg = Config(root)
cfg = get_config(cfg, config_type)
assert cfg.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['yes'])
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('val')
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['yes', 'yes'])
cfg.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('val')
def test_validator_dependencies():
@ -504,11 +556,11 @@ def test_validator_dependencies():
validator_params=Params(kwargs={'param': ParamOption(ip_admin_eth0)}))
opt2 = StrOption('opt2', '', validator=return_false)
root = OptionDescription('root', '', [ip_admin_eth0, netmask_admin_eth0, opt2])
api = Config(root)
assert api.option('ip_admin_eth0').option.has_dependency() is False
assert api.option('netmask_admin_eth0').option.has_dependency() is True
assert api.option('opt2').option.has_dependency() is False
cfg = Config(root)
assert cfg.option('ip_admin_eth0').option.has_dependency() is False
assert cfg.option('netmask_admin_eth0').option.has_dependency() is True
assert cfg.option('opt2').option.has_dependency() is False
#
assert api.option('ip_admin_eth0').option.has_dependency(False) is True
assert api.option('netmask_admin_eth0').option.has_dependency(False) is False
assert api.option('opt2').option.has_dependency(False) is False
assert cfg.option('ip_admin_eth0').option.has_dependency(False) is True
assert cfg.option('netmask_admin_eth0').option.has_dependency(False) is False
assert cfg.option('opt2').option.has_dependency(False) is False

View file

@ -1,6 +1,7 @@
#this test is much more to test that **it's there** and answers attribute access
from .autopath import do_autopath
do_autopath()
from .config import config_type, get_config
from py.test import raises
@ -36,17 +37,18 @@ def make_description():
return descr
def test_root_config_answers_ok():
def test_root_config_answers_ok(config_type):
"if you hide the root config, the options in this namespace behave normally"
gcdummy = BoolOption('dummy', 'dummy', default=False)
boolop = BoolOption('boolop', 'Test boolean option op', default=True)
descr = OptionDescription('tiramisu', '', [gcdummy, boolop])
api = Config(descr)
cfg = Config(descr)
cfg = get_config(cfg, config_type)
#settings = cfg.cfgimpl_get_settings()
#settings.append('hidden')
assert api.option('dummy').value.get() is False
assert api.option('boolop').value.get() is True
assert cfg.option('dummy').value.get() is False
assert cfg.option('boolop').value.get() is True
#def test_optname_shall_not_start_with_numbers():
@ -54,9 +56,10 @@ def test_root_config_answers_ok():
# raises(ValueError, "descr = OptionDescription('123tiramisu', '', [])")
#
#
def test_option_has_an_api_name():
def test_option_has_an_api_name(config_type):
b = BoolOption('impl_has_dependency', 'dummy', default=True)
descr = OptionDescription('tiramisu', '', [b])
api = Config(descr)
assert api.option('impl_has_dependency').value.get() is True
cfg = Config(descr)
cfg = get_config(cfg, config_type)
assert cfg.option('impl_has_dependency').value.get() is True
assert b.impl_has_dependency() is False

View file

@ -1,12 +1,12 @@
# coding: utf-8
from .autopath import do_autopath
do_autopath()
from .config import config_type, get_config
from py.test import raises
from tiramisu import IntOption, UnicodeOption, OptionDescription, Config
from tiramisu.error import PropertiesOptionError, ConfigError
from tiramisu.api import TIRAMISU_VERSION
from tiramisu.storage import list_sessions, delete_session
@ -20,61 +20,81 @@ def make_description():
return OptionDescription('od1', '', [u1, u2])
def test_permissive():
def test_permissive(config_type):
descr = make_description()
api = Config(descr)
api.property.read_write()
api.property.read_write()
cfg_ori = Config(descr)
cfg_ori.property.read_write()
cfg_ori.property.read_write()
cfg = get_config(cfg_ori, config_type)
props = frozenset()
try:
api.option('u1').value.get()
cfg.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
api.unrestraint.permissive.set(frozenset(['disabled']))
assert api.unrestraint.permissive.get() == frozenset(['disabled'])
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.unrestraint.permissive.set(frozenset(['disabled']))
assert cfg_ori.unrestraint.permissive.get() == frozenset(['disabled'])
cfg = get_config(cfg_ori, config_type)
props = frozenset()
try:
api.option('u1').value.get()
cfg.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
api.property.add('permissive')
api.option('u1').value.get()
api.property.pop('permissive')
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.property.add('permissive')
cfg = get_config(cfg_ori, config_type)
cfg.option('u1').value.get()
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.property.pop('permissive')
cfg = get_config(cfg_ori, config_type)
props = frozenset()
try:
api.option('u1').value.get()
cfg.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
def test_permissive_add():
def test_permissive_add(config_type):
descr = make_description()
api = Config(descr)
api.property.read_write()
api.property.read_write()
cfg_ori = Config(descr)
cfg_ori.property.read_write()
cfg_ori.property.read_write()
cfg = get_config(cfg_ori, config_type)
props = frozenset()
try:
api.option('u1').value.get()
cfg.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
api.unrestraint.permissive.add('disabled')
assert api.unrestraint.permissive.get() == frozenset(['hidden', 'disabled'])
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.unrestraint.permissive.add('disabled')
assert cfg_ori.unrestraint.permissive.get() == frozenset(['hidden', 'disabled'])
cfg = get_config(cfg_ori, config_type)
props = frozenset()
try:
api.option('u1').value.get()
cfg.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
api.property.add('permissive')
api.option('u1').value.get()
api.property.pop('permissive')
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.property.add('permissive')
cfg = get_config(cfg_ori, config_type)
cfg.option('u1').value.get()
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.property.pop('permissive')
cfg = get_config(cfg_ori, config_type)
props = frozenset()
try:
api.option('u1').value.get()
cfg.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
@ -82,22 +102,22 @@ def test_permissive_add():
def test_permissive_pop():
descr = make_description()
api = Config(descr)
api.property.read_write()
api.property.read_write()
cfg = Config(descr)
cfg.property.read_write()
cfg.property.read_write()
props = frozenset()
try:
api.forcepermissive.option('u1').value.get()
cfg.forcepermissive.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
api.unrestraint.permissive.add('disabled')
assert api.unrestraint.permissive.get() == frozenset(['hidden', 'disabled'])
api.forcepermissive.option('u1').value.get()
api.unrestraint.permissive.pop('disabled')
cfg.unrestraint.permissive.add('disabled')
assert cfg.unrestraint.permissive.get() == frozenset(['hidden', 'disabled'])
cfg.forcepermissive.option('u1').value.get()
cfg.unrestraint.permissive.pop('disabled')
props = frozenset()
try:
api.forcepermissive.option('u1').value.get()
cfg.forcepermissive.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
@ -105,140 +125,138 @@ def test_permissive_pop():
def test_permissive_reset():
descr = make_description()
api = Config(descr)
api.property.read_write()
assert api.unrestraint.permissive.get() == frozenset(['hidden'])
cfg = Config(descr)
cfg.property.read_write()
assert cfg.unrestraint.permissive.get() == frozenset(['hidden'])
#
api.unrestraint.permissive.set(frozenset(['disabled']))
assert api.unrestraint.permissive.get() == frozenset(['disabled'])
cfg.unrestraint.permissive.set(frozenset(['disabled']))
assert cfg.unrestraint.permissive.get() == frozenset(['disabled'])
#
api.unrestraint.permissive.reset()
assert api.unrestraint.permissive.get() == frozenset()
cfg.unrestraint.permissive.reset()
assert cfg.unrestraint.permissive.get() == frozenset()
def test_permissive_mandatory():
descr = make_description()
api = Config(descr)
api.property.read_only()
cfg = Config(descr)
cfg.property.read_only()
props = frozenset()
try:
api.option('u1').value.get()
cfg.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
if TIRAMISU_VERSION == 2:
assert frozenset(props) == frozenset(['disabled', 'mandatory'])
else:
assert frozenset(props) == frozenset(['disabled'])
api.unrestraint.permissive.set(frozenset(['mandatory', 'disabled']))
assert api.unrestraint.permissive.get() == frozenset(['mandatory', 'disabled'])
api.property.add('permissive')
api.option('u1').value.get()
api.property.pop('permissive')
cfg.unrestraint.permissive.set(frozenset(['mandatory', 'disabled']))
assert cfg.unrestraint.permissive.get() == frozenset(['mandatory', 'disabled'])
cfg.property.add('permissive')
cfg.option('u1').value.get()
cfg.property.pop('permissive')
try:
api.option('u1').value.get()
cfg.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
if TIRAMISU_VERSION == 2:
assert frozenset(props) == frozenset(['disabled', 'mandatory'])
else:
assert frozenset(props) == frozenset(['disabled'])
def test_permissive_frozen():
descr = make_description()
api = Config(descr)
api.property.read_write()
api.unrestraint.permissive.set(frozenset(['frozen', 'disabled']))
assert api.unrestraint.permissive.get() == frozenset(['frozen', 'disabled'])
assert api.permissive.get() == frozenset(['frozen', 'disabled'])
cfg = Config(descr)
cfg.property.read_write()
cfg.unrestraint.permissive.set(frozenset(['frozen', 'disabled']))
assert cfg.unrestraint.permissive.get() == frozenset(['frozen', 'disabled'])
assert cfg.permissive.get() == frozenset(['frozen', 'disabled'])
try:
api.option('u1').value.set(1)
cfg.option('u1').value.set(1)
except PropertiesOptionError as err:
props = err.proptype
if TIRAMISU_VERSION == 2:
assert frozenset(props) == frozenset(['disabled', 'frozen'])
else:
assert frozenset(props) == frozenset(['disabled'])
api.property.add('permissive')
api.option('u1').value.set(1)
assert api.option('u1').value.get() == 1
api.property.pop('permissive')
cfg.property.add('permissive')
cfg.option('u1').value.set(1)
assert cfg.option('u1').value.get() == 1
cfg.property.pop('permissive')
try:
api.option('u1').value.set(1)
cfg.option('u1').value.set(1)
except PropertiesOptionError as err:
props = err.proptype
if TIRAMISU_VERSION == 2:
assert frozenset(props) == frozenset(['disabled', 'frozen'])
else:
assert frozenset(props) == frozenset(['disabled'])
def test_invalid_permissive():
descr = make_description()
api = Config(descr)
api.property.read_write()
raises(TypeError, "api.unrestraint.permissive.set(['frozen', 'disabled'])")
cfg = Config(descr)
cfg.property.read_write()
raises(TypeError, "cfg.unrestraint.permissive.set(['frozen', 'disabled'])")
def test_forbidden_permissive():
descr = make_description()
api = Config(descr)
api.property.read_write()
raises(ConfigError, "api.permissive.set(frozenset(['force_default_on_freeze']))")
raises(ConfigError, "api.permissive.set(frozenset(['force_metaconfig_on_freeze']))")
cfg = Config(descr)
cfg.property.read_write()
raises(ConfigError, "cfg.permissive.set(frozenset(['force_default_on_freeze']))")
raises(ConfigError, "cfg.permissive.set(frozenset(['force_metaconfig_on_freeze']))")
def test_permissive_option():
def test_permissive_option(config_type):
descr = make_description()
api = Config(descr)
api.property.read_write()
cfg_ori = Config(descr)
cfg_ori.property.read_write()
cfg = get_config(cfg_ori, config_type)
props = frozenset()
try:
api.option('u1').value.get()
cfg.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
props = frozenset()
try:
api.option('u2').value.get()
cfg.option('u2').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
api.unrestraint.option('u1').permissive.set(frozenset(['disabled']))
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.unrestraint.option('u1').permissive.set(frozenset(['disabled']))
cfg = get_config(cfg_ori, config_type)
props = frozenset()
try:
api.option('u1').value.get()
cfg.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert frozenset(props) == frozenset()
props = frozenset()
try:
api.option('u2').value.get()
cfg.option('u2').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
api.property.add('permissive')
api.option('u1').value.get()
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.property.add('permissive')
cfg = get_config(cfg_ori, config_type)
cfg.option('u1').value.get()
props = frozenset()
try:
api.option('u2').value.get()
cfg.option('u2').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
api.property.pop('permissive')
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.property.pop('permissive')
cfg = get_config(cfg_ori, config_type)
props = frozenset()
try:
api.option('u1').value.get()
cfg.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert frozenset(props) == frozenset()
props = frozenset()
try:
api.option('u2').value.get()
cfg.option('u2').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
@ -246,55 +264,55 @@ def test_permissive_option():
def test_permissive_option_cache():
descr = make_description()
api = Config(descr)
api.property.read_write()
cfg = Config(descr)
cfg.property.read_write()
props = frozenset()
try:
api.option('u1').value.get()
cfg.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
props = frozenset()
try:
api.option('u2').value.get()
cfg.option('u2').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
api.unrestraint.option('u1').permissive.set(frozenset(['disabled']))
cfg.unrestraint.option('u1').permissive.set(frozenset(['disabled']))
props = frozenset()
try:
api.option('u1').value.get()
cfg.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert frozenset(props) == frozenset()
props = frozenset()
try:
api.option('u2').value.get()
cfg.option('u2').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
api.property.add('permissive')
api.option('u1').value.get()
cfg.property.add('permissive')
cfg.option('u1').value.get()
props = frozenset()
try:
api.option('u2').value.get()
cfg.option('u2').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
api.property.pop('permissive')
cfg.property.pop('permissive')
props = frozenset()
try:
api.option('u1').value.get()
cfg.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert frozenset(props) == frozenset()
props = frozenset()
try:
api.option('u2').value.get()
cfg.option('u2').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
@ -302,78 +320,85 @@ def test_permissive_option_cache():
def test_permissive_option_mandatory():
descr = make_description()
api = Config(descr)
api.property.read_only()
cfg = Config(descr)
cfg.property.read_only()
props = frozenset()
try:
api.option('u1').value.get()
cfg.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
if TIRAMISU_VERSION == 2:
assert frozenset(props) == frozenset(['disabled', 'mandatory'])
else:
assert frozenset(props) == frozenset(['disabled'])
api.unrestraint.option('u1').permissive.set(frozenset(['mandatory', 'disabled']))
assert api.unrestraint.option('u1').permissive.get() == frozenset(['mandatory', 'disabled'])
api.property.add('permissive')
api.option('u1').value.get()
api.property.pop('permissive')
cfg.unrestraint.option('u1').permissive.set(frozenset(['mandatory', 'disabled']))
assert cfg.unrestraint.option('u1').permissive.get() == frozenset(['mandatory', 'disabled'])
cfg.property.add('permissive')
cfg.option('u1').value.get()
cfg.property.pop('permissive')
try:
api.option('u1').value.get()
cfg.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
if TIRAMISU_VERSION == 2:
assert frozenset(props) == frozenset(['disabled', 'mandatory'])
else:
assert frozenset(props) == frozenset(['disabled'])
def test_permissive_option_frozen():
descr = make_description()
api = Config(descr)
api.property.read_write()
api.unrestraint.option('u1').permissive.set(frozenset(['frozen', 'disabled']))
api.option('u1').value.set(1)
assert api.option('u1').value.get() == 1
api.property.add('permissive')
assert api.option('u1').value.get() == 1
api.property.pop('permissive')
assert api.option('u1').value.get() == 1
cfg = Config(descr)
cfg.property.read_write()
cfg.unrestraint.option('u1').permissive.set(frozenset(['frozen', 'disabled']))
cfg.option('u1').value.set(1)
assert cfg.option('u1').value.get() == 1
cfg.property.add('permissive')
assert cfg.option('u1').value.get() == 1
cfg.property.pop('permissive')
assert cfg.option('u1').value.get() == 1
if TIRAMISU_VERSION == 3:
def test_invalid_option_permissive():
descr = make_description()
api = Config(descr)
api.property.read_write()
raises(TypeError, "api.unrestraint.option('u1').permissive.set(['frozen', 'disabled'])")
cfg = Config(descr)
cfg.property.read_write()
raises(TypeError, "cfg.unrestraint.option('u1').permissive.set(['frozen', 'disabled'])")
def test_remove_option_permissive():
def test_remove_option_permissive(config_type):
var1 = UnicodeOption('var1', '', u'value', properties=('hidden',))
od1 = OptionDescription('od1', '', [var1])
rootod = OptionDescription('rootod', '', [od1])
api = Config(rootod)
api.property.read_write()
raises(PropertiesOptionError, "api.option('od1.var1').value.get()")
api.forcepermissive.option('od1.var1').permissive.set(frozenset(['hidden']))
assert api.forcepermissive.option('od1.var1').permissive.get() == frozenset(['hidden'])
assert api.option('od1.var1').value.get() == 'value'
api.forcepermissive.option('od1.var1').permissive.set(frozenset())
assert api.forcepermissive.option('od1.var1').permissive.get() == frozenset()
raises(PropertiesOptionError, "api.option('od1.var1').value.get()")
cfg_ori = Config(rootod)
cfg_ori.property.read_write()
cfg = get_config(cfg_ori, config_type)
raises(PropertiesOptionError, "cfg.option('od1.var1').value.get()")
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.forcepermissive.option('od1.var1').permissive.set(frozenset(['hidden']))
assert cfg_ori.forcepermissive.option('od1.var1').permissive.get() == frozenset(['hidden'])
cfg = get_config(cfg_ori, config_type)
assert cfg.option('od1.var1').value.get() == 'value'
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.forcepermissive.option('od1.var1').permissive.set(frozenset())
assert cfg_ori.forcepermissive.option('od1.var1').permissive.get() == frozenset()
cfg = get_config(cfg_ori, config_type)
raises(PropertiesOptionError, "cfg.option('od1.var1').value.get()")
def test_reset_option_permissive():
def test_reset_option_permissive(config_type):
var1 = UnicodeOption('var1', '', u'value', properties=('hidden',))
od1 = OptionDescription('od1', '', [var1])
rootod = OptionDescription('rootod', '', [od1])
api = Config(rootod)
api.property.read_write()
raises(PropertiesOptionError, "api.option('od1.var1').value.get()")
api.forcepermissive.option('od1.var1').permissive.set(frozenset(['hidden']))
assert api.forcepermissive.option('od1.var1').permissive.get() == frozenset(['hidden'])
assert api.option('od1.var1').value.get() == 'value'
api.forcepermissive.option('od1.var1').permissive.reset()
assert api.forcepermissive.option('od1.var1').permissive.get() == frozenset()
raises(PropertiesOptionError, "api.option('od1.var1').value.get()")
cfg_ori = Config(rootod)
cfg_ori.property.read_write()
cfg = get_config(cfg_ori, config_type)
raises(PropertiesOptionError, "cfg.option('od1.var1').value.get()")
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.forcepermissive.option('od1.var1').permissive.set(frozenset(['hidden']))
assert cfg_ori.forcepermissive.option('od1.var1').permissive.get() == frozenset(['hidden'])
cfg = get_config(cfg_ori, config_type)
assert cfg.option('od1.var1').value.get() == 'value'
if config_type == 'tiramisu-api':
cfg.send()
cfg_ori.forcepermissive.option('od1.var1').permissive.reset()
assert cfg_ori.forcepermissive.option('od1.var1').permissive.get() == frozenset()
cfg = get_config(cfg_ori, config_type)
raises(PropertiesOptionError, "cfg.option('od1.var1').value.get()")

File diff suppressed because it is too large Load diff

View file

@ -2,12 +2,12 @@
from py.test import raises
from .autopath import do_autopath
do_autopath()
from .config import config_type, get_config
from tiramisu import BoolOption, StrOption, SymLinkOption, \
OptionDescription, Leadership, Config
from tiramisu.error import PropertiesOptionError, ConfigError
from tiramisu.setting import groups, owners
from tiramisu.api import TIRAMISU_VERSION
from tiramisu.storage import list_sessions
@ -20,52 +20,55 @@ def return_value():
#____________________________________________________________
def test_symlink_option():
def test_symlink_option(config_type):
boolopt = BoolOption("b", "", default=False)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
api = Config(descr)
assert api.option('s1.b').value.get() is False
api.option("s1.b").value.set(True)
api.option("s1.b").value.set(False)
assert api.option('s1.b').value.get() is False
assert api.option('c').value.get() is False
api.option('s1.b').value.set(True)
assert api.option('s1.b').value.get() is True
assert api.option('c').value.get() is True
api.option('s1.b').value.set(False)
assert api.option('s1.b').value.get() is False
assert api.option('c').value.get() is False
cfg = Config(descr)
cfg = get_config(cfg, config_type)
assert cfg.option('s1.b').value.get() is False
cfg.option("s1.b").value.set(True)
cfg.option("s1.b").value.set(False)
assert cfg.option('s1.b').value.get() is False
assert cfg.option('c').value.get() is False
cfg.option('s1.b').value.set(True)
assert cfg.option('s1.b').value.get() is True
assert cfg.option('c').value.get() is True
cfg.option('s1.b').value.set(False)
assert cfg.option('s1.b').value.get() is False
assert cfg.option('c').value.get() is False
def test_symlink_assign_option():
def test_symlink_assign_option(config_type):
boolopt = BoolOption("b", "", default=False)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
api = Config(descr)
raises(ConfigError, "api.option('c').value.set(True)")
cfg = Config(descr)
cfg = get_config(cfg, config_type)
raises(ConfigError, "cfg.option('c').value.set(True)")
def test_symlink_del_option():
def test_symlink_del_option(config_type):
boolopt = BoolOption("b", "", default=False)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
api = Config(descr)
raises(TypeError, "api.option('c').value.reset()")
cfg = Config(descr)
cfg = get_config(cfg, config_type)
raises(ConfigError, "cfg.option('c').value.reset()")
def test_symlink_addproperties():
boolopt = BoolOption('b', '', default=True, properties=('test',))
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription('opt', '', [boolopt, linkopt])
api = Config(descr)
api.property.read_write()
raises(TypeError, "api.option('c').property.add('new')")
cfg = Config(descr)
cfg.property.read_write()
raises(TypeError, "cfg.option('c').property.add('new')")
try:
api.option('c').property.reset()
cfg.option('c').property.reset()
except AssertionError:
pass
else:
@ -76,21 +79,21 @@ def test_symlink_getpermissive():
boolopt = BoolOption('b', '', default=True, properties=('test',))
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription('opt', '', [boolopt, linkopt])
api = Config(descr)
api.property.read_write()
api.option('b').permissive.set(frozenset(['perm']))
api.option('c').permissive.get() == frozenset(['perm'])
cfg = Config(descr)
cfg.property.read_write()
cfg.option('b').permissive.set(frozenset(['perm']))
cfg.option('c').permissive.get() == frozenset(['perm'])
def test_symlink_addpermissives():
boolopt = BoolOption('b', '', default=True, properties=('test',))
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription('opt', '', [boolopt, linkopt])
api = Config(descr)
api.property.read_write()
raises(TypeError, "api.option('c').permissive.set(frozenset(['new']))")
cfg = Config(descr)
cfg.property.read_write()
raises(TypeError, "cfg.option('c').permissive.set(frozenset(['new']))")
try:
api.option('c').permissive.reset()
cfg.option('c').permissive.reset()
except AssertionError:
pass
else:
@ -101,11 +104,8 @@ def test_symlink_getproperties():
boolopt = BoolOption('b', '', default=True, properties=('test',))
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription('opt', '', [boolopt, linkopt])
api = Config(descr)
api.property.read_write()
if TIRAMISU_VERSION == 2:
assert boolopt.impl_getproperties() == linkopt.impl_getproperties() == ('test',)
else:
cfg = Config(descr)
cfg.property.read_write()
assert boolopt.impl_getproperties() == linkopt.impl_getproperties() == {'test'}
assert boolopt.impl_has_callback() == linkopt.impl_has_callback() == False
@ -114,89 +114,86 @@ def test_symlink_getcallback():
boolopt = BoolOption('b', '', callback=return_value)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription('opt', '', [boolopt, linkopt])
api = Config(descr)
api.property.read_write()
cfg = Config(descr)
cfg.property.read_write()
assert boolopt.impl_has_callback() == linkopt.impl_has_callback() == True
assert boolopt.impl_get_callback() == linkopt.impl_get_callback() == (return_value, None)
def test_symlink_requires():
def test_symlink_requires(config_type):
boolopt = BoolOption('b', '', default=True)
stropt = StrOption('s', '', requires=[{'option': boolopt,
'expected': False,
'action': 'disabled'}])
linkopt = SymLinkOption("c", stropt)
descr = OptionDescription('opt', '', [boolopt, stropt, linkopt])
api = Config(descr)
api.property.read_write()
assert api.option('b').value.get() is True
assert api.option('s').value.get() is None
assert api.option('c').value.get() is None
api.option('b').value.set(False)
cfg = Config(descr)
cfg.property.read_write()
cfg = get_config(cfg, config_type)
assert cfg.option('b').value.get() is True
assert cfg.option('s').value.get() is None
assert cfg.option('c').value.get() is None
cfg.option('b').value.set(False)
#
props = []
try:
api.option('s').value.get()
cfg.option('s').value.get()
except PropertiesOptionError as err:
props = err.proptype
if TIRAMISU_VERSION == 2:
assert props == ['disabled']
else:
assert props == {'disabled'}
#
props = []
try:
api.option('c').value.get()
cfg.option('c').value.get()
except PropertiesOptionError as err:
props = err.proptype
if TIRAMISU_VERSION == 2:
assert props == ['disabled']
else:
assert props == {'disabled'}
def test_symlink_multi():
def test_symlink_multi(config_type):
boolopt = BoolOption("b", "", default=[False], multi=True)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
api = Config(descr)
assert api.option('s1.b').value.get() == [False]
assert api.option('c').value.get() == [False]
api.option('s1.b').value.set([True])
assert api.option('s1.b').value.get() == [True]
assert api.option('c').value.get() == [True]
api.option('s1.b').value.set([False])
assert api.option('s1.b').value.get() == [False]
assert api.option('c').value.get() == [False]
api.option('s1.b').value.set([False, True])
assert api.option('s1.b').value.get() == [False, True]
assert api.option('c').value.get() == [False, True]
cfg = Config(descr)
cfg = get_config(cfg, config_type)
assert cfg.option('s1.b').value.get() == [False]
assert cfg.option('c').value.get() == [False]
cfg.option('s1.b').value.set([True])
assert cfg.option('s1.b').value.get() == [True]
assert cfg.option('c').value.get() == [True]
cfg.option('s1.b').value.set([False])
assert cfg.option('s1.b').value.get() == [False]
assert cfg.option('c').value.get() == [False]
cfg.option('s1.b').value.set([False, True])
assert cfg.option('s1.b').value.get() == [False, True]
assert cfg.option('c').value.get() == [False, True]
assert boolopt.impl_is_multi() is True
assert linkopt.impl_is_multi() is True
def test_symlink_assign():
if TIRAMISU_VERSION != 2:
def test_symlink_assign(config_type):
boolopt = BoolOption("b", "", default=False)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
api = Config(descr)
raises(ConfigError, "api.option('c').value.set(True)")
cfg = Config(descr)
cfg = get_config(cfg, config_type)
raises(ConfigError, "cfg.option('c').value.set(True)")
def test_symlink_owner():
def test_symlink_owner(config_type):
boolopt = BoolOption("b", "", default=False)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
api = Config(descr)
assert api.option('s1.b').owner.isdefault()
assert api.option('c').owner.isdefault()
api.option('s1.b').value.set(True)
assert not api.option('s1.b').owner.isdefault()
assert not api.option('c').owner.isdefault()
cfg = Config(descr)
cfg = get_config(cfg, config_type)
assert cfg.option('s1.b').owner.isdefault()
assert cfg.option('c').owner.isdefault()
cfg.option('s1.b').value.set(True)
assert not cfg.option('s1.b').owner.isdefault()
assert not cfg.option('c').owner.isdefault()
def test_symlink_get_information():
@ -224,41 +221,44 @@ def test_symlink_followers():
raises(ValueError, "Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])")
def test_symlink_with_leader():
def test_symlink_with_leader(config_type):
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip réseau autorisé", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-réseau", multi=True)
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
leader = SymLinkOption('leader', ip_admin_eth0)
od = OptionDescription('root', '', [interface1, leader])
api = Config(od)
assert api.value.dict() == {'ip_admin_eth0.ip_admin_eth0': [], 'ip_admin_eth0.netmask_admin_eth0': [], 'leader': []}
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['val1', 'val2'])
assert api.value.dict() == {'ip_admin_eth0.ip_admin_eth0': ['val1', 'val2'], 'ip_admin_eth0.netmask_admin_eth0': [None, None], 'leader': ['val1', 'val2']}
cfg = Config(od)
cfg = get_config(cfg, config_type)
assert cfg.value.dict() == {'ip_admin_eth0.ip_admin_eth0': [], 'ip_admin_eth0.netmask_admin_eth0': [], 'leader': []}
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val1', 'val2'])
assert cfg.value.dict() == {'ip_admin_eth0.ip_admin_eth0': ['val1', 'val2'], 'ip_admin_eth0.netmask_admin_eth0': [None, None], 'leader': ['val1', 'val2']}
def test_symlink_with_follower():
def test_symlink_with_follower(config_type):
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip réseau autorisé", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-réseau", multi=True)
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
follower = SymLinkOption('follower', netmask_admin_eth0)
od = OptionDescription('root', '', [interface1, follower])
api = Config(od)
assert api.value.dict() == {'ip_admin_eth0.ip_admin_eth0': [], 'ip_admin_eth0.netmask_admin_eth0': [], 'follower': []}
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['val1', 'val2'])
assert api.value.dict() == {'ip_admin_eth0.ip_admin_eth0': ['val1', 'val2'], 'ip_admin_eth0.netmask_admin_eth0': [None, None], 'follower': [None, None]}
cfg = Config(od)
cfg = get_config(cfg, config_type)
assert cfg.value.dict() == {'ip_admin_eth0.ip_admin_eth0': [], 'ip_admin_eth0.netmask_admin_eth0': [], 'follower': []}
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val1', 'val2'])
print(cfg.value.dict())
assert cfg.value.dict() == {'ip_admin_eth0.ip_admin_eth0': ['val1', 'val2'], 'ip_admin_eth0.netmask_admin_eth0': [None, None], 'follower': [None, None]}
#
assert api.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == None
assert api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.get() == None
assert api.option('follower', 0).value.get() == None
assert api.option('follower', 1).value.get() == None
assert cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == None
assert cfg.option('ip_admin_eth0.netmask_admin_eth0', 1).value.get() == None
assert cfg.option('follower', 0).value.get() == None
assert cfg.option('follower', 1).value.get() == None
#
api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('val3')
assert api.value.dict() == {'ip_admin_eth0.ip_admin_eth0': ['val1', 'val2'], 'ip_admin_eth0.netmask_admin_eth0': [None, 'val3'], 'follower': [None, 'val3']}
cfg.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('val3')
assert cfg.value.dict() == {'ip_admin_eth0.ip_admin_eth0': ['val1', 'val2'], 'ip_admin_eth0.netmask_admin_eth0': [None, 'val3'], 'follower': [None, 'val3']}
#
assert api.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == None
assert api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.get() == 'val3'
assert api.option('follower', 0).value.get() == None
assert api.option('follower', 1).value.get() == 'val3'
assert cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == None
assert cfg.option('ip_admin_eth0.netmask_admin_eth0', 1).value.get() == 'val3'
assert cfg.option('follower', 0).value.get() == None
assert cfg.option('follower', 1).value.get() == 'val3'
#____________________________________________________________
@ -267,36 +267,38 @@ def test_symlink_dependency():
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
api = Config(descr)
assert api.option('s1.b').option.has_dependency() is False
assert api.option('c').option.has_dependency() is True
assert api.option('s1.b').option.has_dependency(False) is True
assert api.option('c').option.has_dependency(False) is False
cfg = Config(descr)
assert cfg.option('s1.b').option.has_dependency() is False
assert cfg.option('c').option.has_dependency() is True
assert cfg.option('s1.b').option.has_dependency(False) is True
assert cfg.option('c').option.has_dependency(False) is False
def test_symlink_makedict():
def test_symlink_makedict(config_type):
boolopt = BoolOption("b", "", default=False)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
api = Config(descr)
assert api.value.dict() == {'c': False, 's1.b': False}
api.option('s1.b').value.set(True)
assert api.value.dict() == {'c': True, 's1.b': True}
cfg = Config(descr)
cfg = get_config(cfg, config_type)
assert cfg.value.dict() == {'c': False, 's1.b': False}
cfg.option('s1.b').value.set(True)
assert cfg.value.dict() == {'c': True, 's1.b': True}
def test_symlink_list():
def test_symlink_list(config_type):
boolopt = BoolOption("b", "", default=False)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
api = Config(descr)
cfg = Config(descr)
cfg = get_config(cfg, config_type)
list_opt = []
for opt in api.option.list():
for opt in cfg.option.list():
list_opt.append(opt.option.path())
assert list_opt == ['c']
#
list_opt = []
for opt in api.option.list(recursive=True):
for opt in cfg.option.list(recursive=True):
list_opt.append(opt.option.path())
assert list_opt == ['c', 's1.b']

View file

@ -278,6 +278,11 @@ class _TiramisuOptionOption(_TiramisuOptionOptionDescription):
option = self._option_bag.option
return option.impl_get_callback()
def validator(self):
"""Get validator for an option (not for optiondescription)"""
option = self._option_bag.option
return option.impl_get_validator()
def pattern(self) -> str:
option = self._option_bag.option
type = option.get_type()
@ -374,7 +379,7 @@ class TiramisuOptionProperty(CommonTiramisuOption):
only_raises=False):
"""Get properties for an option"""
option = self._option_bag.option
self._test_follower_index()
#self._test_follower_index()
if not only_raises:
return self._option_bag.properties
# do not check cache properties/permissives which are not save (unrestraint, ...)
@ -677,7 +682,6 @@ class _TiramisuOption(CommonTiramisu):
if subfunc in self._registers:
subconfig = self._subconfig
if subconfig:
option_bag = self._option_bag
option = self._get_option()
if option.impl_is_optiondescription() and subfunc == 'option':
config = self._get_config()

View file

@ -249,7 +249,7 @@ class SubConfig(object):
_commit=True):
option = option_bag.option
if option.impl_is_symlinkoption():
raise TypeError(_("can't delete a SymLinkOption"))
raise ConfigError(_("can't delete a SymLinkOption"))
values = self.cfgimpl_get_values()
if option_bag.index is not None:
values.reset_follower(option_bag,

View file

@ -85,8 +85,10 @@ class PropertiesOptionError(AttributeError):
def __str__(self):
#this part is a bit slow, so only execute when display
if self.msg:
if self.msg is not None:
return self.msg
if self._settings is None:
return 'error'
req = self._settings.apply_requires(self._option_bag,
True)
#if req != {} or self._orig_opt is not None:

View file

@ -183,6 +183,7 @@ class ConfigBag:
__slots__ = ('context', # link to the current context
'properties', # properties for current context
'true_properties', # properties for current context
'is_unrestraint',
'permissives', # permissives for current context
'expiration_time' # EXPIRATION_TIME
)
@ -206,6 +207,8 @@ class ConfigBag:
if key == 'expiration_time':
self.expiration_time = EXPIRATION_TIME
return self.expiration_time
if key == 'is_unrestraint':
return False
raise KeyError('unknown key {} for ConfigBag'.format(key)) # pragma: no cover
def remove_warnings(self):
@ -215,6 +218,7 @@ class ConfigBag:
self.properties = frozenset(self.properties - {'validator'})
def unrestraint(self):
self.is_unrestraint = True
self.true_properties = self.properties
self.properties = frozenset(['cache'])
@ -437,7 +441,8 @@ class Settings(object):
search_properties=search_properties)
props -= self.getpermissives(opt,
path)
if apply_requires:
#if apply_requires and config_bag.properties == config_bag.true_properties:
if apply_requires and not config_bag.is_unrestraint:
self._p_.setcache(path,
index,
props,
@ -761,15 +766,23 @@ class Settings(object):
def calc_raises_properties(self,
option_bag,
apply_requires=True):
raises_properties = option_bag.config_bag.properties - SPECIAL_PROPERTIES
# remove global permissive properties
if raises_properties and ('permissive' in raises_properties):
raises_properties -= option_bag.config_bag.permissives
if apply_requires and option_bag.properties_setted:
option_properties = option_bag.properties
else:
option_properties = self.getproperties(option_bag,
apply_requires=apply_requires)
return self._calc_raises_properties(option_bag.config_bag.properties,
option_bag.config_bag.permissives,
option_properties)
def _calc_raises_properties(self,
context_properties,
context_permissives,
option_properties):
raises_properties = context_properties - SPECIAL_PROPERTIES
# remove global permissive properties
if raises_properties and ('permissive' in raises_properties):
raises_properties -= context_permissives
properties = option_properties & raises_properties
# at this point an option should not remain in properties
return properties

View file

@ -30,8 +30,6 @@ INPUTS = ['string',
'ip',
'domainname']
ACTION_HIDE = ['hidden', 'disabled']
# return always warning (even if same warning is already returned)
warnings.simplefilter("always", ValueWarning)
@ -137,36 +135,63 @@ class Requires(object):
self.requires = {}
self.options = {}
self.tiramisu_web = tiramisu_web
self.action_hide = self.tiramisu_web.config._config_bag.properties
def manage_requires(self,
childapi,
path,
form,
action_hide,
current_action):
for requires in childapi.option.requires():
len_to_long = len(requires) > 1
if childapi.option.isoptiondescription():
isfollower = False
else:
isfollower = childapi.option.isfollower()
if isfollower:
parent_path = path.rsplit('.', 1)[0]
parent = self.tiramisu_web.config.unrestraint.option(parent_path)
leader = next(parent.list())
self.tiramisu_web.set_remotable(leader.option.path(), form, leader)
for require in requires:
options, action, inverse, \
transitive, same_action, operator = require
options, action, inverse, transitive, same_action, operator = require
if not len_to_long:
len_to_long = len(options) > 1
for option, expected in options:
if isinstance(option, tuple):
for option_param in chain(option[1].args, option[1].kwargs.values()):
if isinstance(option_param, ParamOption):
self.tiramisu_web.set_remotable(option_param.option.impl_getpath(), form)
elif len_to_long:
self.tiramisu_web.set_remotable(option.impl_getpath(), form)
else:
option_path = option.impl_getpath()
if action in self.action_hide:
if childapi.option.isoptiondescription() or isfollower:
self.tiramisu_web.set_remotable(option_path, form)
continue
require_option = self.tiramisu_web.config.unrestraint.option(option_path)
if transitive is False or same_action is False or operator == 'and':
# transitive to "False" not supported yet for a requirement
# same_action to "False" not supported yet for a requirement
# operator "and" not supported yet for a requirement
self.tiramisu_web.set_remotable(path, form, childapi)
return
for option, expected in options:
option_path = option.impl_getpath()
if action in action_hide:
self.tiramisu_web.set_remotable(option_path, form, require_option)
if require_option.option.requires():
for reqs in require_option.option.requires():
for req in reqs:
for subopt, subexp in req[0]:
if not isinstance(subopt, tuple):
self.tiramisu_web.set_remotable(subopt.impl_getpath(), form)
if isinstance(option, ChoiceOption):
choice_obj = self.tiramisu_web.config.unrestraint.option(option_path)
if choice_obj.value.is_values_callback():
self.tiramisu_web.set_remotable(option_path, form, choice_obj)
return
require_option = self.tiramisu_web.config.unrestraint.option(option_path)
if require_option.value.callbacks():
self.tiramisu_web.set_remotable(option_path, form, require_option)
continue
else:
values = self.tiramisu_web.get_enum(choice_obj,
choice_obj.option.ismulti(),
values = self.tiramisu_web.get_enum(require_option,
require_option.option.ismulti(),
option_path,
choice_obj.option.properties())
require_option.option.properties())
for value in values:
if value not in expected:
self.requires.setdefault(path,
@ -205,7 +230,6 @@ class Requires(object):
self.manage_requires(childapi,
path,
form,
ACTION_HIDE,
current_action)
def is_remote(self, path, form):
@ -217,8 +241,6 @@ class Requires(object):
def process(self, form):
dependencies = {}
for path, values in self.requires.items():
if form.get(path, {}).get('remote') is True:
continue
if 'default' in values:
for option in values['default'].get('show', []):
if path == option:
@ -262,7 +284,7 @@ class Requires(object):
if path not in dependencies[option]['expected'][expected]['hide']:
dependencies[option]['expected'][expected]['hide'].append(path)
for path, dependency in dependencies.items():
form[path]['dependencies'] = dependency
form.setdefault(path, {})['dependencies'] = dependency
class TiramisuDict:
@ -375,7 +397,6 @@ class TiramisuDict:
childapi,
path,
leader_len,
props_no_requires,
updates_status)
if order is not None:
order.append(path)
@ -405,6 +426,9 @@ class TiramisuDict:
childtype = child._impl_getopt().__class__.__name__
if childapi_option.issymlinkoption():
web_type = 'symlink'
value = None
defaultmulti = None
is_multi = False
else:
web_type = childapi_option.type()
value = childapi.option.default()
@ -443,6 +467,8 @@ class TiramisuDict:
self.add_help(schema[path],
childapi)
except Exception as err:
import traceback
traceback.print_exc()
if not init:
raise err
error = err
@ -524,6 +550,15 @@ class TiramisuDict:
if path in form:
obj_form.update(form[path])
if not childapi_option.issymlinkoption():
if childapi_option.validator() != (None, None):
obj_form['remote'] = True
params = childapi_option.validator()[1]
if params is not None:
for param in chain(params.args, params.kwargs.values()):
if isinstance(param, ParamContext):
raise ValueError(_('context is not supported from now for {}').format(path))
if isinstance(param, ParamOption):
self.set_remotable(param.option.impl_getpath(), form)
if self.clearable == 'all':
obj_form['clearable'] = True
if self.clearable != 'none':
@ -532,6 +567,8 @@ class TiramisuDict:
obj_form['remote'] = True
if childtype == 'IPOption' and (child.impl_get_extra('_private_only') or not child.impl_get_extra('_allow_reserved') or child.impl_get_extra('_cidr')):
obj_form['remote'] = True
if childtype == 'DateOption':
obj_form['remote'] = True
if not obj_form.get('remote', False):
pattern = childapi_option.pattern()
if pattern is not None:
@ -553,27 +590,31 @@ class TiramisuDict:
childapi):
old_properties = childapi._option_bag.config_bag.properties
del childapi._option_bag.config_bag.properties
if 'permissive' not in childapi._option_bag.config_bag.properties:
has_permissive = 'permissive' in childapi._option_bag.config_bag.properties
if not has_permissive:
childapi._option_bag.config_bag.properties = childapi._option_bag.config_bag.properties | {'permissive'}
# 'display=False' means cannot access only without permissive option
# 'hidden=True' means cannot access with or without permissive option
if childapi.option.properties(only_raises=True):
properties = childapi.property.get(only_raises=True)
if has_permissive:
properties -= self.config.permissive.get()
properties -= childapi.permissive.get()
if properties:
obj['hidden'] = True
childapi._option_bag.config_bag.properties = childapi._option_bag.config_bag.properties - {'permissive'}
if childapi.option.properties(only_raises=True):
properties = childapi.property.get(only_raises=True)
if has_permissive:
properties -= self.config.permissive.get()
properties -= childapi.permissive.get()
if properties:
obj['display'] = False
childapi._option_bag.config_bag.properties = old_properties
def _gen_model_properties(self,
childapi,
path,
index,
props_no_requires):
index):
isfollower = childapi.option.isfollower()
if index is None and isfollower:
# cannot calculated requires with follower without index
props = props_no_requires
else:
props = set(childapi.property.get())
obj = self.gen_properties(props,
isfollower,
@ -589,22 +630,16 @@ class TiramisuDict:
if not isfollower and ismulti:
if 'empty' in properties:
obj['required'] = True
properties.remove('empty')
if 'mandatory' in properties:
obj['needs_len'] = True
properties.remove('mandatory')
elif 'mandatory' in properties:
obj['required'] = True
properties.remove('mandatory')
if 'frozen' in properties:
obj['readOnly'] = True
properties.remove('frozen')
#if 'hidden' in properties:
# obj['hidden'] = True
# properties.remove('hidden')
#if 'disabled' in properties:
# obj['hidden'] = True
# properties.remove('disabled')
if properties:
lprops = list(properties)
lprops.sort()
@ -616,7 +651,6 @@ class TiramisuDict:
childapi,
path,
leader_len,
props_no_requires,
updates_status):
if childapi.option.isoptiondescription():
props = set(childapi.property.get())
@ -633,15 +667,13 @@ class TiramisuDict:
else:
obj = self._gen_model_properties(childapi,
path,
None,
props_no_requires)
None)
if childapi.option.isfollower():
for index in range(leader_len):
follower_childapi = self.config.unrestraint.option(path, index)
sobj = self._gen_model_properties(follower_childapi,
path,
index,
props_no_requires)
index)
self._get_model_value(follower_childapi,
path,
sobj,
@ -657,7 +689,7 @@ class TiramisuDict:
updates_status)
if obj:
if not childapi.option.isoptiondescription() and childapi.option.isfollower():
model.setdefault(path, {})[None] = obj
model.setdefault(path, {})['null'] = obj
else:
model[path] = obj
@ -675,9 +707,8 @@ class TiramisuDict:
del updates_status[path][index]
else:
try:
nchildapi = self.config.option(path, index=index)
with warnings.catch_warnings(record=True) as warns:
value = nchildapi.value.get()
value = self.config.option(path, index=index).value.get()
self._get_value_with_exception(obj,
childapi,
warns)
@ -687,6 +718,13 @@ class TiramisuDict:
[err])
value = self.config.unrestraint.option(path, index=index).value.get()
except PropertiesOptionError as err:
config_bag = self.config._config_bag
settings = config_bag.context.cfgimpl_get_settings()
if settings._calc_raises_properties(config_bag.properties,
config_bag.permissives,
set(err.proptype)):
obj['hidden'] = True
obj['display'] = False
value = childapi.value.get()
if value is not None and value != []:
obj['value'] = value
@ -852,7 +890,6 @@ class TiramisuDict:
buttons = []
else:
form = None
ret = {}
self.walk(rootpath,
None,
schema,