1
# Copyright 2011 OpenStack LLC.
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
17
from nova import context
18
from nova import flags
19
from nova.notifier import api as notifier_api
20
import nova.notifier.no_op_notifier
21
from nova.openstack.common import log
25
ctxt = context.get_admin_context()
26
ctxt2 = context.get_admin_context()
29
class NotifierTestCase(test.TestCase):
30
"""Test case for notifications"""
32
super(NotifierTestCase, self).setUp()
33
self.flags(notification_driver='nova.notifier.no_op_notifier')
35
def test_send_notification(self):
36
self.notify_called = False
38
def mock_notify(cls, *args):
39
self.notify_called = True
41
self.stubs.Set(nova.notifier.no_op_notifier, 'notify',
44
notifier_api.notify(ctxt, 'publisher_id', 'event_type',
45
nova.notifier.api.WARN, dict(a=3))
46
self.assertEqual(self.notify_called, True)
48
def test_verify_message_format(self):
49
"""A test to ensure changing the message format is prohibitively
52
def message_assert(context, message):
53
fields = [('publisher_id', 'publisher_id'),
54
('event_type', 'event_type'),
56
('payload', dict(a=3))]
58
self.assertEqual(message[k], v)
59
self.assertTrue(len(message['message_id']) > 0)
60
self.assertTrue(len(message['timestamp']) > 0)
61
self.assertEqual(context, ctxt)
63
self.stubs.Set(nova.notifier.no_op_notifier, 'notify',
65
notifier_api.notify(ctxt, 'publisher_id', 'event_type',
66
nova.notifier.api.WARN, dict(a=3))
68
def test_send_rabbit_notification(self):
69
self.stubs.Set(nova.flags.FLAGS, 'notification_driver',
70
'nova.notifier.rabbit_notifier')
71
self.mock_notify = False
73
def mock_notify(cls, *args):
74
self.mock_notify = True
76
self.stubs.Set(nova.openstack.common.rpc, 'notify', mock_notify)
77
notifier_api.notify(ctxt, 'publisher_id', 'event_type',
78
nova.notifier.api.WARN, dict(a=3))
80
self.assertEqual(self.mock_notify, True)
82
def test_invalid_priority(self):
83
self.assertRaises(nova.notifier.api.BadPriorityException,
84
notifier_api.notify, ctxt, 'publisher_id',
85
'event_type', 'not a priority', dict(a=3))
87
def test_rabbit_priority_queue(self):
88
flags.DECLARE('notification_topics', 'nova.notifier.rabbit_notifier')
89
self.stubs.Set(nova.flags.FLAGS, 'notification_driver',
90
'nova.notifier.rabbit_notifier')
91
self.stubs.Set(nova.flags.FLAGS, 'notification_topics',
94
self.test_topic = None
96
def mock_notify(context, topic, msg):
97
self.test_topic = topic
99
self.stubs.Set(nova.openstack.common.rpc, 'notify', mock_notify)
100
notifier_api.notify(ctxt, 'publisher_id',
101
'event_type', 'DEBUG', dict(a=3))
102
self.assertEqual(self.test_topic, 'testnotify.debug')
104
def test_error_notification(self):
105
self.stubs.Set(nova.flags.FLAGS, 'notification_driver',
106
'nova.notifier.rabbit_notifier')
107
self.stubs.Set(nova.flags.FLAGS, 'publish_errors', True)
108
LOG = log.getLogger('nova')
112
def mock_notify(context, topic, data):
115
self.stubs.Set(nova.openstack.common.rpc, 'notify', mock_notify)
117
self.assertEqual(1, len(msgs))
119
self.assertEqual(msg['event_type'], 'error_notification')
120
self.assertEqual(msg['priority'], 'ERROR')
121
self.assertEqual(msg['payload']['error'], 'foo')
123
def test_send_notification_by_decorator(self):
124
self.notify_called = False
126
def example_api(arg1, arg2):
129
example_api = nova.notifier.api.notify_decorator(
133
def mock_notify(cls, *args):
134
self.notify_called = True
136
self.stubs.Set(nova.notifier.no_op_notifier, 'notify',
139
self.assertEqual(3, example_api(1, 2))
140
self.assertEqual(self.notify_called, True)
142
def test_decorator_context(self):
143
"""Verify that the notify decorator can extract the 'context' arg."""
144
self.notify_called = False
145
self.context_arg = None
147
def example_api(arg1, arg2, context):
150
def example_api2(arg1, arg2, **kw):
153
example_api = nova.notifier.api.notify_decorator(
157
example_api2 = nova.notifier.api.notify_decorator(
161
def mock_notify(context, cls, _type, _priority, _payload):
162
self.notify_called = True
163
self.context_arg = context
165
self.stubs.Set(nova.notifier.api, 'notify',
168
# Test positional context
169
self.assertEqual(3, example_api(1, 2, ctxt))
170
self.assertEqual(self.notify_called, True)
171
self.assertEqual(self.context_arg, ctxt)
173
self.notify_called = False
174
self.context_arg = None
177
self.assertEqual(3, example_api2(1, 2, context=ctxt2))
178
self.assertEqual(self.notify_called, True)
179
self.assertEqual(self.context_arg, ctxt2)
181
# Test missing context
182
self.assertEqual(3, example_api2(1, 2, bananas="delicious"))
183
self.assertEqual(self.notify_called, True)
184
self.assertEqual(self.context_arg, None)