Fix notify_decorator errors
* getattr is built-in function, so should not be used as attribute. * When ctxt is None, rpc.get_notifier('api').info(ctxt, name, body) leads NoneType exception at context.to_dict(). So we need guarantee one valid context here. Closes-Bug: #1476931 Change-Id: I25953467aca641448d25b2d63352f756ec3655aa
This commit is contained in:
@@ -80,13 +80,15 @@ def notify_decorator(name, fn):
|
|||||||
for key in kwarg:
|
for key in kwarg:
|
||||||
body['kwarg'][key] = kwarg[key]
|
body['kwarg'][key] = kwarg[key]
|
||||||
|
|
||||||
ctxt = common_context.get_context_from_function_and_args(
|
ctxt = (common_context.get_context_from_function_and_args(
|
||||||
fn, args, kwarg)
|
fn, args, kwarg) or
|
||||||
|
common_context.get_current() or
|
||||||
|
nova.context.RequestContext())
|
||||||
|
|
||||||
notifier = rpc.get_notifier('api',
|
notifier = rpc.get_notifier('api',
|
||||||
publisher_id=(CONF.default_publisher_id
|
publisher_id=(CONF.default_publisher_id
|
||||||
or CONF.host))
|
or CONF.host))
|
||||||
method = notifier.getattr(CONF.default_notification_level.lower(),
|
method = getattr(notifier, CONF.default_notification_level.lower(),
|
||||||
'info')
|
'info')
|
||||||
method(ctxt, name, body)
|
method(ctxt, name, body)
|
||||||
|
|
||||||
|
@@ -70,6 +70,8 @@ class NotificationsTestCase(test.TestCase):
|
|||||||
|
|
||||||
self.instance = self._wrapped_create()
|
self.instance = self._wrapped_create()
|
||||||
|
|
||||||
|
self.decorated_function_called = False
|
||||||
|
|
||||||
def _wrapped_create(self, params=None):
|
def _wrapped_create(self, params=None):
|
||||||
instance_type = flavors.get_flavor_by_name('m1.tiny')
|
instance_type = flavors.get_flavor_by_name('m1.tiny')
|
||||||
inst = objects.Instance(image_ref=1,
|
inst = objects.Instance(image_ref=1,
|
||||||
@@ -462,6 +464,28 @@ class NotificationsTestCase(test.TestCase):
|
|||||||
self.assertEqual(0, len(fake_notifier.NOTIFICATIONS))
|
self.assertEqual(0, len(fake_notifier.NOTIFICATIONS))
|
||||||
self.assertEqual(0, mock_log_exception.call_count)
|
self.assertEqual(0, mock_log_exception.call_count)
|
||||||
|
|
||||||
|
def _decorated_function(self, arg1, arg2):
|
||||||
|
self.decorated_function_called = True
|
||||||
|
|
||||||
|
def test_notify_decorator(self):
|
||||||
|
func_name = self._decorated_function.__name__
|
||||||
|
|
||||||
|
# Decorated with notify_decorator like monkey_patch
|
||||||
|
self._decorated_function = notifications.notify_decorator(
|
||||||
|
func_name,
|
||||||
|
self._decorated_function)
|
||||||
|
|
||||||
|
ctxt = o_context.RequestContext()
|
||||||
|
|
||||||
|
self._decorated_function(1, ctxt)
|
||||||
|
|
||||||
|
self.assertEqual(1, len(fake_notifier.NOTIFICATIONS))
|
||||||
|
n = fake_notifier.NOTIFICATIONS[0]
|
||||||
|
self.assertEqual(n.priority, 'INFO')
|
||||||
|
self.assertEqual(n.event_type, func_name)
|
||||||
|
self.assertEqual(n.context, ctxt)
|
||||||
|
self.assertTrue(self.decorated_function_called)
|
||||||
|
|
||||||
|
|
||||||
class NotificationsFormatTestCase(test.NoDBTestCase):
|
class NotificationsFormatTestCase(test.NoDBTestCase):
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user