Permalink
Browse files

Add TestExtensionManager

Create a special extension manager class that uses pre-configured
extension instances instead of loading them from a plugin set.
This makes setting up controlled environments for testing much
easier.

Signed-off-by: Doug Hellmann <doug.hellmann@dreamhost.com>
  • Loading branch information...
1 parent 9031434 commit 9090fa09eab53c7bcd6243398a52aecd35087c85 @dhellmann dhellmann committed Oct 29, 2012
Showing with 69 additions and 3 deletions.
  1. +5 −0 docs/source/history.rst
  2. +6 −0 docs/source/managers.rst
  3. +8 −3 stevedore/extension.py
  4. +50 −0 stevedore/tests/manager.py
@@ -2,6 +2,11 @@
History
=========
+0.5
+
+ - Add :class:`~stevedore.tests.manager.TestExtensionManager` for
+ writing tests for classes that use extension managers.
+
0.4
- Removed the ``name`` argument to plugin constructors.
@@ -63,3 +63,9 @@ Extension
:members:
:show-inheritance:
+TestExtensionManager
+====================
+
+.. autoclass:: stevedore.tests.manager.TestExtensionManager
+ :members:
+ :show-inheritance:
@@ -52,7 +52,12 @@ def __init__(self, namespace,
invoke_args=(),
invoke_kwds={}):
self.namespace = namespace
- self.extensions = []
+ self.extensions = self._load_plugins(invoke_on_load,
+ invoke_args,
+ invoke_kwds)
+
+ def _load_plugins(self, invoke_on_load, invoke_args, invoke_kwds):
+ extensions = []
for ep in pkg_resources.iter_entry_points(self.namespace):
LOG.debug('found extension %r', ep)
try:
@@ -62,13 +67,13 @@ def __init__(self, namespace,
invoke_kwds,
)
if ext:
- self.extensions.append(ext)
+ extensions.append(ext)
except KeyboardInterrupt:
raise
except Exception as err:
LOG.error('Could not load %r: %s', ep.name, err)
LOG.exception(err)
- return
+ return extensions
def _load_one_plugin(self, ep, invoke_on_load, invoke_args, invoke_kwds):
plugin = ep.load()
@@ -0,0 +1,50 @@
+"""TestExtensionManager
+
+Extension manager used only for testing.
+"""
+
+import logging
+
+from stevedore import extension
+
+
+LOG = logging.getLogger(__name__)
+
+
+class TestExtensionManager(extension.ExtensionManager):
+ """ExtensionManager that is explicitly initialized for tests.
+
+ :param extensions: Pre-configured Extension instances to use
+ instead of loading them from entry points.
+ :type extensions: list of :class:`~stevedore.extension.Extension`
+ :param namespace: The namespace for the entry points.
+ :type namespace: str
+ :param invoke_on_load: Boolean controlling whether to invoke the
+ object returned by the entry point after the driver is loaded.
+ :type invoke_on_load: bool
+ :param invoke_args: Positional arguments to pass when invoking
+ the object returned by the entry point. Only used if invoke_on_load
+ is True.
+ :type invoke_args: tuple
+ :param invoke_kwds: Named arguments to pass when invoking
+ the object returned by the entry point. Only used if invoke_on_load
+ is True.
+ :type invoke_kwds: dict
+ """
+
+ def __init__(self, extensions,
+ namespace='test',
+ invoke_on_load=False,
+ invoke_args=(),
+ invoke_kwds={}):
+ super(TestExtensionManager, self).__init__(namespace,
+ invoke_on_load,
+ invoke_args,
+ invoke_kwds,
+ )
+ self.extensions = extensions
+
+ def _load_plugins(self, invoke_on_load,
+ invoke_args,
+ invoke_kwds):
+ return []

0 comments on commit 9090fa0

Please sign in to comment.