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.

The remainder was auto-generated with the following (hacky) script, with
one or two manual tweaks after the fact:

  import glob

  for path in glob.glob('watcher/tests/**/*.py', recursive=True):
      with open(path) as fh:
          lines = fh.readlines()
      if 'import mock\n' not in lines:
          continue
      import_group_found = False
      create_first_party_group = False
      for num, line in enumerate(lines):
          line = line.strip()
          if line.startswith('import ') or line.startswith('from '):
              tokens = line.split()
              for lib in (
                  'ddt', 'six', 'webob', 'fixtures', 'testtools'
                  'neutron', 'cinder', 'ironic', 'keystone', 'oslo',
              ):
                  if lib in tokens[1]:
                      create_first_party_group = True
                      break
              if create_first_party_group:
                  break
              import_group_found = True
          if not import_group_found:
              continue
          if line.startswith('import ') or line.startswith('from '):
              tokens = line.split()
              if tokens[1] > 'unittest':
                  break
              elif tokens[1] == 'unittest' and (
                  len(tokens) == 2 or tokens[4] > 'mock'
              ):
                  break
          elif not line:
              break
      if create_first_party_group:
          lines.insert(num, 'from unittest import mock\n\n')
      else:
          lines.insert(num, 'from unittest import mock\n')
      del lines[lines.index('import mock\n')]
      with open(path, 'w+') as fh:
          fh.writelines(lines)

Co-Authored-By: Sean McGinnis <sean.mcginnis@gmail.com>

Change-Id: Icf35d3a6c10c529e07d1a4edaa36f504e5bf553a
This commit is contained in:
zhangbailin 2020-04-26 17:03:13 +08:00
parent 6ff95efaf6
commit 8a36ad5f87
110 changed files with 122 additions and 110 deletions

View File

@ -6,7 +6,6 @@ coverage>=4.5.1 # Apache-2.0
doc8>=0.8.0 # Apache-2.0
freezegun>=0.3.10 # Apache-2.0
hacking>=3.0.1,<3.1.0 # Apache-2.0
mock>=2.0.0 # BSD
oslotest>=3.3.0 # Apache-2.0
os-testr>=1.0.0 # Apache-2.0
testscenarios>=0.5.0 # Apache-2.0/BSD

View File

@ -23,7 +23,7 @@
# NOTE(deva): import auth_token so we can override a config option
import copy
import mock
from unittest import mock
from urllib import parse as urlparse
from oslo_config import cfg

View File

@ -14,7 +14,8 @@
"""Tests for the Pecan API hooks."""
import mock
from unittest import mock
from oslo_config import cfg
import oslo_messaging as messaging
from oslo_serialization import jsonutils

View File

@ -15,7 +15,7 @@
from apscheduler.schedulers import background
import datetime
import freezegun
import mock
from unittest import mock
from watcher.api import scheduling
from watcher.notifications import service

View File

@ -12,7 +12,7 @@
import datetime
import itertools
import mock
from unittest import mock
from oslo_config import cfg
from oslo_serialization import jsonutils

View File

@ -12,7 +12,7 @@
import datetime
import itertools
import mock
from unittest import mock
from oslo_config import cfg
from oslo_serialization import jsonutils

View File

@ -12,7 +12,7 @@
import datetime
import itertools
import mock
from unittest import mock
from urllib import parse as urlparse
from webtest.app import AppError

View File

@ -13,7 +13,7 @@
import datetime
from dateutil import tz
import itertools
import mock
from unittest import mock
from urllib import parse as urlparse
from oslo_config import cfg

View File

@ -13,7 +13,7 @@
# See the 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

@ -10,7 +10,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from urllib import parse as urlparse
from oslo_config import cfg

View File

@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from unittest import mock
import webtest
import wsme
from wsme import types as wtypes

View File

@ -10,7 +10,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.decision_engine import rpcapi as deapi
from watcher import objects

View File

@ -15,7 +15,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.applier.action_plan import default
from watcher.applier import default as ap_applier

View File

@ -14,7 +14,7 @@
# limitations under the License.
import jsonschema
import mock
from unittest import mock
from watcher.applier.actions import base as baction
from watcher.applier.actions import change_node_power_state

