From 74895a5cbe34a151cb349637e41d980c1cd2b660 Mon Sep 17 00:00:00 2001 From: Sean McGinnis Date: Sat, 18 Apr 2020 12:01:20 -0500 Subject: [PATCH] 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. Change-Id: I44ac9a021a0bc3249b86f252b53cee3c8059f185 Signed-off-by: Sean McGinnis --- test-requirements.txt | 1 - trove/tests/api/instances_resize.py | 3 +- trove/tests/api/mgmt/instances_actions.py | 2 +- .../unittests/api/common/test_extensions.py | 3 +- .../unittests/backup/test_backupagent.py | 2 +- .../unittests/cluster/test_mongodb_cluster.py | 2 +- .../common/test_conductor_serializer.py | 2 +- .../unittests/common/test_crypto_utils.py | 3 +- trove/tests/unittests/common/test_dbmodels.py | 2 +- .../tests/unittests/common/test_serializer.py | 2 +- .../test_datastore_version_metadata.py | 2 +- trove/tests/unittests/guestagent/test_api.py | 3 +- .../unittests/guestagent/test_backups.py | 206 +----------------- .../guestagent/test_couchbase_manager.py | 2 +- .../guestagent/test_galera_cluster_api.py | 2 +- trove/tests/unittests/hacking/test_check.py | 2 +- trove/tests/unittests/taskmanager/test_api.py | 2 +- .../unittests/taskmanager/test_models.py | 2 +- trove/tests/unittests/trove_testtools.py | 2 +- .../unittests/volume_type/test_volume_type.py | 2 +- .../volume_type/test_volume_type_views.py | 2 +- 21 files changed, 31 insertions(+), 218 deletions(-) diff --git a/test-requirements.txt b/test-requirements.txt index c8dbd865a1..c6afacb2ee 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -12,7 +12,6 @@ WebTest>=2.0.27 # MIT wsgi-intercept>=1.4.1 # MIT License proboscis>=1.2.5.3 # Apache-2.0 python-troveclient>=2.2.0 # Apache-2.0 -mock>=2.0.0 # BSD testtools>=2.2.0 # MIT pymongo!=3.1,>=3.0.2 # Apache-2.0 redis>=2.10.0 # MIT diff --git a/trove/tests/api/instances_resize.py b/trove/tests/api/instances_resize.py index 9c9b4bb9bc..739cc4f4e1 100644 --- a/trove/tests/api/instances_resize.py +++ b/trove/tests/api/instances_resize.py @@ -13,9 +13,10 @@ # License for the specific language governing permissions and limitations # under the License. -import mock from novaclient.exceptions import BadRequest from novaclient.v2.servers import Server +from unittest import mock + from oslo_messaging._drivers.common import RPCException from proboscis import test from testtools import TestCase diff --git a/trove/tests/api/mgmt/instances_actions.py b/trove/tests/api/mgmt/instances_actions.py index db40543321..798d5547c9 100644 --- a/trove/tests/api/mgmt/instances_actions.py +++ b/trove/tests/api/mgmt/instances_actions.py @@ -12,7 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -import mock from novaclient.v2.servers import Server from proboscis import after_class from proboscis.asserts import assert_equal @@ -20,6 +19,7 @@ from proboscis.asserts import assert_raises from proboscis import before_class from proboscis import SkipTest from proboscis import test +from unittest import mock from trove.backup import models as backup_models from trove.backup import state diff --git a/trove/tests/unittests/api/common/test_extensions.py b/trove/tests/unittests/api/common/test_extensions.py index 737f0e1ff9..d034c77301 100644 --- a/trove/tests/unittests/api/common/test_extensions.py +++ b/trove/tests/unittests/api/common/test_extensions.py @@ -13,9 +13,10 @@ # under the License. -import mock import os import pkg_resources +from unittest import mock + from six.moves import configparser as config_parser import trove diff --git a/trove/tests/unittests/backup/test_backupagent.py b/trove/tests/unittests/backup/test_backupagent.py index 3802caca42..53fa1ecce1 100644 --- a/trove/tests/unittests/backup/test_backupagent.py +++ b/trove/tests/unittests/backup/test_backupagent.py @@ -13,8 +13,8 @@ # limitations under the License. import hashlib -import mock import os +from unittest import mock from mock import Mock, MagicMock, patch, ANY, DEFAULT, call from oslo_utils import netutils diff --git a/trove/tests/unittests/cluster/test_mongodb_cluster.py b/trove/tests/unittests/cluster/test_mongodb_cluster.py index 81d509661c..dd58406260 100644 --- a/trove/tests/unittests/cluster/test_mongodb_cluster.py +++ b/trove/tests/unittests/cluster/test_mongodb_cluster.py @@ -13,7 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. -import mock +from unittest import mock import uuid from novaclient import exceptions as nova_exceptions diff --git a/trove/tests/unittests/common/test_conductor_serializer.py b/trove/tests/unittests/common/test_conductor_serializer.py index ae5e5ca0c7..cc4faea15f 100644 --- a/trove/tests/unittests/common/test_conductor_serializer.py +++ b/trove/tests/unittests/common/test_conductor_serializer.py @@ -14,7 +14,7 @@ # under the License. # -import mock +from unittest import mock from trove.common import cfg from trove.common.rpc import conductor_guest_serializer as gsz diff --git a/trove/tests/unittests/common/test_crypto_utils.py b/trove/tests/unittests/common/test_crypto_utils.py index 815a5213d4..2d54092f59 100644 --- a/trove/tests/unittests/common/test_crypto_utils.py +++ b/trove/tests/unittests/common/test_crypto_utils.py @@ -14,8 +14,9 @@ # under the License. # -import mock import os +from unittest import mock + import six from trove.common import crypto_utils diff --git a/trove/tests/unittests/common/test_dbmodels.py b/trove/tests/unittests/common/test_dbmodels.py index 02c46c48fa..7eb9e52d17 100644 --- a/trove/tests/unittests/common/test_dbmodels.py +++ b/trove/tests/unittests/common/test_dbmodels.py @@ -13,7 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. -import mock +from unittest import mock from trove.common.db import models from trove.tests.unittests import trove_testtools diff --git a/trove/tests/unittests/common/test_serializer.py b/trove/tests/unittests/common/test_serializer.py index ab4696b3f7..595be49d7e 100644 --- a/trove/tests/unittests/common/test_serializer.py +++ b/trove/tests/unittests/common/test_serializer.py @@ -14,7 +14,7 @@ # under the License. # -import mock +from unittest import mock from trove.common.rpc import serializer from trove.tests.unittests import trove_testtools diff --git a/trove/tests/unittests/datastore/test_datastore_version_metadata.py b/trove/tests/unittests/datastore/test_datastore_version_metadata.py index 973341b4cd..ad40d63ed2 100644 --- a/trove/tests/unittests/datastore/test_datastore_version_metadata.py +++ b/trove/tests/unittests/datastore/test_datastore_version_metadata.py @@ -12,7 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. -import mock +from unittest import mock from trove.common import clients from trove.common import exception diff --git a/trove/tests/unittests/guestagent/test_api.py b/trove/tests/unittests/guestagent/test_api.py index b03a186c1e..4e2b700915 100644 --- a/trove/tests/unittests/guestagent/test_api.py +++ b/trove/tests/unittests/guestagent/test_api.py @@ -12,7 +12,8 @@ # License for the specific language governing permissions and limitations # under the License. from eventlet import Timeout -import mock +from unittest import mock + import oslo_messaging as messaging from oslo_messaging.rpc.client import RemoteError from testtools.matchers import Is diff --git a/trove/tests/unittests/guestagent/test_backups.py b/trove/tests/unittests/guestagent/test_backups.py index 88daf026d2..d6f63777ab 100644 --- a/trove/tests/unittests/guestagent/test_backups.py +++ b/trove/tests/unittests/guestagent/test_backups.py @@ -12,18 +12,21 @@ # License for the specific language governing permissions and limitations # under the License. -import mock import os -from mock import ANY, call, DEFAULT, Mock, patch, PropertyMock +from unittest import mock +from unittest.mock import ANY +from unittest.mock import call +from unittest.mock import DEFAULT +from unittest.mock import Mock +from unittest.mock import patch + from testtools.testcase import ExpectedException + from trove.common import exception from trove.common import utils from trove.guestagent.common import configuration from trove.guestagent.common.configuration import ImportOverrideStrategy from trove.guestagent.common import operating_system -from trove.guestagent.datastore.experimental.cassandra import ( - service as cass_service -) from trove.guestagent.datastore.experimental.db2 import ( service as db2_service) from trove.guestagent.datastore.experimental.redis.service import RedisApp @@ -555,199 +558,6 @@ class GuestAgentBackupTest(trove_testtools.TestCase): restr.restore_cmd) -class CassandraBackupTest(trove_testtools.TestCase): - - _BASE_BACKUP_CMD = ('sudo tar --transform="s#snapshots/%s/##" -cpPf - ' - '-C "%s" "%s"') - _BASE_RESTORE_CMD = 'sudo tar -xpPf - -C "%(restore_location)s"' - _DATA_DIR = 'data_dir' - _SNAPSHOT_NAME = 'snapshot_name' - _SNAPSHOT_FILES = {'foo.db', 'bar.db'} - _RESTORE_LOCATION = {'restore_location': '/var/lib/cassandra'} - - def setUp(self): - super(CassandraBackupTest, self).setUp() - self.app_status_patcher = patch( - 'trove.guestagent.datastore.experimental.cassandra.service.' - 'CassandraAppStatus') - self.addCleanup(self.app_status_patcher.stop) - self.app_status_patcher.start() - self.get_data_dirs_patcher = patch.object( - cass_service.CassandraApp, 'cassandra_data_dir', - new_callable=PropertyMock) - self.addCleanup(self.get_data_dirs_patcher.stop) - data_dir_mock = self.get_data_dirs_patcher.start() - data_dir_mock.return_value = self._DATA_DIR - self.os_list_patcher = patch.object( - operating_system, 'list_files_in_directory', - return_value=self._SNAPSHOT_FILES) - self.addCleanup(self.os_list_patcher.stop) - self.os_list_patcher.start() - - def tearDown(self): - super(CassandraBackupTest, self).tearDown() - - @patch('trove.guestagent.datastore.experimental.cassandra.service.LOG') - def test_backup_encrypted_zipped_nodetoolsnapshot_command(self, _): - bkp = self._build_backup_runner(True, True) - bkp._run_pre_backup() - self.assertIsNotNone(bkp) - self.assertEqual(self._BASE_BACKUP_CMD % ( - self._SNAPSHOT_NAME, - self._DATA_DIR, - '" "'.join(self._SNAPSHOT_FILES) - ) + PIPE + ZIP + PIPE + ENCRYPT, bkp.command) - self.assertIn(".gz.enc", bkp.manifest) - - @patch('trove.guestagent.datastore.experimental.cassandra.service.LOG') - def test_backup_not_encrypted_not_zipped_nodetoolsnapshot_command(self, _): - bkp = self._build_backup_runner(False, False) - bkp._run_pre_backup() - self.assertIsNotNone(bkp) - self.assertEqual(self._BASE_BACKUP_CMD % ( - self._SNAPSHOT_NAME, - self._DATA_DIR, - '" "'.join(self._SNAPSHOT_FILES) - ), bkp.command) - self.assertNotIn(".gz.enc", bkp.manifest) - - @patch('trove.guestagent.datastore.experimental.cassandra.service.LOG') - def test_backup_not_encrypted_but_zipped_nodetoolsnapshot_command(self, _): - bkp = self._build_backup_runner(False, True) - bkp._run_pre_backup() - self.assertIsNotNone(bkp) - self.assertEqual(self._BASE_BACKUP_CMD % ( - self._SNAPSHOT_NAME, - self._DATA_DIR, - '" "'.join(self._SNAPSHOT_FILES) - ) + PIPE + ZIP, bkp.command) - self.assertIn(".gz", bkp.manifest) - self.assertNotIn(".enc", bkp.manifest) - - @patch('trove.guestagent.datastore.experimental.cassandra.service.LOG') - def test_backup_encrypted_but_not_zipped_nodetoolsnapshot_command(self, _): - bkp = self._build_backup_runner(True, False) - bkp._run_pre_backup() - self.assertIsNotNone(bkp) - self.assertEqual(self._BASE_BACKUP_CMD % ( - self._SNAPSHOT_NAME, - self._DATA_DIR, - '" "'.join(self._SNAPSHOT_FILES) - ) + PIPE + ENCRYPT, bkp.command) - self.assertIn(".enc", bkp.manifest) - self.assertNotIn(".gz", bkp.manifest) - - @mock.patch.object(ImportOverrideStrategy, '_initialize_import_directory') - @patch('trove.guestagent.datastore.experimental.cassandra.service.LOG') - def test_restore_encrypted_but_not_zipped_nodetoolsnapshot_command( - self, mock_logging, _): - restoreBase.RestoreRunner.is_zipped = False - restoreBase.RestoreRunner.is_encrypted = True - restoreBase.RestoreRunner.decrypt_key = CRYPTO_KEY - RunnerClass = utils.import_class(RESTORE_NODETOOLSNAPSHOT_CLS) - rstr = RunnerClass(None, restore_location=self._RESTORE_LOCATION, - location="filename", checksum="md5") - self.assertIsNotNone(rstr) - self.assertEqual(self._BASE_RESTORE_CMD % self._RESTORE_LOCATION, - rstr.base_restore_cmd % self._RESTORE_LOCATION) - - @mock.patch.object(ImportOverrideStrategy, '_initialize_import_directory') - def _build_backup_runner(self, is_encrypted, is_zipped, _): - backupBase.BackupRunner.is_zipped = is_zipped - backupBase.BackupRunner.is_encrypted = is_encrypted - backupBase.BackupRunner.encrypt_key = CRYPTO_KEY - RunnerClass = utils.import_class(BACKUP_NODETOOLSNAPSHOT_CLS) - runner = RunnerClass(self._SNAPSHOT_NAME) - runner._remove_snapshot = mock.MagicMock() - runner._snapshot_all_keyspaces = mock.MagicMock() - runner._find_in_subdirectories = mock.MagicMock( - return_value=self._SNAPSHOT_FILES - ) - - return runner - - -class CouchbaseBackupTests(trove_testtools.TestCase): - - def setUp(self): - super(CouchbaseBackupTests, self).setUp() - self.exec_timeout_patch = patch.object(utils, 'execute_with_timeout', - return_value=('0', '')) - self.exec_timeout_patch.start() - self.backup_runner = utils.import_class(BACKUP_CBBACKUP_CLS) - self.backup_runner_patch = patch.multiple( - self.backup_runner, _run=DEFAULT, - _run_pre_backup=DEFAULT, _run_post_backup=DEFAULT) - - def tearDown(self): - super(CouchbaseBackupTests, self).tearDown() - self.backup_runner_patch.stop() - self.exec_timeout_patch.stop() - - def test_backup_success(self): - backup_runner_mocks = self.backup_runner_patch.start() - with self.backup_runner(12345): - pass - - backup_runner_mocks['_run_pre_backup'].assert_called_once_with() - backup_runner_mocks['_run'].assert_called_once_with() - backup_runner_mocks['_run_post_backup'].assert_called_once_with() - - def test_backup_failed_due_to_run_backup(self): - backup_runner_mocks = self.backup_runner_patch.start() - backup_runner_mocks['_run'].configure_mock( - side_effect=exception.TroveError('test') - ) - with ExpectedException(exception.TroveError, 'test'): - with self.backup_runner(12345): - pass - - backup_runner_mocks['_run_pre_backup'].assert_called_once_with() - backup_runner_mocks['_run'].assert_called_once_with() - self.assertEqual(0, backup_runner_mocks['_run_post_backup'].call_count) - - -class CouchbaseRestoreTests(trove_testtools.TestCase): - - def setUp(self): - super(CouchbaseRestoreTests, self).setUp() - - self.restore_runner = utils.import_class( - RESTORE_CBBACKUP_CLS)( - 'swift', location='http://some.where', - checksum='True_checksum', - restore_location='/tmp/somewhere') - - def tearDown(self): - super(CouchbaseRestoreTests, self).tearDown() - - def test_restore_success(self): - expected_content_length = 123 - self.restore_runner._run_restore = mock.Mock( - return_value=expected_content_length) - self.restore_runner.pre_restore = mock.Mock() - self.restore_runner.post_restore = mock.Mock() - actual_content_length = self.restore_runner.restore() - self.assertEqual( - expected_content_length, actual_content_length) - - def test_restore_failed_due_to_pre_restore(self): - self.restore_runner.post_restore = mock.Mock() - self.restore_runner.pre_restore = mock.Mock( - side_effect=exception.ProcessExecutionError('Error')) - self.restore_runner._run_restore = mock.Mock() - self.assertRaises(exception.ProcessExecutionError, - self.restore_runner.restore) - - def test_restore_failed_due_to_run_restore(self): - self.restore_runner.pre_restore = mock.Mock() - self.restore_runner._run_restore = mock.Mock( - side_effect=exception.ProcessExecutionError('Error')) - self.restore_runner.post_restore = mock.Mock() - self.assertRaises(exception.ProcessExecutionError, - self.restore_runner.restore) - - class MongodbBackupTests(trove_testtools.TestCase): def setUp(self): diff --git a/trove/tests/unittests/guestagent/test_couchbase_manager.py b/trove/tests/unittests/guestagent/test_couchbase_manager.py index 0ac5cc168d..5aaa51166a 100644 --- a/trove/tests/unittests/guestagent/test_couchbase_manager.py +++ b/trove/tests/unittests/guestagent/test_couchbase_manager.py @@ -15,8 +15,8 @@ import os import stat import tempfile +from unittest import mock -import mock from mock import DEFAULT from mock import MagicMock from mock import Mock diff --git a/trove/tests/unittests/guestagent/test_galera_cluster_api.py b/trove/tests/unittests/guestagent/test_galera_cluster_api.py index 809d9e1abe..e37a549a85 100644 --- a/trove/tests/unittests/guestagent/test_galera_cluster_api.py +++ b/trove/tests/unittests/guestagent/test_galera_cluster_api.py @@ -13,7 +13,7 @@ # limitations under the License. from eventlet import Timeout -import mock +from unittest import mock import trove.common.context as context from trove.common import exception diff --git a/trove/tests/unittests/hacking/test_check.py b/trove/tests/unittests/hacking/test_check.py index 3f6c40211a..cfd8e68c35 100644 --- a/trove/tests/unittests/hacking/test_check.py +++ b/trove/tests/unittests/hacking/test_check.py @@ -10,9 +10,9 @@ # License for the specific language governing permissions and limitations # under the License. -import mock import pycodestyle import textwrap +from unittest import mock from trove.hacking import checks as tc from trove.tests.unittests import trove_testtools diff --git a/trove/tests/unittests/taskmanager/test_api.py b/trove/tests/unittests/taskmanager/test_api.py index 16d148855d..9bc6b33551 100644 --- a/trove/tests/unittests/taskmanager/test_api.py +++ b/trove/tests/unittests/taskmanager/test_api.py @@ -14,9 +14,9 @@ # License for the specific language governing permissions and limitations # under the License. -import mock from mock import Mock from mock import patch +from unittest import mock from trove.common import context from trove.common import exception diff --git a/trove/tests/unittests/taskmanager/test_models.py b/trove/tests/unittests/taskmanager/test_models.py index 721a9b4477..a3cda332f5 100644 --- a/trove/tests/unittests/taskmanager/test_models.py +++ b/trove/tests/unittests/taskmanager/test_models.py @@ -13,11 +13,11 @@ # under the License. import os from tempfile import NamedTemporaryFile +from unittest import mock from cinderclient import exceptions as cinder_exceptions import cinderclient.v2.client as cinderclient from cinderclient.v2 import volumes as cinderclient_volumes -import mock from mock import Mock, MagicMock, patch, PropertyMock, call import neutronclient.v2_0.client as neutronclient from novaclient import exceptions as nova_exceptions diff --git a/trove/tests/unittests/trove_testtools.py b/trove/tests/unittests/trove_testtools.py index 89158c6e37..0366d821db 100644 --- a/trove/tests/unittests/trove_testtools.py +++ b/trove/tests/unittests/trove_testtools.py @@ -14,8 +14,8 @@ # under the License. import abc -import mock import testtools +from unittest import mock from trove.common import cfg from trove.common.context import TroveContext diff --git a/trove/tests/unittests/volume_type/test_volume_type.py b/trove/tests/unittests/volume_type/test_volume_type.py index 1e72d1bf8c..443ae15fa4 100644 --- a/trove/tests/unittests/volume_type/test_volume_type.py +++ b/trove/tests/unittests/volume_type/test_volume_type.py @@ -14,7 +14,7 @@ # under the License. # -import mock +from unittest import mock from trove.common import clients from trove.tests.unittests import trove_testtools diff --git a/trove/tests/unittests/volume_type/test_volume_type_views.py b/trove/tests/unittests/volume_type/test_volume_type_views.py index b81b087abb..fa2a6a6045 100644 --- a/trove/tests/unittests/volume_type/test_volume_type_views.py +++ b/trove/tests/unittests/volume_type/test_volume_type_views.py @@ -14,7 +14,7 @@ # under the License. # -import mock +from unittest import mock from trove.tests.unittests import trove_testtools from trove.volume_type import views