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

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