Reinstate deprecation warnings

The commit 4188c40beb cleaned up a lot of
places where the warnings module was just being used as a non-standard way
to log a warning. However, the way the module was used in the rsrc_defn
module is correct and should not have been changed. By passing a
DeprecationWarning to warnings.warn() we not only make it easy to find
deprecated functions in the source, we also make it possible for developers
to test whether deprecated code is unused by forcing the exception to be
raised. There's also no point in translating these warnings, since they are
targeted at developers (mostly developers of third-party plugins) and not
operators per se. This patch reverts the change to that one file and its
tests.

Change-Id: I9cb1e739e9f173c8f70cb944e886cc3b49abc5f3
This commit is contained in:
Zane Bitter 2016-04-13 19:49:04 -04:00
parent 5eede801f6
commit 6d786e6e7f
2 changed files with 20 additions and 13 deletions

View File

@ -14,16 +14,15 @@ import collections
import copy
import itertools
import operator
from oslo_log import log
import warnings
import six
from heat.common import exception
from heat.common.i18n import _LW
from heat.common.i18n import repr_wrapper
from heat.engine import function
from heat.engine import properties
LOG = log.getLogger(__name__)
__all__ = ['ResourceDefinition']
@ -291,11 +290,11 @@ class ResourceDefinition(ResourceDefinitionCore, collections.Mapping):
"""A resource definition that also acts like a cfn template snippet.
This class exists only for backwards compatibility with existing resource
plugins and unit tests; it is deprecated and then could be replaced with
ResourceDefinitionCore as soon as M release.
plugins and unit tests; it is deprecated and will be replaced with
ResourceDefinitionCore, possibly as soon as the Ocata release.
"""
_deprecation_msg = _LW(
_deprecation_msg = (
'Reading the ResourceDefinition as if it were a snippet of a '
'CloudFormation template is deprecated, and the ability to treat it '
'as such will be removed in the future. Resource plugins should use '
@ -330,7 +329,7 @@ class ResourceDefinition(ResourceDefinitionCore, collections.Mapping):
This is for backwards compatibility with existing code that expects a
parsed-JSON template snippet.
"""
LOG.warning(self._deprecation_msg)
warnings.warn(self._deprecation_msg, DeprecationWarning)
yield TYPE
if self._properties is not None:
@ -352,7 +351,7 @@ class ResourceDefinition(ResourceDefinitionCore, collections.Mapping):
This is for backwards compatibility with existing code that expects a
parsed-JSON template snippet.
"""
LOG.warning(self._deprecation_msg)
warnings.warn(self._deprecation_msg, DeprecationWarning)
if key == TYPE:
return self.resource_type
@ -385,8 +384,6 @@ class ResourceDefinition(ResourceDefinitionCore, collections.Mapping):
This is for backwards compatibility with existing code that expects a
parsed-JSON template snippet.
"""
LOG.warning(self._deprecation_msg)
return len(list(iter(self)))
def __repr__(self):

View File

@ -12,6 +12,7 @@
# under the License.
import six
import warnings
from heat.common import exception
from heat.engine.cfn import functions as cfn_funcs
@ -211,8 +212,17 @@ class ResourceDefinitionSnippetTest(common.HeatTestCase):
def test_resource_snippet(self):
rd = rsrc_defn.ResourceDefinition('rsrc', 'SomeType', **self.defn)
with warnings.catch_warnings(record=True) as ws:
warnings.filterwarnings('always')
exp_result = {'Type': 'SomeType'}
exp_result.update(self.expected)
# Work around http://bugs.python.org/issue4180
getattr(rsrc_defn, '__warningregistry__', {}).clear()
self.assertEqual(exp_result, rd)
exp_result = {'Type': 'SomeType'}
exp_result.update(self.expected)
self.assertEqual(exp_result, rd)
self.assertTrue(ws)
for warn in ws:
self.assertTrue(issubclass(warn.category, DeprecationWarning))