1
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2
# not use this file except in compliance with the License. You may obtain
3
# a copy of the License at
5
# http://www.apache.org/licenses/LICENSE-2.0
7
# Unless required by applicable law or agreed to in writing, software
8
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10
# License for the specific language governing permissions and limitations
16
from oslo import messaging
17
from oslo.messaging.notify import log_handler
18
from tests import test_notifier
19
from tests import utils as test_utils
22
class PublishErrorsHandlerTestCase(test_utils.BaseTestCase):
23
"""Tests for log.PublishErrorsHandler"""
25
super(PublishErrorsHandlerTestCase, self).setUp()
26
self.conf.register_opts(messaging.notify.notifier._notifier_opts)
27
self.publisherrorshandler = (log_handler.
28
PublishErrorsHandler(logging.ERROR))
30
def test_emit_cfg_log_notifier_in_notifier_drivers(self):
31
drivers = ['messaging', 'log']
32
self.config(notification_driver=drivers)
35
transport = test_notifier._FakeTransport(self.conf)
36
notifier = messaging.Notifier(transport)
38
def fake_notifier(*args, **kwargs):
41
self.stubs.Set(notifier, 'error', fake_notifier)
43
logrecord = logging.LogRecord(name='name', level='WARN',
44
pathname='/tmp', lineno=1, msg='Message',
45
args=None, exc_info=None)
46
self.publisherrorshandler.emit(logrecord)
47
self.assertTrue(self.stub_flg)
49
@mock.patch.object(messaging.notify.notifier.Notifier, '_notify')
50
def test_emit_notification(self, mock_notify):
51
logrecord = logging.LogRecord(name='name', level='ERROR',
52
pathname='/tmp', lineno=1, msg='Message',
53
args=None, exc_info=None)
54
mock_init = mock.Mock(return_value=None)
55
with mock.patch.object(messaging.notify.notifier.Notifier,
56
'__init__', mock_init):
57
# Recreate the handler so the __init__ mock takes effect.
58
self.publisherrorshandler = (log_handler.
59
PublishErrorsHandler(logging.ERROR))
60
self.publisherrorshandler.emit(logrecord)
61
mock_init.assert_called_with(mock.ANY,
62
publisher_id='error.publisher')
63
mock_notify.assert_called_with(None,