# coding: utf-8 import autopath from tiramisu.setting import groups, owners from tiramisu.config import Config from tiramisu.option import ChoiceOption, BoolOption, IntOption, \ StrOption, OptionDescription from tiramisu.error import SlaveError from py.test import raises def make_description(): numero_etab = StrOption('numero_etab', "identifiant de l'établissement") nom_machine = StrOption('nom_machine', "nom de la machine", default="eoleng") nombre_interfaces = IntOption('nombre_interfaces', "nombre d'interfaces à activer", default=1) activer_proxy_client = BoolOption('activer_proxy_client', "utiliser un proxy", default=False) mode_conteneur_actif = BoolOption('mode_conteneur_actif', "le serveur est en mode conteneur", default=False) adresse_serveur_ntp = StrOption('serveur_ntp', "adresse serveur ntp", multi=True) time_zone = ChoiceOption('time_zone', 'fuseau horaire du serveur', ('Paris', 'Londres'), 'Paris') ip_admin_eth0 = StrOption('ip_admin_eth0', "ip réseau autorisé") netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-réseau") master = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1 = OptionDescription('interface1', '', [master]) interface1.optimpl_set_group_type(groups.family) general = OptionDescription('general', '', [numero_etab, nom_machine, nombre_interfaces, activer_proxy_client, mode_conteneur_actif, adresse_serveur_ntp, time_zone]) general.optimpl_set_group_type(groups.family) creole = OptionDescription('creole', 'first tiramisu configuration', [general, interface1]) descr = OptionDescription('baseconfig', 'baseconifgdescr', [creole]) return descr def test_base_config(): descr = make_description() config = Config(descr) assert config.creole.general.activer_proxy_client is False assert config.creole.general.nom_machine == "eoleng" assert config.find_first(byname='nom_machine', type_='value') == "eoleng" result = {'general.numero_etab': None, 'general.nombre_interfaces': 1, 'general.serveur_ntp': [], 'interface1.ip_admin_eth0.ip_admin_eth0': None, 'general.mode_conteneur_actif': False, 'general.time_zone': 'Paris', 'interface1.ip_admin_eth0.netmask_admin_eth0': None, 'general.nom_machine': 'eoleng', 'general.activer_proxy_client': False} assert config.creole.make_dict() == result result = {'serveur_ntp': [], 'mode_conteneur_actif': False, 'ip_admin_eth0': None, 'time_zone': 'Paris', 'numero_etab': None, 'netmask_admin_eth0': None, 'nom_machine': 'eoleng', 'activer_proxy_client': False, 'nombre_interfaces': 1} assert config.creole.make_dict(flatten=True) == result def test_make_dict_filter(): descr = make_description() config = Config(descr) result = {'general.numero_etab': None, 'general.nombre_interfaces': 1, 'general.serveur_ntp': [], 'general.mode_conteneur_actif': False, 'general.time_zone': 'Paris', 'general.nom_machine': 'eoleng', 'general.activer_proxy_client': False} assert config.creole.make_dict(withoption='numero_etab') == result raises(AttributeError, "config.creole.make_dict(withoption='numero_etab', withvalue='toto')") assert config.creole.make_dict(withoption='numero_etab', withvalue=None) == result def test_get_group_type(): descr = make_description() config = Config(descr) grp = config.unwrap_from_path('creole.general') assert grp.optimpl_get_group_type() == groups.family assert grp.optimpl_get_group_type() == 'family' assert isinstance(grp.optimpl_get_group_type(), groups.GroupType) raises(TypeError, 'grp.optimpl_set_group_type(groups.default)') def test_iter_on_groups(): descr = make_description() config = Config(descr) result = list(config.creole.iter_groups(group_type=groups.family)) group_names = [res[0] for res in result] assert group_names == ['general', 'interface1'] def test_iter_on_empty_group(): config = Config(OptionDescription("name", "descr", [])) result = list(config.iter_groups()) assert result == [] for i in config.iter_groups(): pass for i in config: pass assert [] == list(config) def test_groups_with_master(): 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) interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1.optimpl_set_group_type(groups.master) assert interface1.optimpl_get_group_type() == groups.master def test_groups_with_master_in_config(): 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) interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1.optimpl_set_group_type(groups.master) Config(interface1) assert interface1.optimpl_get_group_type() == groups.master def test_allowed_groups(): 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) interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) raises(ValueError, "interface1.optimpl_set_group_type('toto')") def test_master_not_valid_name(): 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) invalid_group = OptionDescription('interface1', '', [ip_admin_eth0, netmask_admin_eth0]) raises(ValueError, "invalid_group.optimpl_set_group_type(groups.master)") def test_sub_group_in_master_group(): 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) subgroup = OptionDescription("subgroup", '', []) invalid_group = OptionDescription('ip_admin_eth0', '', [subgroup, ip_admin_eth0, netmask_admin_eth0]) raises(ValueError, "invalid_group.optimpl_set_group_type(groups.master)") def test_group_always_has_multis(): 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") group = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) raises(ValueError, "group.optimpl_set_group_type(groups.master)") #____________________________________________________________ def test_values_with_master_and_slaves(): 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) interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1.optimpl_set_group_type(groups.master) maconfig = OptionDescription('toto', '', [interface1]) cfg = Config(maconfig) owner = cfg._cfgimpl_context._cfgimpl_settings.getowner() assert interface1.optimpl_get_group_type() == groups.master assert cfg.getowner("ip_admin_eth0.ip_admin_eth0") == owners.default assert cfg.getowner("ip_admin_eth0.netmask_admin_eth0") == owners.default assert cfg.ip_admin_eth0.netmask_admin_eth0 == [] cfg.ip_admin_eth0.ip_admin_eth0.append("192.168.230.145") assert cfg.ip_admin_eth0.ip_admin_eth0 == ["192.168.230.145"] assert cfg.ip_admin_eth0.netmask_admin_eth0 == [None] assert cfg.getowner("ip_admin_eth0.ip_admin_eth0") == owner assert cfg.getowner("ip_admin_eth0.netmask_admin_eth0") == owners.default def test_reset_values_with_master_and_slaves(): 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) interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1.optimpl_set_group_type(groups.master) maconfig = OptionDescription('toto', '', [interface1]) cfg = Config(maconfig) owner = cfg._cfgimpl_context._cfgimpl_settings.getowner() assert interface1.optimpl_get_group_type() == groups.master assert cfg.getowner("ip_admin_eth0.ip_admin_eth0") == owners.default assert cfg.getowner("ip_admin_eth0.netmask_admin_eth0") == owners.default cfg.ip_admin_eth0.ip_admin_eth0.append("192.168.230.145") assert cfg.getowner("ip_admin_eth0.ip_admin_eth0") == owner assert cfg.getowner("ip_admin_eth0.netmask_admin_eth0") == owners.default del(cfg.ip_admin_eth0.ip_admin_eth0) assert cfg.getowner("ip_admin_eth0.ip_admin_eth0") == owners.default assert cfg.getowner("ip_admin_eth0.netmask_admin_eth0") == owners.default assert cfg.ip_admin_eth0.ip_admin_eth0 == [] assert cfg.ip_admin_eth0.netmask_admin_eth0 == [] def test_values_with_master_and_slaves_slave(): 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) interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1.optimpl_set_group_type(groups.master) maconfig = OptionDescription('toto', '', [interface1]) cfg = Config(maconfig) assert cfg.ip_admin_eth0.netmask_admin_eth0 == [] raises(SlaveError, "cfg.ip_admin_eth0.netmask_admin_eth0 = ['255.255.255.0']") cfg.ip_admin_eth0.ip_admin_eth0.append("192.168.230.145") cfg.ip_admin_eth0.netmask_admin_eth0 = ['255.255.255.0'] cfg.ip_admin_eth0.netmask_admin_eth0[0] = '255.255.255.0' raises(SlaveError, "cfg.ip_admin_eth0.netmask_admin_eth0 = ['255.255.255.0', '255.255.255.0']") raises(SlaveError, "cfg.ip_admin_eth0.netmask_admin_eth0 = []") del(cfg.ip_admin_eth0.netmask_admin_eth0) cfg.ip_admin_eth0.netmask_admin_eth0 = ['255.255.255.0'] cfg.ip_admin_eth0.ip_admin_eth0.append("192.168.230.145") assert cfg.ip_admin_eth0.netmask_admin_eth0 == ['255.255.255.0', None] cfg.ip_admin_eth0.netmask_admin_eth0 = ['255.255.255.0', '255.255.255.0'] raises(SlaveError, 'cfg.ip_admin_eth0.netmask_admin_eth0.pop(1)') def test_values_with_master_and_slaves_master(): 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) interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1.optimpl_set_group_type(groups.master) maconfig = OptionDescription('toto', '', [interface1]) cfg = Config(maconfig) cfg.ip_admin_eth0.ip_admin_eth0.append("192.168.230.145") cfg.ip_admin_eth0.ip_admin_eth0 = ["192.168.230.145"] cfg.ip_admin_eth0.ip_admin_eth0 = ["192.168.230.145", "192.168.230.145"] cfg.ip_admin_eth0.netmask_admin_eth0 = ['255.255.255.0', '255.255.255.0'] raises(SlaveError, 'cfg.ip_admin_eth0.ip_admin_eth0 = ["192.168.230.145"]') cfg.ip_admin_eth0.ip_admin_eth0.pop(1) assert cfg.ip_admin_eth0.ip_admin_eth0 == ["192.168.230.145"]