From f730050f7c885980237c1af0b1e26ef2e968e965 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 26 Oct 2014 16:19:06 +0100 Subject: [PATCH] pass force_permissive to slave for a master or to master for a slave --- ChangeLog | 1 + test/test_parsing_group.py | 67 ++++++++++++++++++++++++++++++++-- tiramisu/option/masterslave.py | 15 +++++--- 3 files changed, 74 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index 174d011..09f244a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,6 +4,7 @@ Sun Oct 26 08:50:38 2014 +0200 Emmanuel Garette * bad characters in DomainnameOption could be in warning level * frozen with force_default_on_freeze can change owner * add force_permissive to config __iter__ + * pass force_permissive to slave for a master or to master for a slave Sat Oct 25 22:48:08 2014 +0200 Emmanuel Garette * cannot add unvalaible consistency for an option diff --git a/test/test_parsing_group.py b/test/test_parsing_group.py index 62c162f..16f76e8 100644 --- a/test/test_parsing_group.py +++ b/test/test_parsing_group.py @@ -4,7 +4,7 @@ from tiramisu.setting import groups, owners from tiramisu.config import Config from tiramisu.option import ChoiceOption, BoolOption, IntOption, \ StrOption, OptionDescription -from tiramisu.error import SlaveError +from tiramisu.error import SlaveError, PropertiesOptionError from py.test import raises @@ -25,8 +25,8 @@ def make_description(): time_zone = ChoiceOption('time_zone', 'fuseau horaire du serveur', ('Paris', 'Londres'), 'Paris') - ip_admin_eth0 = StrOption('ip_admin_eth0', "ip réseau autorisé", properties=('test_perm',)) - netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-réseau", properties=('test_perm',)) + ip_admin_eth0 = StrOption('ip_admin_eth0', "ip réseau autorisé") + netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-réseau") master = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1 = OptionDescription('interface1', '', [master]) @@ -176,6 +176,50 @@ def test_groups_with_master_in_config(): assert interface1.impl_get_group_type() == groups.master +def test_groups_with_master_hidden_in_config(): + ip_admin_eth0 = StrOption('ip_admin_eth0', "ip réseau autorisé", multi=True, properties=('hidden',)) + netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-réseau", multi=True, properties=('hidden',)) + interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) + interface1.impl_set_group_type(groups.master) + cfg = Config(interface1) + cfg.read_write() + cfg.cfgimpl_get_settings().setpermissive(('hidden',)) + cfg.getattr('ip_admin_eth0', force_permissive=True) + cfg.getattr('netmask_admin_eth0', force_permissive=True) + raises(PropertiesOptionError, "cfg.getattr('ip_admin_eth0')") + raises(PropertiesOptionError, "cfg.getattr('netmask_admin_eth0')") + + +def test_groups_with_master_hidden_in_config2(): + 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, properties=('hidden',)) + interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) + interface1.impl_set_group_type(groups.master) + cfg = Config(interface1) + cfg.read_write() + cfg.cfgimpl_get_settings().setpermissive(('hidden',)) + cfg.getattr('ip_admin_eth0', force_permissive=True) + cfg.getattr('netmask_admin_eth0', force_permissive=True) + cfg.getattr('ip_admin_eth0') + raises(PropertiesOptionError, "cfg.getattr('netmask_admin_eth0')") + + +def test_groups_with_master_hidden_in_config3(): + #if master is hidden, slave are hidden too + ip_admin_eth0 = StrOption('ip_admin_eth0', "ip réseau autorisé", multi=True, properties=('hidden',)) + netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-réseau", multi=True) + interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) + interface1.impl_set_group_type(groups.master) + cfg = Config(interface1) + cfg.read_write() + cfg.cfgimpl_get_settings().setpermissive(('hidden',)) + cfg.getattr('ip_admin_eth0', force_permissive=True) + cfg.getattr('netmask_admin_eth0', force_permissive=True) + raises(PropertiesOptionError, "cfg.getattr('ip_admin_eth0')") + raises(PropertiesOptionError, "cfg.getattr('netmask_admin_eth0')") + + + def test_allowed_groups(): 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) @@ -183,6 +227,23 @@ def test_allowed_groups(): raises(ValueError, "interface1.impl_set_group_type('toto')") +def test_values_with_master_disabled_master(): + 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 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) + interface1.impl_set_group_type(groups.master) + maconfig = OptionDescription('toto', '', [interface1]) + cfg = Config(maconfig) + cfg.read_write() + cfg.ip_admin_eth0.ip_admin_eth0.append("192.168.230.145") + cfg.ip_admin_eth0.ip_admin_eth0.pop(0) + cfg.ip_admin_eth0.ip_admin_eth0.append("192.168.230.145") + cfg.ip_admin_eth0.netmask_admin_eth0 = ["192.168.230.145"] + del(cfg.ip_admin_eth0.netmask_admin_eth0) + cfg.cfgimpl_get_settings()[ip_admin_eth0].append('disabled') + raises(PropertiesOptionError, "cfg.ip_admin_eth0.netmask_admin_eth0[0] = '192.168.230.145'") + + def test_master_not_valid_name(): 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) diff --git a/tiramisu/option/masterslave.py b/tiramisu/option/masterslave.py index ee72ec1..09c77b3 100644 --- a/tiramisu/option/masterslave.py +++ b/tiramisu/option/masterslave.py @@ -162,7 +162,8 @@ class MasterSlaves(object): force_properties, validate_properties, None) # not undefined - return self.get_slave_value(values, opt, value, validate, validate_properties) + return self.get_slave_value(values, opt, value, validate, + validate_properties, force_permissive) def setitem(self, values, opt, value, path): if self.is_master(opt): @@ -183,13 +184,14 @@ class MasterSlaves(object): opt.impl_getname(), opt, setitem=True) def get_length(self, values, opt, validate=True, slave_path=undefined, - slave_value=undefined): + slave_value=undefined, force_permissive=False): """get master len with slave option""" masterp = self.getmaster(opt).impl_getpath(values._getcontext()) if slave_value is undefined: slave_path = undefined - return len(self.getitem(values, self.getmaster(opt), masterp, validate, False, - None, True, slave_path, slave_value)) + return len(self.getitem(values, self.getmaster(opt), masterp, validate, + force_permissive, None, True, slave_path, + slave_value)) def validate_slave_length(self, masterlen, valuelen, name, opt, setitem=False): if valuelen > masterlen or (valuelen < masterlen and setitem): # pragma: optional cover @@ -200,7 +202,7 @@ class MasterSlaves(object): name, self.getmaster(opt).impl_getname())) def get_slave_value(self, values, opt, value, validate=True, - validate_properties=True): + validate_properties=True, force_permissive=False): """ if master has length 0: return [] @@ -224,7 +226,8 @@ class MasterSlaves(object): """ #if slave, had values until master's one path = opt.impl_getpath(values._getcontext()) - masterlen = self.get_length(values, opt, validate, path, value) + masterlen = self.get_length(values, opt, validate, path, value, + force_permissive) valuelen = len(value) if validate: self.validate_slave_length(masterlen, valuelen, opt.impl_getname(), opt)