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
5
# http://www.apache.org/licenses/LICENSE-2.0
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
15
from neutron.hacking import checks
16
from neutron.tests import base
19
class HackingTestCase(base.BaseTestCase):
21
def test_log_translations(self):
30
logs = expected_marks.keys()
31
debug = "LOG.debug('OK')"
33
0, len(list(checks.validate_log_translations(debug, debug, 'f'))))
35
bad = 'LOG.%s("Bad")' % log
37
1, len(list(checks.validate_log_translations(bad, bad, 'f'))))
38
ok = "LOG.%s('OK') # noqa" % log
40
0, len(list(checks.validate_log_translations(ok, ok, 'f'))))
41
ok = "LOG.%s(variable)" % log
43
0, len(list(checks.validate_log_translations(ok, ok, 'f'))))
45
for mark in checks._all_hints:
46
stmt = "LOG.%s(%s('test'))" % (log, mark)
48
0 if expected_marks[log] == mark else 1,
49
len(list(checks.validate_log_translations(stmt, stmt,
52
def test_no_translate_debug_logs(self):
53
for hint in checks._all_hints:
54
bad = "LOG.debug(%s('bad'))" % hint
56
1, len(list(checks.no_translate_debug_logs(bad, 'f'))))
58
def test_use_jsonutils(self):
60
msg = ("N321: jsonutils.%(fun)s must be used instead of "
61
"json.%(fun)s" % {'fun': fun})
64
for method in ('dump', 'dumps', 'load', 'loads'):
67
list(checks.use_jsonutils("json.%s(" % method,
68
"./neutron/common/rpc.py")))
71
len(list(checks.use_jsonutils("jsonx.%s(" % method,
72
"./neutron/common/rpc.py"))))
75
len(list(checks.use_jsonutils("json.%sx(" % method,
76
"./neutron/common/rpc.py"))))
79
len(list(checks.use_jsonutils(
81
"./neutron/plugins/openvswitch/agent/xenapi/etc/xapi.d/"
84
def test_assert_called_once_with(self):
87
mock.method(1, 2, 3, test='wow')
88
mock.method.assert_called_once()
92
mock.method(1, 2, 3, test='wow')
93
mock.method.assertCalledOnceWith()
97
mock.method(1, 2, 3, test='wow')
98
mock.method.assert_called_once_with()
101
1, len(list(checks.check_assert_called_once_with(fail_code1,
102
"neutron/tests/test_assert.py"))))
104
1, len(list(checks.check_assert_called_once_with(fail_code2,
105
"neutron/tests/test_assert.py"))))
107
0, len(list(checks.check_assert_called_once_with(pass_code,
108
"neutron/tests/test_assert.py"))))
110
def test_check_oslo_namespace_imports(self):
111
def check(s, fail=True):
112
func = checks.check_oslo_namespace_imports
114
self.assertIsInstance(next(func(s)), tuple)
116
with testtools.ExpectedException(StopIteration):
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')