Complete removal of dependency on the "mock" package

Now that we are python3 only, we should move to using the built
in version of mock that supports all of our testing needs and
remove the dependency on the "mock" package.

This completes removal of all references to "import mock",
changing to "from unittest import mock" in fullstack and
functional tests.

Added a hacking check to enforce it in future patches.

Change-Id: Ifcaf1c21bea0ec3c35278e49cecc90a101a82113
This commit is contained in:
Brian Haley 2020-03-30 13:49:50 -04:00
parent 7594bb0627
commit 8126f88894
51 changed files with 104 additions and 54 deletions

View File

@ -47,6 +47,8 @@ tests_imports_dot = re.compile(r"\bimport[\s]+neutron.tests\b")
tests_imports_from1 = re.compile(r"\bfrom[\s]+neutron.tests\b")
tests_imports_from2 = re.compile(r"\bfrom[\s]+neutron[\s]+import[\s]+tests\b")
import_mock = re.compile(r"\bimport[\s]+mock\b")
@flake8ext
def check_assert_called_once_with(logical_line, filename):
@ -202,9 +204,9 @@ def check_builtins_gettext(logical_line, tokens, filename, lines, noqa):
@flake8ext
def check_no_imports_from_tests(logical_line, filename, noqa):
"""N343 Production code must not import from neutron.tests.*
"""N343 - Production code must not import from neutron.tests.*
"""
msg = ("N343 Production code must not import from neutron.tests.*")
msg = ("N343: Production code must not import from neutron.tests.*")
if noqa:
return
@ -246,6 +248,22 @@ def check_no_sqlalchemy_event_import(logical_line, filename, noqa):
"between unit tests")
@flake8ext
def check_no_import_mock(logical_line, filename, noqa):
"""N347 - Test code must not import mock library
"""
msg = ("N347: Test code must not import mock library")
if noqa:
return
if 'neutron/tests/' not in filename:
return
if re.match(import_mock, logical_line):
yield(0, msg)
def factory(register):
checks.factory(register)
register(check_assert_called_once_with)
@ -258,3 +276,4 @@ def factory(register):
register(check_no_imports_from_tests)
register(check_python3_no_filter)
register(check_no_sqlalchemy_event_import)
register(check_no_import_mock)

View File

@ -24,10 +24,10 @@ import logging
import os
import os.path
import threading
from unittest import mock
import eventlet.timeout
import fixtures
import mock
from neutron_lib.callbacks import manager as registry_manager
from neutron_lib.db import api as db_api
from neutron_lib import fixture

View File

@ -14,8 +14,8 @@
import sys
import types
from unittest import mock
import mock
from neutron_lib import constants
from oslo_config import cfg

View File

@ -14,8 +14,8 @@
# under the License.
import sys
from unittest import mock
import mock
from oslo_config import cfg
from neutron.agent.common import ovs_lib

View File

@ -14,8 +14,8 @@
# under the License.
import functools
from unittest import mock
import mock
from neutron_lib.services.qos import constants as qos_constants
from oslo_utils import uuidutils
import six

View File

@ -15,10 +15,10 @@
# under the License.
import random
from unittest import mock
import eventlet
import fixtures
import mock
from neutron_lib import constants as n_const
from neutron_lib.utils import net
from oslo_config import cfg

View File

@ -14,8 +14,8 @@
# under the License.
import copy
from unittest import mock
import mock
from neutron_lib import constants
from oslo_utils import uuidutils
import testscenarios

View File

@ -13,7 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from neutron_lib import constants
from neutron_lib import exceptions
from oslo_utils import uuidutils

View File

@ -15,8 +15,7 @@
import collections
import mock
from unittest import mock
from neutron_lib import constants
from oslo_utils import uuidutils

View File

@ -13,7 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from neutron_lib import constants
from neutron_lib import exceptions
from oslo_utils import uuidutils

View File

@ -15,11 +15,11 @@
import os
import re
from unittest import mock
from neutron_lib import constants
from oslo_utils import uuidutils
import mock
from neutron.agent.l3 import agent as neutron_l3_agent
from neutron.agent.l3.extensions import port_forwarding as pf
from neutron.agent.linux import ip_lib

View File

@ -15,8 +15,8 @@
import copy
import functools
from unittest import mock
import mock
import netaddr
from neutron_lib import constants
from oslo_config import cfg

View File

@ -15,8 +15,8 @@
import copy
import functools
from unittest import mock
import mock
import netaddr
from neutron_lib.api.definitions import portbindings
from neutron_lib import constants as lib_constants

View File

@ -14,8 +14,8 @@
# under the License.
import copy
from unittest import mock
import mock
from neutron_lib import constants
from oslo_utils import netutils
import testtools

View File

@ -14,9 +14,9 @@
import functools
import os
from unittest import mock
import eventlet
import mock
import netaddr
from oslo_utils import uuidutils

View File

@ -14,8 +14,8 @@
# under the License.
import copy
from unittest import mock
import mock
from neutron_lib.callbacks import events
from neutron_lib.callbacks import registry
from neutron_lib.callbacks import resources

View File

@ -13,7 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from oslo_config import cfg
from oslo_utils import uuidutils

View File

@ -12,7 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from neutron_lib import constants
from oslo_config import cfg

View File

@ -10,7 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from neutron_lib import constants as common_constants
from neutron_lib import exceptions
from oslo_utils import uuidutils

View File

@ -14,8 +14,8 @@
# under the License.
import random
from unittest import mock
import mock
import netaddr
from neutron_lib.services.qos import constants as qos_consts
from oslo_utils import uuidutils

View File

@ -13,7 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from oslo_config import fixture as fixture_config
from oslo_utils import uuidutils
from ovsdbapp.backend.ovs_idl import event

View File

@ -15,10 +15,10 @@
import copy
import os.path
from unittest import mock
import eventlet
import fixtures
import mock
import netaddr
from neutron_lib import constants as lib_const
from oslo_config import fixture as fixture_config

View File

@ -12,7 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from oslo_config import cfg
from oslo_utils import uuidutils
import testtools

View File

@ -15,8 +15,8 @@
# under the License.
import time
from unittest import mock
import mock
from neutron_lib.callbacks import events
from neutron_lib.callbacks import registry
from neutron_lib.callbacks import resources

View File

@ -14,9 +14,9 @@
# under the License.
import collections
from unittest import mock
import uuid
import mock
from neutron_lib import constants as const
from oslo_config import cfg
from ovsdbapp.backend.ovs_idl import idlutils

View File

@ -17,10 +17,10 @@ from datetime import datetime
import errno
import os
import shutil
from unittest import mock
import warnings
import fixtures
import mock
from neutron_lib import fixture
from neutron_lib.plugins import constants
from neutron_lib.plugins import directory

View File

@ -13,8 +13,9 @@
# License for the specific language governing permissions and limitations
# under the License.
from unittest import mock
import fixtures
import mock
from neutron_lib import constants
from neutron.agent.linux import ip_lib

View File

@ -15,9 +15,9 @@
import os
import sys
from unittest import mock
import eventlet
import mock
from neutron_lib import constants as n_const
from neutron.agent.l3 import namespaces

View File

@ -10,7 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from neutron_lib.api.definitions import external_net as extnet_apidef
from neutron_lib import constants
from neutron_lib import context

View File

@ -10,7 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from neutron_lib import constants as n_const
from neutron_lib import context
from neutron_lib.plugins import constants as plugin_constants

View File

@ -14,8 +14,8 @@
# under the License.
import os
from unittest import mock
import mock
from neutron_lib import context
from neutron_lib import exceptions as n_exc
from neutron_lib.plugins import constants

View File

@ -13,7 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from neutron_lib.api import attributes
from neutron_lib.callbacks import events
from neutron_lib import context

View File

@ -12,7 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from neutron_lib import constants
from neutron_lib.services.qos import constants as qos_constants

View File

@ -13,7 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from oslo_config import cfg
from futurist import periodics

View File

@ -12,8 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
import netaddr
from neutron_lib.api.definitions import dns as dns_apidef

View File

@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from neutron.common.ovn import acl as acl_utils
from neutron.common.ovn import constants as ovn_const

View File

@ -12,7 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from oslo_utils import uuidutils
from neutron.common.ovn import constants as ovn_const

View File

@ -13,8 +13,8 @@
# under the License.
import functools
from unittest import mock
import mock
from neutron_lib.api.definitions import portbindings
from oslo_config import cfg
from oslo_utils import uuidutils

View File

@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from neutron_lib.api.definitions import l3_conntrack_helper as apidef
from neutron_lib import exceptions as lib_exc

View File

@ -12,7 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from neutron_lib.agent import topics
from neutron_lib.api.definitions import external_net as extnet_apidef
from neutron_lib.api.definitions import l3 as l3_apidef

View File

@ -12,7 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from neutron_lib.agent import topics
from neutron_lib.api.definitions import external_net as extnet_apidef
from neutron_lib.api.definitions import l3 as l3_apidef

View File

@ -14,8 +14,8 @@
# under the License.
import re
from unittest import mock
import mock
from neutron_lib import constants
from neutron_lib import context as neutron_context
from oslo_config import cfg

View File

@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from neutron.common.ovn import constants as ovn_const
from neutron.common.ovn import utils as ovn_utils

View File

@ -10,7 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from neutron_lib.api.definitions import fip_pf_description as ext_apidef
from neutron_lib.api.definitions import floating_ip_port_forwarding as apidef
from neutron_lib.callbacks import exceptions as c_exc

View File

@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from neutron_lib import constants as n_consts
from neutron_lib.utils import helpers

View File

@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from neutron_lib.utils import net
from oslo_log import log as logging

View File

@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
from neutron_lib.services.trunk import constants

View File

@ -18,9 +18,9 @@ import signal
import socket
import time
import traceback
from unittest import mock
import httplib2
import mock
from neutron_lib import worker as neutron_worker
from oslo_config import cfg
import psutil

View File

@ -13,8 +13,7 @@
# under the License.
import signal
import mock
from unittest import mock
from neutron_lib import exceptions as n_exc

View File

@ -208,6 +208,24 @@ class HackingTestCase(base.BaseTestCase):
self.assertLinePasses(f, "filter(function, range(0,10))")
self.assertLinePasses(f, "lambda x, y: x+y")
def test_check_no_import_mock(self):
pass_line = 'from unittest import mock'
fail_lines = ('import mock',
'import mock as mock_lib')
self.assertEqual(
0, len(list(
checks.check_no_import_mock(
pass_line, "neutron/tests/test_fake.py", None))))
for fail_line in fail_lines:
self.assertEqual(
0, len(list(
checks.check_no_import_mock(
fail_line, "neutron/common/utils.py", None))))
self.assertEqual(
1, len(list(
checks.check_no_import_mock(
fail_line, "neutron/tests/test_fake.py", None))))
# The following is borrowed from hacking/tests/test_doctest.py.
# Tests defined in docstring is easier to understand

View File

@ -7,7 +7,6 @@ coverage!=4.4,>=4.0 # Apache-2.0
fixtures>=3.0.0 # Apache-2.0/BSD
flake8-import-order==0.12 # LGPLv3
pycodestyle>=2.0.0 # MIT
mock>=3.0.0 # BSD
python-subunit>=1.0.0 # Apache-2.0/BSD
testtools>=2.2.0 # MIT
testresources>=2.0.0 # Apache-2.0/BSD