From fbe9e74efc3abae1356c1bdb6fdf88af520ebb29 Mon Sep 17 00:00:00 2001 From: Philip Knouff Date: Thu, 23 Feb 2012 16:08:44 +0000 Subject: [PATCH] Prevent infinite loop in PublishErrorsHandler Fixes bug #883293 Note: this is an update of https://review.openstack.org/#change,1747 originally author by Donald Ngo Change-Id: Ie6ae4f961fb0519df98408baf0a3f8fac0eb6682 --- nova/log.py | 3 +++ nova/tests/test_log.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/nova/log.py b/nova/log.py index f941d0a0..60028a68 100644 --- a/nova/log.py +++ b/nova/log.py @@ -262,6 +262,9 @@ class LegacyNovaFormatter(logging.Formatter): class PublishErrorsHandler(logging.Handler): def emit(self, record): + if 'list_notifier_drivers' in FLAGS: + if 'nova.notifier.log_notifier' in FLAGS.list_notifier_drivers: + return nova.notifier.api.notify('nova.error.publisher', 'error_notification', nova.notifier.api.ERROR, dict(error=record.msg)) diff --git a/nova/tests/test_log.py b/nova/tests/test_log.py index ff3c0791..3c6efba5 100644 --- a/nova/tests/test_log.py +++ b/nova/tests/test_log.py @@ -1,13 +1,17 @@ import cStringIO import json import logging +import sys from nova import context from nova import flags from nova import log +from nova.notifier import api as notifier from nova import test FLAGS = flags.FLAGS +flags.DECLARE('list_notifier_drivers', + 'nova.notifier.list_notifier') def _fake_context(): @@ -74,6 +78,40 @@ class LogHandlerTestCase(test.TestCase): '/some/path/foo-bar.log') +class PublishErrorsHandlerTestCase(test.TestCase): + """Tests for nova.log.PublishErrorsHandler""" + def setUp(self): + super(PublishErrorsHandlerTestCase, self).setUp() + self.handler = logging.Handler() + self.publiserrorshandler = log.PublishErrorsHandler(logging.ERROR) + + def test_emit_cfg_list_notifier_drivers_in_flags(self): + self.stub_flg = False + + def fake_notifier(*args, **kwargs): + self.stub_flg = True + + self.stubs.Set(notifier, 'notify', fake_notifier) + logrecord = logging.LogRecord('name', 'WARN', '/tmp', 1, + 'Message', None, None) + self.publiserrorshandler.emit(logrecord) + self.assertTrue(self.stub_flg) + + def test_emit_cfg_log_notifier_in_list_notifier_drivers(self): + self.flags(list_notifier_drivers=['nova.notifier.rabbit_notifier', + 'nova.notifier.log_notifier']) + self.stub_flg = True + + def fake_notifier(*args, **kwargs): + self.stub_flg = False + + self.stubs.Set(notifier, 'notify', fake_notifier) + logrecord = logging.LogRecord('name', 'WARN', '/tmp', 1, + 'Message', None, None) + self.publiserrorshandler.emit(logrecord) + self.assertTrue(self.stub_flg) + + class NovaFormatterTestCase(test.TestCase): def setUp(self): super(NovaFormatterTestCase, self).setUp()