From 6b0e1f8d12fdfd50c6bcde507bf603a2bf856a18 Mon Sep 17 00:00:00 2001 From: zhangbailin Date: Thu, 2 Apr 2020 18:17:34 +0800 Subject: [PATCH] Use unittest.mock instead of third party mock Now that we no longer support py27, we can use the standard library unittest.mock module instead of the third party mock lib. Most of this is autogenerated, as described below, but there are three manual changes necessary. Change-Id: If9cd7eca3454d9e1d62a0a1753e157adbf965e8d --- cyborg/tests/base.py | 2 +- .../drivers/aichip/huawei/test_ascend.py | 2 +- .../drivers/fpga/intel/test_driver.py | 2 +- .../accelerator/drivers/gpu/test_utils.py | 2 +- .../drivers/spdk/nvmf/test_nvmf.py | 2 +- .../drivers/spdk/vhost/test_vhost.py | 2 +- .../tests/unit/agent/test_resource_tracker.py | 2 +- .../unit/api/controllers/v2/test_arqs.py | 2 +- .../api/controllers/v2/test_deployables.py | 2 +- .../controllers/v2/test_device_profiles.py | 2 +- .../unit/api/controllers/v2/test_devices.py | 2 +- cyborg/tests/unit/common/test_nova_client.py | 2 +- cyborg/tests/unit/conductor/test_manager.py | 2 +- .../tests/unit/objects/test_attach_handle.py | 47 ++++++++----------- .../tests/unit/objects/test_control_path.py | 3 +- cyborg/tests/unit/objects/test_deployable.py | 2 +- cyborg/tests/unit/objects/test_device.py | 38 ++++++--------- .../tests/unit/objects/test_device_profile.py | 3 +- cyborg/tests/unit/objects/test_ext_arq_job.py | 2 +- cyborg/tests/unit/objects/test_extarq.py | 2 +- .../tests/unit/objects/test_fpga_ext_arq.py | 2 +- .../unit/services/_test_placement_client.py | 2 +- cyborg/tests/unit/test_hacking.py | 2 +- requirements.txt | 1 - test-requirements.txt | 1 - 25 files changed, 55 insertions(+), 76 deletions(-) diff --git a/cyborg/tests/base.py b/cyborg/tests/base.py index 77b846b8..2933653a 100644 --- a/cyborg/tests/base.py +++ b/cyborg/tests/base.py @@ -14,6 +14,7 @@ # under the License. import os +from unittest import mock from oslo_config import cfg from oslo_config import fixture as config_fixture @@ -25,7 +26,6 @@ from oslotest import base import contextlib import eventlet -import mock import testtools from cyborg.common import config as cyborg_config diff --git a/cyborg/tests/unit/accelerator/drivers/aichip/huawei/test_ascend.py b/cyborg/tests/unit/accelerator/drivers/aichip/huawei/test_ascend.py index e9dd2511..cac8723b 100644 --- a/cyborg/tests/unit/accelerator/drivers/aichip/huawei/test_ascend.py +++ b/cyborg/tests/unit/accelerator/drivers/aichip/huawei/test_ascend.py @@ -11,7 +11,7 @@ # under the License. import json -import mock +from unittest import mock from cyborg.accelerator.drivers.aichip.huawei.ascend import AscendDriver from cyborg.tests import base diff --git a/cyborg/tests/unit/accelerator/drivers/fpga/intel/test_driver.py b/cyborg/tests/unit/accelerator/drivers/fpga/intel/test_driver.py index ef1350e7..8fd2b6a4 100644 --- a/cyborg/tests/unit/accelerator/drivers/fpga/intel/test_driver.py +++ b/cyborg/tests/unit/accelerator/drivers/fpga/intel/test_driver.py @@ -14,8 +14,8 @@ import fixtures -import mock import os +from unittest import mock from cyborg.accelerator.drivers.fpga.intel.driver import IntelFPGADriver from cyborg.accelerator.drivers.fpga.intel import sysinfo diff --git a/cyborg/tests/unit/accelerator/drivers/gpu/test_utils.py b/cyborg/tests/unit/accelerator/drivers/gpu/test_utils.py index d0f0db5e..ea51bab5 100644 --- a/cyborg/tests/unit/accelerator/drivers/gpu/test_utils.py +++ b/cyborg/tests/unit/accelerator/drivers/gpu/test_utils.py @@ -12,7 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. -import mock +from unittest import mock from oslo_serialization import jsonutils diff --git a/cyborg/tests/unit/accelerator/drivers/spdk/nvmf/test_nvmf.py b/cyborg/tests/unit/accelerator/drivers/spdk/nvmf/test_nvmf.py index b0b2b570..502d982b 100644 --- a/cyborg/tests/unit/accelerator/drivers/spdk/nvmf/test_nvmf.py +++ b/cyborg/tests/unit/accelerator/drivers/spdk/nvmf/test_nvmf.py @@ -13,7 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. -import mock +from unittest import mock from cyborg.accelerator.drivers.spdk.nvmf.nvmf import NVMFDRIVER from cyborg.accelerator.drivers.spdk.util import common_fun diff --git a/cyborg/tests/unit/accelerator/drivers/spdk/vhost/test_vhost.py b/cyborg/tests/unit/accelerator/drivers/spdk/vhost/test_vhost.py index 8f29239e..54d77bcb 100644 --- a/cyborg/tests/unit/accelerator/drivers/spdk/vhost/test_vhost.py +++ b/cyborg/tests/unit/accelerator/drivers/spdk/vhost/test_vhost.py @@ -13,7 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. -import mock +from unittest import mock from cyborg.accelerator.drivers.spdk.util import common_fun from cyborg.accelerator.drivers.spdk.util.pyspdk.vhost_client import VhostTgt diff --git a/cyborg/tests/unit/agent/test_resource_tracker.py b/cyborg/tests/unit/agent/test_resource_tracker.py index ac47ade3..b2d78483 100644 --- a/cyborg/tests/unit/agent/test_resource_tracker.py +++ b/cyborg/tests/unit/agent/test_resource_tracker.py @@ -13,7 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. -import mock +from unittest import mock """Cyborg agent resource_tracker test cases.""" from cyborg.agent.resource_tracker import ResourceTracker diff --git a/cyborg/tests/unit/api/controllers/v2/test_arqs.py b/cyborg/tests/unit/api/controllers/v2/test_arqs.py index 064461f0..9b1f85f3 100644 --- a/cyborg/tests/unit/api/controllers/v2/test_arqs.py +++ b/cyborg/tests/unit/api/controllers/v2/test_arqs.py @@ -13,9 +13,9 @@ # License for the specific language governing permissions and limitations # under the License. -import mock from six.moves import http_client import unittest +from unittest import mock from oslo_serialization import jsonutils diff --git a/cyborg/tests/unit/api/controllers/v2/test_deployables.py b/cyborg/tests/unit/api/controllers/v2/test_deployables.py index 0564f5e4..7d6f4d2e 100644 --- a/cyborg/tests/unit/api/controllers/v2/test_deployables.py +++ b/cyborg/tests/unit/api/controllers/v2/test_deployables.py @@ -13,7 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. -import mock +from unittest import mock from cyborg.tests.unit.api.controllers.v2 import base as v2_test from cyborg.tests.unit import fake_deployable diff --git a/cyborg/tests/unit/api/controllers/v2/test_device_profiles.py b/cyborg/tests/unit/api/controllers/v2/test_device_profiles.py index f5b98b4c..9cd209a9 100644 --- a/cyborg/tests/unit/api/controllers/v2/test_device_profiles.py +++ b/cyborg/tests/unit/api/controllers/v2/test_device_profiles.py @@ -13,8 +13,8 @@ # License for the specific language governing permissions and limitations # under the License. -import mock from six.moves import http_client +from unittest import mock from oslo_serialization import jsonutils diff --git a/cyborg/tests/unit/api/controllers/v2/test_devices.py b/cyborg/tests/unit/api/controllers/v2/test_devices.py index b739cd29..9f339130 100644 --- a/cyborg/tests/unit/api/controllers/v2/test_devices.py +++ b/cyborg/tests/unit/api/controllers/v2/test_devices.py @@ -13,7 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. -import mock +from unittest import mock from cyborg.tests.unit.api.controllers.v2 import base as v2_test from cyborg.tests.unit import fake_device diff --git a/cyborg/tests/unit/common/test_nova_client.py b/cyborg/tests/unit/common/test_nova_client.py index 22ad868c..9a146c60 100644 --- a/cyborg/tests/unit/common/test_nova_client.py +++ b/cyborg/tests/unit/common/test_nova_client.py @@ -14,7 +14,7 @@ # under the License. import copy import fixtures -import mock +from unittest import mock from cyborg.common import exception from cyborg.common import nova_client diff --git a/cyborg/tests/unit/conductor/test_manager.py b/cyborg/tests/unit/conductor/test_manager.py index f6a5c0f3..26856bf3 100644 --- a/cyborg/tests/unit/conductor/test_manager.py +++ b/cyborg/tests/unit/conductor/test_manager.py @@ -10,7 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. import fixtures -import mock +from unittest import mock from oslo_utils.fixture import uuidsentinel as uuids diff --git a/cyborg/tests/unit/objects/test_attach_handle.py b/cyborg/tests/unit/objects/test_attach_handle.py index ca96e86a..0bc9f9db 100644 --- a/cyborg/tests/unit/objects/test_attach_handle.py +++ b/cyborg/tests/unit/objects/test_attach_handle.py @@ -12,8 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. - -import mock +from unittest import mock from cyborg import objects from cyborg.tests.unit.db import base @@ -44,39 +43,33 @@ class TestAttachHandleObject(base.DbTestCase): mock_attach_handle_get.assert_called_once_with(self.context, id) self.assertEqual(self.context, attach_handle._context) - def test_get_attach_handle_by_deployable_id(self): + @mock.patch.object(objects.AttachHandle, 'list') + def test_get_attach_handle_by_deployable_id(self, mock_list): + mock_list.return_value = [self.fake_attach_handle] deployable_id = self.fake_attach_handle['deployable_id'] ah_filter = {'deployable_id': deployable_id} - with mock.patch.object(objects.AttachHandle, 'list', - autospec=True) as mock_attach_handle_list: - mock_attach_handle_list.return_value = [self.fake_attach_handle] - attach_handles = objects.AttachHandle.get_ah_list_by_deployable_id( - self.context, deployable_id) - mock_attach_handle_list.assert_called_once_with( - self.context, ah_filter) - self.assertEqual( - deployable_id, - attach_handles[0]['deployable_id']) + attach_handles = objects.AttachHandle.get_ah_list_by_deployable_id( + self.context, deployable_id) + mock_list.assert_called_once_with(self.context, ah_filter) + self.assertEqual(deployable_id, attach_handles[0]['deployable_id']) - def test_get_attach_handle_by_depid_attachinfo(self): + @mock.patch.object(objects.AttachHandle, 'list') + def test_get_attach_handle_by_depid_attachinfo(self, mock_list): + mock_list.return_value = [self.fake_attach_handle] deployable_id = self.fake_attach_handle['deployable_id'] attach_info = self.fake_attach_handle['attach_info'] ah_filter = {'deployable_id': deployable_id, 'attach_info': attach_info} - with mock.patch.object(objects.AttachHandle, 'list', - autospec=True) as mock_attach_handle_list: - mock_attach_handle_list.return_value = [self.fake_attach_handle] - attach_handles = objects.AttachHandle.get_ah_by_depid_attachinfo( - self.context, deployable_id, attach_info) - mock_attach_handle_list.assert_called_once_with( - self.context, ah_filter) - self.assertEqual(attach_info, attach_handles['attach_info']) + attach_handles = objects.AttachHandle.get_ah_by_depid_attachinfo( + self.context, deployable_id, attach_info) + mock_list.assert_called_once_with(self.context, ah_filter) + self.assertEqual(attach_info, attach_handles['attach_info']) - # test objects.AttachHandle.list() return [] - mock_attach_handle_list.return_value = [] - attach_handle = objects.AttachHandle.get_ah_by_depid_attachinfo( - self.context, deployable_id, attach_info) - self.assertIsNone(attach_handle) + # test objects.AttachHandle.list() return [] + mock_list.return_value = [] + attach_handle = objects.AttachHandle.get_ah_by_depid_attachinfo( + self.context, deployable_id, attach_info) + self.assertIsNone(attach_handle) def test_list(self): with mock.patch.object(self.dbapi, 'attach_handle_list', diff --git a/cyborg/tests/unit/objects/test_control_path.py b/cyborg/tests/unit/objects/test_control_path.py index c1920b54..2ef124ef 100644 --- a/cyborg/tests/unit/objects/test_control_path.py +++ b/cyborg/tests/unit/objects/test_control_path.py @@ -12,8 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. - -import mock +from unittest import mock from cyborg import objects from cyborg.tests.unit.db import base diff --git a/cyborg/tests/unit/objects/test_deployable.py b/cyborg/tests/unit/objects/test_deployable.py index 085d5020..2b314823 100644 --- a/cyborg/tests/unit/objects/test_deployable.py +++ b/cyborg/tests/unit/objects/test_deployable.py @@ -13,7 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. -import mock +from unittest import mock from oslo_db import exception as db_exc diff --git a/cyborg/tests/unit/objects/test_device.py b/cyborg/tests/unit/objects/test_device.py index cda99b62..caea3f33 100644 --- a/cyborg/tests/unit/objects/test_device.py +++ b/cyborg/tests/unit/objects/test_device.py @@ -12,8 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. - -import mock +from unittest import mock from cyborg import objects from cyborg.tests.unit.db import base @@ -47,31 +46,22 @@ class TestDeviceObject(base.DbTestCase): # TODO(chenke) add testcase dbapi.device_get_by_id raise exception. - def test_get_by_hostname(self): + @mock.patch.object(objects.Device, 'list') + def test_get_by_hostname(self, mock_list): hostname = self.fake_device['hostname'] dev_filter = {'hostname': hostname} - with mock.patch.object(objects.Device, 'list', - autospec=True) as mock_device_list: - mock_device_list.return_value = [self.fake_device] - devices = objects.Device.get_list_by_hostname( - self.context, hostname) - mock_device_list.assert_called_once_with( - self.context, dev_filter) - self.assertEqual( - hostname, - devices[0]['hostname']) + mock_list.return_value = [self.fake_device] + devices = objects.Device.get_list_by_hostname(self.context, hostname) + mock_list.assert_called_once_with(self.context, dev_filter) + self.assertEqual(hostname, devices[0]['hostname']) - with mock.patch.object(objects.Device, 'list', - autospec=True) as mock_device_list: - # test objects.Device.list return [] when hostname is None. - mock_device_list.return_value = [] - hostname = None - dev_filter = {'hostname': hostname} - devices = objects.Device.get_list_by_hostname( - self.context, hostname) - mock_device_list.assert_called_once_with( - self.context, dev_filter) - self.assertEqual([], devices) + # test objects.Device.list return [] when hostname is None. + mock_list.return_value = [] + hostname = None + dev_filter = {'hostname': hostname} + devices = objects.Device.get_list_by_hostname(self.context, hostname) + mock_list.assert_called_with(self.context, dev_filter) + self.assertEqual([], devices) def test_list(self): with mock.patch.object(self.dbapi, 'device_list', diff --git a/cyborg/tests/unit/objects/test_device_profile.py b/cyborg/tests/unit/objects/test_device_profile.py index 9ad136b1..e9de14bd 100644 --- a/cyborg/tests/unit/objects/test_device_profile.py +++ b/cyborg/tests/unit/objects/test_device_profile.py @@ -12,8 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. - -import mock +from unittest import mock from cyborg import objects from cyborg.tests.unit.db import base diff --git a/cyborg/tests/unit/objects/test_ext_arq_job.py b/cyborg/tests/unit/objects/test_ext_arq_job.py index b2cf4b2c..023b1835 100644 --- a/cyborg/tests/unit/objects/test_ext_arq_job.py +++ b/cyborg/tests/unit/objects/test_ext_arq_job.py @@ -13,7 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. -import mock +from unittest import mock from oslo_serialization import jsonutils from oslo_utils import timeutils diff --git a/cyborg/tests/unit/objects/test_extarq.py b/cyborg/tests/unit/objects/test_extarq.py index bb43acdc..dff0f4df 100644 --- a/cyborg/tests/unit/objects/test_extarq.py +++ b/cyborg/tests/unit/objects/test_extarq.py @@ -13,8 +13,8 @@ # License for the specific language governing permissions and limitations # under the License. -import mock import six +from unittest import mock from testtools.matchers import HasLength diff --git a/cyborg/tests/unit/objects/test_fpga_ext_arq.py b/cyborg/tests/unit/objects/test_fpga_ext_arq.py index df75ff69..7cc96207 100644 --- a/cyborg/tests/unit/objects/test_fpga_ext_arq.py +++ b/cyborg/tests/unit/objects/test_fpga_ext_arq.py @@ -15,10 +15,10 @@ from io import BytesIO import json -import mock import requests from requests import structures from requests import utils +from unittest import mock from testtools.matchers import HasLength diff --git a/cyborg/tests/unit/services/_test_placement_client.py b/cyborg/tests/unit/services/_test_placement_client.py index 6c3191b5..90a51525 100644 --- a/cyborg/tests/unit/services/_test_placement_client.py +++ b/cyborg/tests/unit/services/_test_placement_client.py @@ -12,7 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. -import mock +from unittest import mock from keystoneauth1 import exceptions as ks_exc from oslo_config import cfg diff --git a/cyborg/tests/unit/test_hacking.py b/cyborg/tests/unit/test_hacking.py index 659beb8f..94fd485b 100644 --- a/cyborg/tests/unit/test_hacking.py +++ b/cyborg/tests/unit/test_hacking.py @@ -13,8 +13,8 @@ # under the License. import textwrap +from unittest import mock -import mock import pycodestyle from cyborg.hacking import checks diff --git a/requirements.txt b/requirements.txt index 80242f59..79b7f11f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -26,7 +26,6 @@ stevedore>=1.5.0 # Apache-2.0 keystonemiddleware>=4.17.0 # Apache-2.0 jsonpatch>=1.16,!=1.20 # BSD psutil>=3.2.2 # BSD -mock>=2.0.0 # BSD python-glanceclient>=2.3.0 # Apache-2.0 oslo.privsep>=1.32.0 # Apache-2.0 cursive>=0.2.1 # Apache-2.0 diff --git a/test-requirements.txt b/test-requirements.txt index 262462f0..2bbca5fa 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -7,7 +7,6 @@ hacking>=3.0.1,<3.1.0 # Apache-2.0 bandit>=1.6.0 # Apache-2.0 coverage>=3.6,!=4.4 # Apache-2.0 fixtures>=3.0.0 # Apache-2.0/BSD -mock>=2.0.0 # BSD ddt>=1.0.1 # MIT oslotest>=3.2.0 # Apache-2.0 stestr>=2.0.0 # Apache-2.0/BSD