diff options
author | Stephen Finucane <stephenfin@redhat.com> | 2020-03-24 15:12:07 +0000 |
---|---|---|
committer | Balazs Gibizer <gibi@redhat.com> | 2022-08-01 17:46:26 +0200 |
commit | 89ef050b8c049b9a6f0e2c70408fc93c826c55e0 (patch) | |
tree | 1e6d7dd62c89e73a57da7e8df2e3f94a390632fa /nova/tests/unit/db/api | |
parent | 1a32196074a14788aed45c7a53646a74628ef978 (diff) | |
download | nova-89ef050b8c049b9a6f0e2c70408fc93c826c55e0.tar.gz |
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 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] https://github.com/testing-cabal/fixtures/pull/49
Change-Id: Id5b04cf2f6ca24af8e366d23f15cf0e5cac8e1cc
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
Diffstat (limited to 'nova/tests/unit/db/api')
-rw-r--r-- | nova/tests/unit/db/api/test_api.py | 2 | ||||
-rw-r--r-- | nova/tests/unit/db/api/test_migrations.py | 3 |
2 files changed, 3 insertions, 2 deletions
diff --git a/nova/tests/unit/db/api/test_api.py b/nova/tests/unit/db/api/test_api.py index 251407612f..6113791a8e 100644 --- a/nova/tests/unit/db/api/test_api.py +++ b/nova/tests/unit/db/api/test_api.py @@ -10,7 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. -import mock +from unittest import mock from nova.db.api import api as db_api from nova import test diff --git a/nova/tests/unit/db/api/test_migrations.py b/nova/tests/unit/db/api/test_migrations.py index 1b14d569db..3b9b17aab2 100644 --- a/nova/tests/unit/db/api/test_migrations.py +++ b/nova/tests/unit/db/api/test_migrations.py @@ -21,10 +21,11 @@ test will then use that DB and username/password combo to run the tests. Refer to the 'tools/test-setup.sh' for an example of how to configure this. """ +from unittest import mock + from alembic import command as alembic_api from alembic import script as alembic_script from migrate.versioning import api as migrate_api -import mock from oslo_db.sqlalchemy import enginefacade from oslo_db.sqlalchemy import test_fixtures from oslo_db.sqlalchemy import test_migrations |