add _second_level_validation (second's one return only warning almost _validator raise)

This commit is contained in:
Emmanuel Garette 2013-09-25 21:10:45 +02:00
parent 4e0f0a5b70
commit 329b9ac349
2 changed files with 31 additions and 1 deletions

View File

@ -3,6 +3,7 @@ from py.test import raises
from tiramisu.config import Config
from tiramisu.option import StrOption, OptionDescription
from tiramisu.setting import groups
def return_true(value, param=None):
@ -88,3 +89,22 @@ def test_validator_warning():
assert cfg.cfgimpl_get_values().get_last_warning() == 'invalid value val1 for option opt3: error'
assert cfg.cfgimpl_get_values().has_warning() is False
raises(ValueError, "cfg.opt2 = 1")
def test_validator_warning_master_slave():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip reseau autorise", multi=True, validator=return_false, only_warning=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-reseau", multi=True, validator=return_if_val, only_warning=True)
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)
cfg.ip_admin_eth0.ip_admin_eth0.append(None)
assert cfg.cfgimpl_get_values().has_warning() is False
cfg.ip_admin_eth0.netmask_admin_eth0 = ['val1']
assert cfg.ip_admin_eth0.netmask_admin_eth0 == ['val1']
assert cfg.cfgimpl_get_values().has_warning() is True
assert cfg.cfgimpl_get_values().get_last_warning() == 'invalid value val1 for option netmask_admin_eth0: error'
cfg.ip_admin_eth0.ip_admin_eth0 = ['val']
assert cfg.ip_admin_eth0.ip_admin_eth0 == ['val']
assert cfg.cfgimpl_get_values().get_last_warning() == 'invalid value val for option ip_admin_eth0: error'

View File

@ -473,7 +473,7 @@ class Option(BaseOption):
def do_validation(_value, _index=None):
if _value is None:
return True
return
ret_validation = None
try:
# valid with self._validator
@ -481,6 +481,7 @@ class Option(BaseOption):
# if not context launch consistency validation
if context is not None:
descr._valid_consistency(self, _value, context, _index)
self._second_level_validation(_value)
except ValueError as err:
msg = _("invalid value {0} for option {1}: {2}").format(
_value, self._name, err)
@ -610,6 +611,9 @@ class Option(BaseOption):
else:
self._state_callback = (callback, cllbck_prms)
def _second_level_validation(self, value):
pass
class ChoiceOption(Option):
"""represents a choice out of several objects.
@ -777,6 +781,9 @@ class IPOption(Option):
only_warning=only_warning)
def _validate(self, value):
IP('{0}/32'.format(value))
def _second_level_validation(self, value):
ip = IP('{0}/32'.format(value))
if not self._allow_reserved and ip.iptype() == 'RESERVED':
raise ValueError(_("IP mustn't not be in reserved class"))
@ -860,6 +867,9 @@ class NetworkOption(Option):
_opt_type = 'network'
def _validate(self, value):
IP(value)
def _second_level_validation(self, value):
ip = IP(value)
if ip.iptype() == 'RESERVED':
raise ValueError(_("network shall not be in reserved class"))