Fix PhaseLoggerMixin in case of compose has _logger = None

80fa723 breaks pungi-config-validate that using ValidationCompose
with _logger set to None.

Signed-off-by: Qixiang Wan <qwan@redhat.com>
This commit is contained in:
Qixiang Wan 2016-11-29 19:43:03 +08:00
parent dd814a5f4d
commit 75934f20e5
2 changed files with 46 additions and 9 deletions

View File

@ -162,12 +162,14 @@ class PhaseLoggerMixin(object):
"""
def __init__(self, *args, **kwargs):
super(PhaseLoggerMixin, self).__init__(*args, **kwargs)
self.logger = logging.getLogger(self.name.upper())
self.logger.setLevel(logging.DEBUG)
format = "%(asctime)s [%(name)-16s] [%(levelname)-8s] %(message)s"
import copy
for handler in self.compose._logger.handlers:
hl = copy.copy(handler)
hl.setFormatter(logging.Formatter(format, datefmt="%Y-%m-%d %H:%M:%S"))
hl.setLevel(logging.DEBUG)
self.logger.addHandler(hl)
self.logger = None
if self.compose._logger and self.compose._logger.handlers:
self.logger = logging.getLogger(self.name.upper())
self.logger.setLevel(logging.DEBUG)
format = "%(asctime)s [%(name)-16s] [%(levelname)-8s] %(message)s"
import copy
for handler in self.compose._logger.handlers:
hl = copy.copy(handler)
hl.setFormatter(logging.Formatter(format, datefmt="%Y-%m-%d %H:%M:%S"))
hl.setLevel(logging.DEBUG)
self.logger.addHandler(hl)

View File

@ -0,0 +1,35 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
import mock
import os
import subprocess
import sys
HERE = os.path.abspath(os.path.dirname(__file__))
BINDIR = os.path.join(HERE, '../bin')
PUNGI_CONFIG_VALIDATE = os.path.join(BINDIR, 'pungi-config-validate')
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from tests import helpers
class ConfigValidateScriptTest(helpers.PungiTestCase):
@mock.patch('kobo.shortcuts.run')
def test_validate_dummy_config(self, run):
DUMMY_CONFIG = os.path.join(HERE, 'data/dummy-pungi.conf')
p = subprocess.Popen([PUNGI_CONFIG_VALIDATE, DUMMY_CONFIG],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
self.assertEqual(0, p.returncode)
self.assertEqual('', stdout)
self.assertEqual('', stderr)
if __name__ == '__main__':
unittest.main()