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
This commit is contained in:
zhangbailin 2020-04-02 18:17:34 +08:00 committed by Brin Zhang
parent 8ed9135de9
commit 6b0e1f8d12
25 changed files with 55 additions and 76 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -13,8 +13,8 @@
# under the License.
import textwrap
from unittest import mock
import mock
import pycodestyle
from cyborg.hacking import checks

View File

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

View File

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