diff options
| author | Jenkins <jenkins@review.openstack.org> | 2012-10-31 21:31:17 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2012-10-31 21:31:17 +0000 |
| commit | 3e04c41c96fbf5916ff5ab8c3a8bfec5021f6b04 (patch) | |
| tree | 0867d3853f94a25023301da940c11c872bf17bca | |
| parent | d7b63972c77cae255872f0837cb043361da47cb2 (diff) | |
| parent | c8cf6f74001ec3ced16a516c7b10a596b75bc34c (diff) | |
| download | oslo-3e04c41c96fbf5916ff5ab8c3a8bfec5021f6b04.tar.gz oslo-3e04c41c96fbf5916ff5ab8c3a8bfec5021f6b04.tar.xz oslo-3e04c41c96fbf5916ff5ab8c3a8bfec5021f6b04.zip | |
Merge "move nova.common.deprecated to openstack-common"
| -rw-r--r-- | openstack/common/log.py | 18 | ||||
| -rw-r--r-- | tests/unit/test_deprecated.py | 53 |
2 files changed, 71 insertions, 0 deletions
diff --git a/openstack/common/log.py b/openstack/common/log.py index 783ff2d..6cb3dfc 100644 --- a/openstack/common/log.py +++ b/openstack/common/log.py @@ -76,6 +76,9 @@ log_opts = [ cfg.BoolOpt('publish_errors', default=False, help='publish error events'), + cfg.BoolOpt('fatal_deprecations', + default=False, + help='make deprecations fatal'), # NOTE(mikal): there are two options here because sometimes we are handed # a full instance (and could include more information), and other times we @@ -170,6 +173,14 @@ class ContextAdapter(logging.LoggerAdapter): def audit(self, msg, *args, **kwargs): self.log(logging.AUDIT, msg, *args, **kwargs) + def deprecated(self, msg, *args, **kwargs): + stdmsg = _("Deprecated Config: %s") % msg + if CONF.fatal_deprecations: + self.critical(stdmsg, *args, **kwargs) + raise DeprecatedConfig(msg=stdmsg) + else: + self.warn(stdmsg, *args, **kwargs) + def process(self, msg, kwargs): if 'extra' not in kwargs: kwargs['extra'] = {} @@ -450,3 +461,10 @@ class ColorHandler(logging.StreamHandler): def format(self, record): record.color = self.LEVEL_COLORS[record.levelno] return logging.StreamHandler.format(self, record) + + +class DeprecatedConfig(Exception): + message = _("Fatal call to deprecated config: %(msg)s") + + def __init__(self, msg): + super(Exception, self).__init__(self.message % dict(msg=msg)) diff --git a/tests/unit/test_deprecated.py b/tests/unit/test_deprecated.py new file mode 100644 index 0000000..770a7b9 --- /dev/null +++ b/tests/unit/test_deprecated.py @@ -0,0 +1,53 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 +# +# Copyright 2010 OpenStack LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from openstack.common import cfg +from openstack.common import log as logging +from tests import utils as test_utils + +LOG = logging.getLogger(__name__) + + +class DeprecatedConfigTestCase(test_utils.BaseTestCase): + def setUp(self): + super(DeprecatedConfigTestCase, self).setUp() + self.warnbuffer = "" + self.critbuffer = "" + + def warn_log(msg): + self.warnbuffer = msg + + def critical_log(msg): + self.critbuffer = msg + + self.stubs.Set(LOG, 'warn', warn_log) + self.stubs.Set(LOG, 'critical', critical_log) + + def test_deprecated(self): + LOG.deprecated('test') + self.assertEqual(self.warnbuffer, 'Deprecated Config: test') + + def test_deprecated_fatal(self): + self.config(fatal_deprecations=True) + self.assertRaises(logging.DeprecatedConfig, + LOG.deprecated, "test2") + self.assertEqual(self.critbuffer, 'Deprecated Config: test2') + + def test_deprecated_logs_only_once(self): + LOG.deprecated('only once!') + LOG.deprecated('only once!') + LOG.deprecated('only once!') + self.assertEqual(self.warnbuffer, 'Deprecated Config: only once!') |
