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. # under the License.
import os import os
from unittest import mock
from oslo_config import cfg from oslo_config import cfg
from oslo_config import fixture as config_fixture from oslo_config import fixture as config_fixture
@ -25,7 +26,6 @@ from oslotest import base
import contextlib import contextlib
import eventlet import eventlet
import mock
import testtools import testtools
from cyborg.common import config as cyborg_config from cyborg.common import config as cyborg_config

View File

@ -11,7 +11,7 @@
# under the License. # under the License.
import json import json
import mock from unittest import mock
from cyborg.accelerator.drivers.aichip.huawei.ascend import AscendDriver from cyborg.accelerator.drivers.aichip.huawei.ascend import AscendDriver
from cyborg.tests import base from cyborg.tests import base

View File

@ -14,8 +14,8 @@
import fixtures import fixtures
import mock
import os import os
from unittest import mock
from cyborg.accelerator.drivers.fpga.intel.driver import IntelFPGADriver from cyborg.accelerator.drivers.fpga.intel.driver import IntelFPGADriver
from cyborg.accelerator.drivers.fpga.intel import sysinfo from cyborg.accelerator.drivers.fpga.intel import sysinfo

View File

@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import mock from unittest import mock
from oslo_serialization import jsonutils from oslo_serialization import jsonutils

View File

@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import mock from unittest import mock
from cyborg.accelerator.drivers.spdk.nvmf.nvmf import NVMFDRIVER from cyborg.accelerator.drivers.spdk.nvmf.nvmf import NVMFDRIVER
from cyborg.accelerator.drivers.spdk.util import common_fun from cyborg.accelerator.drivers.spdk.util import common_fun

View File

@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import mock from unittest import mock
from cyborg.accelerator.drivers.spdk.util import common_fun from cyborg.accelerator.drivers.spdk.util import common_fun
from cyborg.accelerator.drivers.spdk.util.pyspdk.vhost_client import VhostTgt 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 # License for the specific language governing permissions and limitations
# under the License. # under the License.
import mock from unittest import mock
"""Cyborg agent resource_tracker test cases.""" """Cyborg agent resource_tracker test cases."""
from cyborg.agent.resource_tracker import ResourceTracker from cyborg.agent.resource_tracker import ResourceTracker

View File

@ -13,9 +13,9 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import mock
from six.moves import http_client from six.moves import http_client
import unittest import unittest
from unittest import mock
from oslo_serialization import jsonutils from oslo_serialization import jsonutils

View File

@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # 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.api.controllers.v2 import base as v2_test
from cyborg.tests.unit import fake_deployable from cyborg.tests.unit import fake_deployable

View File

@ -13,8 +13,8 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import mock
from six.moves import http_client from six.moves import http_client
from unittest import mock
from oslo_serialization import jsonutils from oslo_serialization import jsonutils

View File

@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # 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.api.controllers.v2 import base as v2_test
from cyborg.tests.unit import fake_device from cyborg.tests.unit import fake_device

View File

@ -14,7 +14,7 @@
# under the License. # under the License.
import copy import copy
import fixtures import fixtures
import mock from unittest import mock
from cyborg.common import exception from cyborg.common import exception
from cyborg.common import nova_client from cyborg.common import nova_client

View File

@ -10,7 +10,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import fixtures import fixtures
import mock from unittest import mock
from oslo_utils.fixture import uuidsentinel as uuids from oslo_utils.fixture import uuidsentinel as uuids

View File

