Merge "Rename context.py to logs.py"

This commit is contained in:
Jenkins 2015-10-30 11:02:08 +00:00 committed by Gerrit Code Review
commit cb28cd9ac0
3 changed files with 34 additions and 36 deletions

View File

@ -30,8 +30,8 @@ from oslo_utils import strutils
import openstackclient
from openstackclient.common import clientmanager
from openstackclient.common import commandmanager
from openstackclient.common import context
from openstackclient.common import exceptions as exc
from openstackclient.common import logs
from openstackclient.common import timing
from openstackclient.common import utils
@ -103,7 +103,7 @@ class OpenStackShell(app.App):
def configure_logging(self):
"""Configure logging for the app."""
self.log_configurator = context.LogConfigurator(self.options)
self.log_configurator = logs.LogConfigurator(self.options)
self.dump_stack_trace = self.log_configurator.dump_trace
def run(self, argv):
@ -249,8 +249,6 @@ class OpenStackShell(app.App):
cloud=self.options.cloud,
argparse=self.options,
)
if self.options.debug is not None:
self.options.debug = False
self.log_configurator.configure(self.cloud)
self.dump_stack_trace = self.log_configurator.dump_trace

View File

@ -14,7 +14,7 @@
import logging
import mock
from openstackclient.common import context
from openstackclient.common import logs
from openstackclient.tests import utils
@ -23,51 +23,51 @@ class TestContext(utils.TestCase):
def test_log_level_from_options(self):
opts = mock.Mock()
opts.verbose_level = 0
self.assertEqual(logging.ERROR, context.log_level_from_options(opts))
self.assertEqual(logging.ERROR, logs.log_level_from_options(opts))
opts.verbose_level = 1
self.assertEqual(logging.WARNING, context.log_level_from_options(opts))
self.assertEqual(logging.WARNING, logs.log_level_from_options(opts))
opts.verbose_level = 2
self.assertEqual(logging.INFO, context.log_level_from_options(opts))
self.assertEqual(logging.INFO, logs.log_level_from_options(opts))
opts.verbose_level = 3
self.assertEqual(logging.DEBUG, context.log_level_from_options(opts))
self.assertEqual(logging.DEBUG, logs.log_level_from_options(opts))
def test_log_level_from_config(self):
cfg = {'verbose_level': 0}
self.assertEqual(logging.ERROR, context.log_level_from_config(cfg))
self.assertEqual(logging.ERROR, logs.log_level_from_config(cfg))
cfg = {'verbose_level': 1}
self.assertEqual(logging.WARNING, context.log_level_from_config(cfg))
self.assertEqual(logging.WARNING, logs.log_level_from_config(cfg))
cfg = {'verbose_level': 2}
self.assertEqual(logging.INFO, context.log_level_from_config(cfg))
self.assertEqual(logging.INFO, logs.log_level_from_config(cfg))
cfg = {'verbose_level': 3}
self.assertEqual(logging.DEBUG, context.log_level_from_config(cfg))
self.assertEqual(logging.DEBUG, logs.log_level_from_config(cfg))
cfg = {'verbose_level': 1, 'log_level': 'critical'}
self.assertEqual(logging.CRITICAL, context.log_level_from_config(cfg))
self.assertEqual(logging.CRITICAL, logs.log_level_from_config(cfg))
cfg = {'verbose_level': 1, 'log_level': 'error'}
self.assertEqual(logging.ERROR, context.log_level_from_config(cfg))
self.assertEqual(logging.ERROR, logs.log_level_from_config(cfg))
cfg = {'verbose_level': 1, 'log_level': 'warning'}
self.assertEqual(logging.WARNING, context.log_level_from_config(cfg))
self.assertEqual(logging.WARNING, logs.log_level_from_config(cfg))
cfg = {'verbose_level': 1, 'log_level': 'info'}
self.assertEqual(logging.INFO, context.log_level_from_config(cfg))
self.assertEqual(logging.INFO, logs.log_level_from_config(cfg))
cfg = {'verbose_level': 1, 'log_level': 'debug'}
self.assertEqual(logging.DEBUG, context.log_level_from_config(cfg))
self.assertEqual(logging.DEBUG, logs.log_level_from_config(cfg))
cfg = {'verbose_level': 1, 'log_level': 'bogus'}
self.assertEqual(logging.WARNING, context.log_level_from_config(cfg))
self.assertEqual(logging.WARNING, logs.log_level_from_config(cfg))
cfg = {'verbose_level': 1, 'log_level': 'info', 'debug': True}
self.assertEqual(logging.DEBUG, context.log_level_from_config(cfg))
self.assertEqual(logging.DEBUG, logs.log_level_from_config(cfg))
@mock.patch('warnings.simplefilter')
def test_set_warning_filter(self, simplefilter):
context.set_warning_filter(logging.ERROR)
logs.set_warning_filter(logging.ERROR)
simplefilter.assert_called_with("ignore")
context.set_warning_filter(logging.WARNING)
logs.set_warning_filter(logging.WARNING)
simplefilter.assert_called_with("ignore")
context.set_warning_filter(logging.INFO)
logs.set_warning_filter(logging.INFO)
simplefilter.assert_called_with("once")
class TestFileFormatter(utils.TestCase):
def test_nothing(self):
formatter = context._FileFormatter()
formatter = logs._FileFormatter()
self.assertEqual(('%(asctime)s.%(msecs)03d %(process)d %(levelname)s '
'%(name)s %(message)s'), formatter.fmt)
@ -77,7 +77,7 @@ class TestFileFormatter(utils.TestCase):
os_project_name = 'projecty'
username = 'usernamey'
options = Opts()
formatter = context._FileFormatter(options=options)
formatter = logs._FileFormatter(options=options)
self.assertEqual(('%(asctime)s.%(msecs)03d %(process)d %(levelname)s '
'%(name)s [cloudy usernamey projecty] %(message)s'),
formatter.fmt)
@ -86,7 +86,7 @@ class TestFileFormatter(utils.TestCase):
config = mock.Mock()
config.config = {'cloud': 'cloudy'}
config.auth = {'project_name': 'projecty', 'username': 'usernamey'}
formatter = context._FileFormatter(config=config)
formatter = logs._FileFormatter(config=config)
self.assertEqual(('%(asctime)s.%(msecs)03d %(process)d %(levelname)s '
'%(name)s [cloudy usernamey projecty] %(message)s'),
formatter.fmt)
@ -119,7 +119,7 @@ class TestLogConfigurator(utils.TestCase):
@mock.patch('logging.StreamHandler')
@mock.patch('logging.getLogger')
@mock.patch('openstackclient.common.context.set_warning_filter')
@mock.patch('openstackclient.common.logs.set_warning_filter')
def test_init(self, warning_filter, getLogger, handle):
getLogger.side_effect = self.loggers
console_logger = mock.Mock()
@ -127,7 +127,7 @@ class TestLogConfigurator(utils.TestCase):
console_logger.setLevel = mock.Mock()
handle.return_value = console_logger
configurator = context.LogConfigurator(self.options)
configurator = logs.LogConfigurator(self.options)
getLogger.assert_called_with('iso8601') # last call
warning_filter.assert_called_with(logging.WARNING)
@ -140,12 +140,12 @@ class TestLogConfigurator(utils.TestCase):
self.assertEqual(False, configurator.dump_trace)
@mock.patch('logging.getLogger')
@mock.patch('openstackclient.common.context.set_warning_filter')
@mock.patch('openstackclient.common.logs.set_warning_filter')
def test_init_no_debug(self, warning_filter, getLogger):
getLogger.side_effect = self.loggers
self.options.debug = True
configurator = context.LogConfigurator(self.options)
configurator = logs.LogConfigurator(self.options)
warning_filter.assert_called_with(logging.DEBUG)
self.requests_log.setLevel.assert_called_with(logging.DEBUG)
@ -153,8 +153,8 @@ class TestLogConfigurator(utils.TestCase):
@mock.patch('logging.FileHandler')
@mock.patch('logging.getLogger')
@mock.patch('openstackclient.common.context.set_warning_filter')
@mock.patch('openstackclient.common.context._FileFormatter')
@mock.patch('openstackclient.common.logs.set_warning_filter')
@mock.patch('openstackclient.common.logs._FileFormatter')
def test_init_log_file(self, formatter, warning_filter, getLogger, handle):
getLogger.side_effect = self.loggers
self.options.log_file = '/tmp/log_file'
@ -165,7 +165,7 @@ class TestLogConfigurator(utils.TestCase):
mock_formatter = mock.Mock()
formatter.return_value = mock_formatter
context.LogConfigurator(self.options)
logs.LogConfigurator(self.options)
handle.assert_called_with(filename=self.options.log_file)
self.root_logger.addHandler.assert_called_with(file_logger)
@ -174,11 +174,11 @@ class TestLogConfigurator(utils.TestCase):
@mock.patch('logging.FileHandler')
@mock.patch('logging.getLogger')
@mock.patch('openstackclient.common.context.set_warning_filter')
@mock.patch('openstackclient.common.context._FileFormatter')
@mock.patch('openstackclient.common.logs.set_warning_filter')
@mock.patch('openstackclient.common.logs._FileFormatter')
def test_configure(self, formatter, warning_filter, getLogger, handle):
getLogger.side_effect = self.loggers
configurator = context.LogConfigurator(self.options)
configurator = logs.LogConfigurator(self.options)
cloud_config = mock.Mock()
config_log = '/tmp/config_log'
cloud_config.config = {