From cdee551ae6145e4646809bf65e71817a63328d9a Mon Sep 17 00:00:00 2001 From: Ben Nemec Date: Fri, 3 Apr 2020 14:22:14 +0000 Subject: [PATCH] Revert "Switch to unittest.mock from mock" This reverts commit ffdf6f10be50bf8c2debd23e076077668b2e9fb6. Switching oslotest to unittest.mock has broken a bunch of other projects because they were (incorrectly) relying on oslotest to pull in third-party mock for them. Until we get everyone migrated, let's hold off on doing this for oslotest. Change-Id: I0bdca39686f3653a56f9e4a554662727e2dbaafd --- lower-constraints.txt | 1 + oslotest/base.py | 2 +- oslotest/mock_fixture.py | 37 ++++++++++-------------- oslotest/tests/unit/test_base.py | 2 +- oslotest/tests/unit/test_log.py | 2 +- oslotest/tests/unit/test_mock_fixture.py | 2 +- oslotest/tests/unit/test_output.py | 6 ++-- oslotest/tests/unit/test_timeout.py | 3 +- requirements.txt | 1 + 9 files changed, 25 insertions(+), 31 deletions(-) diff --git a/lower-constraints.txt b/lower-constraints.txt index 1495402..e4c72a1 100644 --- a/lower-constraints.txt +++ b/lower-constraints.txt @@ -15,6 +15,7 @@ keystoneauth1==3.4.0 linecache2==1.0.0 MarkupSafe==1.0 mccabe==0.2.1 +mock==2.0.0 netaddr==0.7.18 openstackdocstheme==1.18.1 oslo.config==5.2.0 diff --git a/oslotest/base.py b/oslotest/base.py index acb7a33..fb4b02d 100644 --- a/oslotest/base.py +++ b/oslotest/base.py @@ -16,7 +16,6 @@ """Common utilities used in testing""" import logging -from unittest import mock import fixtures from oslotest import createfile @@ -24,6 +23,7 @@ from oslotest import log from oslotest import output from oslotest import timeout +from six.moves import mock import testtools LOG = logging.getLogger(__name__) diff --git a/oslotest/mock_fixture.py b/oslotest/mock_fixture.py index 4e3deab..506cfbd 100644 --- a/oslotest/mock_fixture.py +++ b/oslotest/mock_fixture.py @@ -15,9 +15,9 @@ # under the License. import functools -from unittest import mock import fixtures +import mock def _lazy_autospec_method(mocked_method, original_method, eat_self): @@ -72,10 +72,8 @@ class _AutospecMockMixin(object): original_attr = getattr(original_spec, name) if callable(original_attr): # lazily autospec callable attribute. - eat_self = mock._must_skip( - original_spec, name, - isinstance(original_spec, type) - ) + eat_self = mock.mock._must_skip(original_spec, name, + isinstance(original_spec, type)) _lazy_autospec_method(attr, original_attr, eat_self) @@ -112,17 +110,15 @@ class MockAutospecFixture(fixtures.Fixture): super(MockAutospecFixture, self).setUp() # patch both external and internal usage of Mock / MagicMock. - self.useFixture( - fixtures.MonkeyPatch( - 'unittest.mock.Mock', - _AutospecMock)) - self.useFixture( - fixtures.MonkeyPatch( - 'unittest.mock.MagicMock', - _AutospecMagicMock)) + self.useFixture(fixtures.MonkeyPatch('mock.Mock', _AutospecMock)) + self.useFixture(fixtures.MonkeyPatch('mock.mock.Mock', _AutospecMock)) + self.useFixture(fixtures.MonkeyPatch('mock.MagicMock', + _AutospecMagicMock)) + self.useFixture(fixtures.MonkeyPatch('mock.mock.MagicMock', + _AutospecMagicMock)) -class _patch(mock._patch): +class _patch(mock.mock._patch): """Patch class with working autospec functionality. Currently, mock.patch functionality doesn't handle the autospec parameter @@ -163,11 +159,8 @@ class _patch(mock._patch): if autospec: target = self.getter() original_attr = getattr(target, self.attribute) - eat_self = mock._must_skip( - target, - self.attribute, - isinstance(target, type) - ) + eat_self = mock.mock._must_skip(target, self.attribute, + isinstance(target, type)) new = super(_patch, self).__enter__() @@ -193,11 +186,11 @@ def _safe_attribute_error_wrapper(func): def patch_mock_module(): """Replaces the mock.patch class.""" - mock._patch = _patch + mock.mock._patch = _patch # NOTE(claudiub): mock cannot autospec partial functions properly, # especially those created by LazyLoader objects (scheduler client), # as it will try to copy the partial function's __name__ (which they do # not have). - mock._copy_func_details = _safe_attribute_error_wrapper( - mock._copy_func_details) + mock.mock._copy_func_details = _safe_attribute_error_wrapper( + mock.mock._copy_func_details) diff --git a/oslotest/tests/unit/test_base.py b/oslotest/tests/unit/test_base.py index 86e76d4..b98c672 100644 --- a/oslotest/tests/unit/test_base.py +++ b/oslotest/tests/unit/test_base.py @@ -17,10 +17,10 @@ import logging import os import unittest -from unittest import mock import fixtures import six +from six.moves import mock import testtools from oslotest import base diff --git a/oslotest/tests/unit/test_log.py b/oslotest/tests/unit/test_log.py index f5fdc49..55c02a0 100644 --- a/oslotest/tests/unit/test_log.py +++ b/oslotest/tests/unit/test_log.py @@ -15,8 +15,8 @@ # under the License. import logging -from unittest import mock +from six.moves import mock import testtools from oslotest import log diff --git a/oslotest/tests/unit/test_mock_fixture.py b/oslotest/tests/unit/test_mock_fixture.py index 896bbcb..e578fb4 100644 --- a/oslotest/tests/unit/test_mock_fixture.py +++ b/oslotest/tests/unit/test_mock_fixture.py @@ -13,8 +13,8 @@ # License for the specific language governing permissions and limitations # under the License. +import mock import testtools -from unittest import mock from oslotest import mock_fixture diff --git a/oslotest/tests/unit/test_output.py b/oslotest/tests/unit/test_output.py index 1f67ba8..b4abcf0 100644 --- a/oslotest/tests/unit/test_output.py +++ b/oslotest/tests/unit/test_output.py @@ -13,12 +13,12 @@ # under the License. import sys -from unittest import mock - -import testtools from oslotest import output +from six.moves import mock +import testtools + class CaptureOutputTest(testtools.TestCase): diff --git a/oslotest/tests/unit/test_timeout.py b/oslotest/tests/unit/test_timeout.py index 9c0b32c..38c7a3d 100644 --- a/oslotest/tests/unit/test_timeout.py +++ b/oslotest/tests/unit/test_timeout.py @@ -12,8 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. -from unittest import mock - +from six.moves import mock import testtools from oslotest import timeout diff --git a/requirements.txt b/requirements.txt index 32baed1..9826c3b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ fixtures>=3.0.0 # Apache-2.0/BSD python-subunit>=1.0.0 # Apache-2.0/BSD six>=1.10.0 # MIT testtools>=2.2.0 # MIT +mock>=2.0.0 # BSD