View File

@ -14,7 +14,7 @@
# limitations under the License.
import jsonschema
import mock
from unittest import mock
from watcher.applier.actions import base as baction
from watcher.applier.actions import change_nova_service_state

View File

@ -14,7 +14,7 @@
# limitations under the License.
import jsonschema
import mock
from unittest import mock
from watcher.applier.actions import base as baction
from watcher.applier.actions import migration

View File

@ -14,7 +14,7 @@
# limitations under the License.
import jsonschema
import mock
from unittest import mock
from watcher.applier.actions import base as baction
from watcher.applier.actions import resize

View File

@ -15,7 +15,7 @@
import jsonschema
import mock
from unittest import mock
from watcher.applier.actions import sleep
from watcher.tests import base

View File

@ -13,7 +13,7 @@
# limitations under the License.
import jsonschema
import mock
from unittest import mock
from watcher.applier.actions import base as baction
from watcher.applier.actions import volume_migration

View File

@ -18,7 +18,7 @@
#
import mock
from unittest import mock
from watcher.applier.messaging import trigger
from watcher.common import utils

View File

@ -16,7 +16,7 @@
# limitations under the License.
#
import mock
from unittest import mock
import oslo_messaging as om
from watcher.applier import manager as applier_manager

View File

@ -17,7 +17,8 @@
# limitations under the License.
#
import mock
from unittest import mock
import oslo_messaging as om
from watcher.applier import rpcapi

View File

@ -16,7 +16,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from oslo_config import cfg
from oslo_utils import uuidutils

View File

@ -17,7 +17,7 @@
# limitations under the License.
#
import abc
import mock
from unittest import mock
from watcher.applier.actions import base as abase
from watcher.applier.actions import factory

View File

@ -17,7 +17,7 @@
# limitations under the License.
#
import eventlet
import mock
from unittest import mock
from watcher.applier.workflow_engine import default as tflow
from watcher.common import clients

View File

@ -15,8 +15,8 @@
import copy
import os
from unittest import mock
import mock
from oslo_config import cfg
from oslo_log import log
from oslo_messaging import conffixture

View File

@ -15,8 +15,8 @@
# limitations under the License.
import types
from unittest import mock
import mock
from oslo_config import cfg
from oslo_service import wsgi
from pecan.testing import load_test_app

View File

@ -15,8 +15,8 @@
# limitations under the License.
import types
from unittest import mock
import mock
from oslo_config import cfg
from oslo_service import service
from watcher.applier import sync

View File

@ -15,8 +15,8 @@
# limitations under the License.
import sys
from unittest import mock
import mock
from oslo_config import cfg
from watcher.cmd import dbmanage

View File

@ -15,8 +15,8 @@
# limitations under the License.
import types
from unittest import mock
import mock
from oslo_config import cfg
from oslo_service import service

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from oslo_config import cfg
from stevedore import driver as drivermanager

View File

@ -13,8 +13,8 @@
# limitations under the License.
#
import mock
import time
from unittest import mock
from cinderclient import exceptions as cinder_exception

View File

@ -12,6 +12,8 @@
from ceilometerclient import client as ceclient
import ceilometerclient.v2.client as ceclient_v2
from unittest import mock
from cinderclient import client as ciclient
from cinderclient.v3 import client as ciclient_v3
from glanceclient import client as glclient
@ -21,7 +23,6 @@ from ironicclient import client as irclient
from ironicclient.v1 import client as irclient_v1
from keystoneauth1 import adapter as ka_adapter
from keystoneauth1 import loading as ka_loading
import mock
from monascaclient import client as monclient
from monascaclient.v2_0 import client as monclient_v2
from neutronclient.neutron import client as netclient

View File

@ -17,7 +17,7 @@
# limitations under the License.
#
import mock
from unittest import mock
from watcher.common import clients
from watcher.common import exception

View File

@ -18,10 +18,10 @@
#
import time
from unittest import mock
from novaclient import api_versions
import mock
import glanceclient.exc as glexceptions
import novaclient.exceptions as nvexceptions

View File

@ -11,7 +11,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.common import placement_helper
from watcher.tests import base

View File

@ -15,7 +15,7 @@
# limitations under the License.
import mock
from unittest import mock
from oslo_config import cfg

