Merge "Added dangling mock detection to 'backup' module"

This commit is contained in:
Jenkins 2015-05-11 09:19:41 +00:00 committed by Gerrit Code Review
commit 797eb2b323
4 changed files with 107 additions and 92 deletions

View File

@ -14,13 +14,13 @@
# under the License. # under the License.
# #
import jsonschema import jsonschema
from testtools import TestCase
from testtools.matchers import Equals from testtools.matchers import Equals
from trove.backup.service import BackupController from trove.backup.service import BackupController
from trove.common import apischema from trove.common import apischema
from trove.tests.unittests import trove_testtools
class TestBackupController(TestCase): class TestBackupController(trove_testtools.TestCase):
def setUp(self): def setUp(self):
super(TestBackupController, self).setUp() super(TestBackupController, self).setUp()

View File

@ -13,8 +13,9 @@
import datetime import datetime
from mock import MagicMock, patch from mock import DEFAULT
import testtools from mock import MagicMock
from mock import patch
from trove.backup import models from trove.backup import models
from trove.backup import state from trove.backup import state
@ -23,6 +24,7 @@ from trove.common import exception
from trove.common import utils from trove.common import utils
from trove.instance import models as instance_models from trove.instance import models as instance_models
from trove.taskmanager import api from trove.taskmanager import api
from trove.tests.unittests import trove_testtools
from trove.tests.unittests.util import util 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() api.API.get_client = MagicMock()
class BackupCreateTest(testtools.TestCase): class BackupCreateTest(trove_testtools.TestCase):
def setUp(self): def setUp(self):
super(BackupCreateTest, self).setUp() super(BackupCreateTest, self).setUp()
util.init_db() util.init_db()
@ -69,11 +71,11 @@ class BackupCreateTest(testtools.TestCase):
instance.datastore_version = MagicMock() instance.datastore_version = MagicMock()
instance.datastore_version.id = 'datastore-id-999' instance.datastore_version.id = 'datastore-id-999'
instance.cluster_id = None instance.cluster_id = None
with patch.object(models.Backup, 'validate_can_perform_action', with patch.multiple(models.Backup,
return_value=None): validate_can_perform_action=DEFAULT,
with patch.object(models.Backup, 'verify_swift_auth_token', verify_swift_auth_token=DEFAULT):
return_value=None): with patch.object(api.API, 'create_backup',
api.API.create_backup = MagicMock(return_value=None) MagicMock(return_value=None)):
bu = models.Backup.create(self.context, self.instance_id, bu = models.Backup.create(self.context, self.instance_id,
BACKUP_NAME, BACKUP_DESC) BACKUP_NAME, BACKUP_DESC)
self.created = True self.created = True
@ -106,38 +108,36 @@ class BackupCreateTest(testtools.TestCase):
instance.datastore_version = MagicMock() instance.datastore_version = MagicMock()
instance.datastore_version.id = 'datastore-id-999' instance.datastore_version.id = 'datastore-id-999'
instance.cluster_id = None instance.cluster_id = None
with patch.object(models.Backup, 'validate_can_perform_action', with patch.multiple(models.Backup,
return_value=None): validate_can_perform_action=DEFAULT,
with patch.object(models.Backup, 'verify_swift_auth_token', verify_swift_auth_token=DEFAULT,
return_value=None): get_by_id=MagicMock(return_value=parent)):
api.API.create_backup = MagicMock(return_value=None) with patch.object(api.API, 'create_backup',
with patch.object(models.Backup, 'get_by_id', MagicMock(return_value=None)):
return_value=parent): incremental = models.Backup.create(
self.context,
self.instance_id,
BACKUP_NAME,
BACKUP_DESC,
parent_id='parent_uuid')
incremental = models.Backup.create( self.created = True
self.context,
self.instance_id,
BACKUP_NAME,
BACKUP_DESC,
parent_id='parent_uuid')
self.created = True db_record = models.DBBackup.find_by(id=incremental.id)
self.assertEqual(incremental.id,
db_record = models.DBBackup.find_by(id=incremental.id) db_record['id'])
self.assertEqual(incremental.id, self.assertEqual(BACKUP_NAME,
db_record['id']) db_record['name'])
self.assertEqual(BACKUP_NAME, self.assertEqual(BACKUP_DESC,
db_record['name']) db_record['description'])
self.assertEqual(BACKUP_DESC, self.assertEqual(self.instance_id,
db_record['description']) db_record['instance_id'])
self.assertEqual(self.instance_id, self.assertEqual(state.BackupState.NEW,
db_record['instance_id']) db_record['state'])
self.assertEqual(state.BackupState.NEW, self.assertEqual('parent_uuid',
db_record['state']) db_record['parent_id'])
self.assertEqual('parent_uuid', self.assertEqual(instance.datastore_version.id,
db_record['parent_id']) db_record['datastore_version_id'])
self.assertEqual(instance.datastore_version.id,
db_record['datastore_version_id'])
def test_create_instance_not_found(self): def test_create_instance_not_found(self):
self.assertRaises(exception.NotFound, models.Backup.create, self.assertRaises(exception.NotFound, models.Backup.create,
@ -202,7 +202,7 @@ class BackupCreateTest(testtools.TestCase):
BACKUP_NAME, BACKUP_DESC) BACKUP_NAME, BACKUP_DESC)
class BackupDeleteTest(testtools.TestCase): class BackupDeleteTest(trove_testtools.TestCase):
def setUp(self): def setUp(self):
super(BackupDeleteTest, self).setUp() super(BackupDeleteTest, self).setUp()
util.init_db() util.init_db()
@ -233,7 +233,7 @@ class BackupDeleteTest(testtools.TestCase):
self.context, 'backup_id') self.context, 'backup_id')
class BackupORMTest(testtools.TestCase): class BackupORMTest(trove_testtools.TestCase):
def setUp(self): def setUp(self):
super(BackupORMTest, self).setUp() super(BackupORMTest, self).setUp()
util.init_db() util.init_db()
@ -358,7 +358,7 @@ class BackupORMTest(testtools.TestCase):
self.assertEqual(BACKUP_FILENAME, self.backup.filename) self.assertEqual(BACKUP_FILENAME, self.backup.filename)
class PaginationTests(testtools.TestCase): class PaginationTests(trove_testtools.TestCase):
def setUp(self): def setUp(self):
super(PaginationTests, self).setUp() super(PaginationTests, self).setUp()
@ -416,7 +416,7 @@ class PaginationTests(testtools.TestCase):
self.assertEqual(10, len(backups)) self.assertEqual(10, len(backups))
class OrderingTests(testtools.TestCase): class OrderingTests(trove_testtools.TestCase):
def setUp(self): def setUp(self):
super(OrderingTests, self).setUp() super(OrderingTests, self).setUp()

