Update hacking for Python3

The repo is Python 3, so update hacking to version 3.0 which
supports Python 3.

Update local hacking checks for new flake8.

Change-Id: I37851c466ac14f41506f3d0d7cee75db84580756
This commit is contained in:
Brian Haley 2020-03-30 17:07:30 -04:00
parent 6e7f454ccd
commit c78aa81e7d
6 changed files with 41 additions and 93 deletions

View File

@ -12,31 +12,24 @@
# License for the specific language governing permissions and limitations
# under the License.
"""
Guidelines for writing new hacking checks
- Use only for OVN Octavia provider specific tests. OpenStack
general tests should be submitted to the common 'hacking' module.
- Pick numbers in the range N3xx. Find the current test with
the highest allocated number and then pick the next value.
- Keep the test method code in the source file ordered based
on the N3xx value.
- List the new rule in the top level HACKING.rst file
- Add test cases for each new rule to
ovn_octavia_provider/tests/unit/hacking/test_checks.py
"""
import re
def flake8ext(f):
"""Decorator to indicate flake8 extension.
This is borrowed from hacking.core.flake8ext(), but at now it is used
only for unit tests to know which are ovn_octavia_provider flake8
extensions.
"""
f.name = __name__
return f
# Guidelines for writing new hacking checks
#
# - Use only for OVN Octavia provider specific tests. OpenStack
# general tests should be submitted to the common 'hacking' module.
# - Pick numbers in the range N3xx. Find the current test with
# the highest allocated number and then pick the next value.
# - Keep the test method code in the source file ordered based
# on the N3xx value.
# - List the new rule in the top level HACKING.rst file
# - Add test cases for each new rule to
# ovn_octavia_provider/tests/unit/hacking/test_checks.py
from hacking import core
unittest_imports_dot = re.compile(r"\bimport[\s]+unittest\b")
@ -50,7 +43,7 @@ tests_imports_from2 = re.compile(
no_line_continuation_backslash_re = re.compile(r'.*(\\)\n')
@flake8ext
@core.flake8ext
def check_assert_called_once_with(logical_line, filename):
"""Try to detect unintended calls of nonexistent mock methods like:
@ -79,7 +72,7 @@ def check_assert_called_once_with(logical_line, filename):
yield (0, msg)
@flake8ext
@core.flake8ext
def check_asserttruefalse(logical_line, filename):
"""N328 - Don't use assertEqual(True/False, observed)."""
@ -102,7 +95,7 @@ def check_asserttruefalse(logical_line, filename):
yield (0, msg)
@flake8ext
@core.flake8ext
def check_assertempty(logical_line, filename):
"""Enforce using assertEqual parameter ordering in case of empty objects.
@ -119,7 +112,7 @@ def check_assertempty(logical_line, filename):
yield (0, msg)
@flake8ext
@core.flake8ext
def check_assertisinstance(logical_line, filename):
"""N331 - Enforce using assertIsInstance."""
@ -131,7 +124,7 @@ def check_assertisinstance(logical_line, filename):
yield (0, msg)
@flake8ext
@core.flake8ext
def check_assertequal_for_httpcode(logical_line, filename):
"""N332 - Enforce correct oredering for httpcode in assertEqual."""
@ -143,7 +136,7 @@ def check_assertequal_for_httpcode(logical_line, filename):
yield (0, msg)
@flake8ext
@core.flake8ext
def check_no_imports_from_tests(logical_line, filename):
"""N343 - Production code must not import from ovn_octavia_provider.tests.*
@ -160,7 +153,7 @@ def check_no_imports_from_tests(logical_line, filename):
yield(0, msg)
@flake8ext
@core.flake8ext
def check_python3_no_filter(logical_line):
"""N344 - Use list comprehension instead of filter(lambda)."""
@ -169,35 +162,3 @@ def check_python3_no_filter(logical_line):
if filter_match.match(logical_line):
yield(0, msg)
def check_line_continuation_no_backslash(logical_line, tokens):
"""N346 - Don't use backslashes for line continuation.
:param logical_line: The logical line to check. Not actually used.
:param tokens: List of tokens to check.
:returns: None if the tokens don't contain any issues, otherwise a tuple
is yielded that contains the offending index in the logical
line and a message describe the check validation failure.
"""
backslash = None
for token_type, text, start, end, orig_line in tokens:
m = no_line_continuation_backslash_re.match(orig_line)
if m:
backslash = (start[0], m.start(1))
break
if backslash is not None:
msg = 'N346: Backslash line continuations not allowed'
yield backslash, msg
def factory(register):
register(check_assert_called_once_with)
register(check_asserttruefalse)
register(check_assertempty)
register(check_assertisinstance)
register(check_assertequal_for_httpcode)
register(check_no_imports_from_tests)
register(check_python3_no_filter)
register(check_line_continuation_no_backslash)

View File

@ -731,11 +731,9 @@ class TestOctaviaOvnProviderDriver(
'listeners': []
}
if getattr(member, 'admin_state_up', None):
expected_status['members'][0]['operating_status'] = \
"ONLINE"
expected_status['members'][0]['operating_status'] = "ONLINE"
else:
expected_status['members'][0]['operating_status'] = \
"OFFLINE"
expected_status['members'][0]['operating_status'] = "OFFLINE"
self._wait_for_status_and_validate(lb_data, [expected_status])
def _update_members_in_batch_and_validate(self, lb_data, pool_id,

View File

@ -221,24 +221,3 @@ class HackingTestCase(base.BaseTestCase):
self.assertLinePasses(f, "[obj for obj in data if test(obj)]")
self.assertLinePasses(f, "filter(function, range(0,10))")
self.assertLinePasses(f, "lambda x, y: x+y")
def test_line_continuation_no_backslash(self):
results = list(checks.check_line_continuation_no_backslash(
'', [(1, 'import', (2, 0), (2, 6), 'import \\\n'),
(1, 'os', (3, 4), (3, 6), ' os\n')]))
self.assertEqual(1, len(results))
self.assertEqual((2, 7), results[0][0])
def _get_factory_checks(self, factory):
check_fns = []
def _reg(check_fn):
self.assertTrue(hasattr(check_fn, '__call__'))
self.assertFalse(check_fn in 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)

View File

@ -826,7 +826,7 @@ class TestOvnProviderHelper(TestOvnOctaviaBase):
self.assertEqual(f(status), expected)
self.assertEqual(f(expected), expected)
status = {}
self.assertEqual(f(status), {})
self.assertFalse(f(status))
def test__find_ovn_lbs(self):
self.mock_find_ovn_lbs.stop()

View File

@ -2,7 +2,7 @@
# of appearance. Changing the order has an impact on the overall integration
# process, which may cause wedges in the gate later.
hacking>=1.1.0 # Apache-2.0
hacking>=3.0.0,<3.1.0 # Apache-2.0
bandit!=1.6.0,>=1.1.0 # Apache-2.0
coverage!=4.4,>=4.0 # Apache-2.0

18
tox.ini
View File

@ -110,14 +110,24 @@ show-source = True
exclude=./.*,dist,doc,*egg*,build,releasenotes
import-order-style = pep8
[hacking]
import_exceptions = ovn_octavia_provider.i18n
[flake8:local-plugins]
extension =
N322 = checks:check_assert_called_once_with
N328 = checks:check_asserttruefalse
N330 = checks:check_assertempty
N331 = checks:check_assertisinstance
N332 = checks:check_assertequal_for_httpcode
N343 = checks:check_no_imports_from_tests
N344 = checks:check_python3_no_filter
paths =./ovn_octavia_provider/hacking
[testenv:genconfig]
envdir = {toxworkdir}/shared
commands = {toxinidir}/tools/generate_config_file_samples.sh
[hacking]
import_exceptions = ovn_octavia_provider.i18n
local-check-factory = neutron_lib.hacking.checks.factory
[testenv:lower-constraints]
deps =
-c{toxinidir}/lower-constraints.txt