View File

@ -15,7 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from oslo_config import cfg
from stevedore import extension

View File

@ -14,10 +14,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest import mock
from oslo_utils import uuidutils
import freezegun
import mock
from watcher.common import context as watcher_context
from watcher.common import utils

View File

@ -15,8 +15,8 @@
# limitations under the License.
import datetime
from unittest import mock
import mock
from oslo_config import cfg
from oslo_utils import uuidutils

View File

@ -11,7 +11,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.common import cinder_helper
from watcher.common import exception

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.decision_engine.model.collector import base
from watcher.decision_engine.model import model_root

View File

@ -16,8 +16,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
import os_resource_classes as orc
from unittest import mock
from watcher.common import nova_helper
from watcher.common import placement_helper

View File

@ -16,7 +16,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from oslo_config import cfg
from oslo_log import log

View File

@ -17,7 +17,7 @@
# limitations under the License.
import copy
import mock
from unittest import mock
from oslo_config import cfg
from oslo_log import log

View File

@ -16,7 +16,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from oslo_config import cfg
from watcher.decision_engine.datasources import base as datasource

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.common import clients
from watcher.common import exception

View File

@ -14,7 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from oslo_config import cfg
from watcher.common import clients

View File

@ -16,7 +16,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from oslo_config import cfg
from oslo_log import log

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from mock import MagicMock

View File

@ -14,7 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from oslo_config import cfg
from watcher.common import clients

View File

@ -14,9 +14,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from stevedore import driver as drivermanager
from stevedore import extension as stevedore_extension
from unittest import mock
from watcher.common import clients
from watcher.common import exception

View File

@ -14,8 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from stevedore import extension
from unittest import mock
from watcher.common import exception
from watcher.decision_engine.loading import default as default_loading

View File

@ -14,8 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from stevedore import extension
from unittest import mock
from watcher.common import exception
from watcher.decision_engine.goal import goals

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.decision_engine.audit import continuous as continuous_handler
from watcher.decision_engine.audit import oneshot as oneshot_handler

View File

@ -13,8 +13,9 @@
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
import unittest
from unittest import mock
from watcher.common import exception
from watcher.common import utils

View File

@ -18,8 +18,8 @@
# limitations under the License.
import os
from unittest import mock
import mock
from watcher.decision_engine.model.collector import base
from watcher.decision_engine.model import model_root as modelroot

View File

@ -17,8 +17,8 @@
# limitations under the License.
import os
from unittest import mock
import mock
from watcher.common import utils
from watcher.decision_engine.model.collector import base

View File

@ -16,8 +16,8 @@
import datetime
import os
from unittest import mock
import mock
from oslo_serialization import jsonutils
from watcher.common import cinder_helper

View File

@ -17,8 +17,8 @@
# limitations under the License.
import os
from unittest import mock
import mock
from oslo_serialization import jsonutils
from watcher.common import context

View File

@ -18,8 +18,8 @@
import os
import os_resource_classes as orc
from unittest import mock
import mock
from oslo_serialization import jsonutils
from watcher.common import context

View File

@ -16,8 +16,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
import os
from unittest import mock
from oslo_utils import uuidutils

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.common import exception
from watcher.common import utils

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.common import nova_helper
from watcher.common import utils

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.common import exception
from watcher.common import nova_helper

View File

@ -15,7 +15,7 @@
# limitations under the License.
#
import mock
from unittest import mock
from watcher.decision_engine.scope import baremetal
from watcher.tests import base

View File

@ -16,7 +16,7 @@
#
from jsonschema import validators
import mock
from unittest import mock
from watcher.api.controllers.v1 import audit_template
from watcher.common import exception

View File

@ -15,7 +15,7 @@
# limitations under the License.
#
import mock
from unittest import mock
from watcher.common import cinder_helper
from watcher.common import exception

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.decision_engine.solution import default
from watcher.decision_engine.strategy import strategies

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.common import utils
from watcher.decision_engine.model.collector import manager

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.common import exception
from watcher.decision_engine.loading import default as default_loader

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.common import utils
from watcher.decision_engine.strategy import strategies

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.common import exception
from watcher.decision_engine.datasources import manager

