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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
# 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 testtools
from neutron_lib.hacking import checks
from neutron_lib.hacking import translation_checks as tc
from neutron_lib.tests import _base as base
class HackingTestCase(base.BaseTestCase):
def assertLinePasses(self, func, *args):
with testtools.ExpectedException(StopIteration):
next(func(*args))
def assertLineFails(self, func, *args):
self.assertIsInstance(next(func(*args)), tuple)
def _get_factory_checks(self, factory):
check_fns = []
def _reg(check_fn):
self.assertTrue(hasattr(check_fn, '__call__'))
self.assertNotIn(check_fn, check_fns)
check_fns.append(check_fn)
factory(_reg)
return check_fns
def test_factory(self):
self.assertGreater(len(self._get_factory_checks(checks.factory)), 0)
def test_use_jsonutils(self):
def __get_msg(fun):
msg = ("N521: jsonutils.%(fun)s must be used instead of "
"json.%(fun)s" % {'fun': fun})
return [(0, msg)]
for method in ('dump', 'dumps', 'load', 'loads'):
self.assertEqual(
__get_msg(method),
list(checks.use_jsonutils("json.%s(" % method,
"./neutron/common/rpc.py")))
self.assertEqual(
0,
len(list(checks.use_jsonutils("jsonx.%s(" % method,
"./neutron/common/rpc.py"))))
self.assertEqual(
0,
len(list(checks.use_jsonutils("json.%sx(" % method,
"./neutron/common/rpc.py"))))
self.assertEqual(
0,
len(list(checks.use_jsonutils(
"json.%s" % method,
"./neutron/plugins/ml2/drivers/openvswitch/agent/xenapi/"
"etc/xapi.d/plugins/netwrap"))))
def test_check_contextlib_nested(self):
f = checks.check_no_contextlib_nested
self.assertLineFails(f, 'with contextlib.nested():', '')
self.assertLineFails(f, ' with contextlib.nested():', '')
self.assertLinePasses(f, '# with contextlib.nested():', '')
self.assertLinePasses(f, 'print("with contextlib.nested():")', '')
def test_no_mutable_default_args(self):
self.assertEqual(1, len(list(checks.no_mutable_default_args(
" def fake_suds_context(calls={}):"))))
self.assertEqual(1, len(list(checks.no_mutable_default_args(
"def get_info_from_bdm(virt_type, bdm, mapping=[])"))))
self.assertEqual(0, len(list(checks.no_mutable_default_args(
"defined = []"))))
self.assertEqual(0, len(list(checks.no_mutable_default_args(
"defined, undefined = [], {}"))))
def test_check_neutron_namespace_imports(self):
f = checks.check_neutron_namespace_imports
self.assertLinePasses(f, 'from neutron_lib import constants')
self.assertLinePasses(f, 'import neutron_lib.constants')
self.assertLineFails(f, 'from neutron.common import rpc')
self.assertLineFails(f, 'from neutron import context')
self.assertLineFails(f, 'import neutron.common.config')
def test_no_log_translations(self):
for log in tc._all_log_levels:
for hint in tc._all_hints:
bad = 'LOG.{}({}("Bad"))'.format(log, hint)
self.assertEqual(
1, len(list(tc.no_translate_logs(bad, 'f'))))
# Catch abuses when used with a variable and not a literal
bad = 'LOG.{}({}(msg))'.format(log, hint)
self.assertEqual(
1, len(list(tc.no_translate_logs(bad, 'f'))))
# Do not do validations in tests
ok = 'LOG.%s(_("OK - unit tests"))' % log
self.assertEqual(
0, len(list(tc.no_translate_logs(ok, 'f/tests/f'))))
def test_check_log_warn_deprecated(self):
bad = "LOG.warn('i am deprecated!')"
good = "LOG.warning('zlatan is the best')"
f = tc.check_log_warn_deprecated
self.assertLineFails(f, bad, '')
self.assertLinePasses(f, good, '')
def test_check_localized_exception_messages(self):
f = tc.check_raised_localized_exceptions
self.assertLineFails(f, " raise KeyError('Error text')", '')
self.assertLineFails(f, ' raise KeyError("Error text")', '')
self.assertLinePasses(f, ' raise KeyError(_("Error text"))', '')
self.assertLinePasses(f, ' raise KeyError(_ERR("Error text"))', '')
self.assertLinePasses(f, " raise KeyError(translated_msg)", '')
self.assertLinePasses(f, '# raise KeyError("Not translated")', '')
self.assertLinePasses(f, 'print("raise KeyError("Not '
'translated")")', '')
def test_check_localized_exception_message_skip_tests(self):
f = tc.check_raised_localized_exceptions
self.assertLinePasses(f, "raise KeyError('Error text')",
'neutron_lib/tests/unit/mytest.py')
def test_check_eventlet_imports(self):
f = checks.check_no_eventlet_imports
self.assertLineFails(f, "import eventlet")
self.assertLineFails(f, "import eventlet.timeout")
self.assertLineFails(f, "from eventlet import timeout")
self.assertLineFails(f, "from eventlet.timeout import Timeout")
self.assertLineFails(f, "from eventlet.timeout import (Timeout, X)")
self.assertLinePasses(f, "import is.not.eventlet")
self.assertLinePasses(f, "from is.not.eventlet")
self.assertLinePasses(f, "from mymod import eventlet")
self.assertLinePasses(f, "from mymod.eventlet import amod")
self.assertLinePasses(f, 'print("eventlet not here")')
self.assertLinePasses(f, 'print("eventlet.timeout")')
self.assertLinePasses(f, "from mymod.timeout import (eventlet, X)")
def test_assert_equal_none(self):
self.assertEqual(len(list(checks.assert_equal_none(
"self.assertEqual(A, None)"))), 1)
self.assertEqual(len(list(checks.assert_equal_none(
"self.assertEqual(A, None) # Comment"))), 1)
self.assertEqual(len(list(checks.assert_equal_none(
"self.assertEqual(None, A)"))), 1)
self.assertEqual(len(list(checks.assert_equal_none(
"self.assertEqual(None, A) # Comment"))), 1)
self.assertEqual(len(list(checks.assert_equal_none(
"self.assertEqual((None, None), A)"))), 0)
self.assertEqual(len(list(checks.assert_equal_none(
"self.assertEqual((None, None), A) # Comment"))), 0)
self.assertEqual(len(list(checks.assert_equal_none(
"self.assertEqual(A, (None, None))"))), 0)
self.assertEqual(len(list(checks.assert_equal_none(
"self.assertEqual(A, (None, None)) # Comment"))), 0)
self.assertEqual(len(list(checks.assert_equal_none(
"assertIsNot(A, None)"))), 1)
self.assertEqual(len(list(checks.assert_equal_none(
"assertIsNot(A, None) # Comment"))), 1)
self.assertEqual(len(list(checks.assert_equal_none(
"assertIsNot(None, A)"))), 1)
self.assertEqual(len(list(checks.assert_equal_none(
"assertIsNot(None, A) # Comment"))), 1)
self.assertEqual(len(list(checks.assert_equal_none(
"assertIsNot((None, None), A)"))), 0)
self.assertEqual(len(list(checks.assert_equal_none(
"assertIsNot((None, None), A) # Comment"))), 0)
self.assertEqual(len(list(checks.assert_equal_none(
"assertIsNot(A, (None, None))"))), 0)
self.assertEqual(len(list(checks.assert_equal_none(
"assertIsNot(A, (None, None)) # Comment"))), 0)
self.assertEqual(
len(list(checks.assert_equal_none("self.assertIsNone(A)"))), 0)
self.assertEqual(
len(list(checks.assert_equal_none("self.assertIsNotNone(A)"))), 0)
|