tiramisu/test/test_metaconfig.py
2017-07-24 18:27:24 +02:00

719 lines
33 KiB
Python

from .autopath import do_autopath
do_autopath()
from py.test import raises
from tiramisu.setting import groups, owners
from tiramisu.config import Config, GroupConfig, MetaConfig
from tiramisu.option import IntOption, StrOption, NetworkOption, NetmaskOption, OptionDescription
from tiramisu.error import ConfigError, ConflictError, PropertiesOptionError
owners.addowner('meta')
def return_value(value=None):
return value
def raise_exception():
raise Exception('test')
def make_description():
i1 = IntOption('i1', '')
i2 = IntOption('i2', '', default=1)
i3 = IntOption('i3', '')
i4 = IntOption('i4', '', default=2)
i5 = IntOption('i5', '', default=[2], multi=True)
i6 = IntOption('i6', '', properties=('disabled',))
od1 = OptionDescription('od1', '', [i1, i2, i3, i4, i5, i6])
od2 = OptionDescription('od2', '', [od1])
return od2
def make_metaconfig():
od2 = make_description()
conf1 = Config(od2, session_id='conf3')
conf2 = Config(od2, session_id='conf4')
meta = MetaConfig([conf1, conf2], session_id='meta')
meta.read_write()
meta.cfgimpl_get_settings().setowner(owners.meta)
return meta
#FIXME ne pas mettre 2 meta dans une config
#FIXME ne pas mettre 2 OD differents dans un meta
#FIXME serialization
def test_none():
meta = make_metaconfig()
conf1, conf2 = meta.cfgimpl_get_children()
assert conf1.od1.i3 is conf2.od1.i3 is None
assert conf1.getowner(conf1.unwrap_from_path('od1.i3')) is conf2.getowner(conf2.unwrap_from_path('od1.i3')) is owners.default
meta.od1.i3 = 3
assert conf1.od1.i3 == conf2.od1.i3 == 3
assert conf1.getowner(conf1.unwrap_from_path('od1.i3')) is conf2.getowner(conf2.unwrap_from_path('od1.i3')) is owners.meta
meta.od1.i3 = 3
conf1.od1.i3 = 2
assert conf1.od1.i3 == 2
assert conf2.od1.i3 == 3
assert conf1.getowner(conf1.unwrap_from_path('od1.i3')) is owners.user
assert conf2.getowner(conf2.unwrap_from_path('od1.i3')) is owners.meta
meta.od1.i3 = 4
assert conf1.od1.i3 == 2
assert conf2.od1.i3 == 4
assert conf1.getowner(conf1.unwrap_from_path('od1.i3')) is owners.user
assert conf2.getowner(conf2.unwrap_from_path('od1.i3')) is owners.meta
del(meta.od1.i3)
assert conf1.od1.i3 == 2
assert conf2.od1.i3 is None
assert conf1.getowner(conf1.unwrap_from_path('od1.i3')) is owners.user
assert conf2.getowner(conf2.unwrap_from_path('od1.i3')) is owners.default
del(conf1.od1.i3)
assert conf1.od1.i3 is conf2.od1.i3 is None
assert conf1.getowner(conf1.unwrap_from_path('od1.i3')) is conf2.getowner(conf2.unwrap_from_path('od1.i3')) is owners.default
def test_default():
meta = make_metaconfig()
conf1, conf2 = meta.cfgimpl_get_children()
assert conf1.od1.i2 == conf2.od1.i2 == 1
assert conf1.getowner(conf1.unwrap_from_path('od1.i2')) is conf2.getowner(conf2.unwrap_from_path('od1.i2')) is owners.default
meta.od1.i2 = 3
assert conf1.od1.i2 == conf2.od1.i2 == 3
assert conf1.getowner(conf1.unwrap_from_path('od1.i2')) is conf2.getowner(conf2.unwrap_from_path('od1.i2')) is owners.meta
meta.od1.i2 = 3
conf1.od1.i2 = 2
assert conf1.od1.i2 == 2
assert conf2.od1.i2 == 3
assert conf1.getowner(conf1.unwrap_from_path('od1.i2')) is owners.user
assert conf2.getowner(conf2.unwrap_from_path('od1.i2')) is owners.meta
meta.od1.i2 = 4
assert conf1.od1.i2 == 2
assert conf2.od1.i2 == 4
assert conf1.getowner(conf1.unwrap_from_path('od1.i2')) is owners.user
assert conf2.getowner(conf2.unwrap_from_path('od1.i2')) is owners.meta
del(meta.od1.i2)
assert conf1.od1.i2 == 2
assert conf2.od1.i2 == 1
assert conf1.getowner(conf1.unwrap_from_path('od1.i2')) is owners.user
assert conf2.getowner(conf2.unwrap_from_path('od1.i2')) is owners.default
del(conf1.od1.i2)
assert conf1.od1.i2 == conf2.od1.i2 == 1
assert conf1.getowner(conf1.unwrap_from_path('od1.i2')) is conf2.getowner(conf2.unwrap_from_path('od1.i2')) is owners.default
def test_contexts():
meta = make_metaconfig()
conf1, conf2 = meta.cfgimpl_get_children()
assert conf1.od1.i2 == conf2.od1.i2 == 1
assert conf1.getowner(conf1.unwrap_from_path('od1.i2')) is conf2.getowner(conf2.unwrap_from_path('od1.i2')) is owners.default
errors = meta.set_value('od1.i2', 6, only_config=True)
assert meta.od1.i2 == 1
assert conf1.od1.i2 == conf2.od1.i2 == 6
assert conf1.getowner(conf1.unwrap_from_path('od1.i2')) is conf2.getowner(conf2.unwrap_from_path('od1.i2')) is owners.user
assert len(errors) == 0
def test_find():
meta = make_metaconfig()
i2 = meta.unwrap_from_path('od1.i2')
assert [i2] == meta.find(byname='i2')
assert i2 == meta.find_first(byname='i2')
assert meta.make_dict() == {'od1.i4': 2, 'od1.i1': None, 'od1.i3': None,
'od1.i2': 1, 'od1.i5': [2]}
def test_group_error():
raises(ValueError, "GroupConfig('str')")
raises(ValueError, "GroupConfig(['str'])")
def test_meta_meta():
meta1 = make_metaconfig()
meta2 = MetaConfig([meta1])
assert str(meta1) == """(conf3)
(conf4)
[od1]"""
meta2.cfgimpl_get_settings().setowner(owners.meta)
conf1, conf2 = meta1.cfgimpl_get_children()
assert conf1.od1.i2 == conf2.od1.i2 == 1
assert conf1.getowner(conf1.unwrap_from_path('od1.i2')) is conf2.getowner(conf2.unwrap_from_path('od1.i2')) is owners.default
meta2.od1.i2 = 3
assert conf1.od1.i2 == conf2.od1.i2 == 3
assert conf1.getowner(conf1.unwrap_from_path('od1.i2')) is conf2.getowner(conf2.unwrap_from_path('od1.i2')) is owners.meta
meta2.od1.i2 = 3
conf1.od1.i2 = 2
assert conf1.od1.i2 == 2
assert conf2.od1.i2 == 3
assert conf1.getowner(conf1.unwrap_from_path('od1.i2')) is owners.user
assert conf2.getowner(conf2.unwrap_from_path('od1.i2')) is owners.meta
meta2.od1.i2 = 4
assert conf1.od1.i2 == 2
assert conf2.od1.i2 == 4
assert conf1.getowner(conf1.unwrap_from_path('od1.i2')) is owners.user
assert conf2.getowner(conf2.unwrap_from_path('od1.i2')) is owners.meta
del(meta2.od1.i2)
assert conf1.od1.i2 == 2
assert conf2.od1.i2 == 1
assert conf1.getowner(conf1.unwrap_from_path('od1.i2')) is owners.user
assert conf2.getowner(conf2.unwrap_from_path('od1.i2')) is owners.default
del(conf1.od1.i2)
assert conf1.od1.i2 == conf2.od1.i2 == 1
assert conf1.getowner(conf1.unwrap_from_path('od1.i2')) is conf2.getowner(conf2.unwrap_from_path('od1.i2')) is owners.default
meta1.od1.i2 = 6
assert conf1.od1.i2 == conf2.od1.i2 == 6
assert conf1.getowner(conf1.unwrap_from_path('od1.i2')) is conf2.getowner(conf2.unwrap_from_path('od1.i2')) is owners.meta
def test_meta_config_name():
od = make_description()
meta = MetaConfig(['name1', 'name2'], optiondescription=od)
assert len(meta.cfgimpl_get_children()) == 2
assert meta.name1
assert meta.name2
def test_meta_new_config():
meta = make_metaconfig()
assert len(meta.cfgimpl_get_children()) == 2
meta.new_config('newconf1')
assert len(meta.cfgimpl_get_children()) == 3
def test_meta_new_config_wrong_name():
meta = make_metaconfig()
assert len(meta.cfgimpl_get_children()) == 2
raises(ConflictError, "meta.new_config('conf4')")
assert len(meta.cfgimpl_get_children()) == 2
def test_meta_meta_set():
meta1 = make_metaconfig()
meta2 = MetaConfig([meta1])
meta2.cfgimpl_get_settings().setowner(owners.meta)
meta2.read_write()
conf1, conf2 = meta1.cfgimpl_get_children()
errors1 = meta2.set_value('od1.i1', 7, only_config=True)
errors2 = meta2.set_value('od1.i6', 7, only_config=True)
assert len(errors1) == 0
assert len(errors2) == 2
assert conf1.od1.i1 == conf2.od1.i1 == 7
assert conf1.getowner(conf1.unwrap_from_path('od1.i1')) is conf2.getowner(conf2.unwrap_from_path('od1.i1')) is owners.user
assert [conf1, conf2] == meta2.find_firsts(byname='i1', byvalue=7).cfgimpl_get_children()
conf1.od1.i1 = 8
assert [conf1, conf2] == meta2.find_firsts(byname='i1').cfgimpl_get_children()
assert [conf2] == meta2.find_firsts(byname='i1', byvalue=7).cfgimpl_get_children()
assert [conf1] == meta2.find_firsts(byname='i1', byvalue=8).cfgimpl_get_children()
assert [conf1, conf2] == meta2.find_firsts(byname='i5', byvalue=2).cfgimpl_get_children()
raises(AttributeError, "meta2.find_firsts(byname='i1', byvalue=10)")
raises(AttributeError, "meta2.find_firsts(byname='not', byvalue=10)")
raises(AttributeError, "meta2.find_firsts(byname='i6')")
raises(ValueError, "meta2.set_value('od1.i6', 7, only_config=True, force_default=True)")
raises(ValueError, "meta2.set_value('od1.i6', 7, only_config=True, force_default_if_same=True)")
raises(ValueError, "meta2.set_value('od1.i6', 7, only_config=True, force_dont_change_value=True)")
def test_not_meta():
i1 = IntOption('i1', '')
od1 = OptionDescription('od1', '', [i1])
od2 = OptionDescription('od2', '', [od1])
conf1 = Config(od2, session_id='conf1')
conf2 = Config(od2, session_id='conf2')
conf3 = Config(od2)
try:
conf4 = Config(od2, session_id='conf2')
except ConflictError:
pass
else:
conf3, conf4
raises(ValueError, "GroupConfig(conf1)")
#same name
raises(ConflictError, "GroupConfig([conf2, conf4])")
grp = GroupConfig([conf1, conf2])
raises(ConfigError, 'grp.od1.i1')
conf1, conf2 = grp.cfgimpl_get_children()
errors = grp.set_value('od1.i1', 7)
assert len(errors) == 0
assert grp.conf1.od1.i1 == conf2.od1.i1 == 7
assert grp.conf1.getowner(grp.conf1.unwrap_from_path('od1.i1')) is grp.conf2.getowner(grp.conf2.unwrap_from_path('od1.i1')) is owners.user
def test_group_find_firsts():
i1 = IntOption('i1', '')
od1 = OptionDescription('od1', '', [i1])
od2 = OptionDescription('od2', '', [od1])
conf1 = Config(od2, session_id='conf1')
conf2 = Config(od2, session_id='conf2')
grp = GroupConfig([conf1, conf2])
assert str(grp) == """(conf1)
(conf2)
"""
assert [conf1, conf2] == grp.find_firsts(byname='i1').cfgimpl_get_children()
def test_group_group():
i1 = IntOption('i1', '')
od1 = OptionDescription('od1', '', [i1])
od2 = OptionDescription('od2', '', [od1])
conf1 = Config(od2, session_id='conf9')
conf2 = Config(od2, session_id='conf10')
grp = GroupConfig([conf1, conf2], 'grp')
grp2 = GroupConfig([grp])
errors = grp2.set_value('od1.i1', 2)
assert grp2.grp.conf9.od1.i1 == 2
assert grp2.grp.conf9.getowner(i1) == owners.user
assert len(errors) == 0
def test_meta_path():
meta = make_metaconfig()
assert meta._impl_path is None
assert meta.od1._impl_path == 'od1'
def test_meta_unconsistent():
i1 = IntOption('i1', '')
i2 = IntOption('i2', '', default=1)
i3 = IntOption('i3', '')
i4 = IntOption('i4', '', default=2)
od1 = OptionDescription('od1', '', [i1, i2, i3, i4])
od2 = OptionDescription('od2', '', [od1])
od3 = OptionDescription('od3', '', [od1])
conf1 = Config(od2, session_id='conf5')
conf2 = Config(od2, session_id='conf6')
conf3 = Config(od2, session_id='conf7')
conf4 = Config(od3, session_id='conf8')
conf3, conf4
meta = MetaConfig([conf1, conf2])
meta.cfgimpl_get_settings().setowner(owners.meta)
raises(TypeError, 'MetaConfig("string")')
#same descr but conf1 already in meta
raises(ValueError, "MetaConfig([conf1, conf3])")
#not same descr
raises(ValueError, "MetaConfig([conf3, conf4])")
raises(ConfigError, "meta.conf5.read_only()")
def test_meta_master_slaves():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',))
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
interface1.impl_set_group_type(groups.master)
conf1 = Config(interface1, session_id='conf1')
conf2 = Config(interface1, session_id='conf2')
meta = MetaConfig([conf1, conf2])
meta.read_only()
assert [conf1, conf2] == meta.find_firsts(byname='ip_admin_eth0').cfgimpl_get_children()
assert [conf1, conf2] == meta.find_firsts(byname='netmask_admin_eth0').cfgimpl_get_children()
meta.read_write()
raises(AttributeError, "meta.find_firsts(byname='netmask_admin_eth0')")
assert [conf1, conf2] == meta.find_firsts(byname='netmask_admin_eth0',
check_properties=None).cfgimpl_get_children()
meta.read_only()
assert [conf1, conf2] == meta.find_firsts(byname='netmask_admin_eth0').cfgimpl_get_children()
def test_meta_master_slaves_value():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',))
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
interface1.impl_set_group_type(groups.master)
conf1 = Config(interface1, session_id='conf1')
conf2 = Config(interface1, session_id='conf2')
meta = MetaConfig([conf1, conf2])
meta.conf1.ip_admin_eth0 = ['192.168.1.1']
assert meta.conf1.netmask_admin_eth0 == [None]
del(meta.conf1.ip_admin_eth0)
assert meta.conf1.netmask_admin_eth0 == []
meta.ip_admin_eth0 = ['192.168.1.1']
assert meta.conf1.netmask_admin_eth0 == [None]
meta.netmask_admin_eth0 = ['255.255.255.0']
assert meta.conf1.netmask_admin_eth0 == ['255.255.255.0']
meta.netmask_admin_eth0 = ['255.255.0.0']
assert meta.conf1.netmask_admin_eth0 == ['255.255.0.0']
meta.conf1.ip_admin_eth0 = ['192.168.1.1']
assert meta.conf1.netmask_admin_eth0 == [None]
def test_meta_master_slaves_value_default():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip", multi=True, default=['192.168.1.1'])
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True)
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
interface1.impl_set_group_type(groups.master)
conf1 = Config(interface1, session_id='conf1')
conf2 = Config(interface1, session_id='conf2')
meta = MetaConfig([conf1, conf2])
assert meta.conf1.netmask_admin_eth0 == [None]
meta.ip_admin_eth0 = ['192.168.1.1']
assert meta.conf1.netmask_admin_eth0 == [None]
meta.netmask_admin_eth0 = ['255.255.255.0']
assert meta.conf1.netmask_admin_eth0 == ['255.255.255.0']
meta.netmask_admin_eth0 = ['255.255.0.0']
assert meta.conf1.netmask_admin_eth0 == ['255.255.0.0']
meta.conf1.ip_admin_eth0 = ['192.168.1.1']
assert meta.conf1.netmask_admin_eth0 == [None]
def test_meta_master_slaves_owners():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',))
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
interface1.impl_set_group_type(groups.master)
conf1 = Config(interface1, session_id='conf1')
conf2 = Config(interface1, session_id='conf2')
meta = MetaConfig([conf1, conf2])
meta.cfgimpl_get_settings().setowner(owners.meta)
assert meta.conf1.getowner(ip_admin_eth0) == owners.default
assert meta.conf1.getowner(netmask_admin_eth0) == owners.default
meta.conf1.ip_admin_eth0 = ['192.168.1.1']
assert meta.conf1.getowner(ip_admin_eth0) == owners.user
assert meta.conf1.getowner(netmask_admin_eth0) == owners.default
del(meta.conf1.ip_admin_eth0)
assert meta.conf1.getowner(ip_admin_eth0) == owners.default
assert meta.conf1.getowner(netmask_admin_eth0) == owners.default
meta.ip_admin_eth0 = ['192.168.1.1']
assert meta.conf1.getowner(ip_admin_eth0) == owners.meta
assert meta.conf1.getowner(netmask_admin_eth0) == owners.default
meta.netmask_admin_eth0 = ['255.255.255.0']
assert meta.conf1.getowner(ip_admin_eth0) == owners.meta
assert meta.conf1.getowner(netmask_admin_eth0, 0) == owners.meta
meta.netmask_admin_eth0 = ['255.255.0.0']
assert meta.conf1.getowner(ip_admin_eth0) == owners.meta
assert meta.conf1.getowner(netmask_admin_eth0, 0) == owners.meta
meta.conf1.ip_admin_eth0 = ['192.168.1.1']
assert meta.conf1.getowner(ip_admin_eth0) == owners.user
assert meta.conf1.getowner(netmask_admin_eth0, 0) == owners.default
def test_meta_force_default():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',))
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
interface1.impl_set_group_type(groups.master)
conf1 = Config(interface1, session_id='conf1')
conf1.read_write()
conf2 = Config(interface1, session_id='conf2')
conf2.read_write()
meta = MetaConfig([conf1, conf2])
meta.read_write()
meta.cfgimpl_get_settings().setowner(owners.meta)
assert meta.ip_admin_eth0 == []
assert meta.conf1.ip_admin_eth0 == []
assert meta.conf2.ip_admin_eth0 == []
errors = meta.set_value('ip_admin_eth0', ['192.168.1.1'])
assert len(errors) == 0
assert meta.ip_admin_eth0 == ['192.168.1.1']
assert meta.conf1.ip_admin_eth0 == ['192.168.1.1']
assert meta.conf2.ip_admin_eth0 == ['192.168.1.1']
errors = meta.conf1.ip_admin_eth0 = ['192.168.1.2']
assert meta.ip_admin_eth0 == ['192.168.1.1']
assert meta.conf1.ip_admin_eth0 == ['192.168.1.2']
assert meta.conf2.ip_admin_eth0 == ['192.168.1.1']
errors = meta.set_value('ip_admin_eth0', ['192.168.1.3'])
assert len(errors) == 0
assert meta.ip_admin_eth0 == ['192.168.1.3']
assert meta.conf1.ip_admin_eth0 == ['192.168.1.2']
assert meta.conf2.ip_admin_eth0 == ['192.168.1.3']
errors = meta.set_value('ip_admin_eth0', ['192.168.1.4'], force_default=True)
assert len(errors) == 0
assert meta.ip_admin_eth0 == ['192.168.1.4']
assert meta.conf1.ip_admin_eth0 == ['192.168.1.4']
assert meta.conf2.ip_admin_eth0 == ['192.168.1.4']
def test_meta_force_dont_change_value():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',))
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
interface1.impl_set_group_type(groups.master)
conf1 = Config(interface1, session_id='conf1')
conf1.read_write()
conf2 = Config(interface1, session_id='conf2')
conf2.read_write()
meta = MetaConfig([conf1, conf2])
meta.read_write()
meta.cfgimpl_get_settings().setowner(owners.meta)
assert meta.ip_admin_eth0 == []
assert meta.conf1.ip_admin_eth0 == []
assert meta.conf2.ip_admin_eth0 == []
meta.conf1.ip_admin_eth0 = ['192.168.1.4']
assert meta.conf1.ip_admin_eth0 == ['192.168.1.4']
assert meta.conf2.ip_admin_eth0 == []
assert conf1.getowner(ip_admin_eth0) is owners.user
assert conf2.getowner(ip_admin_eth0) is owners.default
errors = meta.set_value('ip_admin_eth0', ['192.168.1.4'], force_dont_change_value=True)
assert len(errors) == 0
assert meta.ip_admin_eth0 == ['192.168.1.4']
assert meta.conf1.ip_admin_eth0 == ['192.168.1.4']
assert meta.conf2.ip_admin_eth0 == []
assert conf1.getowner(ip_admin_eth0) is owners.user
assert conf2.getowner(ip_admin_eth0) is owners.user
def test_meta_force_default_if_same():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',))
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
interface1.impl_set_group_type(groups.master)
conf1 = Config(interface1, session_id='conf1')
conf1.read_write()
conf2 = Config(interface1, session_id='conf2')
conf2.read_write()
meta = MetaConfig([conf1, conf2])
meta.read_write()
meta.cfgimpl_get_settings().setowner(owners.meta)
#
assert meta.ip_admin_eth0 == []
assert meta.conf1.ip_admin_eth0 == []
assert meta.conf2.ip_admin_eth0 == []
#
meta.conf1.ip_admin_eth0 = ['192.168.1.4']
assert meta.conf1.ip_admin_eth0 == ['192.168.1.4']
assert meta.conf2.ip_admin_eth0 == []
assert conf1.getowner(ip_admin_eth0) is owners.user
assert conf2.getowner(ip_admin_eth0) is owners.default
errors = meta.set_value('ip_admin_eth0', ['192.168.1.4'], force_default_if_same=True)
assert len(errors) == 0
assert meta.ip_admin_eth0 == ['192.168.1.4']
assert meta.conf1.ip_admin_eth0 == ['192.168.1.4']
assert meta.conf2.ip_admin_eth0 == ['192.168.1.4']
assert conf1.getowner(ip_admin_eth0) is owners.meta
assert conf2.getowner(ip_admin_eth0) is owners.meta
#
meta.conf1.ip_admin_eth0 = ['192.168.1.3']
assert meta.conf1.ip_admin_eth0 == ['192.168.1.3']
assert meta.conf2.ip_admin_eth0 == ['192.168.1.4']
assert conf1.getowner(ip_admin_eth0) is owners.user
assert conf2.getowner(ip_admin_eth0) is owners.meta
errors = meta.set_value('ip_admin_eth0', ['192.168.1.5'], force_default_if_same=True)
assert len(errors) == 0
assert meta.ip_admin_eth0 == ['192.168.1.5']
assert meta.conf1.ip_admin_eth0 == ['192.168.1.3']
assert meta.conf2.ip_admin_eth0 == ['192.168.1.5']
assert conf1.getowner(ip_admin_eth0) is owners.user
assert conf2.getowner(ip_admin_eth0) is owners.meta
def test_meta_force_default_if_same_and_dont_change():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',))
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
interface1.impl_set_group_type(groups.master)
conf1 = Config(interface1, session_id='conf1')
conf1.read_write()
conf2 = Config(interface1, session_id='conf2')
conf2.read_write()
meta = MetaConfig([conf1, conf2])
meta.read_write()
meta.cfgimpl_get_settings().setowner(owners.meta)
#
assert meta.ip_admin_eth0 == []
assert meta.conf1.ip_admin_eth0 == []
assert meta.conf2.ip_admin_eth0 == []
#
meta.conf1.ip_admin_eth0 = ['192.168.1.4']
assert meta.conf1.ip_admin_eth0 == ['192.168.1.4']
assert meta.conf2.ip_admin_eth0 == []
assert conf1.getowner(ip_admin_eth0) is owners.user
assert conf2.getowner(ip_admin_eth0) is owners.default
errors = meta.set_value('ip_admin_eth0', ['192.168.1.4'], force_default_if_same=True, force_dont_change_value=True)
assert len(errors) == 0
assert meta.ip_admin_eth0 == ['192.168.1.4']
assert meta.conf1.ip_admin_eth0 == ['192.168.1.4']
assert meta.conf2.ip_admin_eth0 == []
assert conf1.getowner(ip_admin_eth0) is owners.meta
assert conf2.getowner(ip_admin_eth0) is owners.user
#
meta.conf1.ip_admin_eth0 = ['192.168.1.3']
assert meta.conf1.ip_admin_eth0 == ['192.168.1.3']
assert meta.conf2.ip_admin_eth0 == []
assert conf1.getowner(ip_admin_eth0) is owners.user
assert conf2.getowner(ip_admin_eth0) is owners.user
errors = meta.set_value('ip_admin_eth0', ['192.168.1.5'], force_default_if_same=True, force_dont_change_value=True)
assert len(errors) == 0
assert meta.ip_admin_eth0 == ['192.168.1.5']
assert meta.conf1.ip_admin_eth0 == ['192.168.1.3']
assert meta.conf2.ip_admin_eth0 == []
assert conf1.getowner(ip_admin_eth0) is owners.user
assert conf2.getowner(ip_admin_eth0) is owners.user
def test_meta_force_default_and_dont_change():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',))
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
interface1.impl_set_group_type(groups.master)
conf1 = Config(interface1, session_id='conf1')
conf1.read_write()
conf2 = Config(interface1, session_id='conf2')
conf2.read_write()
meta = MetaConfig([conf1, conf2])
meta.read_write()
meta.cfgimpl_get_settings().setowner(owners.meta)
raises(ValueError, "meta.set_value('ip_admin_eth0', ['192.168.1.4'], force_default=True, force_dont_change_value=True)")
def test_meta_properties_meta():
ip_admin_eth0 = NetworkOption('ip_admin_eth0', "ip", multi=True, default=['192.168.1.1'])
netmask_admin_eth0 = NetmaskOption('netmask_admin_eth0', "mask", multi=True, properties=('disabled',))
netmask_admin_eth0.impl_add_consistency('network_netmask', ip_admin_eth0)
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
interface1.impl_set_group_type(groups.master)
conf1 = Config(interface1, session_id='conf1')
conf1.read_write()
conf2 = Config(interface1, session_id='conf2')
conf2.read_write()
meta = MetaConfig([conf1, conf2])
meta.read_write()
assert conf1.make_dict() == {}
def test_meta_exception_meta():
ip_admin_eth0 = NetworkOption('ip_admin_eth0', "ip", multi=True, default=['192.168.1.1'])
netmask_admin_eth0 = NetmaskOption('netmask_admin_eth0', "mask", multi=True, callback=raise_exception)
netmask_admin_eth0.impl_add_consistency('network_netmask', ip_admin_eth0)
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
interface1.impl_set_group_type(groups.master)
conf1 = Config(interface1, session_id='conf1')
conf1.read_write()
conf2 = Config(interface1, session_id='conf2')
conf2.read_write()
meta = MetaConfig([conf1, conf2])
meta.read_write()
raises(Exception, "conf1.make_dict()")
def test_meta_callback():
val1 = StrOption('val1', "", 'val')
val2 = StrOption('val2', "", callback=return_value, callback_params={'': ((val1, False),)})
val3 = StrOption('val3', "", callback=return_value, callback_params={'': ('yes',)})
val4 = StrOption('val4', "", callback=return_value, callback_params={'value': ((val1, False),)})
val5 = StrOption('val5', "", callback=return_value, callback_params={'value': ('yes',)})
maconfig = OptionDescription('rootconfig', '', [val1, val2, val3, val4, val5])
cfg = Config(maconfig, session_id='cfg')
meta = MetaConfig([cfg])
meta.read_write()
assert meta.cfg.make_dict() == {'val3': 'yes', 'val2': 'val', 'val1': 'val', 'val5': 'yes', 'val4': 'val'}
meta.cfg.val1 = 'new'
assert meta.cfg.make_dict() == {'val3': 'yes', 'val2': 'new', 'val1': 'new', 'val5': 'yes', 'val4': 'new'}
del(meta.cfg.val1)
meta.val1 = 'new'
assert meta.cfg.make_dict() == {'val3': 'yes', 'val2': 'new', 'val1': 'new', 'val5': 'yes', 'val4': 'new'}
#del(meta.val1)
meta.cfg.val4 = 'new1'
assert meta.cfg.make_dict() == {'val3': 'yes', 'val2': 'new', 'val1': 'new', 'val5': 'yes', 'val4': 'new1'}
del(meta.cfg.val4)
meta.val4 = 'new1'
assert meta.cfg.make_dict() == {'val3': 'yes', 'val2': 'new', 'val1': 'new', 'val5': 'yes', 'val4': 'new1'}
del(meta.val4)
def test_meta_callback_slave():
val = StrOption('val', "", default='val')
val1 = StrOption('val1', "", multi=True, callback=return_value, callback_params={'': ((val, False),)})
val3 = StrOption('val2', "", multi=True, callback=return_value, callback_params={'': ((val1, False),)})
val4 = StrOption('val3', "", multi=True, callback=return_value, callback_params={'': ((val1, False),)})
interface1 = OptionDescription('val1', '', [val1, val3, val4])
interface1.impl_set_group_type(groups.master)
maconfig = OptionDescription('rootconfig', '', [val, interface1])
cfg = Config(maconfig, session_id='cfg')
meta = MetaConfig([cfg])
meta.read_write()
assert meta.cfg.make_dict() == {'val1.val2': ['val'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}
meta.cfg.val = 'val1'
assert meta.cfg.make_dict() == {'val1.val2': ['val1'], 'val1.val1': ['val1'], 'val1.val3': ['val1'], 'val': 'val1'}
del(meta.cfg.val)
meta.val = 'val1'
assert meta.cfg.make_dict() == {'val1.val2': ['val1'], 'val1.val1': ['val1'], 'val1.val3': ['val1'], 'val': 'val1'}
del(meta.val)
meta.cfg.val1.val2 = ['val2']
assert meta.cfg.make_dict() == {'val1.val2': ['val2'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}
del(meta.cfg.val1.val2)
assert meta.cfg.make_dict() == {'val1.val2': ['val'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}
meta.val1.val2 = ['val2']
assert meta.cfg.make_dict() == {'val1.val2': ['val2'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}
meta.cfg.val1.val3 = ['val6']
assert meta.cfg.make_dict() == {'val1.val2': ['val2'], 'val1.val1': ['val'], 'val1.val3': ['val6'], 'val': 'val'}
del(meta.val1.val2)
del(meta.cfg.val1.val3)
meta.cfg.val1.val1 = ['val3']
assert meta.cfg.make_dict() == {'val1.val2': ['val3'], 'val1.val1': ['val3'], 'val1.val3': ['val3'], 'val': 'val'}
del(meta.cfg.val1.val1)
assert meta.cfg.make_dict() == {'val1.val2': ['val'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}
meta.val1.val1 = ['val3']
assert meta.cfg.make_dict() == {'val1.val2': ['val3'], 'val1.val1': ['val3'], 'val1.val3': ['val3'], 'val': 'val'}
meta.cfg.val1.val2 = ['val2']
assert meta.cfg.make_dict() == {'val1.val2': ['val2'], 'val1.val1': ['val3'], 'val1.val3': ['val3'], 'val': 'val'}
meta.cfg.val1.val1.append('rah')
assert meta.cfg.make_dict() == {'val1.val2': ['val2', 'rah'], 'val1.val1': ['val3', 'rah'], 'val1.val3': ['val3', 'rah'], 'val': 'val'}
meta.val1.val1 = ['val4']
assert meta.cfg.make_dict() == {'val1.val2': ['val2', 'rah'], 'val1.val1': ['val3', 'rah'], 'val1.val3': ['val3', 'rah'], 'val': 'val'}
def test_meta_properties_meta_set_value():
ip_admin_eth0 = NetworkOption('ip_admin_eth0', "ip", multi=True, default=['192.168.1.1'])
netmask_admin_eth0 = NetmaskOption('netmask_admin_eth0', "mask", multi=True, properties=('disabled',))
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
conf1 = Config(interface1, session_id='conf20')
conf2 = Config(interface1, session_id='conf21')
meta = MetaConfig([conf1, conf2])
meta.read_write()
assert conf1.make_dict() == {'ip_admin_eth0': ['192.168.1.1']}
ret = meta.set_value('netmask_admin_eth0', ['255.255.255.255'], only_config=True)
assert len(ret) == 2
assert isinstance(ret[0], PropertiesOptionError)
assert isinstance(ret[1], PropertiesOptionError)
ret = meta.set_value('netmask_admin_eth0', ['255.255.255.255'], force_default=True)
assert len(ret) == 1
assert isinstance(ret[0], PropertiesOptionError)
ret = meta.set_value('netmask_admin_eth0', ['255.255.255.255'], force_dont_change_value=True)
assert len(ret) == 3
assert isinstance(ret[0], PropertiesOptionError)
assert isinstance(ret[1], PropertiesOptionError)
assert isinstance(ret[2], PropertiesOptionError)
ret = meta.set_value('netmask_admin_eth0', ['255.255.255.255'], force_default_if_same=True)
assert len(ret) == 1
assert isinstance(ret[0], PropertiesOptionError)
ret = meta.set_value('ip_admin_eth0', '255.255.255.255', only_config=True)
assert len(ret) == 2
assert isinstance(ret[0], ValueError)
assert isinstance(ret[1], ValueError)
ret = meta.set_value('ip_admin_eth0', '255.255.255.255', force_default=True)
assert len(ret) == 1
assert isinstance(ret[0], ValueError)
ret = meta.set_value('ip_admin_eth0', '255.255.255.255', force_dont_change_value=True)
assert len(ret) == 1
assert isinstance(ret[0], ValueError)
ret = meta.set_value('ip_admin_eth0', '255.255.255.255', force_default_if_same=True)
assert len(ret) == 1
assert isinstance(ret[0], ValueError)
def test_meta_reset():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',))
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
interface1.impl_set_group_type(groups.master)
conf22 = Config(interface1, session_id='conf22')
conf22.read_write()
conf23 = Config(interface1, session_id='conf23')
conf23.read_write()
meta = MetaConfig([conf22, conf23])
meta.read_write()
meta.cfgimpl_get_settings().setowner(owners.meta)
assert meta.ip_admin_eth0 == []
assert meta.conf22.ip_admin_eth0 == []
assert meta.conf23.ip_admin_eth0 == []
errors = meta.set_value('ip_admin_eth0', ['192.168.1.1'])
assert len(errors) == 0
assert meta.ip_admin_eth0 == ['192.168.1.1']
assert meta.conf22.ip_admin_eth0 == ['192.168.1.1']
assert meta.conf23.ip_admin_eth0 == ['192.168.1.1']
meta.conf22.ip_admin_eth0 = ['192.168.1.2']
assert meta.ip_admin_eth0 == ['192.168.1.1']
assert meta.conf22.ip_admin_eth0 == ['192.168.1.2']
assert meta.conf23.ip_admin_eth0 == ['192.168.1.1']
meta.reset('ip_admin_eth0')
assert meta.ip_admin_eth0 == []
assert meta.conf22.ip_admin_eth0 == []
assert meta.conf23.ip_admin_eth0 == []