tiramisu/test/test_config_api.py

417 lines
18 KiB
Python
Raw Normal View History

"configuration objects global API"
2018-03-19 08:33:53 +01:00
from py.test import raises
2017-07-09 09:49:03 +02:00
from .autopath import do_autopath
2015-07-24 17:54:10 +02:00
do_autopath()
2018-03-19 08:33:53 +01:00
from tiramisu import Config, IntOption, FloatOption, StrOption, ChoiceOption, \
2014-02-04 21:14:30 +01:00
BoolOption, FilenameOption, UnicodeOption, SymLinkOption, IPOption, \
2014-02-06 22:17:20 +01:00
PortOption, NetworkOption, NetmaskOption, BroadcastOption, \
2018-03-19 08:33:53 +01:00
DomainnameOption, OptionDescription, getapi
2014-02-06 22:17:20 +01:00
from tiramisu.error import PropertiesOptionError
def make_description():
2013-04-03 12:20:26 +02:00
gcoption = ChoiceOption('name', 'GC name', ('ref', 'framework'), 'ref')
gcdummy = BoolOption('dummy', 'dummy', default=False)
2018-04-10 12:33:51 +02:00
prop = BoolOption('prop', 'prop 1', properties=('disabled',))
prop2 = BoolOption('prop', 'prop 2', properties=('hidden',))
objspaceoption = ChoiceOption('objspace', 'Object space',
('std', 'thunk'), 'std')
booloption = BoolOption('bool', 'Test boolean option', default=True)
2014-02-01 17:25:31 +01:00
booloption2 = BoolOption('bool', 'Test boolean option', default=False)
intoption = IntOption('int', 'Test int option', default=0)
2014-02-01 16:49:16 +01:00
floatoption2 = FloatOption('float', 'Test float option', default=2.3)
floatoption = FloatOption('float', 'Test float option', default=2.3)
stroption = StrOption('str', 'Test string option', default="abc")
boolop = BoolOption('boolop', 'Test boolean option op', default=True)
wantref_option = BoolOption('wantref', 'Tests', default=False)
wantframework_option = BoolOption('wantframework', 'Test', default=False)
2014-02-01 17:25:31 +01:00
gcgroup2 = OptionDescription('gc2', '', [booloption2, prop])
gcgroup = OptionDescription('gc', '', [gcgroup2, gcoption, gcdummy, floatoption, prop2])
descr = OptionDescription('tiramisu', '', [gcgroup, booloption, objspaceoption,
wantref_option, stroption,
wantframework_option,
2014-02-01 16:49:16 +01:00
intoption, boolop, floatoption2])
return descr
2016-10-01 20:15:08 +02:00
def _is_same_opt(opt1, opt2):
if "id" in dir(opt1):
assert opt1.id == opt2.id
else:
assert opt1 == opt2
2018-03-19 08:33:53 +01:00
#def test_iter_config():
# "iteration on config object"
# s = StrOption("string", "", default="string")
# s2 = StrOption("string2", "", default="string2")
# descr = OptionDescription("options", "", [s, s2])
# config = Config(descr)
# assert [(name, value) for name, value in config] == \
# [('string', 'string'), ('string2', 'string2')]
#
#
#def test_iter_config_property():
# "iteration on config object"
# s = StrOption("string", "", default="string", properties=('disabled',))
# s2 = StrOption("string2", "", default="string2")
# descr = OptionDescription("options", "", [s, s2])
# config = Config(descr)
# config.read_only()
# assert [(name, value) for name, value in config] == \
# [('string2', 'string2')]
#
#
#def test_iter_subconfig():
# "iteration on config sub object"
# descr = make_description()
# conf = Config(descr)
# for (name, value), (gname, gvalue) in \
# zip(conf.gc, [("name", "ref"), ("dummy", False)]):
# assert name == gname
# assert value == gvalue
2013-02-27 11:09:13 +01:00
2012-10-11 16:16:43 +02:00
def test_str():
descr = make_description()
c = Config(descr)
c # does not crash
def test_make_dict():
"serialization of the whole config to a dict"
descr = OptionDescription("opt", "", [
OptionDescription("s1", "", [
BoolOption("a", "", default=False),
BoolOption("b", "", default=False, properties=('hidden',))]),
IntOption("int", "", default=42)])
config = Config(descr)
2018-03-19 08:33:53 +01:00
api = getapi(config)
api.property.read_write()
api.permissive.set(frozenset(['hidden']))
d = api.option.make_dict()
assert d == {"s1.a": False, "int": 42}
2018-03-19 08:33:53 +01:00
api.option('int').value.set(43)
api.option('s1.a').value.set(True)
d = api.option.make_dict()
assert d == {"s1.a": True, "int": 43}
2018-03-19 08:33:53 +01:00
d2 = api.option.make_dict(flatten=True)
assert d2 == {'a': True, 'int': 43}
2018-03-19 08:33:53 +01:00
raises(ValueError, 'd2 = api.option.make_dict(withvalue="3")')
d = api.forcepermissive.option.make_dict()
assert d == {"s1.a": True, "s1.b": False, "int": 43}
2014-03-12 14:57:36 +01:00
def test_make_dict_with_disabled():
descr = OptionDescription("opt", "", [
OptionDescription("s1", "", [
BoolOption("a", "", default=False),
BoolOption("b", "", default=False, properties=('disabled',))]),
OptionDescription("s2", "", [
BoolOption("a", "", default=False),
BoolOption("b", "", default=False)], properties=('disabled',)),
IntOption("int", "", default=42)])
config = Config(descr)
2018-03-19 08:33:53 +01:00
api = getapi(config)
api.property.read_only()
d = api.option.make_dict()
assert d == {"s1.a": False, "int": 42}
def test_make_dict_with_disabled_in_callback():
descr = OptionDescription("opt", "", [
OptionDescription("s1", "", [
BoolOption("a", "", default=False),
BoolOption("b", "", default=False, properties=('disabled',))]),
OptionDescription("s2", "", [
BoolOption("a", "", default=False),
BoolOption("b", "", default=False)], properties=('disabled',)),
2014-03-12 14:57:36 +01:00
IntOption("int", "", default=42)])
config = Config(descr)
2018-03-19 08:33:53 +01:00
api = getapi(config)
api.property.read_only()
d = api.option.make_dict()
2014-03-12 14:57:36 +01:00
assert d == {"s1.a": False, "int": 42}
2017-01-06 21:01:24 +01:00
def test_make_dict_fullpath():
descr = OptionDescription("root", "", [
OptionDescription("opt", "", [
OptionDescription("s1", "", [
BoolOption("a", "", default=False),
BoolOption("b", "", default=False, properties=('disabled',))]),
OptionDescription("s2", "", [
BoolOption("a", "", default=False),
BoolOption("b", "", default=False)], properties=('disabled',)),
IntOption("int", "", default=42)]),
IntOption("introot", "", default=42)])
config = Config(descr)
2018-03-19 08:33:53 +01:00
api = getapi(config)
api.property.read_only()
assert api.option.make_dict() == {"opt.s1.a": False, "opt.int": 42, "introot": 42}
assert api.option('opt').make_dict() == {"s1.a": False, "int": 42}
assert api.option.make_dict(fullpath=True) == {"opt.s1.a": False, "opt.int": 42, "introot": 42}
assert api.option('opt').make_dict(fullpath=True) == {"opt.s1.a": False, "opt.int": 42}
2017-01-06 21:01:24 +01:00
2012-10-11 16:16:43 +02:00
def test_find_in_config():
"finds option in config"
descr = make_description()
conf = Config(descr)
2018-03-19 08:33:53 +01:00
api = getapi(conf)
api.property.read_only()
api.permissive.set(frozenset(['hidden']))
ret = api.option.find('dummy')
2016-10-01 20:15:08 +02:00
assert len(ret) == 1
2018-04-10 12:33:51 +02:00
_is_same_opt(ret[0].option.get(), api.option('gc.dummy').option.get())
2017-02-03 23:39:24 +01:00
#
2018-04-10 12:33:51 +02:00
ret = api.option.find('dummy', first=True).option.get()
2018-04-07 20:15:19 +02:00
_is_same_opt(ret, api.option('gc.dummy').option.get())
2017-02-03 23:39:24 +01:00
#
2018-03-19 08:33:53 +01:00
ret = api.option.find('float')
2016-10-01 20:15:08 +02:00
assert len(ret) == 2
2018-04-10 12:33:51 +02:00
_is_same_opt(ret[0].option.get(), api.option('gc.float').option.get())
_is_same_opt(ret[1].option.get(), api.option('float').option.get())
2018-03-19 08:33:53 +01:00
#
2018-04-10 12:33:51 +02:00
_is_same_opt(api.option.find('bool', first=True).option.get(), api.option('gc.gc2.bool').option.get())
_is_same_opt(api.option.find('bool', value=True, first=True).option.get(), api.option('bool').option.get())
_is_same_opt(api.option.find('dummy', first=True).option.get(), api.option('gc.dummy').option.get())
_is_same_opt(api.option.find('float', first=True).option.get(), api.option('gc.float').option.get())
#FIXME cannot find an option without name
2018-03-19 08:33:53 +01:00
#ret = conf.find(bytype=ChoiceOption)
#assert len(ret) == 2
#_is_same_opt(ret[0], conf.unwrap_from_path('gc.name'))
#_is_same_opt(ret[1], conf.unwrap_from_path('objspace'))
2018-04-10 12:33:51 +02:00
#
2018-03-19 08:33:53 +01:00
#_is_same_opt(conf.find_first(bytype=ChoiceOption), conf.unwrap_from_path('gc.name'))
#ret = conf.find(byvalue='ref')
#assert len(ret) == 1
#_is_same_opt(ret[0], conf.unwrap_from_path('gc.name'))
#_is_same_opt(conf.find_first(byvalue='ref'), conf.unwrap_from_path('gc.name'))
2018-04-10 12:33:51 +02:00
#
ret = api.option.find('prop')
assert len(ret) == 1
_is_same_opt(ret[0].option.get(), api.option('gc.prop').option.get())
#
ret = api.option.find('prop', value=None)
ret = api.option.find('prop')
assert len(ret) == 1
_is_same_opt(ret[0].option.get(), api.option('gc.prop').option.get())
#
2018-04-10 12:33:51 +02:00
api.property.read_write()
raises(AttributeError, "assert api.option.find('prop').option.get()")
ret = api.unrestraint.option.find(name='prop')
assert len(ret) == 2
_is_same_opt(ret[0].option.get(), api.unrestraint.option('gc.gc2.prop').option.get())
_is_same_opt(ret[1].option.get(), api.forcepermissive.option('gc.prop').option.get())
#
ret = api.forcepermissive.option.find('prop')
assert len(ret) == 1
_is_same_opt(ret[0].option.get(), api.forcepermissive.option('gc.prop').option.get())
#
_is_same_opt(api.forcepermissive.option.find('prop', first=True).option.get(), api.forcepermissive.option('gc.prop').option.get())
#FIXME cannot find an option without name
# combinaison of filters
2018-03-19 08:33:53 +01:00
#ret = conf.find(bytype=BoolOption, byname='dummy')
#assert len(ret) == 1
#_is_same_opt(ret[0], conf.unwrap_from_path('gc.dummy'))
#_is_same_opt(conf.find_first(bytype=BoolOption, byname='dummy'), conf.unwrap_from_path('gc.dummy'))
2018-04-10 12:33:51 +02:00
#
ret = api.option.find('dummy', value=False)
assert len(ret) == 1
_is_same_opt(ret[0].option.get(), api.option('gc.dummy').option.get())
#
_is_same_opt(api.option.find('dummy', value=False, first=True).option.get(), api.option('gc.dummy').option.get())
#subconfig
ret = api.option('gc').find('dummy')
assert len(ret) == 1
_is_same_opt(ret[0].option.get(), api.option('gc.dummy').option.get())
#
ret = api.option('gc').find('float')
assert len(ret) == 1
_is_same_opt(ret[0].option.get(), api.option('gc.float').option.get())
#
ret = api.option('gc').find('bool')
assert len(ret) == 1
_is_same_opt(ret[0].option.get(), api.option('gc.gc2.bool').option.get())
_is_same_opt(api.option('gc').find('bool', value=False, first=True).option.get(), api.option('gc.gc2.bool').option.get())
#
raises(AttributeError, "assert api.option('gc').find('bool', value=True, first=True).option.get()")
#
raises(AttributeError, "api.option('gc').find('wantref').option.get()")
#
ret = api.unrestraint.option('gc').find('prop')
assert len(ret) == 2
_is_same_opt(ret[0].option.get(), api.unrestraint.option('gc.gc2.prop').option.get())
_is_same_opt(ret[1].option.get(), api.forcepermissive.option('gc.prop').option.get())
#
api.property.read_only()
ret = api.option('gc').find('prop')
assert len(ret) == 1
_is_same_opt(ret[0].option.get(), api.option('gc.prop').option.get())
# not OptionDescription
raises(AttributeError, "api.option.find('gc', first=True)")
raises(AttributeError, "api.option.find('gc2', first=True)")
2018-03-19 08:33:53 +01:00
2018-04-10 12:33:51 +02:00
def test_find_multi():
b = BoolOption('bool', '', multi=True)
o = OptionDescription('od', '', [b])
conf = Config(o)
api = getapi(conf)
#
raises(AttributeError, "api.option.find('bool', value=True)")
raises(AttributeError, "api.option.find('bool', value=True, first=True)")
api.option('bool').value.set([False])
raises(AttributeError, "api.option.find('bool', value=True)")
raises(AttributeError, "api.option.find('bool', value=True, first=True)")
api.option('bool').value.set([False, False])
raises(AttributeError, "api.option.find('bool', value=True)")
raises(AttributeError, "api.option.find('bool', value=True, first=True)")
api.option('bool').value.set([False, False, True])
ret = api.option.find('bool', value=True)
assert len(ret) == 1
_is_same_opt(ret[0].option.get(), b)
_is_same_opt(api.option.find('bool', value=True, first=True).option.get(), b)
def test_does_not_find_in_config():
descr = make_description()
conf = Config(descr)
2018-03-19 08:33:53 +01:00
api = getapi(conf)
api
raises(AttributeError, "api.option.find('IDontExist')")
2013-09-30 21:21:47 +02:00
def test_filename():
a = FilenameOption('a', '')
2013-09-30 21:21:47 +02:00
o = OptionDescription('o', '', [a])
2018-03-19 08:33:53 +01:00
cfg = Config(o)
api = getapi(cfg)
api.option('a').value.set('/')
api.option('a').value.set('/tmp')
api.option('a').value.set('/tmp/')
api.option('a').value.set('/tmp/text.txt')
api.option('a').value.set('tmp')
api.option('a').value.set('tmp/')
api.option('a').value.set('tmp/text.txt')
raises(ValueError, "api.option('a').value.set('/tmp/with space.txt')")
raises(ValueError, "api.option('a').value.set('/tmp/with$.txt')")
#def test_iter_all():
# s = StrOption("string", "", default="string")
# s2 = StrOption("string2", "", default="string2")
# descr = OptionDescription("options", "", [s, s2])
# config = Config(descr)
# assert list(config.iter_all()) == [('string', 'string'), ('string2', 'string2')]
# for i in config.iter_all():
# #test StopIteration
# break
#
#
#def test_iter_all_force_permissive():
# s = StrOption("string", "", default="string")
# s2 = StrOption("string2", "", default="string2")
# s3 = StrOption("string3", "", default="string3", properties=('hidden',))
# descr = OptionDescription("options", "", [s, s2, s3])
# config = Config(descr)
# api = getapi(config)
# api.property.read_write()
# api.permissive.set(('hidden',))
# assert list(config.iter_all()) == [('string', 'string'), ('string2', 'string2')]
# assert list(config.iter_all(force_permissive=True)) == [('string', 'string'),
# ('string2', 'string2'),
# ('string3', 'string3')]
#
#
#def test_iter_all_prop():
# s = StrOption("string", "", default="string", properties=('disabled',))
# s2 = StrOption("string2", "", default="string2")
# descr = OptionDescription("options", "", [s, s2])
# config = Config(descr)
# api = getapi(config)
# api.property.read_only()
# assert list(config.iter_all()) == [('string2', 'string2')]
#def test_impl_getpaths():
# s = StrOption("string", "", default="string", properties=('disabled',))
# s2 = StrOption("string2", "", default="string2")
# s3 = StrOption("string3", "", default="string3")
# s4 = StrOption("string4", "", default="string4", properties=('hidden',))
# od = OptionDescription('od', '', [s3, s4])
# descr = OptionDescription("options", "", [s, s2, od])
# config = Config(descr)
# assert ['string', 'string2', 'od.string3', 'od.string4'] == config.cfgimpl_get_description().impl_getpaths()
# assert ['string', 'string2', 'od', 'od.string3', 'od.string4'] == config.cfgimpl_get_description().impl_getpaths(include_groups=True)
# config.read_write()
# raises(PropertiesOptionError, "config.od.string4")
# assert ['string', 'string2', 'od.string3', 'od.string4'] == config.cfgimpl_get_description().impl_getpaths()
# assert ['string', 'string2', 'od', 'od.string3', 'od.string4'] == config.cfgimpl_get_description().impl_getpaths(include_groups=True)
2014-02-06 22:17:20 +01:00
2014-02-04 21:14:30 +01:00
def test_invalid_option():
2015-07-24 17:54:10 +02:00
ChoiceOption('a', '', ('1', '2'))
2014-02-04 21:14:30 +01:00
raises(TypeError, "ChoiceOption('a', '', [1, 2])")
raises(TypeError, "ChoiceOption('a', '', 1)")
raises(ValueError, "ChoiceOption('a', '', (1,), 3)")
2015-07-24 17:54:10 +02:00
FloatOption('a', '')
2014-02-04 21:14:30 +01:00
raises(ValueError, "FloatOption('a', '', 'string')")
2015-07-24 17:54:10 +02:00
UnicodeOption('a', '')
2014-02-04 21:14:30 +01:00
raises(ValueError, "UnicodeOption('a', '', 1)")
2015-07-24 17:54:10 +02:00
u = UnicodeOption('a', '')
SymLinkOption('a', u)
2014-02-04 21:14:30 +01:00
raises(ValueError, "SymLinkOption('a', 'string')")
2015-07-24 17:54:10 +02:00
IPOption('a', '')
2014-02-04 21:14:30 +01:00
raises(ValueError, "IPOption('a', '', 1)")
raises(ValueError, "IPOption('a', '', 'string')")
2015-07-24 17:54:10 +02:00
PortOption('a', '')
2014-02-04 21:14:30 +01:00
raises(ValueError, "PortOption('a', '', 'string')")
raises(ValueError, "PortOption('a', '', '11:12:13', allow_range=True)")
raises(ValueError, "PortOption('a', '', 11111111111111111111)")
2014-02-06 22:17:20 +01:00
raises(ValueError, "PortOption('a', '', allow_zero=True, allow_wellknown=False, allow_registred=True, allow_private=False)")
raises(ValueError, "PortOption('a', '', allow_zero=True, allow_wellknown=True, allow_registred=False, allow_private=True)")
raises(ValueError, "PortOption('a', '', allow_zero=True, allow_wellknown=False, allow_registred=False, allow_private=True)")
raises(ValueError, "PortOption('a', '', allow_zero=True, allow_wellknown=False, allow_registred=True, allow_private=True)")
raises(ValueError, "PortOption('a', '', allow_zero=False, allow_wellknown=False, allow_registred=False, allow_private=False)")
2015-07-24 17:54:10 +02:00
NetworkOption('a', '')
2014-02-06 22:17:20 +01:00
raises(ValueError, "NetworkOption('a', '', 'string')")
2015-07-24 17:54:10 +02:00
NetmaskOption('a', '')
2014-02-06 22:17:20 +01:00
raises(ValueError, "NetmaskOption('a', '', 'string')")
2015-07-24 17:54:10 +02:00
BroadcastOption('a', '')
2014-02-06 22:17:20 +01:00
raises(ValueError, "BroadcastOption('a', '', 'string')")
2015-07-24 17:54:10 +02:00
DomainnameOption('a', '')
2014-02-06 22:17:20 +01:00
raises(ValueError, "DomainnameOption('a', '', 'string')")
raises(ValueError, "DomainnameOption('a', '', type_='string')")
raises(ValueError, "DomainnameOption('a', '', allow_ip='string')")
raises(ValueError, "DomainnameOption('a', '', allow_without_dot='string')")
raises(ValueError, "DomainnameOption('a', '', 1)")
#
ChoiceOption('a', '', (1,), multi=True, default_multi=1)
raises(ValueError, "ChoiceOption('a', '', (1,), default_multi=1)")
raises(ValueError, "ChoiceOption('a', '', (1,), multi=True, default=[1,], default_multi=2)")
raises(ValueError, "FloatOption('a', '', multi=True, default_multi='string')")
raises(ValueError, "UnicodeOption('a', '', multi=True, default_multi=1)")
raises(ValueError, "IPOption('a', '', multi=True, default_multi=1)")
raises(ValueError, "IPOption('a', '', multi=True, default_multi='string')")
raises(ValueError, "PortOption('a', '', multi=True, default_multi='string')")
raises(ValueError, "PortOption('a', '', multi=True, default_multi='11:12:13', allow_range=True)")
raises(ValueError, "PortOption('a', '', multi=True, default_multi=11111111111111111111)")
raises(ValueError, "NetworkOption('a', '', multi=True, default_multi='string')")
raises(ValueError, "NetmaskOption('a', '', multi=True, default_multi='string')")
raises(ValueError, "BroadcastOption('a', '', multi=True, default_multi='string')")
raises(ValueError, "DomainnameOption('a', '', multi=True, default_multi='string')")
raises(ValueError, "DomainnameOption('a', '', multi=True, default_multi=1)")
2018-04-07 20:15:19 +02:00
def test_help():
stro = StrOption('s', '', multi=True)
od1 = OptionDescription('o', '', [stro])
od2 = OptionDescription('o', '', [od1])
cfg = Config(od2)
api = getapi(cfg)
api.help(_display=False, _valid=True)