~xianghui/ubuntu/trusty/oslo.messaging/icehouse-lp1521958

« back to all changes in this revision

Viewing changes to tests/test_log_handler.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2014-03-27 13:01:34 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20140327130134-va1pxzs253r43n15
Tags: 1.3.0~a9-0ubuntu1
* New upstream release (LP: #1298970)
* debian/control:
  - Add python-oslotest as a build dependency.
  - Use python-oslosphinx instead of python-oslo.sphinx

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
#
 
5
# http://www.apache.org/licenses/LICENSE-2.0
 
6
#
 
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
 
11
# under the License.
 
12
 
 
13
import logging
 
14
import mock
 
15
 
 
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
 
20
 
 
21
 
 
22
class PublishErrorsHandlerTestCase(test_utils.BaseTestCase):
 
23
    """Tests for log.PublishErrorsHandler"""
 
24
    def setUp(self):
 
25
        super(PublishErrorsHandlerTestCase, self).setUp()
 
26
        self.conf.register_opts(messaging.notify.notifier._notifier_opts)
 
27
        self.publisherrorshandler = (log_handler.
 
28
                                     PublishErrorsHandler(logging.ERROR))
 
29
 
 
30
    def test_emit_cfg_log_notifier_in_notifier_drivers(self):
 
31
        drivers = ['messaging', 'log']
 
32
        self.config(notification_driver=drivers)
 
33
        self.stub_flg = True
 
34
 
 
35
        transport = test_notifier._FakeTransport(self.conf)
 
36
        notifier = messaging.Notifier(transport)
 
37
 
 
38
        def fake_notifier(*args, **kwargs):
 
39
            self.stub_flg = False
 
40
 
 
41
        self.stubs.Set(notifier, 'error', fake_notifier)
 
42
 
 
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)
 
48
 
 
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,
 
64
                                           'error_notification',
 
65
                                           {'error': 'Message'},
 
66
                                           'ERROR')