tiramisu/tests/test_symlink.py

306 lines
12 KiB
Python
Raw Normal View History

2013-08-24 22:32:54 +02:00
# coding: utf-8
2018-10-31 08:00:19 +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()
2019-07-04 20:43:47 +02:00
from .config import config_type, get_config
2013-06-13 22:07:58 +02:00
2018-03-19 08:33:53 +01:00
from tiramisu import BoolOption, StrOption, SymLinkOption, \
2019-09-01 09:41:53 +02:00
OptionDescription, Leadership, Config, Calculation, calc_value, Params, ParamOption, ParamValue
2018-03-19 08:33:53 +01:00
from tiramisu.error import PropertiesOptionError, ConfigError
2013-06-13 22:07:58 +02:00
from tiramisu.setting import groups, owners
2018-10-31 08:00:19 +01:00
from tiramisu.storage import list_sessions
2013-06-13 22:07:58 +02:00
2018-10-31 08:00:19 +01:00
def teardown_function(function):
assert list_sessions() == [], 'session list is not empty when leaving "{}"'.format(function.__name__)
2013-06-13 22:07:58 +02:00
def return_value():
pass
2013-06-13 22:07:58 +02:00
#____________________________________________________________
2019-07-04 20:43:47 +02:00
def test_symlink_option(config_type):
2013-06-13 22:07:58 +02:00
boolopt = BoolOption("b", "", default=False)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
2019-07-04 20:43:47 +02:00
cfg = Config(descr)
cfg = get_config(cfg, config_type)
assert cfg.option('s1.b').value.get() is False
cfg.option("s1.b").value.set(True)
cfg.option("s1.b").value.set(False)
assert cfg.option('s1.b').value.get() is False
assert cfg.option('c').value.get() is False
cfg.option('s1.b').value.set(True)
assert cfg.option('s1.b').value.get() is True
assert cfg.option('c').value.get() is True
cfg.option('s1.b').value.set(False)
assert cfg.option('s1.b').value.get() is False
assert cfg.option('c').value.get() is False
def test_symlink_assign_option(config_type):
2018-04-10 12:33:51 +02:00
boolopt = BoolOption("b", "", default=False)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
2019-07-04 20:43:47 +02:00
cfg = Config(descr)
cfg = get_config(cfg, config_type)
raises(ConfigError, "cfg.option('c').value.set(True)")
2018-04-10 12:33:51 +02:00
2019-07-04 20:43:47 +02:00
def test_symlink_del_option(config_type):
2018-04-10 12:33:51 +02:00
boolopt = BoolOption("b", "", default=False)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
2019-07-04 20:43:47 +02:00
cfg = Config(descr)
cfg = get_config(cfg, config_type)
raises(ConfigError, "cfg.option('c').value.reset()")
2018-04-10 12:33:51 +02:00
2018-04-11 16:36:15 +02:00
def test_symlink_addproperties():
boolopt = BoolOption('b', '', default=True, properties=('test',))
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription('opt', '', [boolopt, linkopt])
2019-07-04 20:43:47 +02:00
cfg = Config(descr)
cfg.property.read_write()
raises(TypeError, "cfg.option('c').property.add('new')")
2018-10-31 08:00:19 +01:00
try:
2019-07-04 20:43:47 +02:00
cfg.option('c').property.reset()
2018-10-31 08:00:19 +01:00
except AssertionError:
pass
else:
raise Exception('must raise')
2018-04-11 16:36:15 +02:00
2018-09-15 22:44:49 +02:00
def test_symlink_getpermissive():
boolopt = BoolOption('b', '', default=True, properties=('test',))
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription('opt', '', [boolopt, linkopt])
2019-07-04 20:43:47 +02:00
cfg = Config(descr)
cfg.property.read_write()
cfg.option('b').permissive.set(frozenset(['perm']))
cfg.option('c').permissive.get() == frozenset(['perm'])
2018-09-15 22:44:49 +02:00
2018-10-31 08:00:19 +01:00
def test_symlink_addpermissives():
2018-04-11 16:36:15 +02:00
boolopt = BoolOption('b', '', default=True, properties=('test',))
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription('opt', '', [boolopt, linkopt])
2019-07-04 20:43:47 +02:00
cfg = Config(descr)
cfg.property.read_write()
raises(TypeError, "cfg.option('c').permissive.set(frozenset(['new']))")
2018-10-31 08:00:19 +01:00
try:
2019-07-04 20:43:47 +02:00
cfg.option('c').permissive.reset()
2018-10-31 08:00:19 +01:00
except AssertionError:
pass
else:
raise Exception('must raise')
2018-04-11 16:36:15 +02:00
def test_symlink_getproperties():
boolopt = BoolOption('b', '', default=True, properties=('test',))
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription('opt', '', [boolopt, linkopt])
2019-07-04 20:43:47 +02:00
cfg = Config(descr)
cfg.property.read_write()
assert boolopt.impl_getproperties() == linkopt.impl_getproperties() == {'test'}
assert boolopt.impl_has_callback() == linkopt.impl_has_callback() == False
def test_symlink_getcallback():
boolopt = BoolOption('b', '', callback=return_value)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription('opt', '', [boolopt, linkopt])
2019-07-04 20:43:47 +02:00
cfg = Config(descr)
cfg.property.read_write()
assert boolopt.impl_has_callback() == linkopt.impl_has_callback() == True
assert boolopt.impl_get_callback() == linkopt.impl_get_callback() == (return_value, None)
2019-07-04 20:43:47 +02:00
def test_symlink_requires(config_type):
2013-06-13 22:07:58 +02:00
boolopt = BoolOption('b', '', default=True)
2019-09-01 09:41:53 +02:00
disabled_property = Calculation(calc_value,
Params(ParamValue('disabled'),
kwargs={'condition': ParamOption(boolopt),
'expected': ParamValue(False)}))
stropt = StrOption('s', '', properties=(disabled_property,))
2013-06-13 22:07:58 +02:00
linkopt = SymLinkOption("c", stropt)
descr = OptionDescription('opt', '', [boolopt, stropt, linkopt])
2019-07-04 20:43:47 +02:00
cfg = Config(descr)
cfg.property.read_write()
cfg = get_config(cfg, config_type)
assert cfg.option('b').value.get() is True
assert cfg.option('s').value.get() is None
assert cfg.option('c').value.get() is None
cfg.option('b').value.set(False)
2013-06-13 22:07:58 +02:00
#
props = []
try:
2019-07-04 20:43:47 +02:00
cfg.option('s').value.get()
except PropertiesOptionError as err:
2013-06-13 22:07:58 +02:00
props = err.proptype
2019-07-04 20:43:47 +02:00
assert props == {'disabled'}
2013-06-13 22:07:58 +02:00
#
props = []
try:
2019-07-04 20:43:47 +02:00
cfg.option('c').value.get()
except PropertiesOptionError as err:
2013-06-13 22:07:58 +02:00
props = err.proptype
2019-07-04 20:43:47 +02:00
assert props == {'disabled'}
2013-06-13 22:07:58 +02:00
2019-07-04 20:43:47 +02:00
def test_symlink_multi(config_type):
2013-06-13 22:07:58 +02:00
boolopt = BoolOption("b", "", default=[False], multi=True)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
2019-07-04 20:43:47 +02:00
cfg = Config(descr)
cfg = get_config(cfg, config_type)
assert cfg.option('s1.b').value.get() == [False]
assert cfg.option('c').value.get() == [False]
cfg.option('s1.b').value.set([True])
assert cfg.option('s1.b').value.get() == [True]
assert cfg.option('c').value.get() == [True]
cfg.option('s1.b').value.set([False])
assert cfg.option('s1.b').value.get() == [False]
assert cfg.option('c').value.get() == [False]
cfg.option('s1.b').value.set([False, True])
assert cfg.option('s1.b').value.get() == [False, True]
assert cfg.option('c').value.get() == [False, True]
2013-06-13 22:07:58 +02:00
assert boolopt.impl_is_multi() is True
assert linkopt.impl_is_multi() is True
2019-07-04 20:43:47 +02:00
def test_symlink_assign(config_type):
boolopt = BoolOption("b", "", default=False)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
cfg = Config(descr)
cfg = get_config(cfg, config_type)
raises(ConfigError, "cfg.option('c').value.set(True)")
2018-03-19 08:33:53 +01:00
2019-07-04 20:43:47 +02:00
def test_symlink_owner(config_type):
2013-06-13 22:07:58 +02:00
boolopt = BoolOption("b", "", default=False)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
2019-07-04 20:43:47 +02:00
cfg = Config(descr)
cfg = get_config(cfg, config_type)
assert cfg.option('s1.b').owner.isdefault()
assert cfg.option('c').owner.isdefault()
cfg.option('s1.b').value.set(True)
assert not cfg.option('s1.b').owner.isdefault()
assert not cfg.option('c').owner.isdefault()
2013-06-13 22:07:58 +02:00
def test_symlink_get_information():
boolopt = BoolOption("b", "", default=False)
linkopt = SymLinkOption("c", boolopt)
boolopt.impl_set_information('test', 'test')
assert boolopt.impl_get_information('test') == 'test'
assert linkopt.impl_get_information('test') == 'test'
boolopt.impl_set_information('test', 'test2')
assert boolopt.impl_get_information('test') == 'test2'
assert linkopt.impl_get_information('test') == 'test2'
2019-02-23 19:06:23 +01:00
def test_symlink_leader():
2013-06-13 22:07:58 +02:00
a = StrOption('a', "", multi=True)
ip_admin_eth0 = SymLinkOption('ip_admin_eth0', a)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "", multi=True)
2019-02-23 19:06:23 +01:00
raises(ValueError, "Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])")
2013-06-13 22:07:58 +02:00
2019-02-23 19:06:23 +01:00
def test_symlink_followers():
2013-06-13 22:07:58 +02:00
a = StrOption('a', "", multi=True)
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip réseau autorisé", multi=True)
2013-06-13 22:07:58 +02:00
netmask_admin_eth0 = SymLinkOption('netmask_admin_eth0', a)
2019-02-23 19:06:23 +01:00
raises(ValueError, "Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])")
2017-09-17 15:55:32 +02:00
2019-07-04 20:43:47 +02:00
def test_symlink_with_leader(config_type):
2018-04-19 08:19:03 +02:00
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip réseau autorisé", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-réseau", multi=True)
2019-02-23 19:06:23 +01:00
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
leader = SymLinkOption('leader', ip_admin_eth0)
od = OptionDescription('root', '', [interface1, leader])
2019-07-04 20:43:47 +02:00
cfg = Config(od)
cfg = get_config(cfg, config_type)
assert cfg.value.dict() == {'ip_admin_eth0.ip_admin_eth0': [], 'ip_admin_eth0.netmask_admin_eth0': [], 'leader': []}
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val1', 'val2'])
assert cfg.value.dict() == {'ip_admin_eth0.ip_admin_eth0': ['val1', 'val2'], 'ip_admin_eth0.netmask_admin_eth0': [None, None], 'leader': ['val1', 'val2']}
2018-04-19 08:19:03 +02:00
2019-07-04 20:43:47 +02:00
def test_symlink_with_follower(config_type):
2018-04-19 08:19:03 +02:00
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip réseau autorisé", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-réseau", multi=True)
2019-02-23 19:06:23 +01:00
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
follower = SymLinkOption('follower', netmask_admin_eth0)
od = OptionDescription('root', '', [interface1, follower])
2019-07-04 20:43:47 +02:00
cfg = Config(od)
cfg = get_config(cfg, config_type)
assert cfg.value.dict() == {'ip_admin_eth0.ip_admin_eth0': [], 'ip_admin_eth0.netmask_admin_eth0': [], 'follower': []}
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['val1', 'val2'])
assert cfg.value.dict() == {'ip_admin_eth0.ip_admin_eth0': ['val1', 'val2'], 'ip_admin_eth0.netmask_admin_eth0': [None, None], 'follower': [None, None]}
2018-04-19 08:19:03 +02:00
#
2019-07-04 20:43:47 +02:00
assert cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == None
assert cfg.option('ip_admin_eth0.netmask_admin_eth0', 1).value.get() == None
assert cfg.option('follower', 0).value.get() == None
assert cfg.option('follower', 1).value.get() == None
2018-04-19 08:19:03 +02:00
#
2019-07-04 20:43:47 +02:00
cfg.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('val3')
assert cfg.value.dict() == {'ip_admin_eth0.ip_admin_eth0': ['val1', 'val2'], 'ip_admin_eth0.netmask_admin_eth0': [None, 'val3'], 'follower': [None, 'val3']}
2018-04-19 08:19:03 +02:00
#
2019-07-04 20:43:47 +02:00
assert cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == None
assert cfg.option('ip_admin_eth0.netmask_admin_eth0', 1).value.get() == 'val3'
assert cfg.option('follower', 0).value.get() == None
assert cfg.option('follower', 1).value.get() == 'val3'
2018-04-19 08:19:03 +02:00
2017-09-17 15:55:32 +02:00
#____________________________________________________________
def test_symlink_dependency():
boolopt = BoolOption("b", "", default=False)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
2019-07-04 20:43:47 +02:00
cfg = Config(descr)
assert cfg.option('s1.b').option.has_dependency() is False
assert cfg.option('c').option.has_dependency() is True
assert cfg.option('s1.b').option.has_dependency(False) is True
assert cfg.option('c').option.has_dependency(False) is False
2018-04-19 08:19:03 +02:00
2019-07-04 20:43:47 +02:00
def test_symlink_makedict(config_type):
2018-04-19 08:19:03 +02:00
boolopt = BoolOption("b", "", default=False)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
2019-07-04 20:43:47 +02:00
cfg = Config(descr)
cfg = get_config(cfg, config_type)
assert cfg.value.dict() == {'c': False, 's1.b': False}
cfg.option('s1.b').value.set(True)
assert cfg.value.dict() == {'c': True, 's1.b': True}
2019-07-04 20:43:47 +02:00
def test_symlink_list(config_type):
boolopt = BoolOption("b", "", default=False)
linkopt = SymLinkOption("c", boolopt)
descr = OptionDescription("opt", "",
[linkopt, OptionDescription("s1", "", [boolopt])])
2019-07-04 20:43:47 +02:00
cfg = Config(descr)
cfg = get_config(cfg, config_type)
list_opt = []
2019-07-04 20:43:47 +02:00
for opt in cfg.option.list():
list_opt.append(opt.option.path())
assert list_opt == ['c']
#
list_opt = []
2019-07-04 20:43:47 +02:00
for opt in cfg.option.list(recursive=True):
list_opt.append(opt.option.path())
assert list_opt == ['c', 's1.b']