Browse Source

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
changes/49/716049/7
Brian Haley 2 years ago
parent
commit
8126f88894
  1. 23
      neutron/hacking/checks.py
  2. 2
      neutron/tests/base.py
  3. 2
      neutron/tests/common/agents/l3_agent.py
  4. 2
      neutron/tests/fullstack/agents/ovs_agent.py
  5. 2
      neutron/tests/functional/agent/common/test_ovs_lib.py
  6. 2
      neutron/tests/functional/agent/l2/base.py
  7. 2
      neutron/tests/functional/agent/l2/extensions/test_ovs_agent_qos_extension.py
  8. 3
      neutron/tests/functional/agent/l3/extensions/qos/test_fip_qos_extension.py
  9. 3
      neutron/tests/functional/agent/l3/extensions/test_conntrack_helper_extension.py
  10. 3
      neutron/tests/functional/agent/l3/extensions/test_gateway_ip_qos_extension.py
  11. 2
      neutron/tests/functional/agent/l3/extensions/test_port_forwarding_extension.py
  12. 2
      neutron/tests/functional/agent/l3/framework.py
  13. 2
      neutron/tests/functional/agent/l3/test_dvr_router.py
  14. 2
      neutron/tests/functional/agent/l3/test_ha_router.py
  15. 2
      neutron/tests/functional/agent/l3/test_keepalived_state_change.py
  16. 2
      neutron/tests/functional/agent/l3/test_legacy_router.py
  17. 3
      neutron/tests/functional/agent/l3/test_namespace_manager.py
  18. 3
      neutron/tests/functional/agent/linux/test_dhcp.py
  19. 3
      neutron/tests/functional/agent/linux/test_l3_tc_lib.py
  20. 2
      neutron/tests/functional/agent/linux/test_tc_lib.py
  21. 3
      neutron/tests/functional/agent/ovn/metadata/test_metadata_agent.py
  22. 2
      neutron/tests/functional/agent/test_dhcp_agent.py
  23. 3
      neutron/tests/functional/agent/test_l2_lb_agent.py
  24. 2
      neutron/tests/functional/agent/test_l2_ovs_agent.py
  25. 2
      neutron/tests/functional/agent/test_ovs_lib.py
  26. 2
      neutron/tests/functional/base.py
  27. 3
      neutron/tests/functional/cmd/test_linuxbridge_cleanup.py
  28. 2
      neutron/tests/functional/cmd/test_netns_cleanup.py
  29. 3
      neutron/tests/functional/db/test_network.py
  30. 3
      neutron/tests/functional/pecan_wsgi/test_controllers.py
  31. 2
      neutron/tests/functional/pecan_wsgi/test_functional.py
  32. 3
      neutron/tests/functional/pecan_wsgi/test_hooks.py
  33. 3
      neutron/tests/functional/plugins/ml2/drivers/ovn/mech_driver/ovsdb/extensions/test_qos.py
  34. 3
      neutron/tests/functional/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_maintenance.py
  35. 3
      neutron/tests/functional/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_ovn_db_resources.py
  36. 2
      neutron/tests/functional/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_ovn_db_sync.py
  37. 3
      neutron/tests/functional/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_ovsdb_monitor.py
  38. 2
      neutron/tests/functional/plugins/ml2/drivers/ovn/mech_driver/test_mech_driver.py
  39. 2
      neutron/tests/functional/services/conntrack_helper/test_conntrack_helper.py
  40. 3
      neutron/tests/functional/services/l3_router/test_l3_dvr_ha_router_plugin.py
  41. 3
      neutron/tests/functional/services/l3_router/test_l3_dvr_router_plugin.py
  42. 2
      neutron/tests/functional/services/logapi/test_logging.py
  43. 2
      neutron/tests/functional/services/ovn_l3/test_plugin.py
  44. 3
      neutron/tests/functional/services/portforwarding/test_port_forwarding.py
  45. 2
      neutron/tests/functional/services/trunk/drivers/openvswitch/agent/test_ovsdb_handler.py
  46. 2
      neutron/tests/functional/services/trunk/drivers/openvswitch/agent/test_trunk_manager.py
  47. 2
      neutron/tests/functional/services/trunk/rpc/test_server.py
  48. 2
      neutron/tests/functional/test_server.py
  49. 3
      neutron/tests/functional/tests/common/test_net_helpers.py
  50. 18
      neutron/tests/unit/hacking/test_checks.py
  51. 1
      test-requirements.txt

