From 72c501454ea33b603918de35565a66dda8abd66a Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 15 Apr 2015 10:39:16 +0200 Subject: [PATCH] Use unittest.mock on Python 3 The mock module has been added to Python 3.3 as unittest.mock. The third party mock module doesn't seem to be maintained anymore: the last commit was in April 2013. unittest.mock is more recent, has less bugs and is compatible with Python 3.4. There are bugs on Python 3 in the third party mock module, examples: * https://code.google.com/p/mock/issues/detail?id=225 * https://code.google.com/p/mock/issues/detail?id=234 Oslo Messaging hits these issues when running tests on Python 3.4. Import oslotest to setup six.moves for mock. Change-Id: Ic160101695cea67eb9bbbfcaddb8d3dac64e6804 --- oslo_messaging/tests/__init__.py | 3 +++ oslo_messaging/tests/drivers/test_impl_qpid.py | 2 +- oslo_messaging/tests/drivers/test_impl_rabbit.py | 2 +- oslo_messaging/tests/drivers/test_impl_zmq.py | 2 +- oslo_messaging/tests/executors/test_executor.py | 2 +- oslo_messaging/tests/notify/test_dispatcher.py | 2 +- oslo_messaging/tests/notify/test_listener.py | 2 +- oslo_messaging/tests/notify/test_log_handler.py | 3 +-- oslo_messaging/tests/notify/test_logger.py | 2 +- oslo_messaging/tests/notify/test_middleware.py | 2 +- oslo_messaging/tests/notify/test_notifier.py | 2 +- oslo_messaging/tests/rpc/test_dispatcher.py | 2 +- oslo_messaging/tests/rpc/test_server.py | 2 +- oslo_messaging/tests/test_utils.py | 3 +-- test-requirements-py3.txt | 1 - tests/__init__.py | 3 +++ tests/drivers/test_impl_qpid.py | 2 +- tests/drivers/test_impl_rabbit.py | 2 +- tests/drivers/test_impl_zmq.py | 3 ++- tests/notify/test_dispatcher.py | 2 +- tests/notify/test_listener.py | 2 +- tests/notify/test_log_handler.py | 3 +-- tests/notify/test_logger.py | 2 +- tests/notify/test_middleware.py | 2 +- tests/notify/test_notifier.py | 2 +- tests/rpc/test_dispatcher.py | 2 +- tests/rpc/test_server.py | 2 +- tests/test_warning.py | 2 +- 28 files changed, 32 insertions(+), 29 deletions(-) diff --git a/oslo_messaging/tests/__init__.py b/oslo_messaging/tests/__init__.py index 31e79b810..0222c4e76 100644 --- a/oslo_messaging/tests/__init__.py +++ b/oslo_messaging/tests/__init__.py @@ -13,6 +13,9 @@ # License for the specific language governing permissions and limitations # under the License. +# Import oslotest before importing test submodules to setup six.moves for mock +import oslotest + try: import eventlet except ImportError: diff --git a/oslo_messaging/tests/drivers/test_impl_qpid.py b/oslo_messaging/tests/drivers/test_impl_qpid.py index e39f72a8a..2eb0bb244 100644 --- a/oslo_messaging/tests/drivers/test_impl_qpid.py +++ b/oslo_messaging/tests/drivers/test_impl_qpid.py @@ -17,7 +17,6 @@ import random import threading import time -import mock try: import qpid except ImportError: @@ -30,6 +29,7 @@ import oslo_messaging from oslo_messaging._drivers import amqp from oslo_messaging._drivers import impl_qpid as qpid_driver from oslo_messaging.tests import utils as test_utils +from six.moves import mock load_tests = testscenarios.load_tests_apply_scenarios diff --git a/oslo_messaging/tests/drivers/test_impl_rabbit.py b/oslo_messaging/tests/drivers/test_impl_rabbit.py index 5f75ab431..2187d70b8 100644 --- a/oslo_messaging/tests/drivers/test_impl_rabbit.py +++ b/oslo_messaging/tests/drivers/test_impl_rabbit.py @@ -22,7 +22,6 @@ import uuid import fixtures import kombu import kombu.transport.memory -import mock from oslo_config import cfg from oslo_serialization import jsonutils from oslotest import mockpatch @@ -34,6 +33,7 @@ from oslo_messaging._drivers import amqpdriver from oslo_messaging._drivers import common as driver_common from oslo_messaging._drivers import impl_rabbit as rabbit_driver from oslo_messaging.tests import utils as test_utils +from six.moves import mock load_tests = testscenarios.load_tests_apply_scenarios diff --git a/oslo_messaging/tests/drivers/test_impl_zmq.py b/oslo_messaging/tests/drivers/test_impl_zmq.py index ee0f03446..626c35bf5 100644 --- a/oslo_messaging/tests/drivers/test_impl_zmq.py +++ b/oslo_messaging/tests/drivers/test_impl_zmq.py @@ -17,7 +17,6 @@ import logging import socket import fixtures -import mock from oslo_utils import importutils import testtools @@ -29,6 +28,7 @@ except ImportError: import oslo_messaging from oslo_messaging._drivers import common as rpc_common from oslo_messaging.tests import utils as test_utils +from six.moves import mock # eventlet is not yet py3 compatible, so skip if not installed eventlet = importutils.try_import('eventlet') diff --git a/oslo_messaging/tests/executors/test_executor.py b/oslo_messaging/tests/executors/test_executor.py index 4e3ec517f..221f4931a 100644 --- a/oslo_messaging/tests/executors/test_executor.py +++ b/oslo_messaging/tests/executors/test_executor.py @@ -27,7 +27,6 @@ try: import eventlet except ImportError: eventlet = None -import mock import testscenarios import testtools try: @@ -47,6 +46,7 @@ except ImportError: impl_eventlet = None from oslo_messaging._executors import impl_thread from oslo_messaging.tests import utils as test_utils +from six.moves import mock load_tests = testscenarios.load_tests_apply_scenarios diff --git a/oslo_messaging/tests/notify/test_dispatcher.py b/oslo_messaging/tests/notify/test_dispatcher.py index 0449f8af5..ee86491a0 100644 --- a/oslo_messaging/tests/notify/test_dispatcher.py +++ b/oslo_messaging/tests/notify/test_dispatcher.py @@ -15,13 +15,13 @@ import itertools -import mock from oslo_utils import timeutils import testscenarios import oslo_messaging from oslo_messaging.notify import dispatcher as notify_dispatcher from oslo_messaging.tests import utils as test_utils +from six.moves import mock load_tests = testscenarios.load_tests_apply_scenarios diff --git a/oslo_messaging/tests/notify/test_listener.py b/oslo_messaging/tests/notify/test_listener.py index 0bd032c52..e628dec1e 100644 --- a/oslo_messaging/tests/notify/test_listener.py +++ b/oslo_messaging/tests/notify/test_listener.py @@ -16,13 +16,13 @@ import threading import time -import mock from oslo_config import cfg import testscenarios import oslo_messaging from oslo_messaging.notify import dispatcher from oslo_messaging.tests import utils as test_utils +from six.moves import mock load_tests = testscenarios.load_tests_apply_scenarios diff --git a/oslo_messaging/tests/notify/test_log_handler.py b/oslo_messaging/tests/notify/test_log_handler.py index 72ba20418..dfc214aed 100644 --- a/oslo_messaging/tests/notify/test_log_handler.py +++ b/oslo_messaging/tests/notify/test_log_handler.py @@ -12,12 +12,11 @@ import logging -import mock - import oslo_messaging from oslo_messaging.notify import log_handler from oslo_messaging.tests.notify import test_notifier from oslo_messaging.tests import utils as test_utils +from six.moves import mock class PublishErrorsHandlerTestCase(test_utils.BaseTestCase): diff --git a/oslo_messaging/tests/notify/test_logger.py b/oslo_messaging/tests/notify/test_logger.py index 55f8e9100..b6be6d0cf 100644 --- a/oslo_messaging/tests/notify/test_logger.py +++ b/oslo_messaging/tests/notify/test_logger.py @@ -18,7 +18,6 @@ import logging.config import os import sys -import mock from oslo_utils import timeutils import testscenarios import testtools @@ -26,6 +25,7 @@ import testtools import oslo_messaging from oslo_messaging.tests.notify import test_notifier from oslo_messaging.tests import utils as test_utils +from six.moves import mock load_tests = testscenarios.load_tests_apply_scenarios diff --git a/oslo_messaging/tests/notify/test_middleware.py b/oslo_messaging/tests/notify/test_middleware.py index f98e6424c..7d297df76 100644 --- a/oslo_messaging/tests/notify/test_middleware.py +++ b/oslo_messaging/tests/notify/test_middleware.py @@ -15,11 +15,11 @@ import uuid -import mock import webob from oslo_messaging.notify import middleware from oslo_messaging.tests import utils +from six.moves import mock class FakeApp(object): diff --git a/oslo_messaging/tests/notify/test_notifier.py b/oslo_messaging/tests/notify/test_notifier.py index a8733f7d3..946a72334 100644 --- a/oslo_messaging/tests/notify/test_notifier.py +++ b/oslo_messaging/tests/notify/test_notifier.py @@ -19,7 +19,6 @@ import sys import uuid import fixtures -import mock from oslo_serialization import jsonutils from oslo_utils import timeutils from stevedore import dispatch @@ -34,6 +33,7 @@ from oslo_messaging.notify import _impl_test from oslo_messaging.notify import notifier as msg_notifier from oslo_messaging import serializer as msg_serializer from oslo_messaging.tests import utils as test_utils +from six.moves import mock load_tests = testscenarios.load_tests_apply_scenarios diff --git a/oslo_messaging/tests/rpc/test_dispatcher.py b/oslo_messaging/tests/rpc/test_dispatcher.py index 32e0b09c5..03181d1d0 100644 --- a/oslo_messaging/tests/rpc/test_dispatcher.py +++ b/oslo_messaging/tests/rpc/test_dispatcher.py @@ -13,12 +13,12 @@ # License for the specific language governing permissions and limitations # under the License. -import mock import testscenarios import oslo_messaging from oslo_messaging import serializer as msg_serializer from oslo_messaging.tests import utils as test_utils +from six.moves import mock load_tests = testscenarios.load_tests_apply_scenarios diff --git a/oslo_messaging/tests/rpc/test_server.py b/oslo_messaging/tests/rpc/test_server.py index 7b140851e..5c56285dc 100644 --- a/oslo_messaging/tests/rpc/test_server.py +++ b/oslo_messaging/tests/rpc/test_server.py @@ -15,12 +15,12 @@ import threading -import mock from oslo_config import cfg import testscenarios import oslo_messaging from oslo_messaging.tests import utils as test_utils +from six.moves import mock load_tests = testscenarios.load_tests_apply_scenarios diff --git a/oslo_messaging/tests/test_utils.py b/oslo_messaging/tests/test_utils.py index 16ea682b6..22178d8ea 100644 --- a/oslo_messaging/tests/test_utils.py +++ b/oslo_messaging/tests/test_utils.py @@ -13,11 +13,10 @@ # License for the specific language governing permissions and limitations # under the License. -import mock - from oslo_messaging._drivers import common from oslo_messaging import _utils as utils from oslo_messaging.tests import utils as test_utils +from six.moves import mock class VersionIsCompatibleTestCase(test_utils.BaseTestCase): diff --git a/test-requirements-py3.txt b/test-requirements-py3.txt index f1371952c..cf9d3010d 100644 --- a/test-requirements-py3.txt +++ b/test-requirements-py3.txt @@ -7,7 +7,6 @@ hacking>=0.10.0,<0.11 discover fixtures>=0.3.14 -mock>=1.0 mox3>=0.7.0 python-subunit>=0.0.18 testrepository>=0.0.18 diff --git a/tests/__init__.py b/tests/__init__.py index 31e79b810..0222c4e76 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -13,6 +13,9 @@ # License for the specific language governing permissions and limitations # under the License. +# Import oslotest before importing test submodules to setup six.moves for mock +import oslotest + try: import eventlet except ImportError: diff --git a/tests/drivers/test_impl_qpid.py b/tests/drivers/test_impl_qpid.py index 2c2c0a50c..ae4d806dc 100644 --- a/tests/drivers/test_impl_qpid.py +++ b/tests/drivers/test_impl_qpid.py @@ -17,7 +17,6 @@ import random import threading import time -import mock try: import qpid except ImportError: @@ -30,6 +29,7 @@ from oslo import messaging from oslo_messaging._drivers import amqp from oslo_messaging._drivers import impl_qpid as qpid_driver from oslo_messaging.tests import utils as test_utils +from six.moves import mock load_tests = testscenarios.load_tests_apply_scenarios diff --git a/tests/drivers/test_impl_rabbit.py b/tests/drivers/test_impl_rabbit.py index 7b5821218..f534a4ae0 100644 --- a/tests/drivers/test_impl_rabbit.py +++ b/tests/drivers/test_impl_rabbit.py @@ -20,7 +20,6 @@ import uuid import fixtures import kombu -import mock from oslotest import mockpatch import testscenarios @@ -32,6 +31,7 @@ from oslo_messaging._drivers import amqpdriver from oslo_messaging._drivers import common as driver_common from oslo_messaging._drivers import impl_rabbit as rabbit_driver from oslo_messaging.tests import utils as test_utils +from six.moves import mock load_tests = testscenarios.load_tests_apply_scenarios diff --git a/tests/drivers/test_impl_zmq.py b/tests/drivers/test_impl_zmq.py index 26d6433a2..6af1716e3 100644 --- a/tests/drivers/test_impl_zmq.py +++ b/tests/drivers/test_impl_zmq.py @@ -17,9 +17,10 @@ import logging import socket import fixtures -import mock import testtools +from six.moves import mock + try: import zmq except ImportError: diff --git a/tests/notify/test_dispatcher.py b/tests/notify/test_dispatcher.py index adb861008..5c61840a8 100644 --- a/tests/notify/test_dispatcher.py +++ b/tests/notify/test_dispatcher.py @@ -15,13 +15,13 @@ import itertools -import mock from oslo_utils import timeutils import testscenarios from oslo import messaging from oslo.messaging.notify import dispatcher as notify_dispatcher from oslo_messaging.tests import utils as test_utils +from six.moves import mock load_tests = testscenarios.load_tests_apply_scenarios diff --git a/tests/notify/test_listener.py b/tests/notify/test_listener.py index 984d186a0..80c26a62b 100644 --- a/tests/notify/test_listener.py +++ b/tests/notify/test_listener.py @@ -16,13 +16,13 @@ import threading import time -import mock import testscenarios from oslo.config import cfg from oslo import messaging from oslo.messaging.notify import dispatcher from oslo_messaging.tests import utils as test_utils +from six.moves import mock load_tests = testscenarios.load_tests_apply_scenarios diff --git a/tests/notify/test_log_handler.py b/tests/notify/test_log_handler.py index a094e2c16..3adc572e8 100644 --- a/tests/notify/test_log_handler.py +++ b/tests/notify/test_log_handler.py @@ -12,12 +12,11 @@ import logging -import mock - from oslo import messaging from oslo.messaging.notify import log_handler from oslo_messaging.tests.notify import test_notifier from oslo_messaging.tests import utils as test_utils +from six.moves import mock class PublishErrorsHandlerTestCase(test_utils.BaseTestCase): diff --git a/tests/notify/test_logger.py b/tests/notify/test_logger.py index c5d3f8a3f..06ad82012 100644 --- a/tests/notify/test_logger.py +++ b/tests/notify/test_logger.py @@ -18,7 +18,6 @@ import logging.config import os import sys -import mock from oslo_utils import timeutils import testscenarios import testtools @@ -27,6 +26,7 @@ from oslo import messaging import oslo_messaging from oslo_messaging.tests.notify import test_notifier from oslo_messaging.tests import utils as test_utils +from six.moves import mock load_tests = testscenarios.load_tests_apply_scenarios diff --git a/tests/notify/test_middleware.py b/tests/notify/test_middleware.py index 8bdd1676e..ed81cb0c0 100644 --- a/tests/notify/test_middleware.py +++ b/tests/notify/test_middleware.py @@ -15,11 +15,11 @@ import uuid -import mock import webob from oslo.messaging.notify import middleware from oslo_messaging.tests import utils +from six.moves import mock class FakeApp(object): diff --git a/tests/notify/test_notifier.py b/tests/notify/test_notifier.py index b64d2b52a..9cc8ec06b 100644 --- a/tests/notify/test_notifier.py +++ b/tests/notify/test_notifier.py @@ -19,7 +19,6 @@ import sys import uuid import fixtures -import mock from oslo_serialization import jsonutils from oslo_utils import timeutils from stevedore import dispatch @@ -34,6 +33,7 @@ from oslo_messaging.notify import _impl_log from oslo_messaging.notify import _impl_messaging from oslo_messaging.notify import _impl_test from oslo_messaging.tests import utils as test_utils +from six.moves import mock load_tests = testscenarios.load_tests_apply_scenarios diff --git a/tests/rpc/test_dispatcher.py b/tests/rpc/test_dispatcher.py index c3925e6b3..64181f026 100644 --- a/tests/rpc/test_dispatcher.py +++ b/tests/rpc/test_dispatcher.py @@ -13,12 +13,12 @@ # License for the specific language governing permissions and limitations # under the License. -import mock import testscenarios from oslo import messaging from oslo.messaging import serializer as msg_serializer from oslo_messaging.tests import utils as test_utils +from six.moves import mock load_tests = testscenarios.load_tests_apply_scenarios diff --git a/tests/rpc/test_server.py b/tests/rpc/test_server.py index d5f879c49..b429191e2 100644 --- a/tests/rpc/test_server.py +++ b/tests/rpc/test_server.py @@ -15,12 +15,12 @@ import threading -import mock import testscenarios from oslo.config import cfg from oslo import messaging from oslo_messaging.tests import utils as test_utils +from six.moves import mock load_tests = testscenarios.load_tests_apply_scenarios diff --git a/tests/test_warning.py b/tests/test_warning.py index 136f487c4..589ed88d1 100644 --- a/tests/test_warning.py +++ b/tests/test_warning.py @@ -14,9 +14,9 @@ import imp import os import warnings -import mock from oslotest import base as test_base import six +from six.moves import mock class DeprecationWarningTest(test_base.BaseTestCase):