- add api.owner.get() to get default owner for a context + tests

- slave: get length for a specified slave and test
- add test for deref objects and default_multi
- add issubmulti, getdefault, getdefaultmulti
- start metaconfig support in api
This commit is contained in:
2017-11-16 21:30:07 +01:00
parent 007a22ca94
commit 93fe29b651
6 changed files with 371 additions and 168 deletions

View File

@ -177,7 +177,8 @@ def test_callback_submulti_list_list():
cfg = Config(od)
cfg.read_write()
api = getapi(cfg)
owner = cfg.cfgimpl_get_settings().getowner()
owner = api.owner.get()
assert owner == 'user'
assert api.option('multi').value.get() == [['val', 'val']]
assert api.option('multi').owner.isdefault()
api.option('multi').value.set([['val', 'val'], undefined])
@ -203,46 +204,3 @@ def test_callback_submulti_list_list():
# multi.append()
# assert conf1.multi == [['val', None]]
# assert meta.multi == [['val']]
# ____________________________________________________________
# Master Slaves
def test_values_with_master_and_slaves_submulti():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip reseau autorise", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-reseau", multi=submulti)
interface1 = MasterSlaves('f_ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
conf = OptionDescription('conf', '', [interface1])
cfg = Config(conf)
api = getapi(cfg)
cfg.read_write()
owner = cfg.cfgimpl_get_settings().getowner()
#FIXME
assert interface1.impl_get_group_type() == groups.master
assert api.option('f_ip_admin_eth0.ip_admin_eth0').owner.isdefault()
#FIXME raises(IndexError, "api.option('f_ip_admin_eth0.netmask_admin_eth0', 0).value.get()")
api.option('f_ip_admin_eth0.ip_admin_eth0').value.set(['192.168.230.145'])
assert api.option('f_ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.230.145']
assert api.option('f_ip_admin_eth0.netmask_admin_eth0', 0).value.get() == []
#FIXME raises(IndexError, "api.option('f_ip_admin_eth0.netmask_admin_eth0', 1).value.get()")
assert api.option('f_ip_admin_eth0.ip_admin_eth0').owner.get() == owner
assert api.option('f_ip_admin_eth0.netmask_admin_eth0', 0).owner.isdefault()
api.option('f_ip_admin_eth0.ip_admin_eth0').value.set(['192.168.230.145', '192.168.230.147'])
assert api.option('f_ip_admin_eth0.netmask_admin_eth0', 0).value.get() == []
assert api.option('f_ip_admin_eth0.netmask_admin_eth0', 1).value.get() == []
#FIXME raises(IndexError, "api.option('f_ip_admin_eth0.netmask_admin_eth0', 2).value.get()")
#FIXME raises(IndexError, "api.option('f_ip_admin_eth0.netmask_admin_eth0', 2).value.set(['255.255.255.0'])")
api.option('f_ip_admin_eth0.netmask_admin_eth0', 0).value.set(['255.255.255.0'])
assert api.option('f_ip_admin_eth0.netmask_admin_eth0', 0).value.get() == ['255.255.255.0']
assert api.option('f_ip_admin_eth0.netmask_admin_eth0', 1).value.get() == []
api.option('f_ip_admin_eth0.netmask_admin_eth0', 1).value.set(['255.255.255.255'])
assert api.option('f_ip_admin_eth0.netmask_admin_eth0', 0).value.get() == ['255.255.255.0']
assert api.option('f_ip_admin_eth0.netmask_admin_eth0', 1).value.get() == ['255.255.255.255']
assert api.option('f_ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.230.145', '192.168.230.147']
raises(ValueError, "api.option('f_ip_admin_eth0.ip_admin_eth0').value.set(['192.168.230.145'])")
api.option('f_ip_admin_eth0.ip_admin_eth0').value.pop(1)
assert api.option('f_ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.230.145']
assert api.option('f_ip_admin_eth0.netmask_admin_eth0', 0).value.get() == ['255.255.255.0']
#FIXME raises(IndexError, "api.option('f_ip_admin_eth0.netmask_admin_eth0', 1).value.get()")

View File

@ -1,20 +1,37 @@
"""test API
"""
import weakref
import pytest
from py.test import raises
import weakref
from .autopath import do_autopath
do_autopath()
from tiramisu import Config, StrOption, OptionDescription, MasterSlaves, DynOptionDescription, \
getapi
from tiramisu import Config, MetaConfig, \
StrOption, OptionDescription, MasterSlaves, DynOptionDescription, \
getapi, submulti, undefined
from tiramisu.error import PropertiesOptionError, APIError
from collections import OrderedDict
ICON = u'\u2937'
OPTIONS_TYPE = {'str': {'type': str,
'option': StrOption}
}
}
PROPERTIES = ['hidden', 'disabled']
OWNER = 'user'
# multi is False
FIRST_VALUE = 'myvalue'
SECOND_VALUE = 'myvalue1'
EMPTY_VALUE = None
# multi is True
LIST_FIRST_VALUE = ['myvalue']
LIST_SECOND_VALUE = ['myvalue', 'myvalue1']
LIST_EMPTY_VALUE = []
# multi is submulti
SUBLIST_FIRST_VALUE = [['myvalue']]
SUBLIST_SECOND_VALUE = [['myvalue'], ['myvalue1', 'myvalue2']]
SUBLIST_EMPTY_VALUE = []
DISPLAY = True
#DISPLAY = False
@ -94,6 +111,11 @@ def autocheck_owner_without_value(api, path, **kwargs):
else:
raises(PropertiesOptionError, "api.forcepermissive.option(path).owner.isdefault()")
def _getdefault(api, path, multi, isslave, submulti_):
empty_value = api.unrestraint.option(path).option.getdefault()
if isslave and empty_value == []:
empty_value = api.unrestraint.option(path).option.getdefaultmulti()
return empty_value
@autocheck
def autocheck_value(api, path, **kwargs):
@ -102,25 +124,27 @@ def autocheck_value(api, path, **kwargs):
# check if is a multi, a master or a slave
if not kwargs.get('propertyerror', False):
multi = api.forcepermissive.option(path).option.ismulti()
submulti_ = api.forcepermissive.option(path).option.issubmulti()
ismaster = api.forcepermissive.option(path).option.ismaster()
isslave = api.forcepermissive.option(path).option.isslave()
else:
raises(PropertiesOptionError, "api.forcepermissive.option(path).option.ismulti()")
multi = api.unrestraint.option(path).option.ismulti()
submulti_ = api.unrestraint.option(path).option.issubmulti()
ismaster = api.unrestraint.option(path).option.ismaster()
isslave = api.unrestraint.option(path).option.isslave()
# set default value (different if value is multi or not)
if not multi:
first_value = 'myvalue'
second_value = 'myvalue1'
first_value = FIRST_VALUE
second_value = SECOND_VALUE
elif submulti_ is False:
first_value = LIST_FIRST_VALUE
second_value = LIST_SECOND_VALUE
else:
first_value = ['myvalue']
second_value = ['myvalue', 'myvalue1']
if multi and not isslave:
empty_value = []
else:
empty_value = None
first_value = SUBLIST_FIRST_VALUE
second_value = SUBLIST_SECOND_VALUE
empty_value = _getdefault(api, path, multi, isslave, submulti_)
# test default value (should be empty) without permissive
# cannot test for slave (we cannot get all values for a slave)
@ -239,23 +263,63 @@ def autocheck_value(api, path, **kwargs):
raises(PropertiesOptionError, "api.forcepermissive.option(path).value.get()")
@autocheck
def autocheck_value_slave(api, path, **kwargs):
multi = api.unrestraint.option(path).option.ismulti()
isslave = api.unrestraint.option(path).option.isslave()
if not isslave:
#FIXME verifier pas de len !
return
if kwargs.get('propertyerror', False):
return
submulti_ = api.forcepermissive.option(path).option.issubmulti()
if not kwargs.get('permissive', False):
length = api.option(path).value.len()
else:
length = api.forcepermissive.option(path).value.len()
assert length == 2
value = []
for idx in range(length):
value.append(api.forcepermissive.option(path, idx).value.get())
if not submulti_:
second_value = LIST_SECOND_VALUE
else:
second_value = SUBLIST_SECOND_VALUE
empty_value = _getdefault(api, path, multi, isslave, submulti_)
assert value == [empty_value, second_value[1]]
# cannot access to a slave with index too high
if submulti_ is False:
value = LIST_FIRST_VALUE[0]
else:
value = SUBLIST_FIRST_VALUE[0]
raises(IndexError, "api.forcepermissive.option(path, length).value.get()")
raises(IndexError, "api.forcepermissive.option(path, length).value.set(value)")
raises(IndexError, "api.forcepermissive.option(path, length).value.reset()")
raises(IndexError, "api.forcepermissive.option(path, length).owner.get()")
raises(IndexError, "api.forcepermissive.option(path, length).owner.isdefault()")
raises(IndexError, "api.forcepermissive.option(path, length).property.get()")
raises(IndexError, "api.forcepermissive.option(path, length).owner.set('new_user')")
raises(IndexError, "api.forcepermissive.option(path, length).property.set(('prop',))")
@autocheck
def autocheck_reset_value(api, path, **kwargs):
# check if is a multi, a master or a slave
multi = api.unrestraint.option(path).option.ismulti()
submulti_ = api.unrestraint.option(path).option.issubmulti()
isslave = api.unrestraint.option(path).option.isslave()
# set default value (different if value is multi or not)
if not multi:
first_value = 'myvalue'
second_value = 'myvalue1'
first_value = FIRST_VALUE
second_value = SECOND_VALUE
elif submulti_ is False:
first_value = LIST_FIRST_VALUE
second_value = LIST_SECOND_VALUE
else:
first_value = ['myvalue']
second_value = ['myvalue', 'myvalue1']
if multi and not isslave:
empty_value = []
else:
empty_value = None
first_value = SUBLIST_FIRST_VALUE
second_value = SUBLIST_SECOND_VALUE
empty_value = _getdefault(api, path, multi, isslave, submulti_)
# reset value without permissive
if not kwargs.get('permissive', False) and not kwargs.get('propertyerror', False):
@ -332,7 +396,7 @@ def autocheck_display(api, path, **kwargs):
"""re set value
"""
#FIXME utile ?
print(api.config)
assert api.config
@autocheck
@ -554,13 +618,16 @@ def autocheck_owner_with_value(api, path, **kwargs):
raises(PropertiesOptionError, "api.forcepermissive.option(path).option.isslave()")
isslave = api.unrestraint.option(path).option.isslave()
owner = api.owner.get()
assert owner == OWNER
# get owner without permissive
if not kwargs.get('permissive', False) and not kwargs.get('propertyerror', False):
if not isslave:
assert api.option(path).owner.get() == 'user'
assert api.option(path).owner.get() == owner
else:
assert api.option(path, 0).owner.get() == 'user'
assert api.option(path, 1).owner.get() == 'user'
assert api.option(path, 0).owner.get() == owner
assert api.option(path, 1).owner.get() == owner
else:
if not isslave:
raises(PropertiesOptionError, "api.option(path).owner.get()")
@ -571,10 +638,10 @@ def autocheck_owner_with_value(api, path, **kwargs):
# get owner with permissive
if not kwargs.get('propertyerror', False):
if not isslave:
assert api.forcepermissive.option(path).owner.get() == 'user'
assert api.forcepermissive.option(path).owner.get() == owner
else:
assert api.forcepermissive.option(path, 0).owner.get() == 'default'
assert api.forcepermissive.option(path, 1).owner.get() == 'user'
assert api.forcepermissive.option(path, 1).owner.get() == owner
else:
if not isslave:
raises(PropertiesOptionError, "api.forcepermissive.option(path).owner.get()")
@ -628,10 +695,11 @@ def autocheck_set_owner(api, path, **kwargs):
assert api.option(path, 1).owner.get() == 'new_user'
assert api.forcepermissive.option(path, 1).owner.get() == 'new_user'
elif not kwargs.get('propertyerror', False):
owner = api.owner.get()
if not isslave:
assert api.forcepermissive.option(path).owner.get() == 'user'
assert api.forcepermissive.option(path).owner.get() == owner
else:
assert api.forcepermissive.option(path, 1).owner.get() == 'user'
assert api.forcepermissive.option(path, 1).owner.get() == owner
# set owner with permissive
if not kwargs.get('propertyerror', False):
@ -691,25 +759,32 @@ def autocheck_permissive(api, path, **kwargs):
# check if is a multi, a master or a slave
if not kwargs.get('propertyerror', False):
multi = api.forcepermissive.option(path).option.ismulti()
submulti_ = api.forcepermissive.option(path).option.issubmulti()
ismaster = api.forcepermissive.option(path).option.ismaster()
isslave = api.forcepermissive.option(path).option.isslave()
else:
raises(PropertiesOptionError, "api.forcepermissive.option(path).option.ismulti()")
multi = api.unrestraint.option(path).option.ismulti()
submulti_ = api.unrestraint.option(path).option.issubmulti()
ismaster = api.unrestraint.option(path).option.ismaster()
isslave = api.unrestraint.option(path).option.isslave()
# set default value (different if value is multi or not)
if not multi:
first_value = 'myvalue'
second_value = 'myvalue1'
first_value = FIRST_VALUE
second_value = SECOND_VALUE
elif submulti_ is False:
first_value = LIST_FIRST_VALUE
second_value = LIST_SECOND_VALUE
else:
first_value = ['myvalue']
second_value = ['myvalue', 'myvalue1']
first_value = SUBLIST_FIRST_VALUE
second_value = SUBLIST_SECOND_VALUE
if multi and not isslave:
empty_value = []
empty_value = LIST_EMPTY_VALUE
elif submulti_ and isslave:
empty_value = SUBLIST_EMPTY_VALUE
else:
empty_value = None
empty_value = EMPTY_VALUE
# cannot access to hidden value without forcepermissive
# and to disabled value (with forcepermissive too)
@ -765,35 +840,78 @@ def autocheck_permissive(api, path, **kwargs):
def check_all(api, path, multi, **kwargs):
def check_all(api, path, meta, multi, default, default_multi, **kwargs):
if DISPLAY:
text = u' {} launch tests for {}'.format(ICON, path)
if multi:
text = u' {} launch tests for {}'.format(ICON, path, multi, default)
if multi is True:
text += u' as a multi'
elif multi is submulti:
text += u' as a submulti'
if default is True:
text += u' with default'
if multi is True:
text += u' with default value'
if default_multi is True:
text += u' with default multi'
text += u', kwargs: {}'.format(kwargs)
print(text)
for func in autocheck_registers:
if DISPLAY:
print(u' {} {}'.format(ICON, func.__name__))
func(api, path, **kwargs)
try:
func(api, path, **kwargs)
except Exception as err:
msg = u'error in function {} for {}'.format(func.__name__, path)
if multi is True:
msg += u' as a multi'
elif multi is submulti:
msg += u' as a submulti'
if multi is True:
msg += u' with default value'
print(u'{}: {}'.format(msg, kwargs))
raise err
def make_api(options, multi):
def check_deref(weakrefs):
"""try if all elements are dereferenced
"""
for wrf in weakrefs:
assert wrf() is None
def make_api(options, meta, multi, default, default_multi):
weakrefs = []
def make_option(path, option_infos):
#FIXME
option_type = 'str'
option_properties = []
isslave = False
if option_infos is not None:
for prop in PROPERTIES:
if option_infos.get(prop, False) is True:
option_properties.append(prop)
isslave = option_infos.get('slave', False)
args = [path, "{}'s option".format(path)]
kwargs = {}
if option_properties != []:
kwargs['properties'] = tuple(option_properties)
if multi:
kwargs['multi'] = True
kwargs['multi'] = multi
if default and not submulti:
if multi is False:
value = FIRST_VALUE
elif multi is True:
value = LIST_FIRST_VALUE
else:
value = SUBLIST_EMPTY_VALUE
kwargs['default'] = value
if default_multi:
if multi is not submulti:
value = SECOND_VALUE
else:
value = LIST_SECOND_VALUE
kwargs['default_multi'] = value
tiramisu_option = OPTIONS_TYPE[option_type]['option']
obj = tiramisu_option(*args, **kwargs)
weakrefs.append(weakref.ref(obj))
@ -849,6 +967,9 @@ def make_api(options, multi):
return None, None
cfg = Config(rootod, session_id='conftest')
weakrefs.append(weakref.ref(cfg))
if meta:
cfg = MetaConfig([cfg], session_id='metatest')
weakrefs.append(weakref.ref(cfg))
api = getapi(cfg)
weakrefs.append(weakref.ref(api))
return api, weakrefs
@ -874,8 +995,8 @@ DICT_PATHS = [
('subod.subsubod.third', {'third': {'hidden': True}})]),
#test a config with masterslaves
OrderedDict([('odmaster.first', {'odmaster': {'master': True}}),
('odmaster.second', {'second': {'disabled': True}}),
('odmaster.third', {'third': {'hidden': True}})]),
('odmaster.second', {'second': {'disabled': True, 'slave': True}}),
('odmaster.third', {'third': {'hidden': True, 'slave': True}})]),
##test a config with dynoption
OrderedDict([('subod.first', {'subod': {'dyn': True}}),
('subod.second', {'second': {'disabled': True}}),
@ -940,21 +1061,24 @@ def test_options(paths):
return kwargs
lpaths = list(paths.keys())
for multi in (False, True):
api, weakrefs = make_api(paths, multi)
if api is None:
continue
if len(lpaths) == 9:
check_all(api, lpaths[3], multi, **get_kwargs(lpaths[0]))
check_all(api, lpaths[4], multi, **get_kwargs(lpaths[1]))
check_all(api, lpaths[5], multi, **get_kwargs(lpaths[2]))
check_all(api, lpaths[6], multi, **get_kwargs(lpaths[0]))
check_all(api, lpaths[7], multi, **get_kwargs(lpaths[1]))
check_all(api, lpaths[8], multi, **get_kwargs(lpaths[2]))
else:
check_all(api, lpaths[0], multi, **get_kwargs(lpaths[0]))
check_all(api, lpaths[1], multi, **get_kwargs(lpaths[1]))
check_all(api, lpaths[2], multi, **get_kwargs(lpaths[2]))
del(api)
for wr in weakrefs:
assert wr() is None
for meta in (False, True):
for default_multi in (False, True):
for default in (False, True):
for multi in (False, True, submulti):
if multi is False and default_multi:
continue
api, weakrefs = make_api(paths, meta, multi, default, default_multi)
if api is None:
continue
if len(lpaths) == 9:
check_all(api, lpaths[3], meta, multi, default, default_multi, **get_kwargs(lpaths[0]))
check_all(api, lpaths[4], meta, multi, default, default_multi, **get_kwargs(lpaths[1]))
check_all(api, lpaths[5], meta, multi, default, default_multi, **get_kwargs(lpaths[2]))
check_all(api, lpaths[6], meta, multi, default, default_multi, **get_kwargs(lpaths[0]))
check_all(api, lpaths[7], meta, multi, default, default_multi, **get_kwargs(lpaths[1]))
check_all(api, lpaths[8], meta, multi, default, default_multi, **get_kwargs(lpaths[2]))
else:
for lpath in lpaths:
check_all(api, lpath, meta, multi, default, default_multi, **get_kwargs(lpath))
del api
check_deref(weakrefs)