/
test_notifier.py
732 lines (574 loc) · 27 KB
/
test_notifier.py
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2011 OpenStack, LLC
# 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.
import datetime
import kombu.entity
import mox
import qpid
import qpid.messaging
import stubout
import webob
from glance.common import exception
import glance.context
from glance import notifier
import glance.notifier.notify_kombu
from glance.openstack.common import importutils
import glance.openstack.common.log as logging
import glance.tests.unit.utils as unit_test_utils
from glance.tests import utils
DATETIME = datetime.datetime(2012, 5, 16, 15, 27, 36, 325355)
UUID1 = 'c80a1a6c-bd1f-41c5-90ee-81afedb1d58d'
USER1 = '54492ba0-f4df-4e4e-be62-27f4d76b29cf'
TENANT1 = '6838eb7b-6ded-434a-882c-b344c77fe8df'
TENANT2 = '2c014f32-55eb-467d-8fcb-4bd706012f81'
class ImageStub(glance.domain.Image):
def get_data(self):
return ['01234', '56789']
def set_data(self, data, size=None):
for chunk in data:
pass
class ImageRepoStub(object):
def remove(self, *args, **kwargs):
return 'image_from_get'
def save(self, *args, **kwargs):
return 'image_from_save'
def add(self, *args, **kwargs):
return 'image_from_add'
def get(self, *args, **kwargs):
return 'image_from_get'
def list(self, *args, **kwargs):
return ['images_from_list']
class TestNotifier(utils.BaseTestCase):
def test_invalid_strategy(self):
self.config(notifier_strategy="invalid_notifier")
self.assertRaises(exception.InvalidNotifierStrategy,
notifier.Notifier)
def test_custom_strategy(self):
st = "glance.notifier.notify_noop.NoopStrategy"
self.config(notifier_strategy=st)
#NOTE(bcwaldon): the fact that Notifier is instantiated means we're ok
notifier.Notifier()
class TestLoggingNotifier(utils.BaseTestCase):
"""Test the logging notifier is selected and works properly."""
def setUp(self):
super(TestLoggingNotifier, self).setUp()
self.config(notifier_strategy="logging")
self.called = False
self.logger = logging.getLogger("glance.notifier.notify_log")
self.notifier = notifier.Notifier()
def _called(self, msg):
self.called = msg
def test_warn(self):
self.logger.warn = self._called
self.notifier.warn("test_event", "test_message")
if self.called is False:
self.fail("Did not call logging library correctly.")
def test_info(self):
self.logger.info = self._called
self.notifier.info("test_event", "test_message")
if self.called is False:
self.fail("Did not call logging library correctly.")
def test_erorr(self):
self.logger.error = self._called
self.notifier.error("test_event", "test_message")
if self.called is False:
self.fail("Did not call logging library correctly.")
class TestNoopNotifier(utils.BaseTestCase):
"""Test that the noop notifier works...and does nothing?"""
def setUp(self):
super(TestNoopNotifier, self).setUp()
self.config(notifier_strategy="noop")
self.notifier = notifier.Notifier()
def test_warn(self):
self.notifier.warn("test_event", "test_message")
def test_info(self):
self.notifier.info("test_event", "test_message")
def test_error(self):
self.notifier.error("test_event", "test_message")
class TestRabbitNotifier(utils.BaseTestCase):
"""Test AMQP/Rabbit notifier works."""
def setUp(self):
super(TestRabbitNotifier, self).setUp()
def _fake_connect(rabbit_self):
rabbit_self.connection_errors = ()
rabbit_self.connection = 'fake_connection'
return None
self.notify_kombu = importutils.import_module("glance.notifier."
"notify_kombu")
self.notify_kombu.RabbitStrategy._send_message = self._send_message
self.notify_kombu.RabbitStrategy._connect = _fake_connect
self.called = False
self.config(notifier_strategy="rabbit",
rabbit_retry_backoff=0,
rabbit_notification_topic="fake_topic")
self.notifier = notifier.Notifier()
def _send_message(self, message, routing_key):
self.called = {
"message": message,
"routing_key": routing_key,
}
def test_warn(self):
self.notifier.warn("test_event", "test_message")
if self.called is False:
self.fail("Did not call _send_message properly.")
self.assertEquals("test_message", self.called["message"]["payload"])
self.assertEquals("WARN", self.called["message"]["priority"])
self.assertEquals("fake_topic.warn", self.called["routing_key"])
def test_info(self):
self.notifier.info("test_event", "test_message")
if self.called is False:
self.fail("Did not call _send_message properly.")
self.assertEquals("test_message", self.called["message"]["payload"])
self.assertEquals("INFO", self.called["message"]["priority"])
self.assertEquals("fake_topic.info", self.called["routing_key"])
def test_error(self):
self.notifier.error("test_event", "test_message")
if self.called is False:
self.fail("Did not call _send_message properly.")
self.assertEquals("test_message", self.called["message"]["payload"])
self.assertEquals("ERROR", self.called["message"]["priority"])
self.assertEquals("fake_topic.error", self.called["routing_key"])
def test_unknown_error_on_connect_raises(self):
class MyException(Exception):
pass
def _connect(self):
self.connection_errors = ()
raise MyException('meow')
self.notify_kombu.RabbitStrategy._connect = _connect
self.assertRaises(MyException, notifier.Notifier)
def test_timeout_on_connect_reconnects(self):
info = {'num_called': 0}
def _connect(rabbit_self):
rabbit_self.connection_errors = ()
info['num_called'] += 1
if info['num_called'] == 1:
raise Exception('foo timeout foo')
rabbit_self.connection = 'fake_connection'
self.notify_kombu.RabbitStrategy._connect = _connect
notifier_ = notifier.Notifier()
notifier_.error('test_event', 'test_message')
if self.called is False:
self.fail("Did not call _send_message properly.")
self.assertEquals("test_message", self.called["message"]["payload"])
self.assertEquals("ERROR", self.called["message"]["priority"])
self.assertEquals(info['num_called'], 2)
def test_connection_error_on_connect_reconnects(self):
info = {'num_called': 0}
class MyException(Exception):
pass
def _connect(rabbit_self):
rabbit_self.connection_errors = (MyException, )
info['num_called'] += 1
if info['num_called'] == 1:
raise MyException('meow')
rabbit_self.connection = 'fake_connection'
self.notify_kombu.RabbitStrategy._connect = _connect
notifier_ = notifier.Notifier()
notifier_.error('test_event', 'test_message')
if self.called is False:
self.fail("Did not call _send_message properly.")
self.assertEquals("test_message", self.called["message"]["payload"])
self.assertEquals("ERROR", self.called["message"]["priority"])
self.assertEquals(info['num_called'], 2)
def test_unknown_error_on_send_message_raises(self):
class MyException(Exception):
pass
def _send_message(rabbit_self, msg, routing_key):
raise MyException('meow')
self.notify_kombu.RabbitStrategy._send_message = _send_message
notifier_ = notifier.Notifier()
self.assertRaises(MyException, notifier_.error, 'a', 'b')
def test_timeout_on_send_message_reconnects(self):
info = {'send_called': 0, 'conn_called': 0}
def _connect(rabbit_self):
info['conn_called'] += 1
rabbit_self.connection_errors = ()
rabbit_self.connection = 'fake_connection'
def _send_message(rabbit_self, msg, routing_key):
info['send_called'] += 1
if info['send_called'] == 1:
raise Exception('foo timeout foo')
self._send_message(msg, routing_key)
self.notify_kombu.RabbitStrategy._connect = _connect
self.notify_kombu.RabbitStrategy._send_message = _send_message
notifier_ = notifier.Notifier()
notifier_.error('test_event', 'test_message')
if self.called is False:
self.fail("Did not call _send_message properly.")
self.assertEquals("test_message", self.called["message"]["payload"])
self.assertEquals("ERROR", self.called["message"]["priority"])
self.assertEquals(info['send_called'], 2)
self.assertEquals(info['conn_called'], 2)
def test_connection_error_on_send_message_reconnects(self):
info = {'send_called': 0, 'conn_called': 0}
class MyException(Exception):
pass
def _connect(rabbit_self):
info['conn_called'] += 1
rabbit_self.connection_errors = (MyException, )
rabbit_self.connection = 'fake_connection'
def _send_message(rabbit_self, msg, routing_key):
info['send_called'] += 1
if info['send_called'] == 1:
raise MyException('meow')
self._send_message(msg, routing_key)
self.notify_kombu.RabbitStrategy._connect = _connect
self.notify_kombu.RabbitStrategy._send_message = _send_message
notifier_ = notifier.Notifier()
notifier_.error('test_event', 'test_message')
if self.called is False:
self.fail("Did not call _send_message properly.")
self.assertEquals("test_message", self.called["message"]["payload"])
self.assertEquals("ERROR", self.called["message"]["priority"])
self.assertEquals(info['send_called'], 2)
self.assertEquals(info['conn_called'], 2)
class TestQpidNotifier(utils.BaseTestCase):
"""Test Qpid notifier."""
def setUp(self):
super(TestQpidNotifier, self).setUp()
self.mocker = mox.Mox()
self.mock_connection = None
self.mock_session = None
self.mock_sender = None
self.mock_receiver = None
self.orig_connection = qpid.messaging.Connection
self.orig_session = qpid.messaging.Session
self.orig_sender = qpid.messaging.Sender
self.orig_receiver = qpid.messaging.Receiver
qpid.messaging.Connection = lambda *_x, **_y: self.mock_connection
qpid.messaging.Session = lambda *_x, **_y: self.mock_session
qpid.messaging.Sender = lambda *_x, **_y: self.mock_sender
qpid.messaging.Receiver = lambda *_x, **_y: self.mock_receiver
self.notify_qpid = importutils.import_module("glance.notifier."
"notify_qpid")
self.addCleanup(self.reset_qpid)
self.addCleanup(self.mocker.ResetAll)
def reset_qpid(self):
qpid.messaging.Connection = self.orig_connection
qpid.messaging.Session = self.orig_session
qpid.messaging.Sender = self.orig_sender
qpid.messaging.Receiver = self.orig_receiver
def _test_notify(self, priority, exception=False, exception_send=False):
test_msg = {'a': 'b'}
self.mock_connection = self.mocker.CreateMock(self.orig_connection)
self.mock_session = self.mocker.CreateMock(self.orig_session)
self.mock_sender = self.mocker.CreateMock(self.orig_sender)
self.mock_connection.username = ""
if exception:
self.mock_connection.open().AndRaise(
Exception('Test open Exception'))
else:
self.mock_connection.open()
self.mock_connection.session().AndReturn(self.mock_session)
expected_address = ('glance/notifications.%s ; '
'{"node": {"x-declare": {"auto-delete": true, '
'"durable": false}, "type": "topic"}, '
'"create": "always"}' % priority)
self.mock_session.sender(expected_address).AndReturn(
self.mock_sender)
if exception_send:
self.mock_sender.send(mox.IgnoreArg()).AndRaise(
Exception('Test send Exception'))
# NOTE(afazekas): the opened and close call is expected
# in this case, but not expected if the open fails
else:
self.mock_sender.send(mox.IgnoreArg())
self.mock_connection.opened().AndReturn(True)
self.mock_connection.close()
self.mocker.ReplayAll()
self.config(notifier_strategy="qpid")
notifier = self.notify_qpid.QpidStrategy()
if priority == 'info':
if exception or exception_send:
self.assertRaises(Exception, notifier.info, test_msg)
else:
notifier.info(test_msg)
elif priority == 'warn':
if exception or exception_send:
self.assertRaises(Exception, notifier.warn, test_msg)
else:
notifier.warn(test_msg)
elif priority == 'error':
if exception or exception_send:
self.assertRaises(Exception, notifier.error, test_msg)
else:
notifier.error(test_msg)
self.mocker.VerifyAll()
def test_info(self):
self._test_notify('info')
def test_warn(self):
self._test_notify('warn')
def test_error(self):
self._test_notify('error')
def test_exception_open_successful(self):
self._test_notify('info', exception=True)
def test_info_fail(self):
self._test_notify('info', exception_send=True)
def test_warn_fail(self):
self._test_notify('warn', exception_send=True)
def test_error_fail(self):
self._test_notify('error', exception_send=True)
class TestRabbitContentType(utils.BaseTestCase):
"""Test AMQP/Rabbit notifier works."""
def setUp(self):
super(TestRabbitContentType, self).setUp()
self.stubs = stubout.StubOutForTesting()
def _fake_connect(rabbit_self):
rabbit_self.connection_errors = ()
rabbit_self.connection = 'fake_connection'
rabbit_self.exchange = self._fake_exchange()
return None
def dummy(*args, **kwargs):
pass
self.stubs.Set(kombu.entity.Exchange, 'publish', dummy)
self.stubs.Set(glance.notifier.notify_kombu.RabbitStrategy, '_connect',
_fake_connect)
self.called = False
self.config(notifier_strategy="rabbit",
rabbit_retry_backoff=0,
rabbit_notification_topic="fake_topic")
self.notifier = notifier.Notifier()
def _fake_exchange(self):
class Dummy(object):
class Message(object):
def __init__(message_self, message, content_type):
self.called = {
'message': message,
'content_type': content_type
}
@classmethod
def publish(*args, **kwargs):
pass
return Dummy
def test_content_type_passed(self):
self.notifier.warn("test_event", "test_message")
self.assertEquals(self.called['content_type'], 'application/json')
class TestImageNotifications(utils.BaseTestCase):
"""Test Image Notifications work"""
def setUp(self):
super(TestImageNotifications, self).setUp()
self.image = ImageStub(
image_id=UUID1, name='image-1', status='active', size=1024,
created_at=DATETIME, updated_at=DATETIME, owner=TENANT1,
visibility='public', container_format='ami',
tags=['one', 'two'], disk_format='ami', min_ram=128,
min_disk=10, checksum='ca425b88f047ce8ec45ee90e813ada91',
locations=['http://127.0.0.1'])
self.context = glance.context.RequestContext(tenant=TENANT2,
user=USER1)
self.image_repo_stub = ImageRepoStub()
self.notifier = unit_test_utils.FakeNotifier()
self.image_repo_proxy = glance.notifier.ImageRepoProxy(
self.image_repo_stub, self.context, self.notifier)
self.image_proxy = glance.notifier.ImageProxy(
self.image, self.context, self.notifier)
def test_image_save_notification(self):
self.image_repo_proxy.save(self.image_proxy)
output_logs = self.notifier.get_logs()
self.assertEqual(len(output_logs), 1)
output_log = output_logs[0]
self.assertEqual(output_log['notification_type'], 'INFO')
self.assertEqual(output_log['event_type'], 'image.update')
self.assertEqual(output_log['payload']['id'], self.image.image_id)
if 'location' in output_log['payload']:
self.fail('Notification contained location field.')
def test_image_add_notification(self):
self.image_repo_proxy.add(self.image_proxy)
output_logs = self.notifier.get_logs()
self.assertEqual(len(output_logs), 1)
output_log = output_logs[0]
self.assertEqual(output_log['notification_type'], 'INFO')
self.assertEqual(output_log['event_type'], 'image.create')
self.assertEqual(output_log['payload']['id'], self.image.image_id)
if 'location' in output_log['payload']:
self.fail('Notification contained location field.')
def test_image_delete_notification(self):
self.image_repo_proxy.remove(self.image_proxy)
output_logs = self.notifier.get_logs()
self.assertEqual(len(output_logs), 1)
output_log = output_logs[0]
self.assertEqual(output_log['notification_type'], 'INFO')
self.assertEqual(output_log['event_type'], 'image.delete')
self.assertEqual(output_log['payload']['id'], self.image.image_id)
self.assertTrue(output_log['payload']['deleted'])
if 'location' in output_log['payload']:
self.fail('Notification contained location field.')
def test_image_get(self):
image = self.image_repo_proxy.get(UUID1)
self.assertTrue(isinstance(image, glance.notifier.ImageProxy))
self.assertEqual(image.image, 'image_from_get')
def test_image_list(self):
images = self.image_repo_proxy.list()
self.assertTrue(isinstance(images[0], glance.notifier.ImageProxy))
self.assertEqual(images[0].image, 'images_from_list')
def test_image_get_data_notification(self):
self.image_proxy.size = 10
data = ''.join(self.image_proxy.get_data())
self.assertEqual(data, '0123456789')
output_logs = self.notifier.get_logs()
self.assertEqual(len(output_logs), 1)
output_log = output_logs[0]
self.assertEqual(output_log['notification_type'], 'INFO')
self.assertEqual(output_log['event_type'], 'image.send')
self.assertEqual(output_log['payload']['image_id'],
self.image.image_id)
self.assertEqual(output_log['payload']['receiver_tenant_id'], TENANT2)
self.assertEqual(output_log['payload']['receiver_user_id'], USER1)
self.assertEqual(output_log['payload']['bytes_sent'], 10)
self.assertEqual(output_log['payload']['owner_id'], TENANT1)
def test_image_get_data_size_mismatch(self):
self.image_proxy.size = 11
list(self.image_proxy.get_data())
output_logs = self.notifier.get_logs()
self.assertEqual(len(output_logs), 1)
output_log = output_logs[0]
self.assertEqual(output_log['notification_type'], 'ERROR')
self.assertEqual(output_log['event_type'], 'image.send')
self.assertEqual(output_log['payload']['image_id'],
self.image.image_id)
def test_image_set_data_prepare_notification(self):
insurance = {'called': False}
def data_iterator():
output_logs = self.notifier.get_logs()
self.assertEqual(len(output_logs), 1)
output_log = output_logs[0]
self.assertEqual(output_log['notification_type'], 'INFO')
self.assertEqual(output_log['event_type'], 'image.prepare')
self.assertEqual(output_log['payload']['id'], self.image.image_id)
yield 'abcd'
yield 'efgh'
insurance['called'] = True
self.image_proxy.set_data(data_iterator(), 8)
self.assertTrue(insurance['called'])
def test_image_set_data_upload_and_activate_notification(self):
def data_iterator():
self.notifier.log = []
yield 'abcde'
yield 'fghij'
self.image_proxy.set_data(data_iterator(), 10)
output_logs = self.notifier.get_logs()
self.assertEqual(len(output_logs), 2)
output_log = output_logs[0]
self.assertEqual(output_log['notification_type'], 'INFO')
self.assertEqual(output_log['event_type'], 'image.upload')
self.assertEqual(output_log['payload']['id'], self.image.image_id)
output_log = output_logs[1]
self.assertEqual(output_log['notification_type'], 'INFO')
self.assertEqual(output_log['event_type'], 'image.activate')
self.assertEqual(output_log['payload']['id'], self.image.image_id)
def test_image_set_data_storage_full(self):
def data_iterator():
self.notifier.log = []
yield 'abcde'
raise exception.StorageFull('Modern Major General')
self.assertRaises(webob.exc.HTTPRequestEntityTooLarge,
self.image_proxy.set_data, data_iterator(), 10)
output_logs = self.notifier.get_logs()
self.assertEqual(len(output_logs), 1)
output_log = output_logs[0]
self.assertEqual(output_log['notification_type'], 'ERROR')
self.assertEqual(output_log['event_type'], 'image.upload')
self.assertTrue('Modern Major General' in output_log['payload'])
def test_image_set_data_value_error(self):
def data_iterator():
self.notifier.log = []
yield 'abcde'
raise ValueError('value wrong')
self.assertRaises(webob.exc.HTTPBadRequest,
self.image_proxy.set_data, data_iterator(), 10)
output_logs = self.notifier.get_logs()
self.assertEqual(len(output_logs), 1)
output_log = output_logs[0]
self.assertEqual(output_log['notification_type'], 'ERROR')
self.assertEqual(output_log['event_type'], 'image.upload')
self.assertTrue('value wrong' in output_log['payload'])
def test_image_set_data_duplicate(self):
def data_iterator():
self.notifier.log = []
yield 'abcde'
raise exception.Duplicate('Cant have duplicates')
self.assertRaises(webob.exc.HTTPConflict,
self.image_proxy.set_data, data_iterator(), 10)
output_logs = self.notifier.get_logs()
self.assertEqual(len(output_logs), 1)
output_log = output_logs[0]
self.assertEqual(output_log['notification_type'], 'ERROR')
self.assertEqual(output_log['event_type'], 'image.upload')
self.assertTrue('Cant have duplicates' in output_log['payload'])
def test_image_set_data_storage_write_denied(self):
def data_iterator():
self.notifier.log = []
yield 'abcde'
raise exception.StorageWriteDenied('The Very Model')
self.assertRaises(webob.exc.HTTPServiceUnavailable,
self.image_proxy.set_data, data_iterator(), 10)
output_logs = self.notifier.get_logs()
self.assertEqual(len(output_logs), 1)
output_log = output_logs[0]
self.assertEqual(output_log['notification_type'], 'ERROR')
self.assertEqual(output_log['event_type'], 'image.upload')
self.assertTrue('The Very Model' in output_log['payload'])
def test_image_set_data_forbidden(self):
def data_iterator():
self.notifier.log = []
yield 'abcde'
raise exception.Forbidden('Not allowed')
self.assertRaises(webob.exc.HTTPForbidden,
self.image_proxy.set_data, data_iterator(), 10)
output_logs = self.notifier.get_logs()
self.assertEqual(len(output_logs), 1)
output_log = output_logs[0]
self.assertEqual(output_log['notification_type'], 'ERROR')
self.assertEqual(output_log['event_type'], 'image.upload')
self.assertTrue('Not allowed' in output_log['payload'])
def test_image_set_data_not_found(self):
def data_iterator():
self.notifier.log = []
yield 'abcde'
raise exception.NotFound('Not found')
self.assertRaises(webob.exc.HTTPNotFound,
self.image_proxy.set_data, data_iterator(), 10)
output_logs = self.notifier.get_logs()
self.assertEqual(len(output_logs), 1)
output_log = output_logs[0]
self.assertEqual(output_log['notification_type'], 'ERROR')
self.assertEqual(output_log['event_type'], 'image.upload')
self.assertTrue('Not found' in output_log['payload'])
def test_image_set_data_HTTP_error(self):
def data_iterator():
self.notifier.log = []
yield 'abcde'
raise webob.exc.HTTPError('Http issue')
self.assertRaises(webob.exc.HTTPError,
self.image_proxy.set_data, data_iterator(), 10)
output_logs = self.notifier.get_logs()
self.assertEqual(len(output_logs), 1)
output_log = output_logs[0]
self.assertEqual(output_log['notification_type'], 'ERROR')
self.assertEqual(output_log['event_type'], 'image.upload')
self.assertTrue('Http issue' in output_log['payload'])
def test_image_set_data_error(self):
def data_iterator():
self.notifier.log = []
yield 'abcde'
raise Exception('Failed')
self.assertRaises(Exception,
self.image_proxy.set_data, data_iterator(), 10)
output_logs = self.notifier.get_logs()
self.assertEqual(len(output_logs), 1)
output_log = output_logs[0]
self.assertEqual(output_log['notification_type'], 'ERROR')
self.assertEqual(output_log['event_type'], 'image.upload')
self.assertTrue('Failed' in output_log['payload'])