diff --git a/trove/tests/unittests/backup/test_backup_controller.py b/trove/tests/unittests/backup/test_backup_controller.py index 3faeb2f339..d1f73a4b68 100644 --- a/trove/tests/unittests/backup/test_backup_controller.py +++ b/trove/tests/unittests/backup/test_backup_controller.py @@ -14,13 +14,13 @@ # under the License. # import jsonschema -from testtools import TestCase from testtools.matchers import Equals from trove.backup.service import BackupController from trove.common import apischema +from trove.tests.unittests import trove_testtools -class TestBackupController(TestCase): +class TestBackupController(trove_testtools.TestCase): def setUp(self): super(TestBackupController, self).setUp() diff --git a/trove/tests/unittests/backup/test_backup_models.py b/trove/tests/unittests/backup/test_backup_models.py index 10b770c346..eacc408a23 100644 --- a/trove/tests/unittests/backup/test_backup_models.py +++ b/trove/tests/unittests/backup/test_backup_models.py @@ -13,8 +13,9 @@ import datetime -from mock import MagicMock, patch -import testtools +from mock import DEFAULT +from mock import MagicMock +from mock import patch from trove.backup import models from trove.backup import state @@ -23,6 +24,7 @@ from trove.common import exception from trove.common import utils from trove.instance import models as instance_models from trove.taskmanager import api +from trove.tests.unittests import trove_testtools from trove.tests.unittests.util import util @@ -47,7 +49,7 @@ BACKUP_LOCATION = 'https://hpcs.com/tenant/database_backups/' + BACKUP_FILENAME api.API.get_client = MagicMock() -class BackupCreateTest(testtools.TestCase): +class BackupCreateTest(trove_testtools.TestCase): def setUp(self): super(BackupCreateTest, self).setUp() util.init_db() @@ -69,11 +71,11 @@ class BackupCreateTest(testtools.TestCase): instance.datastore_version = MagicMock() instance.datastore_version.id = 'datastore-id-999' instance.cluster_id = None - with patch.object(models.Backup, 'validate_can_perform_action', - return_value=None): - with patch.object(models.Backup, 'verify_swift_auth_token', - return_value=None): - api.API.create_backup = MagicMock(return_value=None) + with patch.multiple(models.Backup, + validate_can_perform_action=DEFAULT, + verify_swift_auth_token=DEFAULT): + with patch.object(api.API, 'create_backup', + MagicMock(return_value=None)): bu = models.Backup.create(self.context, self.instance_id, BACKUP_NAME, BACKUP_DESC) self.created = True @@ -106,38 +108,36 @@ class BackupCreateTest(testtools.TestCase): instance.datastore_version = MagicMock() instance.datastore_version.id = 'datastore-id-999' instance.cluster_id = None - with patch.object(models.Backup, 'validate_can_perform_action', - return_value=None): - with patch.object(models.Backup, 'verify_swift_auth_token', - return_value=None): - api.API.create_backup = MagicMock(return_value=None) - with patch.object(models.Backup, 'get_by_id', - return_value=parent): + with patch.multiple(models.Backup, + validate_can_perform_action=DEFAULT, + verify_swift_auth_token=DEFAULT, + get_by_id=MagicMock(return_value=parent)): + with patch.object(api.API, 'create_backup', + MagicMock(return_value=None)): + incremental = models.Backup.create( + self.context, + self.instance_id, + BACKUP_NAME, + BACKUP_DESC, + parent_id='parent_uuid') - incremental = models.Backup.create( - self.context, - self.instance_id, - BACKUP_NAME, - BACKUP_DESC, - parent_id='parent_uuid') + self.created = True - self.created = True - - db_record = models.DBBackup.find_by(id=incremental.id) - self.assertEqual(incremental.id, - db_record['id']) - self.assertEqual(BACKUP_NAME, - db_record['name']) - self.assertEqual(BACKUP_DESC, - db_record['description']) - self.assertEqual(self.instance_id, - db_record['instance_id']) - self.assertEqual(state.BackupState.NEW, - db_record['state']) - self.assertEqual('parent_uuid', - db_record['parent_id']) - self.assertEqual(instance.datastore_version.id, - db_record['datastore_version_id']) + db_record = models.DBBackup.find_by(id=incremental.id) + self.assertEqual(incremental.id, + db_record['id']) + self.assertEqual(BACKUP_NAME, + db_record['name']) + self.assertEqual(BACKUP_DESC, + db_record['description']) + self.assertEqual(self.instance_id, + db_record['instance_id']) + self.assertEqual(state.BackupState.NEW, + db_record['state']) + self.assertEqual('parent_uuid', + db_record['parent_id']) + self.assertEqual(instance.datastore_version.id, + db_record['datastore_version_id']) def test_create_instance_not_found(self): self.assertRaises(exception.NotFound, models.Backup.create, @@ -202,7 +202,7 @@ class BackupCreateTest(testtools.TestCase): BACKUP_NAME, BACKUP_DESC) -class BackupDeleteTest(testtools.TestCase): +class BackupDeleteTest(trove_testtools.TestCase): def setUp(self): super(BackupDeleteTest, self).setUp() util.init_db() @@ -233,7 +233,7 @@ class BackupDeleteTest(testtools.TestCase): self.context, 'backup_id') -class BackupORMTest(testtools.TestCase): +class BackupORMTest(trove_testtools.TestCase): def setUp(self): super(BackupORMTest, self).setUp() util.init_db() @@ -358,7 +358,7 @@ class BackupORMTest(testtools.TestCase): self.assertEqual(BACKUP_FILENAME, self.backup.filename) -class PaginationTests(testtools.TestCase): +class PaginationTests(trove_testtools.TestCase): def setUp(self): super(PaginationTests, self).setUp() @@ -416,7 +416,7 @@ class PaginationTests(testtools.TestCase): self.assertEqual(10, len(backups)) -class OrderingTests(testtools.TestCase): +class OrderingTests(trove_testtools.TestCase): def setUp(self): super(OrderingTests, self).setUp() diff --git a/trove/tests/unittests/backup/test_backupagent.py b/trove/tests/unittests/backup/test_backupagent.py index 6eee9be5a7..af690dca42 100644 --- a/trove/tests/unittests/backup/test_backupagent.py +++ b/trove/tests/unittests/backup/test_backupagent.py @@ -17,7 +17,6 @@ from webob.exc import HTTPNotFound import hashlib import os -import testtools from oslo_utils import netutils from trove.common import utils @@ -31,6 +30,7 @@ from trove.guestagent.backup import backupagent from trove.guestagent.strategies.backup.base import BackupRunner from trove.guestagent.strategies.backup.base import UnknownBackupType from trove.guestagent.strategies.storage.base import Storage +from trove.tests.unittests import trove_testtools conductor_api.API.get_client = Mock() conductor_api.API.update_backup = Mock() @@ -160,12 +160,22 @@ class MockStats: f_bfree = 512 * 1024 -class BackupAgentTest(testtools.TestCase): +class BackupAgentTest(trove_testtools.TestCase): def setUp(self): super(BackupAgentTest, self).setUp() - mysql_impl.get_auth_password = MagicMock(return_value='123') - backupagent.get_storage_strategy = MagicMock(return_value=MockSwift) - os.statvfs = MagicMock(return_value=MockStats) + self.get_auth_pwd_patch = patch.object( + mysql_impl, 'get_auth_password', MagicMock(return_value='123')) + self.get_auth_pwd_mock = self.get_auth_pwd_patch.start() + self.addCleanup(self.get_auth_pwd_patch.stop) + self.get_ss_patch = patch.object( + backupagent, 'get_storage_strategy', + MagicMock(return_value=MockSwift)) + self.get_ss_mock = self.get_ss_patch.start() + self.addCleanup(self.get_ss_patch.stop) + self.statvfs_patch = patch.object( + os, 'statvfs', MagicMock(return_value=MockStats)) + self.statvfs_mock = self.statvfs_patch.start() + self.addCleanup(self.statvfs_patch.stop) self.orig_utils_execute_with_timeout = utils.execute_with_timeout self.orig_os_get_ip_address = netutils.get_my_ipv4 @@ -383,41 +393,38 @@ class BackupAgentTest(testtools.TestCase): context=None, backup_info=bkup_info, restore_location='/var/lib/mysql') - def test_backup_incremental_metadata(self): - with patch.object(backupagent, 'get_storage_strategy', - return_value=MockSwift): - MockStorage.save_metadata = Mock() - with patch.object(MockSwift, 'load_metadata', - return_value={'lsn': '54321'}): - meta = { - 'lsn': '12345', - 'parent_location': 'fake', - 'parent_checksum': 'md5', - } + @patch.object(MockSwift, 'load_metadata', return_value={'lsn': '54321'}) + @patch.object(MockStorage, 'save_metadata') + @patch.object(backupagent, 'get_storage_strategy', return_value=MockSwift) + def test_backup_incremental_metadata(self, + get_storage_strategy_mock, + save_metadata_mock, + load_metadata_mock): + meta = { + 'lsn': '12345', + 'parent_location': 'fake', + 'parent_checksum': 'md5', + } + with patch.multiple(mysql_impl.InnoBackupExIncremental, + metadata=MagicMock(return_value=meta), + _run=MagicMock(return_value=True), + __exit__=MagicMock(return_value=True)): + agent = backupagent.BackupAgent() - mysql_impl.InnoBackupExIncremental.metadata = MagicMock( - return_value=meta) - mysql_impl.InnoBackupExIncremental._run = MagicMock( - return_value=True) - mysql_impl.InnoBackupExIncremental.__exit__ = MagicMock( - return_value=True) + bkup_info = {'id': '123', + 'location': 'fake-location', + 'type': 'InnoBackupEx', + 'checksum': 'fake-checksum', + 'parent': {'location': 'fake', 'checksum': 'md5'} + } - agent = backupagent.BackupAgent() + agent.execute_backup(TroveContext(), + bkup_info, + '/var/lib/mysql') - bkup_info = {'id': '123', - 'location': 'fake-location', - 'type': 'InnoBackupEx', - 'checksum': 'fake-checksum', - 'parent': {'location': 'fake', 'checksum': 'md5'} - } - - agent.execute_backup(TroveContext(), - bkup_info, - '/var/lib/mysql') - - self.assertTrue(MockStorage.save_metadata.called_once_with( - ANY, - meta)) + self.assertTrue(MockStorage.save_metadata.called_once_with( + ANY, + meta)) def test_backup_incremental_bad_metadata(self): with patch.object(backupagent, 'get_storage_strategy', diff --git a/trove/tests/unittests/backup/test_storage.py b/trove/tests/unittests/backup/test_storage.py index 5da31bb4a1..b9fe16b6b0 100644 --- a/trove/tests/unittests/backup/test_storage.py +++ b/trove/tests/unittests/backup/test_storage.py @@ -12,12 +12,12 @@ #See the License for the specific language governing permissions and #limitations under the License. -import testtools from mock import Mock, MagicMock, patch import hashlib from trove.common.context import TroveContext from trove.tests.fakes.swift import FakeSwiftConnection +from trove.tests.unittests import trove_testtools from trove.tests.unittests.backup.test_backupagent \ import MockBackup as MockBackupRunner from trove.guestagent.strategies.storage.swift \ @@ -27,7 +27,7 @@ from trove.guestagent.strategies.storage.swift import SwiftStorage from trove.guestagent.strategies.storage.swift import StreamReader -class SwiftStorageSaveChecksumTests(testtools.TestCase): +class SwiftStorageSaveChecksumTests(trove_testtools.TestCase): """SwiftStorage.save is used to save a backup to Swift.""" def setUp(self): @@ -129,14 +129,18 @@ class SwiftStorageSaveChecksumTests(testtools.TestCase): "Incorrect swift location was returned.") -class SwiftStorageUtils(testtools.TestCase): +class SwiftStorageUtils(trove_testtools.TestCase): def setUp(self): super(SwiftStorageUtils, self).setUp() - context = TroveContext() - swift_client = FakeSwiftConnection() - swift.create_swift_client = MagicMock(return_value=swift_client) - self.swift = SwiftStorage(context) + self.context = TroveContext() + self.swift_client = FakeSwiftConnection() + self.create_swift_client_patch = patch.object( + swift, 'create_swift_client', + MagicMock(return_value=self.swift_client)) + self.create_swift_client_mock = self.create_swift_client_patch.start() + self.addCleanup(self.create_swift_client_patch.stop) + self.swift = SwiftStorage(self.context) def tearDown(self): super(SwiftStorageUtils, self).tearDown() @@ -159,7 +163,7 @@ class SwiftStorageUtils(testtools.TestCase): 'AND-THE-UGLY') -class SwiftStorageLoad(testtools.TestCase): +class SwiftStorageLoad(trove_testtools.TestCase): """SwiftStorage.load is used to return SwiftDownloadStream which is used to download a backup object from Swift """ @@ -214,7 +218,7 @@ class MockBackupStream(MockBackupRunner): return 'X' * chunk_size -class StreamReaderTests(testtools.TestCase): +class StreamReaderTests(trove_testtools.TestCase): def setUp(self): super(StreamReaderTests, self).setUp() @@ -269,13 +273,17 @@ class StreamReaderTests(testtools.TestCase): self.assertTrue(self.stream.end_of_file) -class SwiftMetadataTests(testtools.TestCase): +class SwiftMetadataTests(trove_testtools.TestCase): def setUp(self): super(SwiftMetadataTests, self).setUp() self.swift_client = FakeSwiftConnection() self.context = TroveContext() - swift.create_swift_client = MagicMock(return_value=self.swift_client) + self.create_swift_client_patch = patch.object( + swift, 'create_swift_client', + MagicMock(return_value=self.swift_client)) + self.create_swift_client_mock = self.create_swift_client_patch.start() + self.addCleanup(self.create_swift_client_patch.stop) self.swift = SwiftStorage(self.context) def tearDown(self):