View File

@ -18,7 +18,7 @@
#
import collections
import copy
import mock
from unittest import mock
from watcher.applier.loading import default
from watcher.common import clients

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.applier.loading import default
from watcher.common import utils

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.applier.loading import default
from watcher.common import utils

View File

@ -17,7 +17,7 @@
# limitations under the License.
#
import mock
from unittest import mock
from watcher.common import exception
from watcher.decision_engine.model import element

View File

@ -15,7 +15,7 @@
# limitations under the License.
#
import mock
from unittest import mock
from watcher.common import exception
from watcher.decision_engine.model import element

View File

@ -17,7 +17,7 @@
# limitations under the License.
import collections
import mock
from unittest import mock
from watcher.applier.loading import default
from watcher.common import utils

View File

@ -17,7 +17,7 @@
# limitations under the License.
#
import collections
import mock
from unittest import mock
from watcher.applier.loading import default
from watcher.common import utils

View File

@ -15,7 +15,7 @@
# limitations under the License.
#
import mock
from unittest import mock
from watcher.common import clients
from watcher.common import utils

View File

@ -17,7 +17,7 @@
# limitations under the License.
#
import mock
from unittest import mock
from watcher.common import cinder_helper

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.decision_engine.strategy.strategies import base as strategy_base
from watcher.tests import base

View File

@ -17,7 +17,7 @@
# limitations under the License.
#
import collections
import mock
from unittest import mock
from watcher.applier.loading import default
from watcher.common import utils

View File

@ -18,7 +18,7 @@
# limitations under the License.
#
import mock
from unittest import mock
from watcher.decision_engine.model import element
from watcher.decision_engine.solution.base import BaseSolution

View File

@ -17,7 +17,7 @@
# limitations under the License.
#
import collections
import mock
from unittest import mock
from watcher.applier.loading import default
from watcher.common import utils

View File

@ -17,7 +17,7 @@
# limitations under the License.
#
import mock
from unittest import mock
from watcher.common import clients
from watcher.common import utils

View File

@ -12,7 +12,7 @@
# limitations under the License.
import collections
import mock
from unittest import mock
import cinderclient
import novaclient

View File

@ -16,7 +16,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
from watcher.decision_engine import gmr
from watcher.decision_engine.model.collector import manager

View File

@ -14,7 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from unittest import mock
import oslo_messaging as om
from watcher.common import exception
from watcher.common import utils

View File

@ -19,7 +19,7 @@
from apscheduler.schedulers import background
from apscheduler.triggers import interval as interval_trigger
import eventlet
import mock
from unittest import mock
from oslo_config import cfg
from oslo_utils import uuidutils

View File

@ -14,7 +14,7 @@
# See the 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

@ -10,8 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
import requests
from unittest import mock
fakeAuthTokenHeaders = {'X-User-Id': u'773a902f022949619b5c2f32cd89d419',
'X-Roles': u'admin, ResellerAdmin, _member_',

View File

@ -13,7 +13,8 @@
# under the License.
import freezegun
import mock
from unittest import mock
import oslo_messaging as om
from watcher.common import exception

View File

@ -13,7 +13,8 @@
# under the License.
import freezegun
import mock
from unittest import mock
import oslo_messaging as om
from watcher.common import exception

View File

@ -13,7 +13,8 @@
# under the License.
import freezegun
import mock
from unittest import mock
import oslo_messaging as om
from watcher.common import exception

View File

@ -13,8 +13,8 @@
# under the License.
import collections
from unittest import mock
import mock
from oslo_versionedobjects import fixture
from watcher.common import exception

View File

@ -15,9 +15,9 @@
# limitations under the License.
import datetime
from unittest import mock
import freezegun
import mock
import oslo_messaging as om
from watcher.common import rpc

View File

@ -14,9 +14,9 @@
# under the License.
import datetime
from unittest import mock
import iso8601
import mock
from watcher.common import exception
from watcher.common import utils as c_utils

View File

@ -15,9 +15,9 @@
# under the License.
import datetime
from unittest import mock
import iso8601
import mock
from watcher.db.sqlalchemy import api as db_api
from watcher import objects

Some files were not shown because too many files have changed in this diff Show More