Test: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.
This is follow up of 72c501454e and
add hacking rule to avoid regression issue.

oslotest prepares mock for six in oslotest/__init__.py as follow:
six.add_move(six.MovedModule('mock', 'mock', 'unittest.mock')) and
oslo_messaging/tests/__init__.py imports oslotest before importing
test submodules to setup six.moves for mock, then "from six.moves
import mock" works well.

Note: unittest.mock also detects wrong usage of method
assert_called_once_with.

Change-Id: I3d09733789cfa2550cf47c24f2553357d36bcc0d
This commit is contained in:
ChangBo Guo(gcb) 2017-02-04 15:30:23 +08:00
parent f3cc165dba
commit 4f229832c0
13 changed files with 51 additions and 37 deletions

View File

@ -20,6 +20,8 @@ import six
oslo_namespace_imports_dot = re.compile(r"import[\s]+oslo[.][^\s]+")
oslo_namespace_imports_from_dot = re.compile(r"from[\s]+oslo[.]")
oslo_namespace_imports_from_root = re.compile(r"from[\s]+oslo[\s]+import[\s]+")
mock_imports_directly = re.compile(r"import[\s]+mock")
mock_imports_direclty_from = re.compile(r"from[\s]+mock[\s]+import[\s]+")
def check_oslo_namespace_imports(logical_line):
@ -40,6 +42,17 @@ def check_oslo_namespace_imports(logical_line):
yield(0, msg)
def check_mock_imports(logical_line):
if re.match(mock_imports_directly, logical_line):
msg = ("O324: '%s' must be used instead of '%s'.") % (
logical_line.replace('import mock', 'from six.moves import mock'),
logical_line)
yield(0, msg)
elif re.match(mock_imports_direclty_from, logical_line):
msg = "O324: Use mock from six.moves."
yield(0, msg)
class BaseASTChecker(ast.NodeVisitor):
"""Provides a simple framework for writing AST-based checks.
@ -347,3 +360,4 @@ class CheckForLoggingIssues(BaseASTChecker):
def factory(register):
register(CheckForLoggingIssues)
register(check_oslo_namespace_imports)
register(check_mock_imports)

View File

@ -16,5 +16,8 @@
import eventlet
eventlet.monkey_patch()
# Import oslotest before importing test submodules to setup six.moves for mock
# oslotest prepares mock for six in oslotest/__init__.py as follow:
# six.add_move(six.MovedModule('mock', 'mock', 'unittest.mock')) and
# oslo.messaging imports oslotest before importing test submodules to
# setup six.moves for mock, then "from six.moves import mock" works well.
import oslotest

View File

@ -16,11 +16,10 @@ import functools
import unittest
from concurrent import futures
from mock import mock
from mock import patch
from oslo_serialization import jsonutils
from oslo_utils import timeutils
import pika
from six.moves import mock
import oslo_messaging
from oslo_messaging._drivers.pika_driver import pika_commons as pika_drv_cmns
@ -151,7 +150,7 @@ class RpcPikaIncomingMessageTestCase(unittest.TestCase):
self.assertEqual("payload_value",
message.message.get("payload_key", None))
@patch(("oslo_messaging._drivers.pika_driver.pika_message."
@mock.patch(("oslo_messaging._drivers.pika_driver.pika_message."
"PikaOutgoingMessage.send"))
def test_reply_for_cast_message(self, send_reply_mock):
message = pika_drv_msg.RpcPikaIncomingMessage(
@ -171,9 +170,9 @@ class RpcPikaIncomingMessageTestCase(unittest.TestCase):
self.assertEqual(0, send_reply_mock.call_count)
@patch("oslo_messaging._drivers.pika_driver.pika_message."
@mock.patch("oslo_messaging._drivers.pika_driver.pika_message."
"RpcReplyPikaOutgoingMessage")
@patch("tenacity.retry")
@mock.patch("tenacity.retry")
def test_positive_reply_for_call_message(self,
retry_mock,
outgoing_message_mock):
@ -206,9 +205,9 @@ class RpcPikaIncomingMessageTestCase(unittest.TestCase):
stop=mock.ANY, retry=mock.ANY, wait=mock.ANY
)
@patch("oslo_messaging._drivers.pika_driver.pika_message."
@mock.patch("oslo_messaging._drivers.pika_driver.pika_message."
"RpcReplyPikaOutgoingMessage")
@patch("tenacity.retry")
@mock.patch("tenacity.retry")
def test_negative_reply_for_call_message(self,
retry_mock,
outgoing_message_mock):
@ -319,7 +318,7 @@ class PikaOutgoingMessageTestCase(unittest.TestCase):
self._message = {"msg_type": 1, "msg_str": "hello"}
self._context = {"request_id": 555, "token": "it is a token"}
@patch("oslo_serialization.jsonutils.dump_as_bytes",
@mock.patch("oslo_serialization.jsonutils.dump_as_bytes",
new=functools.partial(jsonutils.dump_as_bytes, sort_keys=True))
def test_send_with_confirmation(self):
message = pika_drv_msg.PikaOutgoingMessage(
@ -363,7 +362,7 @@ class PikaOutgoingMessageTestCase(unittest.TestCase):
self.assertEqual({'version': '1.0'}, props.headers)
self.assertTrue(props.message_id)
@patch("oslo_serialization.jsonutils.dump_as_bytes",
@mock.patch("oslo_serialization.jsonutils.dump_as_bytes",
new=functools.partial(jsonutils.dump_as_bytes, sort_keys=True))
def test_send_without_confirmation(self):
message = pika_drv_msg.PikaOutgoingMessage(
@ -421,7 +420,7 @@ class RpcPikaOutgoingMessageTestCase(unittest.TestCase):
self._message = {"msg_type": 1, "msg_str": "hello"}
self._context = {"request_id": 555, "token": "it is a token"}
@patch("oslo_serialization.jsonutils.dump_as_bytes",
@mock.patch("oslo_serialization.jsonutils.dump_as_bytes",
new=functools.partial(jsonutils.dump_as_bytes, sort_keys=True))
def test_send_cast_message(self):
message = pika_drv_msg.RpcPikaOutgoingMessage(
@ -467,7 +466,7 @@ class RpcPikaOutgoingMessageTestCase(unittest.TestCase):
self.assertIsNone(props.reply_to)
self.assertTrue(props.message_id)
@patch("oslo_serialization.jsonutils.dump_as_bytes",
@mock.patch("oslo_serialization.jsonutils.dump_as_bytes",
new=functools.partial(jsonutils.dump_as_bytes, sort_keys=True))
def test_send_call_message(self):
message = pika_drv_msg.RpcPikaOutgoingMessage(
@ -542,7 +541,7 @@ class RpcReplyPikaOutgoingMessageTestCase(unittest.TestCase):
self._msg_id = 12345567
@patch("oslo_serialization.jsonutils.dump_as_bytes",
@mock.patch("oslo_serialization.jsonutils.dump_as_bytes",
new=functools.partial(jsonutils.dump_as_bytes, sort_keys=True))
def test_success_message_send(self):
message = pika_drv_msg.RpcReplyPikaOutgoingMessage(
@ -571,8 +570,8 @@ class RpcReplyPikaOutgoingMessageTestCase(unittest.TestCase):
self.assertIsNone(props.reply_to)
self.assertTrue(props.message_id)
@patch("traceback.format_exception", new=lambda x, y, z: z)
@patch("oslo_serialization.jsonutils.dump_as_bytes",
@mock.patch("traceback.format_exception", new=lambda x, y, z: z)
@mock.patch("oslo_serialization.jsonutils.dump_as_bytes",
new=functools.partial(jsonutils.dump_as_bytes, sort_keys=True))
def test_failure_message_send(self):
failure_info = (oslo_messaging.MessagingException,

View File

@ -17,7 +17,7 @@ import time
import unittest
from concurrent import futures
import mock
from six.moves import mock
from oslo_messaging._drivers.pika_driver import pika_exceptions as pika_drv_exc
from oslo_messaging._drivers.pika_driver import pika_poller

View File

@ -13,7 +13,7 @@
# under the License.
import kafka
import kafka.errors
import mock
from six.moves import mock
import testscenarios
import oslo_messaging
@ -117,7 +117,7 @@ class TestKafkaDriver(test_utils.BaseTestCase):
self.driver.listeners.extend(listeners)
self.driver.cleanup()
for listener in listeners:
listener.close.assert_called_once()
listener.close.assert_called_once_with()
class TestKafkaConnection(test_utils.BaseTestCase):

View File

@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from six.moves import mock
import testtools
import time

View File

@ -10,7 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from six.moves import mock
import testtools
from oslo_messaging._drivers.zmq_driver.poller import green_poller

View File

@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from six.moves import mock
import testtools
import oslo_messaging

View File

@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from six.moves import mock
from oslo_messaging.tests import utils as test_utils

View File

@ -13,9 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from oslo_config import cfg
from six.moves import mock
import testscenarios
import oslo_messaging

View File

@ -17,9 +17,9 @@ import eventlet
import threading
from oslo_config import cfg
from six.moves import mock
import testscenarios
import mock
import oslo_messaging
from oslo_messaging import rpc
from oslo_messaging.rpc import server as rpc_server_module

View File

@ -12,11 +12,10 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from six.moves import mock
import stevedore
import testtools
import mock
from oslo_messaging import server
try:
from oslo_messaging import opts

View File

@ -14,9 +14,9 @@
# under the License.
import fixtures
import mock
from oslo_config import cfg
import six
from six.moves import mock
from stevedore import driver
import testscenarios