Skip to content

Commit

Permalink
Use unittest.mock instead of third party mock
Browse files Browse the repository at this point in the history
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 is one manual change
necessary:

nova/tests/functional/regressions/test_bug_1781286.py
  We need to avoid using 'fixtures.MockPatch' since fixtures is using
  'mock' (the library) under the hood and a call to 'mock.patch.stop'
  found in that test will now "stop" mocks from the wrong library. We
  have discussed making this configurable but the option proposed isn't
  that pretty [1] so this is better.

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('nova/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)

Note that we cannot remove mock from our requirements files yet due to
importing pypowervm unit test code in nova unit tests. This library
still uses the mock lib, and since we are importing test code and that
lib (correctly) only declares mock in its test-requirements.txt, mock
would not otherwise be installed and would cause errors while loading
nova unit test code.

[1] testing-cabal/fixtures#49

Change-Id: Id5b04cf2f6ca24af8e366d23f15cf0e5cac8e1cc
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
  • Loading branch information
stephenfin authored and AnishReddyRavula committed Jan 17, 2024
1 parent 6d2c221 commit e61acb2
Show file tree
Hide file tree
Showing 414 changed files with 684 additions and 434 deletions.
2 changes: 1 addition & 1 deletion nova/test.py
Expand Up @@ -35,9 +35,9 @@
import os.path
import pprint
import sys
from unittest import mock

import fixtures
import mock
from oslo_cache import core as cache
from oslo_concurrency import lockutils
from oslo_config import cfg
Expand Down
2 changes: 1 addition & 1 deletion nova/tests/fixtures/libvirt.py
Expand Up @@ -18,10 +18,10 @@
import textwrap
import time
import typing as ty
from unittest import mock

import fixtures
from lxml import etree
import mock
from oslo_log import log as logging
from oslo_utils.fixture import uuidsentinel as uuids
from oslo_utils import versionutils
Expand Down
2 changes: 1 addition & 1 deletion nova/tests/fixtures/libvirt_imagebackend.py
Expand Up @@ -16,9 +16,9 @@
import collections
import functools
import os
from unittest import mock

import fixtures
import mock

from nova.virt.libvirt import config
from nova.virt.libvirt import driver
Expand Down
13 changes: 11 additions & 2 deletions nova/tests/fixtures/nova.py
Expand Up @@ -22,12 +22,14 @@
import functools
import logging as std_logging
import os
import time
from unittest import mock
import warnings

import eventlet
import fixtures
import futurist
import mock
import mock as mock_the_lib
from openstack import service_description
from oslo_concurrency import lockutils
from oslo_config import cfg
Expand Down Expand Up @@ -1587,7 +1589,14 @@ def fail(*a, **k):
current = __import__(components[0], {}, {})
for component in components[1:]:
current = getattr(current, component)
if not isinstance(getattr(current, attribute), mock.Mock):

# TODO(stephenfin): Remove mock_the_lib check once pypowervm is
# no longer using mock and we no longer have mock in
# requirements
if not isinstance(
getattr(current, attribute),
(mock.Mock, mock_the_lib.Mock),
):
self.useFixture(fixtures.MonkeyPatch(
meth, poison_configure(meth, why)))
except ImportError:
Expand Down
3 changes: 2 additions & 1 deletion nova/tests/fixtures/os_brick.py
Expand Up @@ -10,8 +10,9 @@
# License for the specific language governing permissions and limitations
# under the License.

from unittest import mock

import fixtures
import mock

from os_brick.initiator import connector as brick_connector

Expand Down
Expand Up @@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock
from unittest import mock

from nova.tests.functional.api_sample_tests import api_sample_base

Expand Down
Expand Up @@ -13,8 +13,8 @@
# under the License.

import copy
from unittest import mock

import mock
import testtools

from nova import test
Expand Down
Expand Up @@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock
from unittest import mock

from nova.tests import fixtures
from nova.tests.functional.api_sample_tests import test_servers
Expand Down
2 changes: 1 addition & 1 deletion nova/tests/functional/api_sample_tests/test_evacuate.py
Expand Up @@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock
from unittest import mock

from nova import objects
from nova.tests.functional.api_sample_tests import test_servers
Expand Down
2 changes: 1 addition & 1 deletion nova/tests/functional/api_sample_tests/test_hypervisors.py
Expand Up @@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock
from unittest import mock

from nova.tests.functional.api_sample_tests import api_sample_base

Expand Down
Expand Up @@ -13,7 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock
from unittest import mock

from oslo_utils import versionutils

from nova import exception
Expand Down
2 changes: 1 addition & 1 deletion nova/tests/functional/api_sample_tests/test_networks.py
Expand Up @@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock
from unittest import mock

from nova import exception
from nova.tests import fixtures as nova_fixtures
Expand Down
Expand Up @@ -14,9 +14,9 @@
# under the License.

import datetime
from unittest import mock

import futurist
import mock

from nova.conductor import manager as conductor_manager
from nova import context
Expand Down
Expand Up @@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock
from unittest import mock

from nova.tests.functional.api_sample_tests import test_servers

Expand Down
Expand Up @@ -13,9 +13,9 @@
# under the License.

import datetime
from unittest import mock
from urllib import parse

import mock
from oslo_utils import timeutils

from nova.tests.functional.api_sample_tests import test_servers
Expand Down
2 changes: 1 addition & 1 deletion nova/tests/functional/compute/test_init_host.py
Expand Up @@ -10,8 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock
import time
from unittest import mock

from nova import context as nova_context
from nova import objects
Expand Down
3 changes: 2 additions & 1 deletion nova/tests/functional/compute/test_live_migration.py
Expand Up @@ -12,7 +12,8 @@
# 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.fixture import uuidsentinel as uuids

Expand Down
2 changes: 1 addition & 1 deletion nova/tests/functional/compute/test_resource_tracker.py
Expand Up @@ -12,9 +12,9 @@

import copy
import os
from unittest import mock

import fixtures
import mock
import os_resource_classes as orc
import os_traits
from oslo_utils.fixture import uuidsentinel as uuids
Expand Down
2 changes: 1 addition & 1 deletion nova/tests/functional/db/test_aggregate.py
Expand Up @@ -11,8 +11,8 @@
# under the License.

from copy import deepcopy
from unittest import mock

import mock
from oslo_db import exception as db_exc
from oslo_utils.fixture import uuidsentinel
from oslo_utils import timeutils
Expand Down
3 changes: 2 additions & 1 deletion nova/tests/functional/db/test_compute_api.py
Expand Up @@ -10,7 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock
from unittest import mock

from oslo_utils.fixture import uuidsentinel as uuids

from nova.compute import api as compute_api
Expand Down
3 changes: 2 additions & 1 deletion nova/tests/functional/db/test_host_mapping.py
Expand Up @@ -10,7 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock
from unittest import mock

from oslo_utils.fixture import uuidsentinel as uuids
from oslo_utils import uuidutils

Expand Down
3 changes: 2 additions & 1 deletion nova/tests/functional/db/test_instance_group.py
Expand Up @@ -10,7 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock
from unittest import mock

from oslo_utils.fixture import uuidsentinel as uuids
from oslo_versionedobjects import fixture as ovo_fixture

Expand Down
3 changes: 2 additions & 1 deletion nova/tests/functional/db/test_instance_mapping.py
Expand Up @@ -10,7 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock
from unittest import mock

from oslo_utils.fixture import uuidsentinel
from oslo_utils import uuidutils

Expand Down
3 changes: 2 additions & 1 deletion nova/tests/functional/db/test_quota.py
Expand Up @@ -10,8 +10,9 @@
# License for the specific language governing permissions and limitations
# under the License.

from unittest import mock

import ddt
import mock
from oslo_utils import uuidutils

from nova import context
Expand Down
3 changes: 2 additions & 1 deletion nova/tests/functional/db/test_virtual_interface.py
Expand Up @@ -11,7 +11,8 @@
# under the License.

import datetime
import mock
from unittest import mock

from oslo_config import cfg
from oslo_utils import timeutils

Expand Down
2 changes: 1 addition & 1 deletion nova/tests/functional/libvirt/base.py
Expand Up @@ -15,9 +15,9 @@

import copy
import io
from unittest import mock

import fixtures
import mock

from nova import conf
from nova.tests import fixtures as nova_fixtures
Expand Down
4 changes: 2 additions & 2 deletions nova/tests/functional/libvirt/test_evacuate.py
Expand Up @@ -13,10 +13,10 @@
# under the License.

import collections
import fixtures
import mock
import os.path
from unittest import mock

import fixtures
from oslo_utils import fileutils
from oslo_utils.fixture import uuidsentinel as uuids
from oslo_utils import units
Expand Down
4 changes: 2 additions & 2 deletions nova/tests/functional/libvirt/test_numa_servers.py
Expand Up @@ -13,11 +13,11 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock
import testtools
from unittest import mock

from oslo_config import cfg
from oslo_log import log as logging
import testtools

import nova
from nova.compute import manager
Expand Down
2 changes: 1 addition & 1 deletion nova/tests/functional/libvirt/test_pci_sriov_servers.py
Expand Up @@ -14,12 +14,12 @@
# under the License.

import copy
from unittest import mock
from urllib import parse as urlparse

import ddt
import fixtures
from lxml import etree
import mock
from oslo_config import cfg
from oslo_log import log as logging
from oslo_serialization import jsonutils
Expand Down
4 changes: 2 additions & 2 deletions nova/tests/functional/libvirt/test_report_cpu_traits.py
Expand Up @@ -13,11 +13,11 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock
from unittest import mock

import os_resource_classes as orc
import os_traits as ost


from nova import conf
from nova.db import constants as db_const
from nova import test
Expand Down
2 changes: 1 addition & 1 deletion nova/tests/functional/libvirt/test_reshape.py
Expand Up @@ -12,7 +12,7 @@
# under the License.

import io
import mock
from unittest import mock

from oslo_config import cfg
from oslo_log import log as logging
Expand Down
2 changes: 1 addition & 1 deletion nova/tests/functional/libvirt/test_vtpm.py
Expand Up @@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock
from unittest import mock

from castellan.common.objects import passphrase
from castellan.key_manager import key_manager
Expand Down
Expand Up @@ -12,9 +12,9 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock
import os
import time
from unittest import mock

from oslo_config import cfg
from oslo_serialization import jsonutils
Expand Down
Expand Up @@ -11,8 +11,8 @@
# under the License.

import time
from unittest import mock

import mock

from nova import exception
from nova.tests import fixtures
Expand Down
Expand Up @@ -12,8 +12,9 @@
# License for the specific language governing permissions and limitations
# under the License.

from unittest import mock

import fixtures
import mock

import nova.conf
from nova import exception
Expand Down

0 comments on commit e61acb2

Please sign in to comment.