From 329b9ac3493bcbbece10856b9dcc48dccf47ec13 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Wed, 25 Sep 2013 21:10:45 +0200 Subject: [PATCH] add _second_level_validation (second's one return only warning almost _validator raise) --- test/test_option_validator.py | 20 ++++++++++++++++++++ tiramisu/option.py | 12 +++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/test/test_option_validator.py b/test/test_option_validator.py index 030a7c0..c26e5f6 100644 --- a/test/test_option_validator.py +++ b/test/test_option_validator.py @@ -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' diff --git a/tiramisu/option.py b/tiramisu/option.py index 4112050..c49e5e4 100644 --- a/tiramisu/option.py +++ b/tiramisu/option.py @@ -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"))