Find context arg by type rather than by name.
This requires changes to all the tests that were passing in a string as a bogus context; that seems like an improvement. Change-Id: Ib8fc83cbda25a1cfa8794b72dc13f666549ef892
This commit is contained in:
@@ -25,7 +25,7 @@ SHOULD include dedicated exception logging.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import functools
|
import functools
|
||||||
import inspect
|
import itertools
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import webob.exc
|
import webob.exc
|
||||||
@@ -1073,19 +1073,17 @@ class CouldNotFetchImage(NovaException):
|
|||||||
|
|
||||||
|
|
||||||
def get_context_from_function_and_args(function, args, kwargs):
|
def get_context_from_function_and_args(function, args, kwargs):
|
||||||
"""Find an arg named 'context' and return the value.
|
"""Find an arg of type RequestContext and return it.
|
||||||
|
|
||||||
This is useful in a couple of decorators where we don't
|
This is useful in a couple of decorators where we don't
|
||||||
know much about the function we're wrapping.
|
know much about the function we're wrapping.
|
||||||
"""
|
"""
|
||||||
context = None
|
|
||||||
if 'context' in kwargs.keys():
|
|
||||||
context = kwargs['context']
|
|
||||||
else:
|
|
||||||
(iargs, _iva, _ikw, _idf) = inspect.getargspec(function)
|
|
||||||
if 'context' in iargs:
|
|
||||||
index = iargs.index('context')
|
|
||||||
if len(args) > index:
|
|
||||||
context = args[index]
|
|
||||||
|
|
||||||
return context
|
# import here to avoid circularity:
|
||||||
|
from nova import context
|
||||||
|
|
||||||
|
for arg in itertools.chain(kwargs.values(), args):
|
||||||
|
if isinstance(arg, context.RequestContext):
|
||||||
|
return arg
|
||||||
|
|
||||||
|
return None
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import nova
|
import nova
|
||||||
|
from nova import context
|
||||||
from nova import flags
|
from nova import flags
|
||||||
from nova import log
|
from nova import log
|
||||||
import nova.notifier.no_op_notifier
|
import nova.notifier.no_op_notifier
|
||||||
@@ -21,6 +22,10 @@ from nova.notifier import api as notifier_api
|
|||||||
from nova import test
|
from nova import test
|
||||||
|
|
||||||
|
|
||||||
|
ctxt = context.get_admin_context()
|
||||||
|
ctxt2 = context.get_admin_context()
|
||||||
|
|
||||||
|
|
||||||
class NotifierTestCase(test.TestCase):
|
class NotifierTestCase(test.TestCase):
|
||||||
"""Test case for notifications"""
|
"""Test case for notifications"""
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@@ -36,7 +41,7 @@ class NotifierTestCase(test.TestCase):
|
|||||||
self.stubs.Set(nova.notifier.no_op_notifier, 'notify',
|
self.stubs.Set(nova.notifier.no_op_notifier, 'notify',
|
||||||
mock_notify)
|
mock_notify)
|
||||||
|
|
||||||
notifier_api.notify('contextarg', 'publisher_id', 'event_type',
|
notifier_api.notify(ctxt, 'publisher_id', 'event_type',
|
||||||
nova.notifier.api.WARN, dict(a=3))
|
nova.notifier.api.WARN, dict(a=3))
|
||||||
self.assertEqual(self.notify_called, True)
|
self.assertEqual(self.notify_called, True)
|
||||||
|
|
||||||
@@ -53,11 +58,11 @@ class NotifierTestCase(test.TestCase):
|
|||||||
self.assertEqual(message[k], v)
|
self.assertEqual(message[k], v)
|
||||||
self.assertTrue(len(message['message_id']) > 0)
|
self.assertTrue(len(message['message_id']) > 0)
|
||||||
self.assertTrue(len(message['timestamp']) > 0)
|
self.assertTrue(len(message['timestamp']) > 0)
|
||||||
self.assertEqual(context, 'contextarg')
|
self.assertEqual(context, ctxt)
|
||||||
|
|
||||||
self.stubs.Set(nova.notifier.no_op_notifier, 'notify',
|
self.stubs.Set(nova.notifier.no_op_notifier, 'notify',
|
||||||
message_assert)
|
message_assert)
|
||||||
notifier_api.notify('contextarg', 'publisher_id', 'event_type',
|
notifier_api.notify(ctxt, 'publisher_id', 'event_type',
|
||||||
nova.notifier.api.WARN, dict(a=3))
|
nova.notifier.api.WARN, dict(a=3))
|
||||||
|
|
||||||
def test_send_rabbit_notification(self):
|
def test_send_rabbit_notification(self):
|
||||||
@@ -69,14 +74,14 @@ class NotifierTestCase(test.TestCase):
|
|||||||
self.mock_notify = True
|
self.mock_notify = True
|
||||||
|
|
||||||
self.stubs.Set(nova.rpc, 'notify', mock_notify)
|
self.stubs.Set(nova.rpc, 'notify', mock_notify)
|
||||||
notifier_api.notify('contextarg', 'publisher_id', 'event_type',
|
notifier_api.notify(ctxt, 'publisher_id', 'event_type',
|
||||||
nova.notifier.api.WARN, dict(a=3))
|
nova.notifier.api.WARN, dict(a=3))
|
||||||
|
|
||||||
self.assertEqual(self.mock_notify, True)
|
self.assertEqual(self.mock_notify, True)
|
||||||
|
|
||||||
def test_invalid_priority(self):
|
def test_invalid_priority(self):
|
||||||
self.assertRaises(nova.notifier.api.BadPriorityException,
|
self.assertRaises(nova.notifier.api.BadPriorityException,
|
||||||
notifier_api.notify, 'contextarg', 'publisher_id',
|
notifier_api.notify, ctxt, 'publisher_id',
|
||||||
'event_type', 'not a priority', dict(a=3))
|
'event_type', 'not a priority', dict(a=3))
|
||||||
|
|
||||||
def test_rabbit_priority_queue(self):
|
def test_rabbit_priority_queue(self):
|
||||||
@@ -92,7 +97,7 @@ class NotifierTestCase(test.TestCase):
|
|||||||
self.test_topic = topic
|
self.test_topic = topic
|
||||||
|
|
||||||
self.stubs.Set(nova.rpc, 'notify', mock_notify)
|
self.stubs.Set(nova.rpc, 'notify', mock_notify)
|
||||||
notifier_api.notify('contextarg', 'publisher_id',
|
notifier_api.notify(ctxt, 'publisher_id',
|
||||||
'event_type', 'DEBUG', dict(a=3))
|
'event_type', 'DEBUG', dict(a=3))
|
||||||
self.assertEqual(self.test_topic, 'testnotify.debug')
|
self.assertEqual(self.test_topic, 'testnotify.debug')
|
||||||
|
|
||||||
@@ -161,17 +166,17 @@ class NotifierTestCase(test.TestCase):
|
|||||||
mock_notify)
|
mock_notify)
|
||||||
|
|
||||||
# Test positional context
|
# Test positional context
|
||||||
self.assertEqual(3, example_api(1, 2, "contextname"))
|
self.assertEqual(3, example_api(1, 2, ctxt))
|
||||||
self.assertEqual(self.notify_called, True)
|
self.assertEqual(self.notify_called, True)
|
||||||
self.assertEqual(self.context_arg, "contextname")
|
self.assertEqual(self.context_arg, ctxt)
|
||||||
|
|
||||||
self.notify_called = False
|
self.notify_called = False
|
||||||
self.context_arg = None
|
self.context_arg = None
|
||||||
|
|
||||||
# Test named context
|
# Test named context
|
||||||
self.assertEqual(3, example_api2(1, 2, context="contextname2"))
|
self.assertEqual(3, example_api2(1, 2, context=ctxt2))
|
||||||
self.assertEqual(self.notify_called, True)
|
self.assertEqual(self.notify_called, True)
|
||||||
self.assertEqual(self.context_arg, "contextname2")
|
self.assertEqual(self.context_arg, ctxt2)
|
||||||
|
|
||||||
# Test missing context
|
# Test missing context
|
||||||
self.assertEqual(3, example_api2(1, 2, bananas="delicious"))
|
self.assertEqual(3, example_api2(1, 2, bananas="delicious"))
|
||||||
|
|||||||
Reference in New Issue
Block a user