tiramisu/tests/test_metaconfig.py

1396 lines
69 KiB
Python
Raw Normal View History

2019-12-24 15:24:20 +01:00
import pytest
2017-07-09 09:49:03 +02:00
from .autopath import do_autopath
2015-07-24 17:54:10 +02:00
do_autopath()
2013-09-17 09:02:10 +02:00
from tiramisu.setting import groups, owners
2019-02-10 20:59:03 +01:00
from tiramisu import IntOption, StrOption, NetworkOption, NetmaskOption, BoolOption, ChoiceOption, \
2019-02-23 19:06:23 +01:00
IPOption, OptionDescription, Leadership, Config, GroupConfig, MetaConfig, \
2019-10-27 11:09:15 +01:00
Calculation, Params, ParamOption, ParamValue, calc_value, ParamSelfOption, \
valid_network_netmask, valid_not_equal
2019-02-23 19:06:23 +01:00
from tiramisu.error import ConfigError, ConflictError, PropertiesOptionError, LeadershipError, APIError
2018-10-31 08:00:19 +01:00
from tiramisu.storage import list_sessions
2020-01-22 20:46:18 +01:00
from .config import config_type, get_config, delete_sessions, event_loop
2018-10-31 08:00:19 +01:00
2013-09-17 09:02:10 +02:00
owners.addowner('config')
2018-03-19 08:33:53 +01:00
owners.addowner('meta1')
owners.addowner('meta2')
2013-09-17 09:02:10 +02:00
2017-07-16 23:11:12 +02:00
def return_value(value=None):
return value
2019-02-10 20:59:03 +01:00
def return_condition(val, condition, expected):
if condition == expected:
return val
return None
2017-02-11 17:22:50 +01:00
def raise_exception():
raise Exception('test')
2013-09-17 09:02:10 +02:00
def make_description():
i1 = IntOption('i1', '')
i2 = IntOption('i2', '', default=1)
i3 = IntOption('i3', '')
i4 = IntOption('i4', '', default=2)
2014-02-04 21:14:30 +01:00
i5 = IntOption('i5', '', default=[2], multi=True)
i6 = IntOption('i6', '', properties=('disabled',))
od1 = OptionDescription('od1', '', [i1, i2, i3, i4, i5, i6])
2013-09-17 09:02:10 +02:00
od2 = OptionDescription('od2', '', [od1])
2017-07-21 18:03:34 +02:00
return od2
2019-12-24 15:24:20 +01:00
async def make_metaconfig(double=False):
2017-07-21 18:03:34 +02:00
od2 = make_description()
2020-01-22 20:46:18 +01:00
conf1 = await Config(od2, session_id='conf1', delete_old_session=True)
2019-12-24 15:24:20 +01:00
await conf1.property.read_write()
2020-01-22 20:46:18 +01:00
conf2 = await Config(od2, session_id='conf2', delete_old_session=True)
2019-12-24 15:24:20 +01:00
await conf2.property.read_write()
2020-01-22 20:46:18 +01:00
meta = await MetaConfig([conf1, conf2], session_id='meta', delete_old_session=True)
2018-03-19 08:33:53 +01:00
if double:
2019-12-24 15:24:20 +01:00
await meta.owner.set(owners.meta2)
2020-01-22 20:46:18 +01:00
meta = await MetaConfig([meta], session_id='doublemeta', delete_old_session=True)
2019-12-24 15:24:20 +01:00
await meta.property.read_write()
await meta.owner.set(owners.meta1)
2018-08-15 08:35:22 +02:00
return meta
2013-09-17 09:02:10 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_unknown_config():
meta = await make_metaconfig()
2020-01-22 20:46:18 +01:00
with pytest.raises(ConfigError):
2019-12-24 15:24:20 +01:00
await meta.config('unknown')
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_error_metaconfig():
2019-02-25 20:30:20 +01:00
od2 = make_description()
2019-12-24 15:24:20 +01:00
conf1 = await Config(od2, session_id='conf1')
2020-01-22 20:46:18 +01:00
with pytest.raises(TypeError):
2019-12-24 15:24:20 +01:00
await MetaConfig([await GroupConfig([conf1])], session_id='meta')
2020-01-22 20:46:18 +01:00
await delete_sessions(conf1)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_path():
meta = await make_metaconfig()
assert await meta.config.path() == 'meta'
ret = await meta.config('conf1')
assert await ret.config.path() == 'meta.conf1'
ret = await meta.config('conf2')
assert await ret.config.path() == 'meta.conf2'
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_none():
meta = await make_metaconfig()
conf1 = await meta.config('conf1')
conf2 = await meta.config('conf2')
assert await meta.option('od1.i3').value.get() is await conf1.option('od1.i3').value.get() is await conf2.option('od1.i3').value.get() is None
assert await meta.option('od1.i3').owner.get() is await conf1.option('od1.i3').owner.get() is await conf2.option('od1.i3').owner.get() is owners.default
#
await meta.option('od1.i3').value.set(3)
assert await meta.option('od1.i3').value.get() == await conf1.option('od1.i3').value.get() == await conf2.option('od1.i3').value.get() == 3
assert await meta.option('od1.i3').owner.get() is await conf1.option('od1.i3').owner.get() is await conf2.option('od1.i3').owner.get() is owners.meta1
#
await conf1.option('od1.i3').value.set(2)
assert await meta.option('od1.i3').value.get() == await conf2.option('od1.i3').value.get() == 3
assert await conf1.option('od1.i3').value.get() == 2
assert await meta.option('od1.i3').owner.get() is await conf2.option('od1.i3').owner.get() is owners.meta1
assert await conf1.option('od1.i3').owner.get() is owners.user
#
await meta.option('od1.i3').value.set(4)
assert await meta.option('od1.i3').value.get() == await conf2.option('od1.i3').value.get() == 4
assert await conf1.option('od1.i3').value.get() == 2
assert await meta.option('od1.i3').owner.get() is await conf2.option('od1.i3').owner.get() is owners.meta1
assert await conf1.option('od1.i3').owner.get() is owners.user
#
await meta.option('od1.i3').value.reset()
assert await meta.option('od1.i3').value.get() is await conf2.option('od1.i3').value.get() is None
assert await conf1.option('od1.i3').value.get() == 2
assert await meta.option('od1.i3').owner.get() is await conf2.option('od1.i3').owner.get() is owners.default
assert await conf1.option('od1.i3').owner.get() is owners.user
#
await conf1.option('od1.i3').value.reset()
assert await meta.option('od1.i3').value.get() is await conf1.option('od1.i3').value.get() is await conf2.option('od1.i3').value.get() is None
assert await meta.option('od1.i3').owner.get() is await conf1.option('od1.i3').owner.get() is await conf2.option('od1.i3').owner.get() is owners.default
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_metaconfig_reset(config_type):
meta = await make_metaconfig()
meta_api = await get_config(meta, config_type)
assert await meta_api.option('od1.i2').value.get() == 1
await meta_api.option('od1.i2').value.set(2)
2019-08-03 21:03:23 +02:00
if config_type == 'tiramisu-api':
meta_api.send()
2019-12-24 15:24:20 +01:00
conf1 = await meta.config('conf1')
conf2 = await meta.config('conf2')
await conf1.option('od1.i2').value.set(3)
assert await meta.option('od1.i2').value.get() == 2
assert await conf1.option('od1.i2').value.get() == 3
assert await conf2.option('od1.i2').value.get() == 2
await meta.config.reset()
assert await meta.option('od1.i2').value.get() == 1
assert await conf1.option('od1.i2').value.get() == 3
assert await conf2.option('od1.i2').value.get() == 1
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_default():
meta = await make_metaconfig()
conf1 = await meta.config('conf1')
conf2 = await meta.config('conf2')
assert await meta.option('od1.i2').value.get() == await conf1.option('od1.i2').value.get() == await conf2.option('od1.i2').value.get() == 1
assert await meta.option('od1.i2').owner.get() is await conf1.option('od1.i2').owner.get() is await conf2.option('od1.i2').owner.get() is owners.default
#
await meta.option('od1.i2').value.set(3)
assert await meta.option('od1.i2').value.get() == await conf1.option('od1.i2').value.get() == await conf2.option('od1.i2').value.get() == 3
assert await meta.option('od1.i2').owner.get() is await conf1.option('od1.i2').owner.get() is await conf2.option('od1.i2').owner.get() is owners.meta1
#
await conf1.option('od1.i2').value.set(2)
assert await meta.option('od1.i2').value.get() == await conf2.option('od1.i2').value.get() == 3
assert await conf1.option('od1.i2').value.get() == 2
assert await meta.option('od1.i2').owner.get() is await conf2.option('od1.i2').owner.get() is owners.meta1
assert await conf1.option('od1.i2').owner.get() is owners.user
#
await meta.option('od1.i2').value.set(4)
assert await meta.option('od1.i2').value.get() == await conf2.option('od1.i2').value.get() == 4
assert await conf1.option('od1.i2').value.get() == 2
assert await meta.option('od1.i2').owner.get() is await conf2.option('od1.i2').owner.get() is owners.meta1
assert await conf1.option('od1.i2').owner.get() is owners.user
#
await meta.option('od1.i2').value.reset()
assert await meta.option('od1.i2').value.get() == await conf2.option('od1.i2').value.get() == 1
assert await conf1.option('od1.i2').value.get() == 2
assert await meta.option('od1.i2').owner.get() is await conf2.option('od1.i2').owner.get() is owners.default
assert await conf1.option('od1.i2').owner.get() is owners.user
#
await conf1.option('od1.i2').value.reset()
assert await meta.option('od1.i2').value.get() == await conf1.option('od1.i2').value.get() == await conf2.option('od1.i2').value.get() == 1
assert await meta.option('od1.i2').owner.get() is await conf1.option('od1.i2').owner.get() is await conf2.option('od1.i2').owner.get() is owners.default
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_contexts():
meta = await make_metaconfig()
errors = await meta.value.set('od1.i2', 6, only_config=True)
2017-07-21 18:03:34 +02:00
assert len(errors) == 0
2019-12-24 15:24:20 +01:00
conf1 = await meta.config('conf1')
assert await meta.option('od1.i2').value.get() == 1
assert await meta.option('od1.i2').owner.get() == owners.default
assert await conf1.option('od1.i2').value.get() == await conf1.option('od1.i2').value.get() == 6
assert await conf1.option('od1.i2').owner.get() == await conf1.option('od1.i2').owner.get() is owners.user
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2013-09-17 09:02:10 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_find():
meta = await make_metaconfig()
ret = list(await meta.option.find('i2'))
2018-04-10 12:33:51 +02:00
assert len(ret) == 1
2019-12-24 15:24:20 +01:00
assert 1 == await ret[0].value.get()
ret = await meta.option.find('i2', first=True)
assert 1 == await ret.value.get()
assert await meta.value.dict() == {'od1.i4': 2, 'od1.i1': None, 'od1.i3': None,
2018-03-19 08:33:53 +01:00
'od1.i2': 1, 'od1.i5': [2]}
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2013-09-17 09:02:10 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_meta():
meta = await make_metaconfig(double=True)
meta1 = await meta.config('meta')
conf1 = await meta.config('meta.conf1')
conf2 = await meta.config('meta.conf2')
assert await meta.option('od1.i2').value.get() == await meta1.option('od1.i2').value.get() == await conf1.option('od1.i2').value.get() == await conf2.option('od1.i2').value.get() == 1
assert await meta.option('od1.i2').owner.get() is await meta1.option('od1.i2').owner.get() is await conf1.option('od1.i2').owner.get() is await conf2.option('od1.i2').owner.get() is owners.default
#
await meta.option('od1.i2').value.set(3)
assert await meta.option('od1.i2').value.get() == await meta1.option('od1.i2').value.get() == await conf1.option('od1.i2').value.get() == await conf2.option('od1.i2').value.get() == 3
assert await meta.option('od1.i2').owner.get() is await meta1.option('od1.i2').owner.get() is await conf1.option('od1.i2').owner.get() is await conf2.option('od1.i2').owner.get() is owners.meta1
#
await conf1.option('od1.i2').value.set(2)
assert await meta.option('od1.i2').value.get() == await meta1.option('od1.i2').value.get() == await conf2.option('od1.i2').value.get() == 3
assert await conf1.option('od1.i2').value.get() == 2
assert await meta.option('od1.i2').owner.get() is await meta1.option('od1.i2').owner.get() is await conf2.option('od1.i2').owner.get() is owners.meta1
assert await conf1.option('od1.i2').owner.get() is owners.user
#
await meta1.option('od1.i2').value.set(4)
assert await meta.option('od1.i2').value.get() == 3
assert await meta1.option('od1.i2').value.get() == await conf2.option('od1.i2').value.get() == 4
assert await conf1.option('od1.i2').value.get() == 2
assert await meta.option('od1.i2').owner.get() is owners.meta1
assert await meta1.option('od1.i2').owner.get() is await conf2.option('od1.i2').owner.get() is owners.meta2
assert await conf1.option('od1.i2').owner.get() is owners.user
#
await meta1.option('od1.i2').value.reset()
assert await meta.option('od1.i2').value.get() == await meta1.option('od1.i2').value.get() == await conf2.option('od1.i2').value.get() == 3
assert await conf1.option('od1.i2').value.get() == 2
assert await meta.option('od1.i2').owner.get() is await meta1.option('od1.i2').owner.get() is await conf2.option('od1.i2').owner.get() is owners.meta1
assert await conf1.option('od1.i2').owner.get() is owners.user
#
await meta.option('od1.i2').value.reset()
assert await meta.option('od1.i2').value.get() == await meta1.option('od1.i2').value.get() == await conf2.option('od1.i2').value.get() == 1
assert await conf1.option('od1.i2').value.get() == 2
assert await meta.option('od1.i2').owner.get() is await meta1.option('od1.i2').owner.get() is await conf2.option('od1.i2').owner.get() is owners.default
assert await conf1.option('od1.i2').owner.get() is owners.user
#
await conf1.option('od1.i2').value.reset()
assert await meta.option('od1.i2').value.get() == await meta1.option('od1.i2').value.get() == await conf1.option('od1.i2').value.get() == await conf2.option('od1.i2').value.get() == 1
assert await meta.option('od1.i2').owner.get() is await meta1.option('od1.i2').owner.get() is await conf1.option('od1.i2').owner.get() is await conf2.option('od1.i2').owner.get() is owners.default
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_new_config():
2018-03-19 08:33:53 +01:00
od = make_description()
2019-12-24 15:24:20 +01:00
meta = await MetaConfig(['name1', 'name2'], optiondescription=od)
assert len(list(await meta.config.list())) == 2
await meta.config.new('newconf1')
assert len(list(await meta.config.list())) == 3
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2017-07-21 18:03:34 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_new_config_owner():
od = make_description()
2019-12-24 15:24:20 +01:00
meta = await MetaConfig(['name1', 'name2'], optiondescription=od)
await meta.owner.set('meta')
await meta.config.new('newconf1')
assert await meta.owner.get() == 'meta'
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_new_metaconfig():
od = make_description()
2019-12-24 15:24:20 +01:00
meta = await MetaConfig(['name1', 'name2'], optiondescription=od)
await meta.config.new('newconf1', type='metaconfig')
newconf1 = await meta.config('newconf1')
await newconf1.config.new('newconf2', type='metaconfig')
newconf2 = await newconf1.config('newconf2')
await newconf2.config.new('newconf3')
newconf3 = await newconf2.config('newconf3')
2020-01-22 20:46:18 +01:00
assert await newconf3.session.id() == 'newconf3'
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_pop_config():
2018-09-13 07:00:12 +02:00
od = make_description()
2019-12-24 15:24:20 +01:00
meta = await MetaConfig(['name1', 'name2'], optiondescription=od)
await meta.option('od1.i1').value.set(2)
#
2019-12-24 15:24:20 +01:00
assert len(list(await meta.config.list())) == 2
await meta.config.new('newconf1')
newconf1 = await meta.config('newconf1')
assert await newconf1.value.dict() == {'od1.i1': 2, 'od1.i2': 1, 'od1.i3': None, 'od1.i4': 2, 'od1.i5': [2], 'od1.i6': None}
#
2019-12-24 15:24:20 +01:00
assert len(list(await meta.config.list())) == 3
newconf1 = await meta.config.pop('newconf1')
try:
2019-12-24 15:24:20 +01:00
await meta.config('newconf1')
except ConfigError:
pass
else:
raise Exception('must raise')
2019-12-24 15:24:20 +01:00
assert await newconf1.value.dict() == {'od1.i1': None, 'od1.i2': 1, 'od1.i3': None, 'od1.i4': 2, 'od1.i5': [2], 'od1.i6': None}
#
2019-12-24 15:24:20 +01:00
assert len(list(await meta.config.list())) == 2
2020-01-22 20:46:18 +01:00
with pytest.raises(ConfigError):
2019-12-24 15:24:20 +01:00
await meta.config.pop('newconf1')
2020-01-22 20:46:18 +01:00
await delete_sessions([meta, newconf1])
2018-09-13 07:00:12 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_add_config():
od = make_description()
od2 = make_description()
2020-01-22 20:46:18 +01:00
meta = await MetaConfig(['name1', 'name2'], optiondescription=od, delete_old_session=True)
2019-12-24 15:24:20 +01:00
await meta.option('od1.i1').value.set(2)
#
2019-12-24 15:24:20 +01:00
assert len(list(await meta.config.list())) == 2
2020-01-22 20:46:18 +01:00
config = await Config(od, session_id='new', delete_old_session=True)
2019-12-24 15:24:20 +01:00
assert await config.value.dict() == {'od1.i1': None, 'od1.i2': 1, 'od1.i3': None, 'od1.i4': 2, 'od1.i5': [2], 'od1.i6': None}
await meta.config.add(config)
#
2019-12-24 15:24:20 +01:00
assert len(list(await meta.config.list())) == 3
assert await config.value.dict() == {'od1.i1': 2, 'od1.i2': 1, 'od1.i3': None, 'od1.i4': 2, 'od1.i5': [2], 'od1.i6': None}
#
2020-01-22 20:46:18 +01:00
with pytest.raises(ConflictError):
2019-12-24 15:24:20 +01:00
await meta.config.add(config)
2020-01-22 20:46:18 +01:00
newconfig = await Config(od2)
with pytest.raises(ValueError):
await meta.config.add(newconfig)
await delete_sessions([meta, newconfig])
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_add_config_readd():
od = make_description()
2019-12-24 15:24:20 +01:00
meta = await MetaConfig([], optiondescription=od)
meta2 = await MetaConfig([], optiondescription=od)
config = await Config(od, session_id='new')
#
2019-12-24 15:24:20 +01:00
await meta.config.add(config)
await meta2.config.add(config)
assert len(list(await config.config.parents())) == 2
2020-01-22 20:46:18 +01:00
await delete_sessions([meta, meta2])
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_new_config_wrong_name():
2018-03-19 08:33:53 +01:00
od = make_description()
2019-12-24 15:24:20 +01:00
meta = await MetaConfig(['name1', 'name2'], optiondescription=od)
assert len(list(await meta.config.list())) == 2
2020-01-22 20:46:18 +01:00
with pytest.raises(ConflictError):
2019-12-24 15:24:20 +01:00
await meta.config.new('name1')
assert len(list(await meta.config.list())) == 2
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_load_config():
od = make_description()
meta = await MetaConfig(['name1', 'name2'], optiondescription=od)
assert len(list(await meta.config.list())) == 2
await meta.config.load('name1')
assert len(list(await meta.config.list())) == 3
await delete_sessions(meta)
@pytest.mark.asyncio
async def test_meta_load_config_wrong_name():
od = make_description()
meta = await MetaConfig(['name1', 'name2'], optiondescription=od)
assert len(list(await meta.config.list())) == 2
with pytest.raises(ConfigError):
await meta.config.load('name3')
assert len(list(await meta.config.list())) == 2
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_meta_set():
meta = await make_metaconfig(double=True)
errors1 = await meta.value.set('od1.i1', 7, only_config=True)
errors2 = await meta.value.set('od1.i6', 7, only_config=True)
2017-07-21 18:03:34 +02:00
assert len(errors1) == 0
assert len(errors2) == 2
2019-12-24 15:24:20 +01:00
conf1 = await meta.config('meta.conf1')
conf2 = await meta.config('meta.conf2')
assert await conf1.option('od1.i1').value.get() == await conf2.option('od1.i1').value.get() == 7
2018-08-17 22:12:50 +02:00
#
dconfigs = []
2019-12-24 15:24:20 +01:00
ret = await meta.config.find('i1', value=7)
for conf in await ret.config.list():
dconfigs.append(conf._config_bag.context)
2019-12-24 15:24:20 +01:00
assert [conf1._config_bag.context, conf2._config_bag.context] == dconfigs
await conf1.option('od1.i1').value.set(8)
2018-08-17 22:12:50 +02:00
#
dconfigs = []
2019-12-24 15:24:20 +01:00
ret = await meta.config.find('i1')
for conf in await ret.config.list():
dconfigs.append(conf._config_bag.context)
2019-12-24 15:24:20 +01:00
assert [conf1._config_bag.context, conf2._config_bag.context] == dconfigs
ret = await meta.config.find('i1', value=7)
assert len(await ret.config.list()) == 1
assert conf2._config_bag.context == list(await ret.config.list())[0]._config_bag.context
ret = await meta.config.find('i1', value=8)
assert len(await ret.config.list()) == 1
assert conf1._config_bag.context == list(await ret.config.list())[0]._config_bag.context
2018-08-17 22:12:50 +02:00
#
dconfigs = []
2019-12-24 15:24:20 +01:00
ret = await meta.config.find('i5', value=2)
for conf in await ret.config.list():
dconfigs.append(conf._config_bag.context)
2019-12-24 15:24:20 +01:00
assert [conf1._config_bag.context, conf2._config_bag.context] == dconfigs
#
2020-01-22 20:46:18 +01:00
with pytest.raises(AttributeError):
2019-12-24 15:24:20 +01:00
await meta.config.find('i1', value=10)
2020-01-22 20:46:18 +01:00
with pytest.raises(AttributeError):
2019-12-24 15:24:20 +01:00
await meta.config.find('not', value=10)
2020-01-22 20:46:18 +01:00
with pytest.raises(AttributeError):
2019-12-24 15:24:20 +01:00
await meta.config.find('i6')
2020-01-22 20:46:18 +01:00
with pytest.raises(ValueError):
2019-12-24 15:24:20 +01:00
await meta.value.set('od1.i6', 7, only_config=True, force_default=True)
2020-01-22 20:46:18 +01:00
with pytest.raises(ValueError):
2019-12-24 15:24:20 +01:00
await meta.value.set('od1.i6', 7, only_config=True, force_default_if_same=True)
2020-01-22 20:46:18 +01:00
with pytest.raises(ValueError):
2019-12-24 15:24:20 +01:00
await meta.value.set('od1.i6', 7, only_config=True, force_dont_change_value=True)
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_not_meta():
2013-09-17 09:02:10 +02:00
i1 = IntOption('i1', '')
od1 = OptionDescription('od1', '', [i1])
od2 = OptionDescription('od2', '', [od1])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od2, session_id='conf1')
conf2 = await Config(od2, session_id='conf2')
conf3 = await Config(od2)
conf4 = await Config(od2, session_id='conf4')
2020-01-22 20:46:18 +01:00
with pytest.raises(TypeError):
2019-12-24 15:24:20 +01:00
await GroupConfig(conf1)
2018-03-19 08:33:53 +01:00
#same name
2020-01-22 20:46:18 +01:00
#with pytest.raises(ConflictError):
2019-12-24 15:24:20 +01:00
# GroupConfig([conf2, conf4], session_id='conf2')")
2020-01-22 20:46:18 +01:00
with pytest.raises(ConflictError):
2019-12-24 15:24:20 +01:00
await GroupConfig([conf2, conf2], session_id='conf8')
grp = await GroupConfig([conf1, conf2])
2020-01-22 20:46:18 +01:00
with pytest.raises(APIError):
2019-12-24 15:24:20 +01:00
await grp.option('od1.i1').value.get()
conf1, conf2 = await grp.config.list()
errors = await grp.value.set('od1.i1', 7)
2018-03-19 08:33:53 +01:00
assert len(errors) == 0
2019-12-24 15:24:20 +01:00
conf1 = await grp.config('conf1')
conf2 = await grp.config('conf2')
assert await conf1.option('od1.i1').value.get() == await conf2.option('od1.i1').value.get() == 7
assert await conf1.option('od1.i1').owner.get() is await conf2.option('od1.i1').owner.get() is owners.user
await grp.option('od1.i1').value.reset()
assert await conf1.option('od1.i1').owner.get() is await conf2.option('od1.i1').owner.get() is owners.default
2020-01-22 20:46:18 +01:00
await delete_sessions([conf1, conf2, conf3, conf4])
2013-09-17 09:02:10 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_group_find_firsts():
i1 = IntOption('i1', '')
od1 = OptionDescription('od1', '', [i1])
od2 = OptionDescription('od2', '', [od1])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od2, session_id='conf1')
conf2 = await Config(od2, session_id='conf2')
grp = await GroupConfig([conf1, conf2])
ret = await grp.config.find('i1')
newconf1, newconf2 = await grp.config.list()
conf1._config_bag.context == newconf1._config_bag.context
conf2._config_bag.context == newconf2._config_bag.context
2020-01-22 20:46:18 +01:00
await delete_sessions([conf1, conf2])
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_group_group():
i1 = IntOption('i1', '')
od1 = OptionDescription('od1', '', [i1])
od2 = OptionDescription('od2', '', [od1])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od2, session_id='conf9')
conf2 = await Config(od2, session_id='conf10')
grp = await GroupConfig([conf1, conf2], 'grp')
grp2 = await GroupConfig([grp])
errors = await grp2.value.set('od1.i1', 2)
2017-07-21 18:03:34 +02:00
assert len(errors) == 0
2019-12-24 15:24:20 +01:00
conf9 = await grp2.config('grp.conf9')
assert await conf9.option('od1.i1').value.get() == 2
assert await conf9.option('od1.i1').owner.get() is owners.user
2020-01-22 20:46:18 +01:00
await delete_sessions([conf1, conf2, conf9])
2014-02-02 22:47:46 +01:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_group_group_path():
2019-02-06 21:47:11 +01:00
i1 = IntOption('i1', '')
od1 = OptionDescription('od1', '', [i1])
od2 = OptionDescription('od2', '', [od1])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od2, session_id='conf9')
conf2 = await Config(od2, session_id='conf10')
grp = await GroupConfig([conf1, conf2], 'grp')
grp2 = await GroupConfig([grp], 'grp2')
assert await grp2.config.path() == 'grp2'
newgrp = await grp2.config('grp')
assert await newgrp.config.path() == 'grp'
newgrp = await grp2.config('grp.conf9')
assert await newgrp.config.path() == 'conf9'
newgrp = await grp2.config('grp.conf10')
assert await newgrp.config.path() == 'conf10'
2020-01-22 20:46:18 +01:00
await delete_sessions([conf1, conf2])
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_unconsistent():
2014-02-02 22:47:46 +01:00
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])
i5 = IntOption('i5', '')
od3 = OptionDescription('od3', '', [i5])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od2, session_id='conf1')
conf2 = await Config(od2, session_id='conf2')
conf3 = await Config(od2, session_id='conf3')
conf4 = await Config(od3, session_id='conf4')
meta = await MetaConfig([conf1, conf2])
await meta.owner.set(owners.meta1)
2020-01-22 20:46:18 +01:00
with pytest.raises(TypeError):
2019-12-24 15:24:20 +01:00
await MetaConfig("string")
#same descr but conf1 already in meta
2019-12-24 15:24:20 +01:00
assert len(list(await conf1.config.parents())) == 1
assert len(list(await conf3.config.parents())) == 0
new_meta = await MetaConfig([conf1, conf3])
assert len(list(await conf1.config.parents())) == 2
assert len(list(await conf3.config.parents())) == 1
#not same descr
2020-01-22 20:46:18 +01:00
with pytest.raises(ValueError):
2019-12-24 15:24:20 +01:00
await MetaConfig([conf3, conf4])
2020-01-22 20:46:18 +01:00
await delete_sessions([meta, new_meta, conf4])
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_leadership():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',))
2019-02-23 19:06:23 +01:00
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
2018-03-19 08:33:53 +01:00
od = OptionDescription('root', '', [interface1])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od, session_id='conf1')
conf2 = await Config(od, session_id='conf2')
meta = await MetaConfig([conf1, conf2])
await meta.property.read_only()
ret = await meta.config.find('ip_admin_eth0')
configs = await ret.config.list()
assert len(configs) == 2
assert conf1._config_bag.context == configs[0]._config_bag.context
assert conf2._config_bag.context == configs[1]._config_bag.context
ret = await meta.config.find('netmask_admin_eth0')
configs = await ret.config.list()
assert len(configs) == 2
assert conf1._config_bag.context == configs[0]._config_bag.context
assert conf2._config_bag.context == configs[1]._config_bag.context
await meta.property.read_write()
2020-01-22 20:46:18 +01:00
with pytest.raises(AttributeError):
2019-12-24 15:24:20 +01:00
await meta.config.find('netmask_admin_eth0')
ret = await meta.unrestraint.config.find('netmask_admin_eth0')
configs = await ret.config.list()
assert conf1._config_bag.context == configs[0]._config_bag.context
assert conf2._config_bag.context == configs[1]._config_bag.context
await meta.property.read_only()
ret = await meta.config.find('netmask_admin_eth0')
configs = await ret.config.list()
assert conf1._config_bag.context == configs[0]._config_bag.context
assert conf2._config_bag.context == configs[1]._config_bag.context
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_leadership_value():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',))
2019-02-23 19:06:23 +01:00
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
2018-03-19 08:33:53 +01:00
od = OptionDescription('root', '', [interface1])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od, session_id='conf1')
conf2 = await Config(od, session_id='conf2')
meta = await MetaConfig([conf1, conf2], session_id="meta")
await conf1.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.8'])
assert await conf1.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == None
2020-01-22 20:46:18 +01:00
with pytest.raises(APIError):
2019-12-24 15:24:20 +01:00
await conf1.option('ip_admin_eth0.ip_admin_eth0', 0).value.get()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await conf1.option('ip_admin_eth0.ip_admin_eth0').value.reset()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await meta.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.1'])
assert await conf1.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == None
await meta.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('255.255.255.0')
assert await conf1.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.255.0'
await meta.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('255.255.0.0')
assert await conf1.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.0.0'
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await conf1.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.1'])
assert await conf1.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.0.0'
#
2019-12-24 15:24:20 +01:00
await meta.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.2.1', '192.168.3.1'])
await meta.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('255.255.255.0')
#
2019-12-24 15:24:20 +01:00
assert await conf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.1']
assert await conf1.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.0.0'
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_leadership_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)
2019-02-23 19:06:23 +01:00
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
2018-03-19 08:33:53 +01:00
od = OptionDescription('root', '', [interface1])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od, session_id='conf1')
conf2 = await Config(od, session_id='conf2')
meta = await MetaConfig([conf1, conf2])
newconf1 = await meta.config('conf1')
assert await newconf1.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == None
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await meta.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.1'])
assert await newconf1.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == None
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await meta.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('255.255.255.0')
assert await newconf1.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.255.0'
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await meta.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('255.255.0.0')
assert await newconf1.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.0.0'
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.1'])
assert await newconf1.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.0.0'
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_leadership_owners():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',))
2019-02-23 19:06:23 +01:00
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
2018-03-19 08:33:53 +01:00
od = OptionDescription('root', '', [interface1])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od, session_id='conf1')
conf2 = await Config(od, session_id='conf2')
meta = await MetaConfig([conf1, conf2])
await meta.owner.set(owners.meta1)
newconf1 = await meta.config('conf1')
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').owner.isdefault()
2020-01-22 20:46:18 +01:00
with pytest.raises(LeadershipError):
2019-12-24 15:24:20 +01:00
await newconf1.option('ip_admin_eth0.netmask_admin_eth0', 0).owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.1'])
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').owner.get() == owners.user
assert await newconf1.option('ip_admin_eth0.netmask_admin_eth0', 0).owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.reset()
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await meta.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.1'])
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').owner.get() == owners.meta1
assert await newconf1.option('ip_admin_eth0.netmask_admin_eth0', 0).owner.isdefault()
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await meta.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('255.255.255.0')
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').owner.get() == owners.meta1
assert await newconf1.option('ip_admin_eth0.netmask_admin_eth0', 0).owner.get() == owners.meta1
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await meta.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('255.255.0.0')
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').owner.get() == owners.meta1
assert await newconf1.option('ip_admin_eth0.netmask_admin_eth0', 0).owner.get() == owners.meta1
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.1'])
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').owner.get() == owners.user
assert await newconf1.option('ip_admin_eth0.netmask_admin_eth0', 0).owner.get() == owners.meta1
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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',))
2019-02-23 19:06:23 +01:00
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
2018-03-19 08:33:53 +01:00
od = OptionDescription('root', '', [interface1])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od, session_id='conf1')
conf2 = await Config(od, session_id='conf2')
meta = await MetaConfig([conf1, conf2])
await meta.property.read_write()
await meta.owner.set('meta1')
newconf1 = await meta.config('conf1')
newconf2 = await meta.config('conf2')
assert await meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
#
errors = await meta.value.set('ip_admin_eth0.ip_admin_eth0', ['192.168.1.1'])
2017-07-21 18:03:34 +02:00
assert len(errors) == 0
2019-12-24 15:24:20 +01:00
assert await meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.1']
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.1']
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.1']
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.2'])
assert await meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.1']
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.2']
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.1']
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
errors = await meta.value.set('ip_admin_eth0.ip_admin_eth0', ['192.168.1.3'])
2017-07-21 18:03:34 +02:00
assert len(errors) == 0
2019-12-24 15:24:20 +01:00
assert await meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.3']
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.2']
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.3']
2018-03-19 08:33:53 +01:00
#
2019-12-24 15:24:20 +01:00
errors = await meta.value.set('ip_admin_eth0.ip_admin_eth0', ['192.168.1.4'], force_default=True)
2017-07-21 18:03:34 +02:00
assert len(errors) == 0
2019-12-24 15:24:20 +01:00
assert await meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.4']
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.4']
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.4']
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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',))
2019-02-23 19:06:23 +01:00
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
2018-03-19 08:33:53 +01:00
od = OptionDescription('root', '', [interface1])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od, session_id='conf1')
conf2 = await Config(od, session_id='conf2')
meta = await MetaConfig([conf1, conf2])
await meta.property.read_write()
await meta.owner.set('meta1')
assert await meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
newconf1 = await meta.config('conf1')
newconf2 = await meta.config('conf2')
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.4'])
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.4']
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').owner.get() is owners.user
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').owner.isdefault()
errors = await meta.value.set('ip_admin_eth0.ip_admin_eth0', ['192.168.1.4'], force_dont_change_value=True)
2017-07-21 18:03:34 +02:00
assert len(errors) == 0
2019-12-24 15:24:20 +01:00
assert await meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.4']
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.4']
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').owner.get() is owners.user
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').owner.get() is owners.user
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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',))
2019-02-23 19:06:23 +01:00
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
2018-03-19 08:33:53 +01:00
od = OptionDescription('root', '', [interface1])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od, session_id='conf1')
conf2 = await Config(od, session_id='conf2')
meta = await MetaConfig([conf1, conf2])
await meta.property.read_write()
await meta.owner.set('meta1')
#
newconf1 = await meta.config('conf1')
newconf2 = await meta.config('conf2')
assert await meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
#
await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.4'])
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.4']
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').owner.get() is owners.user
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').owner.isdefault()
errors = await meta.value.set('ip_admin_eth0.ip_admin_eth0', ['192.168.1.4'], force_default_if_same=True)
2017-07-21 18:03:34 +02:00
assert len(errors) == 0
2019-12-24 15:24:20 +01:00
assert await meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.4']
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.4']
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.4']
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').owner.get() is owners.meta1
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').owner.get() is owners.meta1
#
await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.3'])
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.3']
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.4']
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').owner.get() is owners.user
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').owner.get() is owners.meta1
errors = await meta.value.set('ip_admin_eth0.ip_admin_eth0', ['192.168.1.5'], force_default_if_same=True)
2017-07-21 18:03:34 +02:00
assert len(errors) == 0
2019-12-24 15:24:20 +01:00
assert await meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.5']
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.3']
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.5']
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').owner.get() is owners.user
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').owner.get() is owners.meta1
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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',))
2019-02-23 19:06:23 +01:00
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
2018-03-19 08:33:53 +01:00
od = OptionDescription('root', '', [interface1])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od, session_id='conf1')
conf2 = await Config(od, session_id='conf2')
meta = await MetaConfig([conf1, conf2])
await meta.property.read_write()
await meta.owner.set('meta1')
#
newconf1 = await meta.config('conf1')
newconf2 = await meta.config('conf2')
assert await meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
#
await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.4'])
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.4']
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').owner.get() is owners.user
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').owner.isdefault()
errors = await meta.value.set('ip_admin_eth0.ip_admin_eth0', ['192.168.1.4'], force_default_if_same=True, force_dont_change_value=True)
2017-07-21 18:03:34 +02:00
assert len(errors) == 0
2019-12-24 15:24:20 +01:00
assert await meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.4']
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.4']
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').owner.get() is owners.meta1
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').owner.get() is owners.user
#
await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.3'])
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.3']
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').owner.get() is owners.user
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').owner.get() is owners.user
errors = await meta.value.set('ip_admin_eth0.ip_admin_eth0', ['192.168.1.5'], force_default_if_same=True, force_dont_change_value=True)
2017-07-21 18:03:34 +02:00
assert len(errors) == 0
2019-12-24 15:24:20 +01:00
assert await meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.5']
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.3']
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').owner.get() is owners.user
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').owner.get() is owners.user
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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',))
2019-02-23 19:06:23 +01:00
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
2018-03-19 08:33:53 +01:00
od = OptionDescription('root', '', [interface1])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od, session_id='rconf1')
conf2 = await Config(od, session_id='rconf2')
meta = await MetaConfig([conf1, conf2])
await meta.property.read_write()
await meta.owner.set('meta1')
2020-01-22 20:46:18 +01:00
with pytest.raises(ValueError):
2019-12-24 15:24:20 +01:00
await meta.value.set('ip_admin_eth0.ip_admin_eth0', ['192.168.1.4'], force_default=True, force_dont_change_value=True)
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2017-02-11 17:22:50 +01:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_properties_meta():
2017-02-11 17:22:50 +01:00
ip_admin_eth0 = NetworkOption('ip_admin_eth0', "ip", multi=True, default=['192.168.1.1'])
2019-10-27 11:09:15 +01:00
netmask_admin_eth0 = NetmaskOption('netmask_admin_eth0', "mask", multi=True, validators=[Calculation(valid_network_netmask, Params((ParamOption(ip_admin_eth0), ParamSelfOption())))])
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0], properties=('disabled',))
2018-03-19 08:33:53 +01:00
od = OptionDescription('root', '', [interface1])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od, session_id='conf1')
conf2 = await Config(od, session_id='conf2')
await conf1.property.read_write()
await conf2.property.read_write()
meta = await MetaConfig([conf1, conf2])
await meta.property.read_write()
ret = await meta.config('conf1')
assert await ret.value.dict() == {}
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_exception_meta():
2017-02-11 17:22:50 +01:00
ip_admin_eth0 = NetworkOption('ip_admin_eth0', "ip", multi=True, default=['192.168.1.1'])
2019-10-27 11:09:15 +01:00
netmask_admin_eth0 = NetmaskOption('netmask_admin_eth0', "mask", Calculation(raise_exception), multi=True, validators=[Calculation(valid_network_netmask, Params((ParamOption(ip_admin_eth0), ParamSelfOption())))])
2019-02-23 19:06:23 +01:00
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
2018-03-19 08:33:53 +01:00
od = OptionDescription('root', '', [interface1])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od, session_id='conf1')
conf2 = await Config(od, session_id='conf2')
meta = await MetaConfig([conf1, conf2])
await meta.property.read_write()
2020-01-22 20:46:18 +01:00
with pytest.raises(Exception):
2019-12-24 15:24:20 +01:00
await conf1.make_dict()
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2017-07-16 23:11:12 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_properties_requires1():
opt1 = BoolOption('opt1', 'opt1', False)
opt2 = BoolOption('opt2', "")
2019-09-01 09:41:53 +02:00
disabled_property = Calculation(calc_value,
Params(ParamValue('disabled'),
kwargs={'condition': ParamOption(opt1, todict=True),
'expected': ParamValue(False)}))
od2 = OptionDescription('od2', "", [opt2], properties=(disabled_property,))
2019-10-27 11:09:15 +01:00
opt3 = BoolOption('opt3', '', validators=[Calculation(valid_not_equal, Params((ParamOption(opt2), ParamSelfOption())))])
od = OptionDescription('root', '', [opt1, od2, opt3])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od, session_id='conf1')
await conf1.property.read_write()
meta = await MetaConfig([conf1], 'meta')
await meta.property.read_write()
await meta.option('opt1').value.set(True)
#
2019-12-24 15:24:20 +01:00
await conf1.option('od2.opt2').value.set(False)
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_properties_requires_mandatory():
2019-02-10 20:59:03 +01:00
probes = BoolOption('probes', 'probes available', False)
eth0_method = ChoiceOption('eth0_method', '', ('static', 'dhcp'), 'static')
ip_address = IPOption('ip_address', '')
2019-09-01 09:41:53 +02:00
mandatory_property = Calculation(calc_value,
Params(ParamValue('mandatory'),
kwargs={'condition': ParamOption(probes),
'expected': ParamValue('yes'),
'default': ParamValue(None)}))
2019-09-28 16:32:48 +02:00
ip_eth0 = IPOption('ip_eth0', "ip", Calculation(return_condition, Params(kwargs={'val': ParamOption(ip_address), 'condition': ParamOption(eth0_method), 'expected': ParamValue('dhcp')})), properties=(mandatory_property,))
2019-10-27 11:09:15 +01:00
ip_gw = IPOption('ip_gw', 'gw', validators=[Calculation(valid_not_equal, Params((ParamOption(ip_eth0), ParamSelfOption())))])
2019-02-10 20:59:03 +01:00
od = OptionDescription('root', '', [ip_gw, probes, eth0_method, ip_address, ip_eth0])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od, session_id='conf1')
await conf1.property.read_write()
meta = await MetaConfig([conf1], 'meta')
2019-02-10 20:59:03 +01:00
#
2019-12-24 15:24:20 +01:00
await meta.option('probes').value.set(True)
await meta.option('ip_address').value.set('1.1.1.1')
await meta.option('ip_gw').value.set('1.1.1.2')
await conf1.option('eth0_method').value.set('dhcp')
await conf1.property.read_only()
assert await conf1.value.dict(fullpath=True) == {'probes': True, 'eth0_method': 'dhcp', 'ip_address': '1.1.1.1', 'ip_eth0': '1.1.1.1', 'ip_gw': '1.1.1.2'}
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-02-10 20:59:03 +01:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_callback():
2017-07-16 23:11:12 +02:00
val1 = StrOption('val1', "", 'val')
2019-09-28 16:32:48 +02:00
val2 = StrOption('val2', "", Calculation(return_value, Params(ParamOption(val1))))
val3 = StrOption('val3', "", Calculation(return_value, Params(ParamValue('yes'))))
val4 = StrOption('val4', "", Calculation(return_value, Params(kwargs={'value': ParamOption(val1)})))
val5 = StrOption('val5', "", Calculation(return_value, Params(kwargs={'value': ParamValue('yes')})))
2017-07-16 23:11:12 +02:00
maconfig = OptionDescription('rootconfig', '', [val1, val2, val3, val4, val5])
2019-12-24 15:24:20 +01:00
cfg = await Config(maconfig, session_id='cfg')
meta = await MetaConfig([cfg])
await meta.property.read_write()
newcfg = await meta.config('cfg')
assert await newcfg.value.dict() == {'val3': 'yes', 'val2': 'val', 'val1': 'val', 'val5': 'yes', 'val4': 'val'}
await newcfg.option('val1').value.set('new')
assert await newcfg.value.dict() == {'val3': 'yes', 'val2': 'new', 'val1': 'new', 'val5': 'yes', 'val4': 'new'}
await newcfg.option('val1').value.reset()
await meta.option('val1').value.set('new')
assert await newcfg.value.dict() == {'val3': 'yes', 'val2': 'new', 'val1': 'new', 'val5': 'yes', 'val4': 'new'}
await newcfg.option('val4').value.set('new1')
assert await newcfg.value.dict() == {'val3': 'yes', 'val2': 'new', 'val1': 'new', 'val5': 'yes', 'val4': 'new1'}
await newcfg.option('val4').value.reset()
await meta.option('val4').value.set('new1')
assert await newcfg.value.dict() == {'val3': 'yes', 'val2': 'new', 'val1': 'new', 'val5': 'yes', 'val4': 'new1'}
await meta.option('val4').value.reset()
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_callback_follower():
2017-07-16 23:11:12 +02:00
val = StrOption('val', "", default='val')
2019-09-28 16:32:48 +02:00
val1 = StrOption('val1', "", [Calculation(return_value, Params(ParamOption(val)))], multi=True)
val3 = StrOption('val2', "", Calculation(return_value, Params(ParamOption(val1))), multi=True)
val4 = StrOption('val3', "", Calculation(return_value, Params(ParamOption(val1))), multi=True)
2019-02-23 19:06:23 +01:00
interface1 = Leadership('val1', '', [val1, val3, val4])
2018-03-19 08:33:53 +01:00
od = OptionDescription('root', '', [interface1])
2017-07-16 23:11:12 +02:00
maconfig = OptionDescription('rootconfig', '', [val, interface1])
2019-12-24 15:24:20 +01:00
conf1 = await Config(maconfig, session_id='conf1')
meta = await MetaConfig([conf1])
await meta.property.read_write()
assert await conf1.value.dict() == {'val1.val2': ['val'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}
2019-08-05 22:31:56 +02:00
#
2019-12-24 15:24:20 +01:00
await conf1.option('val').value.set('val1')
assert await conf1.value.dict() == {'val1.val2': ['val1'], 'val1.val1': ['val1'], 'val1.val3': ['val1'], 'val': 'val1'}
2019-08-05 22:31:56 +02:00
#
2019-12-24 15:24:20 +01:00
await conf1.option('val').value.reset()
await meta.option('val').value.set('val1')
assert await conf1.value.dict() == {'val1.val2': ['val1'], 'val1.val1': ['val1'], 'val1.val3': ['val1'], 'val': 'val1'}
2019-08-05 22:31:56 +02:00
#
2019-12-24 15:24:20 +01:00
await meta.option('val').value.reset()
await conf1.option('val1.val2', 0).value.set('val2')
assert await conf1.value.dict() == {'val1.val2': ['val2'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}
2019-08-05 22:31:56 +02:00
#
2019-12-24 15:24:20 +01:00
await conf1.option('val1.val2', 0).value.reset()
assert await conf1.value.dict() == {'val1.val2': ['val'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}
2019-08-05 22:31:56 +02:00
#
2019-12-24 15:24:20 +01:00
await meta.option('val1.val2', 0).value.set('val2')
assert await conf1.value.dict() == {'val1.val2': ['val2'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}
2019-08-05 22:31:56 +02:00
#
2019-12-24 15:24:20 +01:00
await meta.option('val1.val1').value.set(['val'])
assert await conf1.value.dict() == {'val1.val2': ['val2'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}
2019-08-05 22:31:56 +02:00
#
2019-12-24 15:24:20 +01:00
await conf1.option('val1.val3', 0).value.set('val6')
assert await conf1.value.dict() == {'val1.val2': ['val2'], 'val1.val1': ['val'], 'val1.val3': ['val6'], 'val': 'val'}
2019-08-05 22:31:56 +02:00
#
2019-12-24 15:24:20 +01:00
await meta.option('val1.val2', 0).value.reset()
await conf1.option('val1.val3', 0).value.reset()
await conf1.option('val1.val1').value.set(['val3'])
assert await conf1.value.dict() == {'val1.val2': ['val3'], 'val1.val1': ['val3'], 'val1.val3': ['val3'], 'val': 'val'}
2019-08-05 22:31:56 +02:00
#
2019-12-24 15:24:20 +01:00
await conf1.option('val1.val1').value.reset()
assert await conf1.value.dict() == {'val1.val2': ['val'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}
2019-08-05 22:31:56 +02:00
#
2019-12-24 15:24:20 +01:00
await meta.option('val1.val1').value.set(['val3'])
assert await conf1.value.dict() == {'val1.val2': ['val3'], 'val1.val1': ['val3'], 'val1.val3': ['val3'], 'val': 'val'}
2019-08-05 22:31:56 +02:00
#
2019-12-24 15:24:20 +01:00
await conf1.option('val1.val2', 0).value.set('val2')
assert await conf1.value.dict() == {'val1.val2': ['val2'], 'val1.val1': ['val3'], 'val1.val3': ['val3'], 'val': 'val'}
2019-08-05 22:31:56 +02:00
#
2019-12-24 15:24:20 +01:00
await meta.option('val1.val1').value.set(['val3', 'rah'])
assert await conf1.value.dict() == {'val1.val2': ['val2', 'rah'], 'val1.val1': ['val3', 'rah'], 'val1.val3': ['val3', 'rah'], 'val': 'val'}
2019-08-05 22:31:56 +02:00
#
2019-12-24 15:24:20 +01:00
await meta.option('val1.val1').value.pop(1)
await meta.option('val1.val1').value.set(['val4'])
assert await conf1.value.dict() == {'val1.val2': ['val2'], 'val1.val1': ['val4'], 'val1.val3': ['val4'], 'val': 'val'}
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2018-03-19 08:33:53 +01:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_reset():
2018-03-19 08:33:53 +01:00
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',))
2019-02-23 19:06:23 +01:00
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
2018-03-19 08:33:53 +01:00
od = OptionDescription('root', '', [interface1])
2019-12-24 15:24:20 +01:00
conf1 = await Config(od, session_id='conf1')
conf2 = await Config(od, session_id='conf2')
meta = await MetaConfig([conf1, conf2])
await meta.property.read_write()
await meta.owner.set('meta1')
newconf1 = await meta.config('conf1')
newconf2 = await meta.config('conf2')
assert await meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
errors = await meta.value.set('ip_admin_eth0.ip_admin_eth0', ['192.168.1.1'])
2018-03-19 08:33:53 +01:00
assert len(errors) == 0
2019-12-24 15:24:20 +01:00
assert await meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.1']
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.1']
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.1']
await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.2'])
assert await meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.1']
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.2']
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.1']
await meta.value.reset('ip_admin_eth0.ip_admin_eth0')
assert await meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
assert await newconf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
assert await newconf2.option('ip_admin_eth0.ip_admin_eth0').value.get() == []
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_properties_meta_copy():
2018-09-05 20:22:16 +02:00
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])
2019-12-24 15:24:20 +01:00
conf1 = await Config(interface1, session_id='conf1')
conf2 = await Config(interface1, session_id='conf2')
await conf1.property.read_write()
await conf2.property.read_write()
meta = await MetaConfig([conf1, conf2], session_id='meta1')
await meta.property.read_write()
newconf1 = await meta.config('conf1')
newconf2 = await meta.config('conf2')
conf3 = await newconf1.config.copy(session_id='conf3')
2019-08-05 22:31:56 +02:00
# old fashion
2019-12-24 15:24:20 +01:00
meta2 = await conf3.config.metaconfig()
2020-01-22 20:46:18 +01:00
assert await meta.session.id() == await meta2.session.id()
2019-08-05 22:31:56 +02:00
# new method
2019-12-24 15:24:20 +01:00
meta2 = list(await conf3.config.parents())
2019-08-05 22:31:56 +02:00
assert len(meta2) == 1
2020-01-22 20:46:18 +01:00
assert await meta.session.id() == await meta2[0].session.id()
2019-12-24 15:24:20 +01:00
assert await newconf1.value.dict() == {'ip_admin_eth0': ['192.168.1.1']}
assert await newconf2.value.dict() == {'ip_admin_eth0': ['192.168.1.1']}
newconf3 = await meta.config('conf3')
assert await newconf3.value.dict() == {'ip_admin_eth0': ['192.168.1.1']}
await meta.option('ip_admin_eth0').value.set(['192.168.1.2'])
assert await newconf1.value.dict() == {'ip_admin_eth0': ['192.168.1.2']}
assert await newconf2.value.dict() == {'ip_admin_eth0': ['192.168.1.2']}
assert await newconf3.value.dict() == {'ip_admin_eth0': ['192.168.1.2']}
ret = await meta.value.set('ip_admin_eth0', ['192.168.1.3'], force_default_if_same=True)
assert await newconf1.value.dict() == {'ip_admin_eth0': ['192.168.1.3']}
assert await newconf2.value.dict() == {'ip_admin_eth0': ['192.168.1.3']}
assert await newconf3.value.dict() == {'ip_admin_eth0': ['192.168.1.3']}
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_properties_meta_deepcopy():
2018-09-05 20:22:16 +02:00
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])
2019-12-24 15:24:20 +01:00
conf1 = await Config(interface1, session_id='conf1')
conf2 = await Config(interface1, session_id='conf2')
await conf1.property.read_write()
await conf2.property.read_write()
meta = await MetaConfig([conf1, conf2])
await meta.permissive.add('hidden')
await meta.property.read_write()
newconf1 = await meta.config('conf1')
newconf2 = await meta.config('conf2')
meta2 = await newconf1.config.deepcopy(session_id='conf3')
newconf3 = await meta2.config('conf3')
2018-09-05 20:22:16 +02:00
assert meta != meta2
2019-12-24 15:24:20 +01:00
assert await meta.permissive.get() == await meta2.permissive.get()
assert await newconf1.value.dict() == {'ip_admin_eth0': ['192.168.1.1']}
assert await newconf2.value.dict() == {'ip_admin_eth0': ['192.168.1.1']}
assert await newconf3.value.dict() == {'ip_admin_eth0': ['192.168.1.1']}
await meta.option('ip_admin_eth0').value.set(['192.168.1.2'])
assert await newconf1.value.dict() == {'ip_admin_eth0': ['192.168.1.2']}
assert await newconf2.value.dict() == {'ip_admin_eth0': ['192.168.1.2']}
assert await newconf3.value.dict() == {'ip_admin_eth0': ['192.168.1.1']}
await meta.value.set('ip_admin_eth0', ['192.168.1.3'], force_default_if_same=True)
assert await newconf1.value.dict() == {'ip_admin_eth0': ['192.168.1.3']}
assert await newconf2.value.dict() == {'ip_admin_eth0': ['192.168.1.3']}
assert await newconf3.value.dict() == {'ip_admin_eth0': ['192.168.1.1']}
2020-01-22 20:46:18 +01:00
await delete_sessions([meta, meta2])
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_properties_submeta_deepcopy():
2018-09-13 17:00:52 +02:00
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])
2019-12-24 15:24:20 +01:00
conf1 = await Config(interface1, session_id='conf1')
await conf1.property.read_write()
meta1 = await MetaConfig([conf1], session_id='meta1')
meta2 = await MetaConfig([meta1], session_id='meta2')
meta_copy = await conf1.config.deepcopy(session_id='conf2',
2018-09-13 17:00:52 +02:00
metaconfig_prefix='copy_')
2020-01-22 20:46:18 +01:00
assert await meta_copy.session.id() == 'copy_meta2'
2019-12-24 15:24:20 +01:00
newcopy = await meta_copy.config('copy_meta1')
2020-01-22 20:46:18 +01:00
assert await newcopy.session.id() == 'copy_meta1'
2019-12-24 15:24:20 +01:00
newcopy = await newcopy.config('conf2')
2020-01-22 20:46:18 +01:00
assert await newcopy.session.id() == 'conf2'
await delete_sessions([meta2, meta_copy])
2018-09-13 17:00:52 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_properties_deepcopy_meta():
2019-05-28 07:19:26 +02:00
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])
2019-12-24 15:24:20 +01:00
conf1 = await Config(interface1, session_id='conf1')
await conf1.property.read_write()
meta1 = await MetaConfig([conf1], session_id='meta1')
meta2 = await MetaConfig([meta1], session_id='meta2')
meta_copy = await meta1.config.deepcopy(session_id='meta3',
metaconfig_prefix='copy_')
2020-01-22 20:46:18 +01:00
assert await meta_copy.session.id() == 'copy_meta2'
2019-12-24 15:24:20 +01:00
newcopy = await meta_copy.config('meta3')
2020-01-22 20:46:18 +01:00
assert await newcopy.session.id() == 'meta3'
2019-12-24 15:24:20 +01:00
assert list(await newcopy.config.list()) == []
2020-01-22 20:46:18 +01:00
await delete_sessions([meta2, meta_copy])
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_properties_submeta_deepcopy_owner():
ip_admin_eth0 = NetworkOption('ip_admin_eth0', "ip")
netmask_admin_eth0 = NetmaskOption('netmask_admin_eth0', "mask")
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
2019-12-24 15:24:20 +01:00
conf1 = await Config(interface1, session_id='conf1')
await conf1.owner.set('conf1_user')
await conf1.property.read_write()
meta1 = await MetaConfig([conf1], session_id='meta1')
await meta1.owner.set('meta1_user')
meta2 = await MetaConfig([meta1], session_id='meta2')
await meta2.owner.set('meta2_user')
#
await conf1.option('ip_admin_eth0').value.set('192.168.0.1')
assert await conf1.option('ip_admin_eth0').owner.get() == 'conf1_user'
await meta1.option('ip_admin_eth0').value.set('192.168.0.2')
assert await meta1.option('ip_admin_eth0').owner.get() == 'meta1_user'
await meta2.option('ip_admin_eth0').value.set('192.168.0.3')
assert await meta2.option('ip_admin_eth0').owner.get() == 'meta2_user'
#
meta2_copy = await conf1.config.deepcopy(session_id='conf2',
metaconfig_prefix='copy_')
2019-12-24 15:24:20 +01:00
await meta2_copy.option('netmask_admin_eth0').value.set('255.255.255.255')
assert await meta2_copy.option('ip_admin_eth0').value.get() == '192.168.0.3'
assert await meta2_copy.option('ip_admin_eth0').owner.get() == 'meta2_user'
assert await meta2_copy.option('netmask_admin_eth0').owner.get() == 'meta2_user'
#
meta1_copy = await meta2_copy.config('copy_meta1')
await meta1_copy.option('netmask_admin_eth0').value.set('255.255.255.255')
assert await meta1_copy.option('ip_admin_eth0').value.get() == '192.168.0.2'
assert await meta1_copy.option('ip_admin_eth0').owner.get() == 'meta1_user'
assert await meta1_copy.option('netmask_admin_eth0').owner.get() == 'meta1_user'
#
conf2 = await meta1_copy.config('conf2')
await conf2.owner.set('conf2_user')
await conf2.option('netmask_admin_eth0').value.set('255.255.255.255')
assert await conf2.option('netmask_admin_eth0').owner.get() == 'conf2_user'
assert await conf2.option('ip_admin_eth0').value.get() == '192.168.0.1'
assert await conf2.option('ip_admin_eth0').owner.get() == 'conf1_user'
2020-01-22 20:46:18 +01:00
await delete_sessions([meta1, meta2, conf2, meta2_copy])
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async 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])
2019-12-24 15:24:20 +01:00
conf1 = await Config(interface1, session_id='conf1')
conf2 = await Config(interface1, session_id='conf2')
await conf1.property.read_write()
await conf2.property.read_write()
meta = await MetaConfig([conf1, conf2])
await meta.property.read_write()
ret = await meta.config('conf1')
assert await ret.value.dict() == {'ip_admin_eth0': ['192.168.1.1']}
ret = await meta.value.set('netmask_admin_eth0', ['255.255.255.255'], only_config=True)
assert len(ret) == 2
assert isinstance(ret[0], PropertiesOptionError)
assert isinstance(ret[1], PropertiesOptionError)
2018-10-31 08:00:19 +01:00
del ret[1]
del ret[0]
del ret
2019-12-24 15:24:20 +01:00
ret = await meta.value.set('netmask_admin_eth0', ['255.255.255.255'], force_default=True)
assert len(ret) == 1
assert isinstance(ret[0], PropertiesOptionError)
2018-10-31 08:00:19 +01:00
del ret[0]
del ret
2019-12-24 15:24:20 +01:00
ret = await meta.value.set('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)
2018-10-31 08:00:19 +01:00
del ret[2]
del ret[1]
del ret[0]
del ret
2019-12-24 15:24:20 +01:00
ret = await meta.value.set('netmask_admin_eth0', ['255.255.255.255'], force_default_if_same=True)
assert len(ret) == 1
assert isinstance(ret[0], PropertiesOptionError)
2018-10-31 08:00:19 +01:00
del ret[0]
del ret
2019-12-24 15:24:20 +01:00
ret = await meta.value.set('ip_admin_eth0', '255.255.255.255', only_config=True)
assert len(ret) == 2
assert isinstance(ret[0], ValueError)
assert isinstance(ret[1], ValueError)
2018-10-31 08:00:19 +01:00
del ret[1]
del ret[0]
del ret
2019-12-24 15:24:20 +01:00
ret = await meta.value.set('ip_admin_eth0', '255.255.255.255', force_default=True)
assert len(ret) == 1
assert isinstance(ret[0], ValueError)
2018-10-31 08:00:19 +01:00
del ret[0]
del ret
2019-12-24 15:24:20 +01:00
ret = await meta.value.set('ip_admin_eth0', '255.255.255.255', force_dont_change_value=True)
assert len(ret) == 1
assert isinstance(ret[0], ValueError)
2018-10-31 08:00:19 +01:00
del ret[0]
del ret
2019-12-24 15:24:20 +01:00
ret = await meta.value.set('ip_admin_eth0', '255.255.255.255', force_default_if_same=True)
assert len(ret) == 1
assert isinstance(ret[0], ValueError)
2018-10-31 08:00:19 +01:00
del ret[0]
del ret
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_metaconfig_force_metaconfig_on_freeze():
dummy1 = StrOption('dummy1', 'doc dummy', default='default', properties=('force_metaconfig_on_freeze',))
group = OptionDescription('group', '', [dummy1])
2019-12-24 15:24:20 +01:00
cfg = await Config(group, session_id='cfg')
await cfg.owner.set(owners.config)
meta1 = await MetaConfig([cfg], session_id='meta1')
await meta1.owner.set(owners.meta1)
meta2 = await MetaConfig([meta1], session_id='meta2')
await meta2.owner.set(owners.meta2)
await cfg.property.read_write()
2019-12-24 15:24:20 +01:00
await cfg.option('dummy1').property.add('frozen')
#
2019-12-24 15:24:20 +01:00
assert await cfg.option('dummy1').value.get() == 'default'
assert await cfg.option('dummy1').owner.get() == 'default'
#
2019-12-24 15:24:20 +01:00
await meta2.option('dummy1').value.set('meta2')
#
2019-12-24 15:24:20 +01:00
assert await cfg.option('dummy1').value.get() == 'meta2'
assert await cfg.option('dummy1').owner.get() == 'meta2'
#
2019-12-24 15:24:20 +01:00
await cfg.option('dummy1').property.pop('frozen')
await cfg.option('dummy1').value.set('cfg')
await cfg.option('dummy1').property.add('frozen')
#
2019-12-24 15:24:20 +01:00
assert await cfg.option('dummy1').value.get() == 'meta2'
assert await cfg.option('dummy1').owner.get() == 'meta2'
#
2019-12-24 15:24:20 +01:00
await meta1.option('dummy1').value.set('meta1')
#
2019-12-24 15:24:20 +01:00
assert await cfg.option('dummy1').value.get() == 'meta1'
assert await cfg.option('dummy1').owner.get() == 'meta1'
#
2019-12-24 15:24:20 +01:00
await cfg.option('dummy1').property.pop('frozen')
assert await cfg.option('dummy1').value.get() == 'cfg'
assert await cfg.option('dummy1').owner.get() == 'config'
2020-01-22 20:46:18 +01:00
await delete_sessions([meta1, meta2])
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_metaconfig_force_metaconfig_on_freeze_option():
dummy1 = StrOption('dummy1', 'doc dummy', default='default')
dummy2 = StrOption('dummy2', 'doc dummy', default='default', properties=('force_default_on_freeze',))
group = OptionDescription('group', '', [dummy1, dummy2])
2019-12-24 15:24:20 +01:00
cfg = await Config(group, session_id='cfg')
await cfg.owner.set(owners.config)
meta1 = await MetaConfig([cfg], session_id='meta1')
await meta1.owner.set(owners.meta1)
meta2 = await MetaConfig([meta1], session_id='meta2')
await meta2.owner.set(owners.meta2)
await cfg.property.read_write()
await cfg.option('dummy1').property.add('frozen')
await cfg.option('dummy1').property.add('force_metaconfig_on_freeze')
await cfg.option('dummy2').property.add('frozen')
#
assert await cfg.option('dummy1').value.get() == 'default'
assert await cfg.option('dummy1').owner.get() == 'default'
assert await cfg.option('dummy2').value.get() == 'default'
assert await cfg.option('dummy2').owner.get() == 'default'
#
await meta2.option('dummy1').value.set('meta2')
await meta2.option('dummy2').value.set('meta2')
#
assert await cfg.option('dummy1').value.get() == 'meta2'
assert await cfg.option('dummy1').owner.get() == 'meta2'
assert await cfg.option('dummy2').value.get() == 'default'
assert await cfg.option('dummy2').owner.get() == 'default'
#
await cfg.option('dummy1').property.pop('frozen')
await cfg.option('dummy2').property.pop('frozen')
await cfg.option('dummy1').value.set('cfg')
await cfg.option('dummy2').value.set('cfg')
await cfg.option('dummy1').property.add('frozen')
await cfg.option('dummy2').property.add('frozen')
#
assert await cfg.option('dummy1').value.get() == 'meta2'
assert await cfg.option('dummy1').owner.get() == 'meta2'
assert await cfg.option('dummy2').value.get() == 'default'
assert await cfg.option('dummy2').owner.get() == 'default'
#
await meta1.option('dummy1').value.set('meta1')
await meta1.option('dummy2').value.set('meta1')
#
assert await cfg.option('dummy1').value.get() == 'meta1'
assert await cfg.option('dummy1').owner.get() == 'meta1'
assert await cfg.option('dummy2').value.get() == 'default'
assert await cfg.option('dummy2').owner.get() == 'default'
#
await meta1.option('dummy1').property.add('force_metaconfig_on_freeze')
assert await cfg.option('dummy1').value.get() == 'meta2'
assert await cfg.option('dummy1').owner.get() == 'meta2'
#
await meta2.option('dummy1').property.add('force_metaconfig_on_freeze')
assert await cfg.option('dummy1').value.get() == 'default'
assert await cfg.option('dummy1').owner.get() == 'default'
#
await meta1.option('dummy1').property.pop('force_metaconfig_on_freeze')
assert await cfg.option('dummy1').value.get() == 'meta1'
assert await cfg.option('dummy1').owner.get() == 'meta1'
#
await cfg.option('dummy1').property.pop('frozen')
assert await cfg.option('dummy1').value.get() == 'cfg'
assert await cfg.option('dummy1').owner.get() == 'config'
2020-01-22 20:46:18 +01:00
await delete_sessions([meta1, meta2])
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_meta_get_config():
od = make_description()
2019-12-24 15:24:20 +01:00
meta = await MetaConfig(['name1', 'name2'], optiondescription=od)
await meta.config.new('meta1', type='metaconfig')
assert isinstance(await meta.config.get('meta1'), MetaConfig)
assert isinstance(await meta.config.get('name1'), Config)
2020-01-22 20:46:18 +01:00
with pytest.raises(ConfigError):
2019-12-24 15:24:20 +01:00
await meta.config.get('unknown')
2020-01-22 20:46:18 +01:00
await delete_sessions(meta)