summaryrefslogtreecommitdiff
path: root/nova/tests/test_hooks.py
diff options
context:
space:
mode:
authorDaniel L Jones <jonesld@us.ibm.com>2013-06-28 16:10:24 -0500
committerDaniel L Jones <jonesld@us.ibm.com>2013-07-02 12:56:45 -0500
commit3dbc333263a4900bfdc6b615bbd38a8be562f719 (patch)
treea266f8c33524781a46fde82f34b66368986c7e72 /nova/tests/test_hooks.py
parent601c1ede039d2fefef7ee39ce7dbf43fe16dc9db (diff)
downloadnova-3dbc333263a4900bfdc6b615bbd38a8be562f719.tar.gz
Added functionality for nova hooks pass functions
Nova hooks do not pass the function they wrap to the hook as a parameter. This patch adds an optional parameter to add_hook which will change the function signatures for pre and post, enabling hooks to acquire and use information about the function they are wrapped around. Additionally adds a unit test for the new hooks in test_hooks.py. Change-Id: Ie328b2460ef0e063892944c97934c12076ed4979
Diffstat (limited to 'nova/tests/test_hooks.py')
-rw-r--r--nova/tests/test_hooks.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/nova/tests/test_hooks.py b/nova/tests/test_hooks.py
index 1f46252949..669e894a7b 100644
--- a/nova/tests/test_hooks.py
+++ b/nova/tests/test_hooks.py
@@ -42,6 +42,16 @@ class SampleHookB(SampleHookA):
self._add_called("post", kwargs)
+class SampleHookC(SampleHookA):
+ name = "c"
+
+ def pre(self, f, *args, **kwargs):
+ self._add_called("pre" + f.__name__, kwargs)
+
+ def post(self, f, rv, *args, **kwargs):
+ self._add_called("post" + f.__name__, kwargs)
+
+
class MockEntryPoint(object):
def __init__(self, cls):
@@ -85,3 +95,27 @@ class HookTestCase(test.TestCase):
called_order = []
self._hooked(42, called=called_order)
self.assertEqual(['prea', 'preb', 'postb'], called_order)
+
+
+class HookTestCaseWithFunction(HookTestCase):
+ def _mock_load_plugins(self, iload, iargs, ikwargs):
+ return [
+ stevedore.extension.Extension('function_hook',
+ MockEntryPoint(SampleHookC), SampleHookC, SampleHookC()),
+ ]
+
+ @hooks.add_hook('function_hook', pass_function=True)
+ def _hooked(self, a, b=1, c=2, called=None):
+ return 42
+
+ def test_basic(self):
+ self.assertEqual(42, self._hooked(1))
+ mgr = hooks._HOOKS['function_hook']
+
+ self.assertEqual(1, len(mgr.extensions))
+ self.assertEqual(SampleHookC, mgr.extensions[0].plugin)
+
+ def test_order_of_execution(self):
+ called_order = []
+ self._hooked(42, called=called_order)
+ self.assertEqual(['pre_hookedc', 'post_hookedc'], called_order)