def test_Registry(): """ Test the `ipalib.plugable.Registry` class """ class Base1(object): pass class Base2(object): pass class plugin1(Base1): pass class plugin2(Base2): pass # Test creation of Registry: r = plugable.Registry() # Check that TypeError is raised trying to register something that isn't # a class: p = plugin1() e = raises(TypeError, r(), p) assert str(e) == 'plugin must be callable; got %r' % p # Check that registration works r()(plugin1) # Check that DuplicateError is raised trying to register exact class # again: e = raises(errors.PluginDuplicateError, r(), plugin1) assert e.plugin is plugin1 # Check that overriding works class base1_extended(Base1): pass class plugin1(base1_extended): # pylint: disable=function-redefined pass r(override=True)(plugin1) # Test that another plugin can be registered: r()(plugin2) # Setup to test more registration: class plugin1a(Base1): pass r()(plugin1a) class plugin1b(Base1): pass r()(plugin1b) class plugin2a(Base2): pass r()(plugin2a) class plugin2b(Base2): pass r()(plugin2b)
def get_package(server_info, client): NO_FINGERPRINT = object() fingerprint = NO_FINGERPRINT if server_info.is_valid(): fingerprint = server_info.get('fingerprint', fingerprint) if fingerprint is not None: try: try: if fingerprint is NO_FINGERPRINT: schema = Schema(client) else: schema = Schema(client, fingerprint) except SchemaUpToDate as e: schema = Schema(client, e.fingerprint) except NotAvailable: fingerprint = None ttl = None except SchemaUpToDate as e: fingerprint = e.fingerprint ttl = e.ttl else: fingerprint = schema.fingerprint ttl = schema.ttl server_info['fingerprint'] = fingerprint server_info.update_validity(ttl) if fingerprint is None: raise NotAvailable() fingerprint = str(fingerprint) package_name = '{}${}'.format(__name__, fingerprint) package_dir = '{}${}'.format(os.path.splitext(__file__)[0], fingerprint) try: return sys.modules[package_name] except KeyError: pass package = types.ModuleType(package_name) package.__file__ = os.path.join(package_dir, '__init__.py') package.modules = ['plugins'] sys.modules[package_name] = package module_name = '.'.join((package_name, 'plugins')) module = types.ModuleType(module_name) module.__file__ = os.path.join(package_dir, 'plugins.py') module.register = plugable.Registry() for plugin_cls in (_SchemaCommandPlugin, _SchemaObjectPlugin): for full_name in schema[plugin_cls.schema_key]: plugin = plugin_cls(schema, str(full_name)) plugin = module.register()(plugin) sys.modules[module_name] = module for full_name, topic in six.iteritems(schema['topics']): name = str(topic['name']) module_name = '.'.join((package_name, name)) try: module = sys.modules[module_name] except KeyError: module = sys.modules[module_name] = types.ModuleType(module_name) module.__file__ = os.path.join(package_dir, '{}.py'.format(name)) module.__doc__ = topic.get('doc') if 'topic_topic' in topic: module.topic = str(topic['topic_topic']).partition('/')[0] else: module.topic = None return package
def test_API(self): """ Test the `ipalib.plugable.API` class. """ assert issubclass(plugable.API, plugable.ReadOnly) register = plugable.Registry() # Setup the test bases, create the API: @register.base() class base0(plugable.Plugin): def method(self, n): return n @register.base() class base1(plugable.Plugin): def method(self, n): return n + 1 api = plugable.API([base0, base1], []) api.env.mode = 'unit_test' api.env.in_tree = True @register() class base0_plugin0(base0): pass @register() class base0_plugin1(base0): pass @register() class base0_plugin2(base0): pass @register() class base1_plugin0(base1): pass @register() class base1_plugin1(base1): pass @register() class base1_plugin2(base1): pass # Test API instance: assert api.isdone('bootstrap') is False assert api.isdone('finalize') is False api.finalize() assert api.isdone('bootstrap') is True assert api.isdone('finalize') is True def get_base_name(b): return 'base%d' % b def get_plugin_name(b, p): return 'base%d_plugin%d' % (b, p) for b in xrange(2): base_name = get_base_name(b) base = locals()[base_name] ns = getattr(api, base_name) assert isinstance(ns, plugable.NameSpace) assert read_only(api, base_name) is ns assert len(ns) == 3 for p in xrange(3): plugin_name = get_plugin_name(b, p) plugin = locals()[plugin_name] inst = ns[plugin_name] assert isinstance(inst, base) assert isinstance(inst, plugin) assert inst.name == plugin_name assert read_only(ns, plugin_name) is inst assert inst.method(7) == 7 + b # Test that calling finilize again raises AssertionError: e = raises(StandardError, api.finalize) assert str(e) == 'API.finalize() already called', str(e)
def test_Registry(): """ Test the `ipalib.plugable.Registry` class """ class Base1(object): pass class Base2(object): pass class Base3(object): pass class plugin1(Base1): pass class plugin2(Base2): pass class plugin3(Base3): pass # Test creation of Registry: register = plugable.Registry() def b(klass): register.base()(klass) def r(klass, override=False): register(override=override)(klass) # Check that TypeError is raised trying to register base that isn't # a class: p = Base1() e = raises(TypeError, b, p) assert str(e) == 'plugin base must be a class; got %r' % p # Check that base registration works b(Base1) i = tuple(register.iter(Base1)) assert len(i) == 1 assert i[0][0] is Base1 assert not i[0][1] # Check that DuplicateError is raised trying to register exact class # again: e = raises(errors.PluginDuplicateError, b, Base1) assert e.plugin is Base1 # Test that another base can be registered: b(Base2) i = tuple(register.iter(Base2)) assert len(i) == 1 assert i[0][0] is Base2 assert not i[0][1] # Test iter: i = tuple(register.iter(Base1, Base2)) assert len(i) == 2 assert i[0][0] is Base1 assert not i[0][1] assert i[1][0] is Base2 assert not i[1][1] e = raises(TypeError, register.iter, Base1, Base2, Base3) assert str(e) == 'unknown plugin base %r' % Base3 # Check that TypeError is raised trying to register something that isn't # a class: p = plugin1() e = raises(TypeError, r, p) assert str(e) == 'plugin must be a class; got %r' % p # Check that SubclassError is raised trying to register a class that is # not a subclass of an allowed base: e = raises(errors.PluginSubclassError, r, plugin3) assert e.plugin is plugin3 # Check that registration works r(plugin1) i = tuple(register.iter(Base1)) assert len(i) == 1 assert i[0][0] is Base1 assert i[0][1] == {plugin1} # Check that DuplicateError is raised trying to register exact class # again: e = raises(errors.PluginDuplicateError, r, plugin1) assert e.plugin is plugin1 # Check that OverrideError is raised trying to register class with same # name and same base: orig1 = plugin1 class base1_extended(Base1): pass class plugin1(base1_extended): # pylint: disable=function-redefined pass e = raises(errors.PluginOverrideError, r, plugin1) assert e.base == 'Base1' assert e.name == 'plugin1' assert e.plugin is plugin1 # Check that overriding works r(plugin1, override=True) i = tuple(register.iter(Base1)) assert len(i) == 1 assert i[0][0] is Base1 assert i[0][1] == {plugin1} # Check that MissingOverrideError is raised trying to override a name # not yet registerd: e = raises(errors.PluginMissingOverrideError, r, plugin2, override=True) assert e.base == 'Base2' assert e.name == 'plugin2' assert e.plugin is plugin2 # Test that another plugin can be registered: i = tuple(register.iter(Base2)) assert len(i) == 1 assert i[0][0] is Base2 assert not i[0][1] r(plugin2) i = tuple(register.iter(Base2)) assert len(i) == 1 assert i[0][0] is Base2 assert i[0][1] == {plugin2} # Setup to test more registration: class plugin1a(Base1): pass r(plugin1a) class plugin1b(Base1): pass r(plugin1b) class plugin2a(Base2): pass r(plugin2a) class plugin2b(Base2): pass r(plugin2b) # Again test iter: i = tuple(register.iter(Base1, Base2)) assert len(i) == 2 assert i[0][0] is Base1 assert i[0][1] == {plugin1, plugin1a, plugin1b} assert i[1][0] is Base2 assert i[1][1] == {plugin2, plugin2a, plugin2b}