remove slave values when delete a master without value
authorEmmanuel Garette <egarette@cadoles.com>
Sun, 19 Apr 2015 07:37:46 +0000 (09:37 +0200)
committerEmmanuel Garette <egarette@cadoles.com>
Sun, 19 Apr 2015 07:37:46 +0000 (09:37 +0200)
ChangeLog
test/test_parsing_group.py
tiramisu/value.py

index 6b4a4a4..180268f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,7 @@ Sun Apr 19 09:14:21 2015 +0200 Emmanuel Garette <egarette@cadoles.com>
        * valid Option is an unicode or a string if needed
        * difference between option/optiondescription in PropertiesOptionError
        message
+       * remove slave values when delete a master without value
 
 Sat Apr 18 22:42:53 2015 +0200 Emmanuel Garette <egarette@cadoles.com>
        * refactor validation, build a fake context (with new Values and
index 404b0bb..dc299d7 100644 (file)
@@ -335,6 +335,47 @@ def test_reset_values_with_master_and_slaves():
     assert cfg.ip_admin_eth0.netmask_admin_eth0 == []
 
 
+def test_reset_values_with_master_and_slaves_default():
+    ip_admin_eth0 = StrOption('ip_admin_eth0', "ip réseau autorisé", multi=True, default=['192.168.230.145'])
+    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) == owners.default
+    assert cfg.getowner(netmask_admin_eth0) == owners.default
+
+    cfg.ip_admin_eth0.ip_admin_eth0[0] = "192.168.230.146"
+    assert cfg.getowner(ip_admin_eth0) == owner
+    assert cfg.getowner(netmask_admin_eth0) == owners.default
+    del(cfg.ip_admin_eth0.ip_admin_eth0)
+    assert cfg.getowner(ip_admin_eth0) == owners.default
+    assert cfg.getowner(netmask_admin_eth0) == owners.default
+    assert cfg.ip_admin_eth0.ip_admin_eth0 == ['192.168.230.145']
+    assert cfg.ip_admin_eth0.netmask_admin_eth0 == [None]
+
+    cfg.ip_admin_eth0.ip_admin_eth0[0] = "192.168.230.146"
+    cfg.ip_admin_eth0.netmask_admin_eth0[0] = "255.255.255.0"
+    assert cfg.getowner(ip_admin_eth0) == owner
+    assert cfg.getowner(netmask_admin_eth0) == owner
+    del(cfg.ip_admin_eth0.ip_admin_eth0)
+    assert cfg.getowner(ip_admin_eth0) == owners.default
+    assert cfg.getowner(netmask_admin_eth0) == owners.default
+    assert cfg.ip_admin_eth0.ip_admin_eth0 == ['192.168.230.145']
+    assert cfg.ip_admin_eth0.netmask_admin_eth0 == [None]
+
+    cfg.ip_admin_eth0.netmask_admin_eth0[0] = "255.255.255.0"
+    assert cfg.getowner(ip_admin_eth0) == owners.default
+    assert cfg.getowner(netmask_admin_eth0) == owner
+    del(cfg.ip_admin_eth0.ip_admin_eth0)
+    assert cfg.getowner(ip_admin_eth0) == owners.default
+    assert cfg.getowner(netmask_admin_eth0) == owners.default
+    assert cfg.ip_admin_eth0.ip_admin_eth0 == ['192.168.230.145']
+    assert cfg.ip_admin_eth0.netmask_admin_eth0 == [None]
+
+
 def test_values_with_master_and_slaves_slave():
     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)
@@ -558,3 +599,16 @@ def test_multi_non_valid_value():
     cfg.read_write()
     cfg.ip_admin_eth0 = ['a']
     raises(ValueError, 'cfg.ip_admin_eth0[0] = 1')
+
+
+def test_multi_master_default_slave():
+    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", default_multi="255.255.255.0", 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.1.1')
+    cfg.cfgimpl_reset_cache()
+    assert cfg.ip_admin_eth0.ip_admin_eth0 == ['192.168.1.1']
index ae774b7..6c14315 100644 (file)
@@ -158,18 +158,21 @@ class Values(object):
         if validate:
             context.cfgimpl_get_settings().validate_properties(opt, False,
                                                                True, path)
-        if self._contains(path):
-            if validate:
-                setting = context.cfgimpl_get_settings()
-                fake_context = context._gen_fake_values()
-                setting_properties = setting._getproperties()
-                fake_value = fake_context.cfgimpl_get_values()
-                fake_value.reset(opt, path, validate=False)
-                opt.impl_validate(getattr(fake_context, path),
-                                  fake_context, 'validator' in setting_properties)
-            context.cfgimpl_reset_cache()
-            if opt.impl_is_master_slaves('master'):
-                opt.impl_get_master_slaves().reset(opt, self)
+
+        hasvalue = self._contains(path)
+
+        if hasvalue and validate:
+            setting = context.cfgimpl_get_settings()
+            fake_context = context._gen_fake_values()
+            setting_properties = setting._getproperties()
+            fake_value = fake_context.cfgimpl_get_values()
+            fake_value.reset(opt, path, validate=False)
+            opt.impl_validate(getattr(fake_context, path),
+                              fake_context, 'validator' in setting_properties)
+        context.cfgimpl_reset_cache()
+        if opt.impl_is_master_slaves('master'):
+            opt.impl_get_master_slaves().reset(opt, self)
+        if hasvalue:
             self._p_.resetvalue(path)
 
     def _isempty(self, opt, value):