~ubuntu-branches/ubuntu/vivid/neutron/vivid-updates

« back to all changes in this revision

Viewing changes to neutron/tests/unit/hacking/test_checks.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2015-03-30 11:17:19 UTC
  • mfrom: (1.1.21)
  • Revision ID: package-import@ubuntu.com-20150330111719-h0gx7233p4jkkgfh
Tags: 1:2015.1~b3-0ubuntu1
* New upstream milestone release:
  - d/control: Align version requirements with upstream.
  - d/control: Add new dependency on oslo-log.
  - d/p/*: Rebase.
  - d/control,d/neutron-plugin-hyperv*: Dropped, decomposed into
    separate project upstream.
  - d/control,d/neutron-plugin-openflow*: Dropped, decomposed into
    separate project upstream.
  - d/neutron-common.install: Add neutron-rootwrap-daemon and 
    neutron-keepalived-state-change binaries.
  - d/rules: Ignore neutron-hyperv-agent when installing; only for Windows.
  - d/neutron-plugin-cisco.install: Drop neutron-cisco-cfg-agent as
    decomposed into separate project upstream.
  - d/neutron-plugin-vmware.install: Drop neutron-check-nsx-config and
    neutron-nsx-manage as decomposed into separate project upstream.
  - d/control: Add dependency on python-neutron-fwaas to neutron-l3-agent.
* d/pydist-overrides: Add overrides for oslo packages.
* d/control: Fixup type in package description (LP: #1263539).
* d/p/fixup-driver-test-execution.patch: Cherry pick fix from upstream VCS
  to support unit test exection in out-of-tree vendor drivers.
* d/neutron-common.postinst: Allow general access to /etc/neutron but limit
  access to root/neutron to /etc/neutron/neutron.conf to support execution
  of unit tests in decomposed vendor drivers.
* d/control: Add dependency on python-neutron-fwaas to neutron-l3-agent
  package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
2
#    not use this file except in compliance with the License. You may obtain
 
3
#    a copy of the License at
 
4
#
 
5
#         http://www.apache.org/licenses/LICENSE-2.0
 
6
#
 
7
#    Unless required by applicable law or agreed to in writing, software
 
8
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
9
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
10
#    License for the specific language governing permissions and limitations
 
11
#    under the License.
 
12
 
 
13
import testtools
 
14
 
 
15
from neutron.hacking import checks
 
16
from neutron.tests import base
 
17
 
 
18
 
 
19
class HackingTestCase(base.BaseTestCase):
 
20
 
 
21
    def test_log_translations(self):
 
22
        expected_marks = {
 
23
            'error': '_LE',
 
24
            'info': '_LI',
 
25
            'warn': '_LW',
 
26
            'warning': '_LW',
 
27
            'critical': '_LC',
 
28
            'exception': '_LE',
 
29
        }
 
30
        logs = expected_marks.keys()
 
31
        debug = "LOG.debug('OK')"
 
32
        self.assertEqual(
 
33
            0, len(list(checks.validate_log_translations(debug, debug, 'f'))))
 
34
        for log in logs:
 
35
            bad = 'LOG.%s("Bad")' % log
 
36
            self.assertEqual(
 
37
                1, len(list(checks.validate_log_translations(bad, bad, 'f'))))
 
38
            ok = "LOG.%s('OK')    # noqa" % log
 
39
            self.assertEqual(
 
40
                0, len(list(checks.validate_log_translations(ok, ok, 'f'))))
 
41
            ok = "LOG.%s(variable)" % log
 
42
            self.assertEqual(
 
43
                0, len(list(checks.validate_log_translations(ok, ok, 'f'))))
 
44
 
 
45
            for mark in checks._all_hints:
 
46
                stmt = "LOG.%s(%s('test'))" % (log, mark)
 
47
                self.assertEqual(
 
48
                    0 if expected_marks[log] == mark else 1,
 
49
                    len(list(checks.validate_log_translations(stmt, stmt,
 
50
                                                              'f'))))
 
51
 
 
52
    def test_no_translate_debug_logs(self):
 
53
        for hint in checks._all_hints:
 
54
            bad = "LOG.debug(%s('bad'))" % hint
 
55
            self.assertEqual(
 
56
                1, len(list(checks.no_translate_debug_logs(bad, 'f'))))
 
57
 
 
58
    def test_use_jsonutils(self):
 
59
        def __get_msg(fun):
 
60
            msg = ("N321: jsonutils.%(fun)s must be used instead of "
 
61
                   "json.%(fun)s" % {'fun': fun})
 
62
            return [(0, msg)]
 
63
 
 
64
        for method in ('dump', 'dumps', 'load', 'loads'):
 
65
            self.assertEqual(
 
66
                __get_msg(method),
 
67
                list(checks.use_jsonutils("json.%s(" % method,
 
68
                                          "./neutron/common/rpc.py")))
 
69
 
 
70
            self.assertEqual(0,
 
71
                len(list(checks.use_jsonutils("jsonx.%s(" % method,
 
72
                                              "./neutron/common/rpc.py"))))
 
73
 
 
74
            self.assertEqual(0,
 
75
                len(list(checks.use_jsonutils("json.%sx(" % method,
 
76
                                              "./neutron/common/rpc.py"))))
 
77
 
 
78
            self.assertEqual(0,
 
79
                len(list(checks.use_jsonutils(
 
80
                    "json.%s" % method,
 
81
                    "./neutron/plugins/openvswitch/agent/xenapi/etc/xapi.d/"
 
82
                    "plugins/netwrap"))))
 
83
 
 
84
    def test_assert_called_once_with(self):
 
85
        fail_code1 = """
 
86
               mock = Mock()
 
87
               mock.method(1, 2, 3, test='wow')
 
88
               mock.method.assert_called_once()
 
89
               """
 
90
        fail_code2 = """
 
91
               mock = Mock()
 
92
               mock.method(1, 2, 3, test='wow')
 
93
               mock.method.assertCalledOnceWith()
 
94
               """
 
95
        pass_code = """
 
96
               mock = Mock()
 
97
               mock.method(1, 2, 3, test='wow')
 
98
               mock.method.assert_called_once_with()
 
99
               """
 
100
        self.assertEqual(
 
101
            1, len(list(checks.check_assert_called_once_with(fail_code1,
 
102
                                            "neutron/tests/test_assert.py"))))
 
103
        self.assertEqual(
 
104
            1, len(list(checks.check_assert_called_once_with(fail_code2,
 
105
                                            "neutron/tests/test_assert.py"))))
 
106
        self.assertEqual(
 
107
            0, len(list(checks.check_assert_called_once_with(pass_code,
 
108
                                            "neutron/tests/test_assert.py"))))
 
109
 
 
110
    def test_check_oslo_namespace_imports(self):
 
111
        def check(s, fail=True):
 
112
            func = checks.check_oslo_namespace_imports
 
113
            if fail:
 
114
                self.assertIsInstance(next(func(s)), tuple)
 
115
            else:
 
116
                with testtools.ExpectedException(StopIteration):
 
117
                    next(func(s))
 
118
 
 
119
        check('from oslo_utils import importutils', fail=False)
 
120
        check('import oslo_messaging', fail=False)
 
121
        check('from oslo.utils import importutils')
 
122
        check('from oslo import messaging')
 
123
        check('import oslo.messaging')