View File

@ -17,7 +17,6 @@ from webob.exc import HTTPNotFound
import hashlib import hashlib
import os import os
import testtools
from oslo_utils import netutils from oslo_utils import netutils
from trove.common import utils 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 BackupRunner
from trove.guestagent.strategies.backup.base import UnknownBackupType from trove.guestagent.strategies.backup.base import UnknownBackupType
from trove.guestagent.strategies.storage.base import Storage from trove.guestagent.strategies.storage.base import Storage
from trove.tests.unittests import trove_testtools
conductor_api.API.get_client = Mock() conductor_api.API.get_client = Mock()
conductor_api.API.update_backup = Mock() conductor_api.API.update_backup = Mock()
@ -160,12 +160,22 @@ class MockStats:
f_bfree = 512 * 1024 f_bfree = 512 * 1024
class BackupAgentTest(testtools.TestCase): class BackupAgentTest(trove_testtools.TestCase):
def setUp(self): def setUp(self):
super(BackupAgentTest, self).setUp() super(BackupAgentTest, self).setUp()
mysql_impl.get_auth_password = MagicMock(return_value='123') self.get_auth_pwd_patch = patch.object(
backupagent.get_storage_strategy = MagicMock(return_value=MockSwift) mysql_impl, 'get_auth_password', MagicMock(return_value='123'))
os.statvfs = MagicMock(return_value=MockStats) 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_utils_execute_with_timeout = utils.execute_with_timeout
self.orig_os_get_ip_address = netutils.get_my_ipv4 self.orig_os_get_ip_address = netutils.get_my_ipv4
@ -383,41 +393,38 @@ class BackupAgentTest(testtools.TestCase):
context=None, backup_info=bkup_info, context=None, backup_info=bkup_info,
restore_location='/var/lib/mysql') restore_location='/var/lib/mysql')
def test_backup_incremental_metadata(self): @patch.object(MockSwift, 'load_metadata', return_value={'lsn': '54321'})
with patch.object(backupagent, 'get_storage_strategy', @patch.object(MockStorage, 'save_metadata')
return_value=MockSwift): @patch.object(backupagent, 'get_storage_strategy', return_value=MockSwift)
MockStorage.save_metadata = Mock() def test_backup_incremental_metadata(self,
with patch.object(MockSwift, 'load_metadata', get_storage_strategy_mock,
return_value={'lsn': '54321'}): save_metadata_mock,
meta = { load_metadata_mock):
'lsn': '12345', meta = {
'parent_location': 'fake', 'lsn': '12345',
'parent_checksum': 'md5', '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( bkup_info = {'id': '123',
return_value=meta) 'location': 'fake-location',
mysql_impl.InnoBackupExIncremental._run = MagicMock( 'type': 'InnoBackupEx',
return_value=True) 'checksum': 'fake-checksum',
mysql_impl.InnoBackupExIncremental.__exit__ = MagicMock( 'parent': {'location': 'fake', 'checksum': 'md5'}
return_value=True) }
agent = backupagent.BackupAgent() agent.execute_backup(TroveContext(),
bkup_info,
'/var/lib/mysql')
bkup_info = {'id': '123', self.assertTrue(MockStorage.save_metadata.called_once_with(
'location': 'fake-location', ANY,
'type': 'InnoBackupEx', meta))
'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))
def test_backup_incremental_bad_metadata(self): def test_backup_incremental_bad_metadata(self):
with patch.object(backupagent, 'get_storage_strategy', with patch.object(backupagent, 'get_storage_strategy',

View File

@ -12,12 +12,12 @@
#See the License for the specific language governing permissions and #See the License for the specific language governing permissions and
#limitations under the License. #limitations under the License.
import testtools
from mock import Mock, MagicMock, patch from mock import Mock, MagicMock, patch
import hashlib import hashlib
from trove.common.context import TroveContext from trove.common.context import TroveContext
from trove.tests.fakes.swift import FakeSwiftConnection from trove.tests.fakes.swift import FakeSwiftConnection
from trove.tests.unittests import trove_testtools
from trove.tests.unittests.backup.test_backupagent \ from trove.tests.unittests.backup.test_backupagent \
import MockBackup as MockBackupRunner import MockBackup as MockBackupRunner
from trove.guestagent.strategies.storage.swift \ 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 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.""" """SwiftStorage.save is used to save a backup to Swift."""
def setUp(self): def setUp(self):
@ -129,14 +129,18 @@ class SwiftStorageSaveChecksumTests(testtools.TestCase):
"Incorrect swift location was returned.") "Incorrect swift location was returned.")
class SwiftStorageUtils(testtools.TestCase): class SwiftStorageUtils(trove_testtools.TestCase):
def setUp(self): def setUp(self):
super(SwiftStorageUtils, self).setUp() super(SwiftStorageUtils, self).setUp()
context = TroveContext() self.context = TroveContext()
swift_client = FakeSwiftConnection() self.swift_client = FakeSwiftConnection()
swift.create_swift_client = MagicMock(return_value=swift_client) self.create_swift_client_patch = patch.object(
self.swift = SwiftStorage(context) 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): def tearDown(self):
super(SwiftStorageUtils, self).tearDown() super(SwiftStorageUtils, self).tearDown()
@ -159,7 +163,7 @@ class SwiftStorageUtils(testtools.TestCase):
'AND-THE-UGLY') 'AND-THE-UGLY')
class SwiftStorageLoad(testtools.TestCase): class SwiftStorageLoad(trove_testtools.TestCase):
"""SwiftStorage.load is used to return SwiftDownloadStream which is used """SwiftStorage.load is used to return SwiftDownloadStream which is used
to download a backup object from Swift to download a backup object from Swift
""" """
@ -214,7 +218,7 @@ class MockBackupStream(MockBackupRunner):
return 'X' * chunk_size return 'X' * chunk_size
class StreamReaderTests(testtools.TestCase): class StreamReaderTests(trove_testtools.TestCase):
def setUp(self): def setUp(self):
super(StreamReaderTests, self).setUp() super(StreamReaderTests, self).setUp()
@ -269,13 +273,17 @@ class StreamReaderTests(testtools.TestCase):
self.assertTrue(self.stream.end_of_file) self.assertTrue(self.stream.end_of_file)
class SwiftMetadataTests(testtools.TestCase): class SwiftMetadataTests(trove_testtools.TestCase):
def setUp(self): def setUp(self):
super(SwiftMetadataTests, self).setUp() super(SwiftMetadataTests, self).setUp()
self.swift_client = FakeSwiftConnection() self.swift_client = FakeSwiftConnection()
self.context = TroveContext() 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) self.swift = SwiftStorage(self.context)
def tearDown(self): def tearDown(self):