diff --git a/oslo/concurrency/__init__.py b/oslo/concurrency/__init__.py index afd4685..b73c33f 100644 --- a/oslo/concurrency/__init__.py +++ b/oslo/concurrency/__init__.py @@ -22,7 +22,7 @@ def deprecated(): ('The oslo namespace package is deprecated. Please use %s instead.' % new_name), DeprecationWarning, - stacklevel=1, + stacklevel=3, ) diff --git a/tests/test_warning.py b/tests/test_warning.py index ba1c4df..ec71e0c 100644 --- a/tests/test_warning.py +++ b/tests/test_warning.py @@ -11,9 +11,12 @@ # under the License. import imp +import os +import warnings import mock from oslotest import base as test_base +import six class DeprecationWarningTest(test_base.BaseTestCase): @@ -27,3 +30,32 @@ class DeprecationWarningTest(test_base.BaseTestCase): self.assertIn('oslo_concurrency', args[0][0]) self.assertIn('deprecated', args[0][0]) self.assertTrue(issubclass(args[0][1], DeprecationWarning)) + + def test_real_warning(self): + with warnings.catch_warnings(record=True) as warning_msgs: + warnings.resetwarnings() + warnings.simplefilter('always', DeprecationWarning) + import oslo.concurrency + + # Use a separate function to get the stack level correct + # so we know the message points back to this file. This + # corresponds to an import or reload, which isn't working + # inside the test under Python 3.3. That may be due to a + # difference in the import implementation not triggering + # warnings properly when the module is reloaded, or + # because the warnings module is mostly implemented in C + # and something isn't cleanly resetting the global state + # used to track whether a warning needs to be + # emitted. Whatever the cause, we definitely see the + # warnings.warn() being invoked on a reload (see the test + # above) and warnings are reported on the console when we + # run the tests. A simpler test script run outside of + # testr does correctly report the warnings. + def foo(): + oslo.concurrency.deprecated() + + foo() + self.assertEqual(1, len(warning_msgs)) + msg = warning_msgs[0] + self.assertIn('oslo_concurrency', six.text_type(msg.message)) + self.assertEqual('test_warning.py', os.path.basename(msg.filename))