1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
# Copyright 2015 Tesora Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
from mock import Mock, patch
from oslo_utils import timeutils
from trove.common import cfg
from trove.common.context import TroveContext
from trove.common import exception
from trove.common import notification
from trove.common.notification import EndNotification, StartNotification
from trove.conductor import api as conductor_api
from trove import rpc
from trove.tests.unittests import trove_testtools
class TestEndNotification(trove_testtools.TestCase):
def setUp(self):
super(TestEndNotification, self).setUp()
self.context = trove_testtools.TroveTestContext(self)
def _server_call(self, server_type):
with patch.object(self.context, "notification",
server_type=server_type) as notification:
with EndNotification(self.context):
pass
self.assertTrue(notification.notify_end.called)
def _server_exception(self, server_type):
with patch.object(self.context, "notification",
server_type=server_type) as notification:
try:
with EndNotification(self.context):
raise exception.TroveError()
except Exception:
self.assertTrue(notification.notify_exc_info.called)
def test_api_server_call(self):
self._server_call('api')
def test_api_server_exception(self):
self._server_exception('api')
def test_taskmanager_server_call(self):
self._server_call('taskmanager')
def test_taskmanager_server_exception(self):
self._server_exception('taskmanager')
def test_conductor_server_call(self):
with patch.object(conductor_api, 'API') as api:
with patch.object(self.context, "notification",
server_type='conductor'):
with EndNotification(self.context):
pass
self.assertTrue(api(self.context).notify_end.called)
def test_conductor_server_exception(self):
with patch.object(conductor_api, 'API') as api:
with patch.object(self.context, "notification",
server_type='conductor'):
try:
with EndNotification(self.context):
raise exception.TroveError()
except Exception:
self.assertTrue(api(self.context).notify_exc_info.called)
class TestStartNotification(trove_testtools.TestCase):
def setUp(self):
super(TestStartNotification, self).setUp()
self.context = trove_testtools.TroveTestContext(self)
def test_api_call(self):
with patch.object(self.context, "notification",
server_type='api') as notification:
with StartNotification(self.context):
pass
self.assertTrue(notification.notify_start.called)
def test_taskmanager_call(self):
with patch.object(self.context, "notification",
server_type='taskmanager') as notification:
with StartNotification(self.context):
pass
self.assertTrue(notification.notify_start.called)
def test_conductor_call(self):
with patch.object(conductor_api, 'API'):
with patch.object(self.context, "notification",
server_type='conductor') as notification:
with StartNotification(self.context):
pass
self.assertTrue(notification.notify_start.called)
class TestNotificationCastWrapper(trove_testtools.TestCase):
def test_no_notification(self):
with notification.NotificationCastWrapper(TroveContext(), "foo"):
pass
def test_with_notification(self):
context = trove_testtools.TroveTestContext(self)
self.assertTrue(context.notification.needs_end_notification)
with notification.NotificationCastWrapper(context, "foo"):
self.assertEqual('foo', context.notification.server_type)
self.assertEqual('api', context.notification.server_type)
self.assertFalse(context.notification.needs_end_notification)
class TestTroveBaseTraits(trove_testtools.TestCase):
def setUp(self):
super(TestTroveBaseTraits, self).setUp()
self.instance = Mock(db_info=Mock(created=timeutils.utcnow()))
@patch.object(rpc, 'get_notifier')
def test_n(self, notifier):
notification.TroveBaseTraits(
instance=self.instance).notify('event_type', 'publisher')
self.assertTrue(notifier().info.called)
a, _ = notifier().info.call_args
payload = a[2]
required_payload_keys = [
'created_at', 'name', 'instance_id', 'instance_name',
'instance_type_id', 'launched_at', 'nova_instance_id', 'region',
'state_description', 'state', 'tenant_id', 'user_id'
]
self.assertTrue(set(required_payload_keys).issubset(set(payload)))
@patch.object(rpc, 'get_notifier')
def test_notification_after_serialization(self, notifier):
orig_notify = notification.TroveBaseTraits(instance=self.instance)
serialized = orig_notify.serialize(None)
new_notify = notification.TroveBaseTraits().deserialize(None,
serialized)
new_notify.notify('event_type', 'publisher')
self.assertTrue(notifier().info.called)
class TestTroveCommonTraits(trove_testtools.TestCase):
def setUp(self):
super(TestTroveCommonTraits, self).setUp()
self.instance = Mock(db_info=Mock(created=timeutils.utcnow()))
@patch.object(cfg.CONF, 'get', Mock())
@patch.object(rpc, 'get_notifier')
def test_notification(self, notifier):
notification.TroveCommonTraits(
instance=self.instance).notify('event_type', 'publisher')
self.assertTrue(notifier().info.called)
a, _ = notifier().info.call_args
payload = a[2]
self.assertIn('availability_zone', payload)
@patch.object(cfg.CONF, 'get', Mock())
@patch.object(rpc, 'get_notifier')
def test_notification_after_serialization(self, notifier):
orig_notify = notification.TroveCommonTraits(instance=self.instance)
serialized = orig_notify.serialize(None)
new_notify = notification.TroveCommonTraits().deserialize(None,
serialized)
new_notify.notify('event_type', 'publisher')
self.assertTrue(notifier().info.called)
class TestTroveInstanceCreate(trove_testtools.TestCase):
def setUp(self):
super(TestTroveInstanceCreate, self).setUp()
self.instance = Mock(db_info=Mock(created=timeutils.utcnow()))
@patch.object(cfg.CONF, 'get', Mock())
@patch.object(rpc, 'get_notifier')
def test_notification(self, notifier):
notification.TroveInstanceCreate(instance=self.instance).notify()
self.assertTrue(notifier().info.called)
@patch.object(cfg.CONF, 'get', Mock())
@patch.object(rpc, 'get_notifier')
def test_notification_after_serialization(self, notifier):
orig_notify = notification.TroveInstanceCreate(instance=self.instance)
serialized = orig_notify.serialize(None)
new_notify = notification.TroveInstanceCreate().deserialize(None,
serialized)
new_notify.notify()
self.assertTrue(notifier().info.called)
class TestTroveInstanceDelete(trove_testtools.TestCase):
def setUp(self):
super(TestTroveInstanceDelete, self).setUp()
self.instance = Mock(db_info=Mock(created=timeutils.utcnow()))
@patch.object(cfg.CONF, 'get', Mock())
@patch.object(rpc, 'get_notifier')
def test_notification(self, notifier):
notification.TroveInstanceDelete(instance=self.instance).notify()
self.assertTrue(notifier().info.called)
@patch.object(cfg.CONF, 'get', Mock())
@patch.object(rpc, 'get_notifier')
def test_notification_after_serialization(self, notifier):
orig_notify = notification.TroveInstanceDelete(instance=self.instance)
serialized = orig_notify.serialize(None)
new_notify = notification.TroveInstanceDelete().deserialize(None,
serialized)
new_notify.notify()
self.assertTrue(notifier().info.called)
class TestTroveInstanceModifyVolume(trove_testtools.TestCase):
def setUp(self):
super(TestTroveInstanceModifyVolume, self).setUp()
self.instance = Mock(db_info=Mock(created=timeutils.utcnow()))
@patch.object(cfg.CONF, 'get', Mock())
@patch.object(rpc, 'get_notifier')
def test_notification(self, notifier):
notification.TroveInstanceModifyVolume(instance=self.instance).notify()
self.assertTrue(notifier().info.called)
@patch.object(cfg.CONF, 'get', Mock())
@patch.object(rpc, 'get_notifier')
def test_notification_after_serialization(self, notifier):
orig_notify = notification.TroveInstanceModifyVolume(
instance=self.instance)
serialized = orig_notify.serialize(None)
new_notify = notification.TroveInstanceModifyVolume().deserialize(
None, serialized)
new_notify.notify()
self.assertTrue(notifier().info.called)
class TestTroveInstanceModifyFlavor(trove_testtools.TestCase):
def setUp(self):
super(TestTroveInstanceModifyFlavor, self).setUp()
self.instance = Mock(db_info=Mock(created=timeutils.utcnow()))
@patch.object(cfg.CONF, 'get', Mock())
@patch.object(rpc, 'get_notifier')
def test_notification(self, notifier):
notification.TroveInstanceModifyFlavor(instance=self.instance).notify()
self.assertTrue(notifier().info.called)
@patch.object(cfg.CONF, 'get', Mock())
@patch.object(rpc, 'get_notifier')
def test_notification_after_serialization(self, notifier):
orig_notify = notification.TroveInstanceModifyFlavor(
instance=self.instance)
serialized = orig_notify.serialize(None)
new_notify = notification.TroveInstanceModifyFlavor().deserialize(
None, serialized)
new_notify.notify()
self.assertTrue(notifier().info.called)
class TestDBaaSQuota(trove_testtools.TestCase):
@patch.object(rpc, 'get_notifier')
def test_notification(self, notifier):
notification.DBaaSQuotas(None, Mock(), Mock()).notify()
self.assertTrue(notifier().info.called)
class DBaaSTestNotification(notification.DBaaSAPINotification):
def event_type(self):
return 'instance_test'
def required_start_traits(self):
return ['name', 'flavor_id', 'datastore']
def optional_start_traits(self):
return ['databases', 'users']
def required_end_traits(self):
return ['instance_id']
class TestDBaaSNotification(trove_testtools.TestCase):
def setUp(self):
super(TestDBaaSNotification, self).setUp()
self.test_n = DBaaSTestNotification(Mock(), request=Mock())
def test_missing_required_start_traits(self):
self.assertRaisesRegexp(exception.TroveError,
self.test_n.required_start_traits()[0],
self.test_n.notify_start)
def test_invalid_start_traits(self):
self.assertRaisesRegexp(exception.TroveError,
"The following required keys",
self.test_n.notify_start, foo='bar')
def test_missing_required_end_traits(self):
self.assertRaisesRegexp(exception.TroveError,
self.test_n.required_end_traits()[0],
self.test_n.notify_end)
def test_invalid_end_traits(self):
self.assertRaisesRegexp(exception.TroveError,
"The following required keys",
self.test_n.notify_end, foo='bar')
def test_missing_required_error_traits(self):
self.assertRaisesRegexp(exception.TroveError,
self.test_n.required_error_traits()[0],
self.test_n._notify, 'error',
self.test_n.required_error_traits(), [])
@patch.object(rpc, 'get_notifier')
def test_start_event(self, notifier):
self.test_n.notify_start(name='foo', flavor_id=7, datastore='db')
self.assertTrue(notifier().info.called)
a, _ = notifier().info.call_args
self.assertEqual('dbaas.instance_test.start', a[1])
@patch.object(rpc, 'get_notifier')
def test_end_event(self, notifier):
self.test_n.notify_end(instance_id='foo')
self.assertTrue(notifier().info.called)
a, _ = notifier().info.call_args
self.assertEqual('dbaas.instance_test.end', a[1])
@patch.object(rpc, 'get_notifier')
def test_verify_base_values(self, notifier):
self.test_n.notify_start(name='foo', flavor_id=7, datastore='db')
self.assertTrue(notifier().info.called)
a, _ = notifier().info.call_args
payload = a[2]
self.assertIn('client_ip', payload)
self.assertIn('request_id', payload)
self.assertIn('server_type', payload)
self.assertIn('server_ip', payload)
self.assertIn('tenant_id', payload)
@patch.object(rpc, 'get_notifier')
def test_verify_required_start_args(self, notifier):
self.test_n.notify_start(name='foo', flavor_id=7, datastore='db')
self.assertTrue(notifier().info.called)
a, _ = notifier().info.call_args
payload = a[2]
self.assertIn('name', payload)
self.assertIn('flavor_id', payload)
self.assertIn('datastore', payload)
self.assertTrue('users' not in payload)
@patch.object(rpc, 'get_notifier')
def test_verify_optional_start_args(self, notifier):
self.test_n.notify_start(name='foo', flavor_id=7, datastore='db',
users='the users')
self.assertTrue(notifier().info.called)
a, _ = notifier().info.call_args
payload = a[2]
self.assertIn('users', payload)
@patch.object(rpc, 'get_notifier')
def test_verify_required_end_args(self, notifier):
self.test_n.notify_end(instance_id='foo')
self.assertTrue(notifier().info.called)
a, _ = notifier().info.call_args
payload = a[2]
self.assertIn('instance_id', payload)
def _test_notify_callback(self, fn, *args, **kwargs):
with patch.object(rpc, 'get_notifier') as notifier:
mock_callback = Mock()
self.test_n.register_notify_callback(mock_callback)
mock_context = Mock()
mock_context.notification = Mock()
self.test_n.context = mock_context
fn(*args, **kwargs)
self.assertTrue(notifier().info.called)
self.assertTrue(mock_callback.called)
self.test_n.register_notify_callback(None)
def test_notify_callback(self):
required_keys = {
'datastore': 'ds',
'name': 'name',
'flavor_id': 'flav_id',
'instance_id': 'inst_id',
}
self._test_notify_callback(self.test_n.notify_start,
**required_keys)
self._test_notify_callback(self.test_n.notify_end,
**required_keys)
self._test_notify_callback(self.test_n.notify_exc_info,
'error', 'exc')
|