From: Cedric Brandily Date: Thu, 26 Nov 2015 15:21:30 +0000 (+0000) Subject: Correct unwatch_log to support python <= 2.7.5 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=eadd68f1774e4b9c0b11d361d52f761e4588a244;p=openstack-build%2Fneutron-build.git Correct unwatch_log to support python <= 2.7.5 Currently unwatch_log[1] uses WatchedFileHandler.delay attribute to build a FileHandler instance from a WatchedFileHandler instance but this attribute doesn't exist before 2.7.6. This change removes WatchedFileHandler.delay usage and uses default FileHandler delay (it also ensures we open immediately the log file). [1] neutron.agent.linux.daemon Closes-Bug: #1520271 Change-Id: I70a156c4aeeda8a3adcc1036d670732c21ffa335 --- diff --git a/neutron/agent/linux/daemon.py b/neutron/agent/linux/daemon.py index 7f786e241..f043920a7 100644 --- a/neutron/agent/linux/daemon.py +++ b/neutron/agent/linux/daemon.py @@ -81,10 +81,11 @@ def unwatch_log(): to_replace = [h for h in log_root.handlers if isinstance(h, handlers.WatchedFileHandler)] for handler in to_replace: + # NOTE(cbrandily): we use default delay(=False) to ensure the log file + # is opened before privileges drop. new_handler = std_logging.FileHandler(handler.baseFilename, mode=handler.mode, - encoding=handler.encoding, - delay=handler.delay) + encoding=handler.encoding) log_root.removeHandler(handler) log_root.addHandler(new_handler) diff --git a/neutron/tests/unit/agent/linux/test_daemon.py b/neutron/tests/unit/agent/linux/test_daemon.py index 749b1e264..c093909f0 100644 --- a/neutron/tests/unit/agent/linux/test_daemon.py +++ b/neutron/tests/unit/agent/linux/test_daemon.py @@ -13,7 +13,8 @@ # License for the specific language governing permissions and limitations # under the License. - +import logging +from logging import handlers import os import sys @@ -33,6 +34,23 @@ class FakeEntry(object): setattr(self, name, value) +class TestUnwatchLog(base.BaseTestCase): + + def test_unwatch_log(self): + stream_handler = logging.StreamHandler() + logger = logging.Logger('fake') + logger.addHandler(stream_handler) + logger.addHandler(handlers.WatchedFileHandler('/tmp/filename1')) + + with mock.patch('logging.getLogger', return_value=logger): + daemon.unwatch_log() + self.assertEqual(2, len(logger.handlers)) + logger.handlers.remove(stream_handler) + observed = logger.handlers[0] + self.assertEqual(logging.FileHandler, type(observed)) + self.assertEqual('/tmp/filename1', observed.baseFilename) + + class TestPrivileges(base.BaseTestCase): def test_setuid_with_name(self): with mock.patch('pwd.getpwnam', return_value=FakeEntry('pw_uid', 123)):