summaryrefslogtreecommitdiff
path: root/tests/test_warning.py
blob: 8e7d96cc0b9447c2218309c40c9341ea6a60e713 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#    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.

import imp
import os
import warnings

import mock
from oslotest import base as test_base
import six


class DeprecationWarningTest(test_base.BaseTestCase):

    @mock.patch('warnings.warn')
    def test_warning(self, mock_warn):
        import oslo.middleware
        imp.reload(oslo.middleware)
        self.assertTrue(mock_warn.called)
        args = mock_warn.call_args
        self.assertIn('oslo_middleware', 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.middleware

            # 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.middleware.deprecated()

            foo()
            self.assertEqual(1, len(warning_msgs))
            msg = warning_msgs[0]
            self.assertIn('oslo_middleware', six.text_type(msg.message))
            self.assertEqual('test_warning.py', os.path.basename(msg.filename))