@ -12,8 +12,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from unittest import mock
import mock
from cyborg import objects from cyborg import objects
from cyborg.tests.unit.db import base 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) mock_attach_handle_get.assert_called_once_with(self.context, id)
self.assertEqual(self.context, attach_handle._context) 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'] deployable_id = self.fake_attach_handle['deployable_id']
ah_filter = {'deployable_id': deployable_id} ah_filter = {'deployable_id': deployable_id}
with mock.patch.object(objects.AttachHandle, 'list', attach_handles = objects.AttachHandle.get_ah_list_by_deployable_id(
autospec=True) as mock_attach_handle_list: self.context, deployable_id)
mock_attach_handle_list.return_value = [self.fake_attach_handle] mock_list.assert_called_once_with(self.context, ah_filter)
attach_handles = objects.AttachHandle.get_ah_list_by_deployable_id( self.assertEqual(deployable_id, attach_handles[0]['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'])
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'] deployable_id = self.fake_attach_handle['deployable_id']
attach_info = self.fake_attach_handle['attach_info'] attach_info = self.fake_attach_handle['attach_info']
ah_filter = {'deployable_id': deployable_id, ah_filter = {'deployable_id': deployable_id,
'attach_info': attach_info} 'attach_info': attach_info}
with mock.patch.object(objects.AttachHandle, 'list', attach_handles = objects.AttachHandle.get_ah_by_depid_attachinfo(
autospec=True) as mock_attach_handle_list: self.context, deployable_id, attach_info)
mock_attach_handle_list.return_value = [self.fake_attach_handle] mock_list.assert_called_once_with(self.context, ah_filter)
attach_handles = objects.AttachHandle.get_ah_by_depid_attachinfo( self.assertEqual(attach_info, attach_handles['attach_info'])
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'])
# test objects.AttachHandle.list() return [] # test objects.AttachHandle.list() return []
mock_attach_handle_list.return_value = [] mock_list.return_value = []
attach_handle = objects.AttachHandle.get_ah_by_depid_attachinfo( attach_handle = objects.AttachHandle.get_ah_by_depid_attachinfo(
self.context, deployable_id, attach_info) self.context, deployable_id, attach_info)
self.assertIsNone(attach_handle) self.assertIsNone(attach_handle)
def test_list(self): def test_list(self):
with mock.patch.object(self.dbapi, 'attach_handle_list', with mock.patch.object(self.dbapi, 'attach_handle_list',

View File

@ -12,8 +12,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from unittest import mock
import mock
from cyborg import objects from cyborg import objects
from cyborg.tests.unit.db import base from cyborg.tests.unit.db import base

View File

@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import mock from unittest import mock
from oslo_db import exception as db_exc from oslo_db import exception as db_exc

View File

@ -12,8 +12,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from unittest import mock
import mock
from cyborg import objects from cyborg import objects
from cyborg.tests.unit.db import base 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. # 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'] hostname = self.fake_device['hostname']
dev_filter = {'hostname': hostname} dev_filter = {'hostname': hostname}
with mock.patch.object(objects.Device, 'list', mock_list.return_value = [self.fake_device]
autospec=True) as mock_device_list: devices = objects.Device.get_list_by_hostname(self.context, hostname)
mock_device_list.return_value = [self.fake_device] mock_list.assert_called_once_with(self.context, dev_filter)
devices = objects.Device.get_list_by_hostname( self.assertEqual(hostname, devices[0]['hostname'])
self.context, hostname)
mock_device_list.assert_called_once_with(
self.context, dev_filter)
self.assertEqual(
hostname,
devices[0]['hostname'])
with mock.patch.object(objects.Device, 'list', # test objects.Device.list return [] when hostname is None.
autospec=True) as mock_device_list: mock_list.return_value = []
# test objects.Device.list return [] when hostname is None. hostname = None
mock_device_list.return_value = [] dev_filter = {'hostname': hostname}
hostname = None devices = objects.Device.get_list_by_hostname(self.context, hostname)
dev_filter = {'hostname': hostname} mock_list.assert_called_with(self.context, dev_filter)
devices = objects.Device.get_list_by_hostname( self.assertEqual([], devices)
self.context, hostname)
mock_device_list.assert_called_once_with(
self.context, dev_filter)
self.assertEqual([], devices)
def test_list(self): def test_list(self):
with mock.patch.object(self.dbapi, 'device_list', with mock.patch.object(self.dbapi, 'device_list',

View File

@ -12,8 +12,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from unittest import mock
import mock
from cyborg import objects from cyborg import objects
from cyborg.tests.unit.db import base from cyborg.tests.unit.db import base

View File

@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import mock from unittest import mock
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
from oslo_utils import timeutils from oslo_utils import timeutils

View File

@ -13,8 +13,8 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import mock
import six import six
from unittest import mock
from testtools.matchers import HasLength from testtools.matchers import HasLength

View File

@ -15,10 +15,10 @@
from io import BytesIO from io import BytesIO
import json import json
import mock
import requests import requests
from requests import structures from requests import structures
from requests import utils from requests import utils
from unittest import mock
from testtools.matchers import HasLength from testtools.matchers import HasLength

View File

@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import mock from unittest import mock
from keystoneauth1 import exceptions as ks_exc from keystoneauth1 import exceptions as ks_exc
from oslo_config import cfg from oslo_config import cfg

View File

@ -13,8 +13,8 @@
# under the License. # under the License.
import textwrap import textwrap
from unittest import mock
import mock
import pycodestyle import pycodestyle
from cyborg.hacking import checks 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 keystonemiddleware>=4.17.0 # Apache-2.0
jsonpatch>=1.16,!=1.20 # BSD jsonpatch>=1.16,!=1.20 # BSD
psutil>=3.2.2 # BSD psutil>=3.2.2 # BSD
mock>=2.0.0 # BSD
python-glanceclient>=2.3.0 # Apache-2.0 python-glanceclient>=2.3.0 # Apache-2.0
oslo.privsep>=1.32.0 # Apache-2.0 oslo.privsep>=1.32.0 # Apache-2.0
cursive>=0.2.1 # 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 bandit>=1.6.0 # Apache-2.0
coverage>=3.6,!=4.4 # Apache-2.0 coverage>=3.6,!=4.4 # Apache-2.0
fixtures>=3.0.0 # Apache-2.0/BSD fixtures>=3.0.0 # Apache-2.0/BSD
mock>=2.0.0 # BSD
ddt>=1.0.1 # MIT ddt>=1.0.1 # MIT
oslotest>=3.2.0 # Apache-2.0 oslotest>=3.2.0 # Apache-2.0
stestr>=2.0.0 # Apache-2.0/BSD stestr>=2.0.0 # Apache-2.0/BSD