From 9ffd2e6505bf963c997aa4b11a0c2cbe37e98c44 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 10 Feb 2019 20:59:03 +0100 Subject: [PATCH] add tests --- test/test_metaconfig.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/test/test_metaconfig.py b/test/test_metaconfig.py index adc46fe..3b435a8 100644 --- a/test/test_metaconfig.py +++ b/test/test_metaconfig.py @@ -3,8 +3,8 @@ from .autopath import do_autopath do_autopath() from tiramisu.setting import groups, owners -from tiramisu import IntOption, StrOption, NetworkOption, NetmaskOption, \ - OptionDescription, MasterSlaves, Config, GroupConfig, MetaConfig, \ +from tiramisu import IntOption, StrOption, NetworkOption, NetmaskOption, BoolOption, ChoiceOption, \ + IPOption, OptionDescription, MasterSlaves, Config, GroupConfig, MetaConfig, \ Params, ParamOption, ParamValue from tiramisu.error import ConfigError, ConflictError, PropertiesOptionError, SlaveError, APIError from tiramisu.storage import list_sessions @@ -22,6 +22,12 @@ def return_value(value=None): return value +def return_condition(val, condition, expected): + if condition == expected: + return val + return None + + def raise_exception(): raise Exception('test') @@ -678,6 +684,28 @@ def test_meta_exception_meta(): raises(Exception, "conf1.make_dict()") +def test_meta_properties_requires_mandatory(): + probes = BoolOption('probes', 'probes available', False) + eth0_method = ChoiceOption('eth0_method', '', ('static', 'dhcp'), 'static') + ip_address = IPOption('ip_address', '') + ip_eth0 = IPOption('ip_eth0', "ip", requires=({'option': probes, 'expected': True, 'action': 'mandatory'},), callback=return_condition, callback_params=Params(kwargs={'val': ParamOption(ip_address), 'condition': ParamOption(eth0_method), 'expected': ParamValue('dhcp')})) + ip_gw = IPOption('ip_gw', 'gw') + ip_gw.impl_add_consistency('not_equal', ip_eth0) + od = OptionDescription('root', '', [ip_gw, probes, eth0_method, ip_address, ip_eth0]) + conf1 = Config(od, session_id='conf1') + conf1.property.read_write() + meta = MetaConfig([conf1], 'meta') + # + meta.option('probes').value.set(True) + meta.option('ip_address').value.set('1.1.1.1') + meta.option('ip_gw').value.set('1.1.1.2') + conf1.option('eth0_method').value.set('dhcp') + conf1.property.read_only() + meta._config_bag.context.cfgimpl_reset_cache(None, None) + conf1._config_bag.context.cfgimpl_reset_cache(None, None) + assert 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'} + + def test_meta_callback(): val1 = StrOption('val1', "", 'val') val2 = StrOption('val2', "", callback=return_value, callback_params=Params(ParamOption(val1)))