From 1d8c248d1bed627c92adfd2b1524c4bc04bdf244 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Fri, 17 May 2013 18:11:14 +0200 Subject: [PATCH 1/2] Multi: don't touch slave's value if it's default one's + don't check slave properties (if, for example, disabled for example) --- test/test_parsing_group.py | 61 ++++++++++++++++++++++++++++++++++++++ tiramisu/value.py | 48 +++++++++++++++++++----------- 2 files changed, 91 insertions(+), 18 deletions(-) diff --git a/test/test_parsing_group.py b/test/test_parsing_group.py index 18b1de1..0365f52 100644 --- a/test/test_parsing_group.py +++ b/test/test_parsing_group.py @@ -42,6 +42,7 @@ def make_description(): def test_base_config(): descr = make_description() config = Config(descr) + config.read_write() assert config.creole.general.activer_proxy_client is False assert config.creole.general.nom_machine == "eoleng" assert config.find_first(byname='nom_machine', type_='value') == "eoleng" @@ -61,6 +62,7 @@ def test_base_config(): def test_make_dict_filter(): descr = make_description() config = Config(descr) + config.read_write() result = {'general.numero_etab': None, 'general.nombre_interfaces': 1, 'general.serveur_ntp': [], 'general.mode_conteneur_actif': False, 'general.time_zone': 'Paris', 'general.nom_machine': 'eoleng', @@ -73,6 +75,7 @@ def test_make_dict_filter(): def test_get_group_type(): descr = make_description() config = Config(descr) + config.read_write() grp = config.unwrap_from_path('creole.general') assert grp.impl_get_group_type() == groups.family assert grp.impl_get_group_type() == 'family' @@ -83,6 +86,7 @@ def test_get_group_type(): def test_iter_on_groups(): descr = make_description() config = Config(descr) + config.read_write() result = list(config.creole.iter_groups(group_type=groups.family)) group_names = [res[0] for res in result] assert group_names == ['general', 'interface1'] @@ -90,6 +94,7 @@ def test_iter_on_groups(): def test_iter_on_empty_group(): config = Config(OptionDescription("name", "descr", [])) + config.read_write() result = list(config.iter_groups()) assert result == [] for i in config.iter_groups(): @@ -153,6 +158,7 @@ def test_values_with_master_and_slaves(): interface1.impl_set_group_type(groups.master) maconfig = OptionDescription('toto', '', [interface1]) cfg = Config(maconfig) + cfg.read_write() owner = cfg.cfgimpl_get_settings().getowner() assert interface1.impl_get_group_type() == groups.master assert cfg.getowner("ip_admin_eth0.ip_admin_eth0") == owners.default @@ -172,6 +178,7 @@ def test_reset_values_with_master_and_slaves(): interface1.impl_set_group_type(groups.master) maconfig = OptionDescription('toto', '', [interface1]) cfg = Config(maconfig) + cfg.read_write() owner = cfg.cfgimpl_get_settings().getowner() assert interface1.impl_get_group_type() == groups.master assert cfg.getowner("ip_admin_eth0.ip_admin_eth0") == owners.default @@ -193,6 +200,7 @@ def test_values_with_master_and_slaves_slave(): interface1.impl_set_group_type(groups.master) maconfig = OptionDescription('toto', '', [interface1]) cfg = Config(maconfig) + cfg.read_write() assert cfg.ip_admin_eth0.netmask_admin_eth0 == [] raises(SlaveError, "cfg.ip_admin_eth0.netmask_admin_eth0 = ['255.255.255.0']") cfg.ip_admin_eth0.ip_admin_eth0.append("192.168.230.145") @@ -215,6 +223,7 @@ def test_values_with_master_and_slaves_master(): 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 = ["192.168.230.145"] cfg.ip_admin_eth0.ip_admin_eth0 = ["192.168.230.145", "192.168.230.145"] @@ -222,3 +231,55 @@ def test_values_with_master_and_slaves_master(): raises(SlaveError, 'cfg.ip_admin_eth0.ip_admin_eth0 = ["192.168.230.145"]') cfg.ip_admin_eth0.ip_admin_eth0.pop(1) assert cfg.ip_admin_eth0.ip_admin_eth0 == ["192.168.230.145"] + + +def test_values_with_master_owner(): + 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() + owner = cfg.cfgimpl_get_settings().getowner() + assert cfg.getowner("ip_admin_eth0.ip_admin_eth0") == owners.default + assert cfg.getowner("ip_admin_eth0.netmask_admin_eth0") == owners.default + cfg.ip_admin_eth0.ip_admin_eth0.append("192.168.230.145") + assert cfg.getowner("ip_admin_eth0.ip_admin_eth0") == owner + assert cfg.getowner("ip_admin_eth0.netmask_admin_eth0") == owners.default + cfg.ip_admin_eth0.ip_admin_eth0.pop(0) + assert cfg.getowner("ip_admin_eth0.ip_admin_eth0") == owner + assert cfg.getowner("ip_admin_eth0.netmask_admin_eth0") == owners.default + + +def test_values_with_master_disabled(): + 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"] + cfg.ip_admin_eth0.ip_admin_eth0.pop(0) + del(cfg.ip_admin_eth0.netmask_admin_eth0) + cfg.cfgimpl_get_settings()[netmask_admin_eth0].append('disabled') + cfg.ip_admin_eth0.ip_admin_eth0.append("192.168.230.145") + cfg.ip_admin_eth0.ip_admin_eth0.pop(0) + + #delete with value in disabled var + cfg.cfgimpl_get_settings()[netmask_admin_eth0].remove('disabled') + cfg.ip_admin_eth0.ip_admin_eth0.append("192.168.230.145") + cfg.ip_admin_eth0.netmask_admin_eth0 = ["192.168.230.145"] + cfg.cfgimpl_get_settings()[netmask_admin_eth0].append('disabled') + cfg.ip_admin_eth0.ip_admin_eth0.pop(0) + + #append with value in disabled var + cfg.cfgimpl_get_settings()[netmask_admin_eth0].remove('disabled') + cfg.ip_admin_eth0.ip_admin_eth0.append("192.168.230.145") + cfg.ip_admin_eth0.netmask_admin_eth0 = ["192.168.230.145"] + cfg.cfgimpl_get_settings()[netmask_admin_eth0].append('disabled') + cfg.ip_admin_eth0.ip_admin_eth0.append('192.168.230.43') diff --git a/tiramisu/value.py b/tiramisu/value.py index 9367b39..d3e6507 100644 --- a/tiramisu/value.py +++ b/tiramisu/value.py @@ -93,18 +93,21 @@ class Values(object): return self._values def getitem(self, opt, validate=True, force_permissive=False, - force_properties=None): + force_properties=None, validate_properties=True): if opt in self._cache: exp = time() value, created = self._cache[opt] if exp < created: return value - val = self._getitem(opt, validate, force_permissive, force_properties) - if validate and force_permissive is False and force_properties is None: + val = self._getitem(opt, validate, force_permissive, force_properties, + validate_properties) + if validate and validate_properties and force_permissive is False and \ + force_properties is None: self._set_cache(opt, val) return val - def _getitem(self, opt, validate, force_permissive, force_properties): + def _getitem(self, opt, validate, force_permissive, force_properties, + validate_properties): # options with callbacks setting = self.context.cfgimpl_get_settings() value = self._get_value(opt, validate) @@ -133,9 +136,10 @@ class Values(object): if self.is_default_owner(opt) and \ 'force_store_value' in setting[opt]: self.setitem(opt, value, is_write=False) - setting.validate_properties(opt, False, False, value=value, - force_permissive=force_permissive, - force_properties=force_properties) + if validate_properties: + setting.validate_properties(opt, False, False, value=value, + force_permissive=force_permissive, + force_properties=force_properties) return value def __setitem__(self, opt, value): @@ -152,13 +156,15 @@ class Values(object): self._setvalue(opt, value, force_permissive=force_permissive, is_write=is_write) - def _setvalue(self, opt, value, force_permissive=False, force_properties=None, is_write=True): + def _setvalue(self, opt, value, force_permissive=False, force_properties=None, + is_write=True, validate_properties=True): self.context.cfgimpl_reset_cache() setting = self.context.cfgimpl_get_settings() - setting.validate_properties(opt, False, is_write, - value=value, - force_permissive=force_permissive, - force_properties=force_properties) + if validate_properties: + setting.validate_properties(opt, False, is_write, + value=value, + force_permissive=force_permissive, + force_properties=force_properties) self._values[opt] = (setting.getowner(), value) def getowner(self, opt): @@ -276,11 +282,13 @@ class Multi(list): for slave in self.opt.impl_get_master_slaves(): values = self.context.cfgimpl_get_values() if not values.is_default_owner(slave): - values[slave].append(slave.impl_getdefault_multi(), - force=True) + #get multi without valid properties + values.getitem(slave, validate_properties=False).append( + slave.impl_getdefault_multi(), + force=True) self._validate(value) - #assume not checking mandatory property - self.context.cfgimpl_get_values()._setvalue(self.opt, self) + #set value without valid properties + self.context.cfgimpl_get_values()._setvalue(self.opt, self, validate_properties=not force) super(Multi, self).append(value) def _validate(self, value): @@ -302,6 +310,10 @@ class Multi(list): " which is a slave").format(self.opt._name)) elif self.opt.impl_get_multitype() == multitypes.master: for slave in self.opt.impl_get_master_slaves(): - self.context.cfgimpl_get_values()[slave].pop(key, force=True) - self.context.cfgimpl_get_values()._setvalue(self.opt, self) + values = self.context.cfgimpl_get_values() + if not values.is_default_owner(slave): + #get multi without valid properties + values.getitem(slave, validate_properties=False).pop(key, force=True) + #set value without valid properties + self.context.cfgimpl_get_values()._setvalue(self.opt, self, validate_properties=not force) return super(Multi, self).pop(key) From e131fdc5a58885800c02b8e98b38d8b68c2ea4a7 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Tue, 21 May 2013 11:09:00 +0200 Subject: [PATCH 2/2] add cfgimpl_get_children for MetaConfig and informations --- tiramisu/config.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tiramisu/config.py b/tiramisu/config.py index 317e862..eebaca1 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -508,6 +508,10 @@ class MetaConfig(ConfigCommon): self._impl_settings = Setting(self) self._impl_values = Values(self) self._impl_meta = None + self._impl_informations = {} + + def cfgimpl_get_children(self): + return self._impl_children def cfgimpl_get_context(self): "a meta config is a config wich has a setting, that is itself"