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 <sean.mcginnis@gmail.com>
This commit is contained in:
Sean McGinnis 2020-04-18 12:01:20 -05:00 committed by Lingxian Kong
parent 429c39890e
commit 74895a5cbe
21 changed files with 31 additions and 218 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

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 uuid
from novaclient import exceptions as nova_exceptions

View File

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

View File

@ -14,8 +14,9 @@
# under the License.
#
import mock
import os
from unittest import mock
import six
from trove.common import crypto_utils

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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