diff --git a/moto/compat.py b/moto/compat.py index c0acd28a6..311b50273 100644 --- a/moto/compat.py +++ b/moto/compat.py @@ -8,3 +8,9 @@ try: import collections.abc as collections_abc # noqa except ImportError: import collections as collections_abc # noqa + +try: + from unittest.mock import patch # noqa +except ImportError: + # for python 2.7 + from mock import patch # noqa diff --git a/moto/core/models.py b/moto/core/models.py index b7ab1d3c8..f8ec9ee37 100644 --- a/moto/core/models.py +++ b/moto/core/models.py @@ -19,10 +19,10 @@ from distutils.version import LooseVersion from six.moves.urllib.parse import urlparse from werkzeug.wrappers import Request -import mock from moto import settings import responses from moto.packages.httpretty import HTTPretty +from moto.compat import patch from .utils import ( convert_httpretty_response, convert_regex_to_flask_path, @@ -56,7 +56,7 @@ class BaseMockAWS(object): "AWS_SECRET_ACCESS_KEY": "foobar_secret", } self.ORIG_KEYS = {} - self.default_session_mock = mock.patch("boto3.DEFAULT_SESSION", None) + self.default_session_mock = patch("boto3.DEFAULT_SESSION", None) if self.__class__.nested_count == 0: self.reset() @@ -94,7 +94,12 @@ class BaseMockAWS(object): if self.__class__.nested_count == 0: if self.__class__.mocks_active: - self.default_session_mock.stop() + try: + self.default_session_mock.stop() + except RuntimeError: + # We only need to check for this exception in Python 3.6 and 3.7 + # https://bugs.python.org/issue36366 + pass self.unmock_env_variables() self.__class__.mocks_active = False self.disable_patching() @@ -456,7 +461,6 @@ class ServerModeMockAWS(BaseMockAWS): self.reset() from boto3 import client as real_boto3_client, resource as real_boto3_resource - import mock def fake_boto3_client(*args, **kwargs): region = self._get_region(*args, **kwargs) @@ -501,10 +505,10 @@ class ServerModeMockAWS(BaseMockAWS): if message_body is not None: self.send(message_body) - self._client_patcher = mock.patch("boto3.client", fake_boto3_client) - self._resource_patcher = mock.patch("boto3.resource", fake_boto3_resource) + self._client_patcher = patch("boto3.client", fake_boto3_client) + self._resource_patcher = patch("boto3.resource", fake_boto3_resource) if six.PY2: - self._httplib_patcher = mock.patch( + self._httplib_patcher = patch( "httplib.HTTPConnection._send_output", fake_httplib_send_output ) diff --git a/setup.py b/setup.py index 628ade795..81a41dec9 100755 --- a/setup.py +++ b/setup.py @@ -60,7 +60,6 @@ install_requires += [ "configparser<5.0; python_version < '3'", "Jinja2>=2.10.1", "Jinja2<3.0.0; python_version < '3'", - "mock", "mock<=3.0.5; python_version < '3'", "more-itertools", "more-itertools==5.0.0; python_version < '3'", diff --git a/tests/test_cloudformation/test_stack_parsing.py b/tests/test_cloudformation/test_stack_parsing.py index 9692e36cb..7dc8b95e2 100644 --- a/tests/test_cloudformation/test_stack_parsing.py +++ b/tests/test_cloudformation/test_stack_parsing.py @@ -2,8 +2,8 @@ from __future__ import unicode_literals import json import yaml -from mock import patch import sure # noqa +from tests.compat import patch from moto.cloudformation.exceptions import ValidationError from moto.cloudformation.models import FakeStack diff --git a/tests/test_core/test_server.py b/tests/test_core/test_server.py index 205a2ad0f..905b662d5 100644 --- a/tests/test_core/test_server.py +++ b/tests/test_core/test_server.py @@ -1,7 +1,8 @@ from __future__ import unicode_literals -from mock import patch + import sure # noqa +from tests.compat import patch from moto.server import main, create_backend_app, DomainDispatcherApplication diff --git a/tests/test_dynamodbstreams/test_dynamodbstreams.py b/tests/test_dynamodbstreams/test_dynamodbstreams.py index 70efc5289..8982617ae 100644 --- a/tests/test_dynamodbstreams/test_dynamodbstreams.py +++ b/tests/test_dynamodbstreams/test_dynamodbstreams.py @@ -36,7 +36,10 @@ class TestCore: self.stream_arn = None for m in self.mocks: - m.stop() + try: + m.stop() + except RuntimeError: + pass def test_verify_stream(self): conn = boto3.client("dynamodb", region_name="us-east-1") @@ -200,7 +203,10 @@ class TestEdges: def teardown(self): for m in self.mocks: - m.stop() + try: + m.stop() + except RuntimeError: + pass def test_enable_stream_on_table(self): conn = boto3.client("dynamodb", region_name="us-east-1") diff --git a/tests/test_packages/test_httpretty.py b/tests/test_packages/test_httpretty.py index ccf9b98ef..ba6ba95d2 100644 --- a/tests/test_packages/test_httpretty.py +++ b/tests/test_packages/test_httpretty.py @@ -1,7 +1,6 @@ # #!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import unicode_literals -import mock from moto.packages.httpretty.core import ( HTTPrettyRequest, diff --git a/tests/test_rds2/test_filters.py b/tests/test_rds2/test_filters.py index 5f6668219..ab9aeb4ef 100644 --- a/tests/test_rds2/test_filters.py +++ b/tests/test_rds2/test_filters.py @@ -28,7 +28,10 @@ class TestDBInstanceFilters(object): @classmethod def teardown_class(cls): - cls.mock_rds.stop() + try: + cls.mock_rds.stop() + except RuntimeError: + pass def test_invalid_filter_name_raises_error(self): with pytest.raises(ClientError) as ex: @@ -203,7 +206,10 @@ class TestDBSnapshotFilters(object): @classmethod def teardown_class(cls): - cls.mock_rds.stop() + try: + cls.mock_rds.stop() + except RuntimeError: + pass def test_invalid_filter_name_raises_error(self): with pytest.raises(ClientError) as ex: