Merge "Test:Use unittest.mock on Python 3"

This commit is contained in:
Jenkins 2017-02-16 01:37:24 +00:00 committed by Gerrit Code Review
commit 706cfde8d3
13 changed files with 51 additions and 37 deletions

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

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

@ -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,8 +150,8 @@ class RpcPikaIncomingMessageTestCase(unittest.TestCase):
self.assertEqual("payload_value",
message.message.get("payload_key", None))
@patch(("oslo_messaging._drivers.pika_driver.pika_message."
"PikaOutgoingMessage.send"))
@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(
self._pika_engine, self._channel, self._method, self._properties,
@ -171,9 +170,9 @@ class RpcPikaIncomingMessageTestCase(unittest.TestCase):
self.assertEqual(0, send_reply_mock.call_count)
@patch("oslo_messaging._drivers.pika_driver.pika_message."
"RpcReplyPikaOutgoingMessage")
@patch("tenacity.retry")
@mock.patch("oslo_messaging._drivers.pika_driver.pika_message."
"RpcReplyPikaOutgoingMessage")
@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."
"RpcReplyPikaOutgoingMessage")
@patch("tenacity.retry")
@mock.patch("oslo_messaging._drivers.pika_driver.pika_message."
"RpcReplyPikaOutgoingMessage")
@mock.patch("tenacity.retry")
def test_negative_reply_for_call_message(self,
retry_mock,
outgoing_message_mock):
@ -319,8 +318,8 @@ 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",
new=functools.partial(jsonutils.dump_as_bytes, sort_keys=True))
@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(
self._pika_engine, self._message, self._context
@ -363,8 +362,8 @@ class PikaOutgoingMessageTestCase(unittest.TestCase):
self.assertEqual({'version': '1.0'}, props.headers)
self.assertTrue(props.message_id)
@patch("oslo_serialization.jsonutils.dump_as_bytes",
new=functools.partial(jsonutils.dump_as_bytes, sort_keys=True))
@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(
self._pika_engine, self._message, self._context
@ -421,8 +420,8 @@ 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",
new=functools.partial(jsonutils.dump_as_bytes, sort_keys=True))
@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(
self._pika_engine, self._message, self._context
@ -467,8 +466,8 @@ class RpcPikaOutgoingMessageTestCase(unittest.TestCase):
self.assertIsNone(props.reply_to)
self.assertTrue(props.message_id)
@patch("oslo_serialization.jsonutils.dump_as_bytes",
new=functools.partial(jsonutils.dump_as_bytes, sort_keys=True))
@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(
self._pika_engine, self._message, self._context
@ -542,8 +541,8 @@ class RpcReplyPikaOutgoingMessageTestCase(unittest.TestCase):
self._msg_id = 12345567
@patch("oslo_serialization.jsonutils.dump_as_bytes",
new=functools.partial(jsonutils.dump_as_bytes, sort_keys=True))
@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(
self._pika_engine, self._msg_id, reply="all_fine"
@ -571,9 +570,9 @@ 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",
new=functools.partial(jsonutils.dump_as_bytes, sort_keys=True))
@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,
oslo_messaging.MessagingException("Error message"),

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

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

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

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

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

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

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

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

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

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