2013-09-19 21:38:46 +02:00
|
|
|
import autopath
|
2013-09-26 21:56:06 +02:00
|
|
|
import warnings
|
2013-09-19 21:38:46 +02:00
|
|
|
from py.test import raises
|
|
|
|
|
|
|
|
from tiramisu.config import Config
|
|
|
|
from tiramisu.option import StrOption, OptionDescription
|
2013-09-25 21:10:45 +02:00
|
|
|
from tiramisu.setting import groups
|
2013-09-26 21:56:06 +02:00
|
|
|
from tiramisu.error import ValueWarning
|
2014-01-27 14:55:53 +01:00
|
|
|
from tiramisu.i18n import _
|
2013-09-19 21:38:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
def return_true(value, param=None):
|
|
|
|
if value == 'val' and param in [None, 'yes']:
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def return_false(value, param=None):
|
|
|
|
if value == 'val' and param in [None, 'yes']:
|
2013-09-24 23:19:20 +02:00
|
|
|
raise ValueError('error')
|
2013-09-19 21:38:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
def return_val(value, param=None):
|
|
|
|
return 'val'
|
|
|
|
|
|
|
|
|
2013-09-24 23:19:20 +02:00
|
|
|
def return_if_val(value):
|
|
|
|
if value != 'val':
|
|
|
|
raise ValueError('error')
|
|
|
|
|
|
|
|
|
2013-11-23 23:34:17 +01:00
|
|
|
#FIXME il y a une validation sur default_multi ?
|
|
|
|
|
2013-09-19 21:38:46 +02:00
|
|
|
def test_validator():
|
|
|
|
opt1 = StrOption('opt1', '', validator=return_true, default='val')
|
|
|
|
raises(ValueError, "StrOption('opt2', '', validator=return_false, default='val')")
|
|
|
|
opt2 = StrOption('opt2', '', validator=return_false)
|
2013-09-24 23:19:20 +02:00
|
|
|
root = OptionDescription('root', '', [opt1, opt2])
|
2013-09-19 21:38:46 +02:00
|
|
|
cfg = Config(root)
|
|
|
|
assert cfg.opt1 == 'val'
|
|
|
|
raises(ValueError, "cfg.opt2 = 'val'")
|
|
|
|
|
|
|
|
|
|
|
|
def test_validator_params():
|
|
|
|
opt1 = StrOption('opt1', '', validator=return_true, validator_params={'': ('yes',)}, default='val')
|
|
|
|
raises(ValueError, "StrOption('opt2', '', validator=return_false, validator_params={'': ('yes',)}, default='val')")
|
|
|
|
opt2 = StrOption('opt2', '', validator=return_false, validator_params={'': ('yes',)})
|
2013-09-24 23:19:20 +02:00
|
|
|
root = OptionDescription('root', '', [opt1, opt2])
|
2013-09-19 21:38:46 +02:00
|
|
|
cfg = Config(root)
|
|
|
|
assert cfg.opt1 == 'val'
|
|
|
|
raises(ValueError, "cfg.opt2 = 'val'")
|
|
|
|
|
|
|
|
|
|
|
|
def test_validator_params_key():
|
|
|
|
opt1 = StrOption('opt1', '', validator=return_true, validator_params={'param': ('yes',)}, default='val')
|
|
|
|
raises(TypeError, "StrOption('opt2', '', validator=return_true, validator_params={'param_unknown': ('yes',)}, default='val')")
|
|
|
|
root = OptionDescription('root', '', [opt1])
|
|
|
|
cfg = Config(root)
|
|
|
|
assert cfg.opt1 == 'val'
|
|
|
|
|
|
|
|
|
|
|
|
def test_validator_params_option():
|
|
|
|
opt0 = StrOption('opt0', '', default='val')
|
|
|
|
raises(ValueError, "opt1 = StrOption('opt1', '', validator=return_true, validator_params={'': ((opt0, False),)}, default='val')")
|
2013-09-24 23:19:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_validator_multi():
|
|
|
|
opt1 = StrOption('opt1', '', validator=return_if_val, multi=True)
|
|
|
|
root = OptionDescription('root', '', [opt1])
|
|
|
|
cfg = Config(root)
|
|
|
|
assert cfg.opt1 == []
|
|
|
|
cfg.opt1.append('val')
|
|
|
|
assert cfg.opt1 == ['val']
|
|
|
|
raises(ValueError, "cfg.opt1.append('val1')")
|
|
|
|
raises(ValueError, "cfg.opt1 = ['val', 'val1']")
|
|
|
|
|
|
|
|
|
|
|
|
def test_validator_warning():
|
2013-09-27 09:52:18 +02:00
|
|
|
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)
|
2013-09-24 23:19:20 +02:00
|
|
|
root = OptionDescription('root', '', [opt1, opt2, opt3])
|
|
|
|
cfg = Config(root)
|
|
|
|
assert cfg.opt1 == 'val'
|
2013-09-26 21:56:06 +02:00
|
|
|
warnings.simplefilter("always", ValueWarning)
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
cfg.opt1 = 'val'
|
|
|
|
assert w == []
|
|
|
|
#
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
cfg.opt2 = 'val'
|
|
|
|
assert len(w) == 1
|
|
|
|
assert w[0].message.opt == opt2
|
2014-03-12 21:56:53 +01:00
|
|
|
assert str(w[0].message) == _("warning on the value of the option {0}: {1}").format('opt2', 'error')
|
2013-09-26 21:56:06 +02:00
|
|
|
#
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
cfg.opt3.append('val')
|
|
|
|
assert w == []
|
|
|
|
#
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
cfg.opt3.append('val1')
|
|
|
|
assert len(w) == 1
|
|
|
|
assert w[0].message.opt == opt3
|
2014-03-12 21:56:53 +01:00
|
|
|
assert str(w[0].message) == _("warning on the value of the option {0}: {1}").format('opt3', 'error')
|
2013-09-24 23:19:20 +02:00
|
|
|
raises(ValueError, "cfg.opt2 = 1")
|
2013-09-26 21:56:06 +02:00
|
|
|
#
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
cfg.opt2 = 'val'
|
|
|
|
cfg.opt3.append('val')
|
|
|
|
assert len(w) == 2
|
|
|
|
assert w[0].message.opt == opt2
|
2014-03-12 21:56:53 +01:00
|
|
|
assert str(w[0].message) == _("warning on the value of the option {0}: {1}").format('opt2', 'error')
|
2013-09-26 21:56:06 +02:00
|
|
|
assert w[1].message.opt == opt3
|
2014-03-12 21:56:53 +01:00
|
|
|
assert str(w[1].message) == _("warning on the value of the option {0}: {1}").format('opt3', 'error')
|
2013-09-25 21:10:45 +02:00
|
|
|
|
|
|
|
|
2015-04-18 23:11:57 +02:00
|
|
|
def test_validator_warning_disabled():
|
|
|
|
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])
|
|
|
|
cfg = Config(root)
|
|
|
|
cfg.cfgimpl_get_settings().remove('warnings')
|
|
|
|
assert cfg.opt1 == 'val'
|
|
|
|
warnings.simplefilter("always", ValueWarning)
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
cfg.opt1 = 'val'
|
|
|
|
assert w == []
|
|
|
|
#
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
cfg.opt2 = 'val'
|
|
|
|
assert w == []
|
|
|
|
#
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
cfg.opt3.append('val')
|
|
|
|
assert w == []
|
|
|
|
#
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
cfg.opt3.append('val1')
|
|
|
|
assert w == []
|
|
|
|
raises(ValueError, "cfg.opt2 = 1")
|
|
|
|
#
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
cfg.opt2 = 'val'
|
|
|
|
cfg.opt3.append('val')
|
|
|
|
assert w == []
|
|
|
|
|
|
|
|
|
2013-09-25 21:10:45 +02:00
|
|
|
def test_validator_warning_master_slave():
|
2013-09-27 09:52:18 +02:00
|
|
|
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip reseau autorise", multi=True, validator=return_false, warnings_only=True)
|
|
|
|
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-reseau", multi=True, validator=return_if_val, warnings_only=True)
|
2013-09-25 21:10:45 +02:00
|
|
|
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
|
|
|
|
interface1.impl_set_group_type(groups.master)
|
|
|
|
assert interface1.impl_get_group_type() == groups.master
|
|
|
|
root = OptionDescription('root', '', [interface1])
|
|
|
|
cfg = Config(root)
|
2013-09-26 21:56:06 +02:00
|
|
|
warnings.simplefilter("always", ValueWarning)
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
cfg.ip_admin_eth0.ip_admin_eth0.append(None)
|
|
|
|
assert w == []
|
|
|
|
#
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
cfg.ip_admin_eth0.netmask_admin_eth0 = ['val1']
|
|
|
|
assert len(w) == 1
|
|
|
|
assert w[0].message.opt == netmask_admin_eth0
|
2014-03-12 21:56:53 +01:00
|
|
|
assert str(w[0].message) == _("warning on the value of the option {0}: {1}").format('netmask_admin_eth0', 'error')
|
2013-09-26 21:56:06 +02:00
|
|
|
#
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
cfg.ip_admin_eth0.ip_admin_eth0 = ['val']
|
|
|
|
assert len(w) == 1
|
|
|
|
assert w[0].message.opt == ip_admin_eth0
|
2014-03-12 21:56:53 +01:00
|
|
|
assert str(w[0].message) == _("warning on the value of the option {0}: {1}").format('ip_admin_eth0', 'error')
|
2013-09-26 21:56:06 +02:00
|
|
|
#
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
cfg.ip_admin_eth0.ip_admin_eth0 = ['val', 'val1', 'val1']
|
|
|
|
assert len(w) == 1
|
|
|
|
assert w[0].message.opt == ip_admin_eth0
|
2014-03-12 21:56:53 +01:00
|
|
|
assert str(w[0].message) == _("warning on the value of the option {0}: {1}").format('ip_admin_eth0', 'error')
|
2013-09-26 21:56:06 +02:00
|
|
|
#
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
cfg.ip_admin_eth0.ip_admin_eth0 = ['val1', 'val', 'val1']
|
|
|
|
assert len(w) == 1
|
|
|
|
assert w[0].message.opt == ip_admin_eth0
|
2014-03-12 21:56:53 +01:00
|
|
|
assert str(w[0].message) == _("warning on the value of the option {0}: {1}").format('ip_admin_eth0', 'error')
|
2013-09-26 21:56:06 +02:00
|
|
|
#
|
|
|
|
warnings.resetwarnings()
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
cfg.ip_admin_eth0.ip_admin_eth0 = ['val1', 'val1', 'val']
|
|
|
|
assert len(w) == 1
|
|
|
|
assert w[0].message.opt == ip_admin_eth0
|
2014-03-12 21:56:53 +01:00
|
|
|
assert str(w[0].message) == _("warning on the value of the option {0}: {1}").format('ip_admin_eth0', 'error')
|