23
neutron/hacking/checks.py

@ -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)

2
neutron/tests/base.py

@ -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

2
neutron/tests/common/agents/l3_agent.py

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

2
neutron/tests/fullstack/agents/ovs_agent.py

@ -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

2
neutron/tests/functional/agent/common/test_ovs_lib.py

@ -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

2
neutron/tests/functional/agent/l2/base.py

@ -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

2
neutron/tests/functional/agent/l2/extensions/test_ovs_agent_qos_extension.py

@ -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

3
neutron/tests/functional/agent/l3/extensions/qos/test_fip_qos_extension.py

@ -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

3
neutron/tests/functional/agent/l3/extensions/test_conntrack_helper_extension.py

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

3
neutron/tests/functional/agent/l3/extensions/test_gateway_ip_qos_extension.py

@ -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

2
neutron/tests/functional/agent/l3/extensions/test_port_forwarding_extension.py

@ -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

2
neutron/tests/functional/agent/l3/framework.py

@ -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

2
neutron/tests/functional/agent/l3/test_dvr_router.py

@ -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

2
neutron/tests/functional/agent/l3/test_ha_router.py

@ -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

2
neutron/tests/functional/agent/l3/test_keepalived_state_change.py

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

2
neutron/tests/functional/agent/l3/test_legacy_router.py

@ -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

3
neutron/tests/functional/agent/l3/test_namespace_manager.py

@ -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

3
neutron/tests/functional/agent/linux/test_dhcp.py

@ -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

3
neutron/tests/functional/agent/linux/test_l3_tc_lib.py

@ -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

2
neutron/tests/functional/agent/linux/test_tc_lib.py

@ -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

3
neutron/tests/functional/agent/ovn/metadata/test_metadata_agent.py

@ -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

2
neutron/tests/functional/agent/test_dhcp_agent.py

@ -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

3
neutron/tests/functional/agent/test_l2_lb_agent.py

@ -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

2
neutron/tests/functional/agent/test_l2_ovs_agent.py

@ -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

2
neutron/tests/functional/agent/test_ovs_lib.py

@ -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

2
neutron/tests/functional/base.py

@ -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

3
neutron/tests/functional/cmd/test_linuxbridge_cleanup.py

@ -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

2
neutron/tests/functional/cmd/test_netns_cleanup.py

@ -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

3
neutron/tests/functional/db/test_network.py

@ -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

3
neutron/tests/functional/pecan_wsgi/test_controllers.py

@ -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

2
neutron/tests/functional/pecan_wsgi/test_functional.py

@ -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

3
neutron/tests/functional/pecan_wsgi/test_hooks.py

@ -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

3
neutron/tests/functional/plugins/ml2/drivers/ovn/mech_driver/ovsdb/extensions/test_qos.py

@ -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

3
neutron/tests/functional/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_maintenance.py

@ -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

3
neutron/tests/functional/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_ovn_db_resources.py

@ -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

2
neutron/tests/functional/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_ovn_db_sync.py

@ -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

3
neutron/tests/functional/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_ovsdb_monitor.py

@ -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

2
neutron/tests/functional/plugins/ml2/drivers/ovn/mech_driver/test_mech_driver.py

@ -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

2
neutron/tests/functional/services/conntrack_helper/test_conntrack_helper.py

@ -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

3
neutron/tests/functional/services/l3_router/test_l3_dvr_ha_router_plugin.py

@ -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

3
neutron/tests/functional/services/l3_router/test_l3_dvr_router_plugin.py

@ -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

2
neutron/tests/functional/services/logapi/test_logging.py

@ -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

2
neutron/tests/functional/services/ovn_l3/test_plugin.py

@ -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

3
neutron/tests/functional/services/portforwarding/test_port_forwarding.py

@ -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

2
neutron/tests/functional/services/trunk/drivers/openvswitch/agent/test_ovsdb_handler.py

@ -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

2
neutron/tests/functional/services/trunk/drivers/openvswitch/agent/test_trunk_manager.py

@ -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

2
neutron/tests/functional/services/trunk/rpc/test_server.py

@ -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

2
neutron/tests/functional/test_server.py

@ -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

3
neutron/tests/functional/tests/common/test_net_helpers.py

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

18
neutron/tests/unit/hacking/test_checks.py

@ -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

1
test-requirements.txt

@ -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

Loading…
Cancel
Save