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