From 6cce3a72aea648793bf87650336fe8fe5343cc39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Beraud?= Date: Tue, 9 Jun 2020 10:50:03 +0200 Subject: [PATCH] Use unittest.mock instead of mock The mock third party library was needed for mock support in py2 runtimes. Since we now only support py36 and later, we can use the standard lib unittest.mock module instead. Also added and enabled a hacking check that would have caught this. Change-Id: Idb10f84fd32c50db24f844352cb85de452181439 --- lower-constraints.txt | 1 - octavia/hacking/checks.py | 17 +++++++++++++++++ .../drivers/driver_agent/test_driver_updater.py | 6 +++--- tox.ini | 1 + 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lower-constraints.txt b/lower-constraints.txt index 368820fd48..d083c95c58 100644 --- a/lower-constraints.txt +++ b/lower-constraints.txt @@ -64,7 +64,6 @@ logutils==0.3.5 Mako==1.0.7 MarkupSafe==1.0 mccabe==0.4.0 -mock==2.0.0 monotonic==1.4 mox3==0.25.0 msgpack==0.5.6 diff --git a/octavia/hacking/checks.py b/octavia/hacking/checks.py index 52070bb719..eec747609b 100644 --- a/octavia/hacking/checks.py +++ b/octavia/hacking/checks.py @@ -66,6 +66,8 @@ untranslated_exception_re = re.compile(r"raise (?:\w*)\((.*)\)") no_eventlet_re = re.compile(r'(import|from)\s+[(]?eventlet') no_line_continuation_backslash_re = re.compile(r'.*(\\)\n') no_logging_re = re.compile(r'(import|from)\s+[(]?logging') +import_mock_re = re.compile(r"\bimport[\s]+mock\b") +import_from_mock_re = re.compile(r"\bfrom[\s]+mock[\s]+import\b") def _translation_checks_not_enforced(filename): @@ -258,3 +260,18 @@ def check_no_logging_imports(logical_line): if no_logging_re.match(logical_line): msg = 'O348 Usage of Python logging module not allowed, use oslo_log' yield logical_line.index('logging'), msg + + +@core.flake8ext +def check_no_import_mock(logical_line): + """O349 - Test code must not import mock library. + + :param logical_line: The logical line to check. + :returns: None if the logical line passes the check, otherwise a tuple + is yielded that contains the offending index in logical line + and a message describe the check validation failure. + """ + if (import_mock_re.match(logical_line) or + import_from_mock_re.match(logical_line)): + msg = 'O349 Test code must not import mock library, use unittest.mock' + yield 0, msg diff --git a/octavia/tests/unit/api/drivers/driver_agent/test_driver_updater.py b/octavia/tests/unit/api/drivers/driver_agent/test_driver_updater.py index 46c9a27298..dd453516d5 100644 --- a/octavia/tests/unit/api/drivers/driver_agent/test_driver_updater.py +++ b/octavia/tests/unit/api/drivers/driver_agent/test_driver_updater.py @@ -13,13 +13,13 @@ # under the License. import copy from unittest import mock +from unittest.mock import call -from mock import call +from octavia_lib.api.drivers import exceptions as driver_exceptions +from octavia_lib.common import constants as lib_consts from octavia.api.drivers.driver_agent import driver_updater import octavia.tests.unit.base as base -from octavia_lib.api.drivers import exceptions as driver_exceptions -from octavia_lib.common import constants as lib_consts class TestDriverUpdater(base.TestCase): diff --git a/tox.ini b/tox.ini index e2f81631ad..9eb64feb5c 100644 --- a/tox.ini +++ b/tox.ini @@ -190,6 +190,7 @@ extension = O346 = checks:check_line_continuation_no_backslash O347 = checks:revert_must_have_kwargs O348 = checks:check_no_logging_imports + O349 = checks:check_no_import_mock paths = ./octavia/hacking