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
(cherry picked from commit eadd68f177)
This commit is contained in:
Cedric Brandily 2015-11-26 15:21:30 +00:00 committed by abregman
parent d0159dc99b
commit ccebcde399
2 changed files with 22 additions and 3 deletions

View File

@ -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)

View File

@ -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
@ -32,6 +33,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)):