diff --git a/trove/tests/unittests/backup/test_backupagent.py b/trove/tests/unittests/backup/test_backupagent.py index c5ae67a17e..0fa783d14e 100644 --- a/trove/tests/unittests/backup/test_backupagent.py +++ b/trove/tests/unittests/backup/test_backupagent.py @@ -16,7 +16,7 @@ import hashlib import mock import os -from mock import Mock, MagicMock, patch, ANY, DEFAULT +from mock import Mock, MagicMock, patch, ANY, DEFAULT, call from oslo_utils import netutils from webob.exc import HTTPNotFound @@ -329,28 +329,27 @@ class BackupAgentTest(trove_testtools.TestCase): agent.execute_backup(context=None, backup_info=backup_info, runner=MockBackup) - self.assertTrue( - conductor_api.API.update_backup.called_once_with( - ANY, - backup_id=backup_info['id'], - state=BackupState.NEW)) - - self.assertTrue( - conductor_api.API.update_backup.called_once_with( + conductor_api.API.update_backup.assert_has_calls([ + call( ANY, backup_id=backup_info['id'], + sent=ANY, size=ANY, - state=BackupState.BUILDING)) - - self.assertTrue( - conductor_api.API.update_backup.called_once_with( + state=BackupState.BUILDING + ), + call( ANY, backup_id=backup_info['id'], - checksum=ANY, + checksum='fake-checksum', location=ANY, - note=ANY, - backup_type=backup_info['type'], - state=BackupState.COMPLETED)) + note='w00t', + sent=ANY, + size=ANY, + backup_type=MockBackup.backup_type, + state=BackupState.COMPLETED, + success=True + ) + ]) @patch.object(conductor_api.API, 'get_client', Mock(return_value=Mock())) @patch.object(conductor_api.API, 'update_backup', @@ -369,28 +368,27 @@ class BackupAgentTest(trove_testtools.TestCase): context=None, backup_info=backup_info, runner=MockCheckProcessBackup) - self.assertTrue( - conductor_api.API.update_backup.called_once_with( - ANY, - backup_id=backup_info['id'], - state=BackupState.NEW)) - - self.assertTrue( - conductor_api.API.update_backup.called_once_with( + conductor_api.API.update_backup.assert_has_calls([ + call( ANY, backup_id=backup_info['id'], + sent=ANY, size=ANY, - state=BackupState.BUILDING)) - - self.assertTrue( - conductor_api.API.update_backup.called_once_with( + state=BackupState.BUILDING + ), + call( ANY, backup_id=backup_info['id'], - checksum=ANY, + checksum='fake-checksum', location=ANY, - note=ANY, - backup_type=backup_info['type'], - state=BackupState.FAILED)) + note='w00t', + sent=ANY, + size=ANY, + backup_type=MockCheckProcessBackup.backup_type, + state=BackupState.FAILED, + success=True + ) + ]) @patch.object(conductor_api.API, 'get_client', Mock(return_value=Mock())) @patch.object(conductor_api.API, 'update_backup', @@ -415,11 +413,26 @@ class BackupAgentTest(trove_testtools.TestCase): context=None, backup_info=backup_info, runner=MockLossyBackup) - self.assertTrue( - conductor_api.API.update_backup.called_once_with( + conductor_api.API.update_backup.assert_has_calls([ + call(ANY, + backup_id=backup_info['id'], + sent=ANY, + size=ANY, + state=BackupState.BUILDING + ), + call( ANY, backup_id=backup_info['id'], - state=BackupState.FAILED)) + checksum='y', + location='z', + note='Error', + sent=ANY, + size=ANY, + backup_type=MockLossyBackup.backup_type, + state=BackupState.FAILED, + success=False + )] + ) def test_execute_restore(self): """This test should ensure backup agent @@ -465,12 +478,12 @@ class BackupAgentTest(trove_testtools.TestCase): @patch.object(MySqlApp, 'get_data_dir', return_value='/var/lib/mysql/data') @patch.object(conductor_api.API, 'get_client', Mock(return_value=Mock())) @patch.object(MockSwift, 'load_metadata', return_value={'lsn': '54321'}) - @patch.object(MockStorage, 'save_metadata') + @patch.object(MockSwift, 'save') @patch.object(backupagent, 'get_storage_strategy', return_value=MockSwift) @patch('trove.guestagent.backup.backupagent.LOG') def test_backup_incremental_metadata(self, mock_logging, get_storage_strategy_mock, - save_metadata_mock, + save_mock, load_metadata_mock, get_datadir_mock): meta = { @@ -484,22 +497,21 @@ class BackupAgentTest(trove_testtools.TestCase): __exit__=MagicMock(return_value=True)): agent = backupagent.BackupAgent() + expected_metadata = {'datastore': 'mysql', + 'datastore_version': 'bo.gus'} bkup_info = {'id': '123', 'location': 'fake-location', 'type': 'InnoBackupEx', 'checksum': 'fake-checksum', - 'parent': {'location': 'fake', 'checksum': 'md5'}, - 'datastore': 'mysql', - 'datastore_version': 'bo.gus' - } + 'parent': {'location': 'fake', 'checksum': 'md5'}} + bkup_info.update(expected_metadata) agent.execute_backup(TroveContext(), bkup_info, '/var/lib/mysql/data') - self.assertTrue(MockStorage.save_metadata.called_once_with( - ANY, - meta)) + save_mock.assert_called_once_with( + ANY, ANY, metadata=expected_metadata) @patch.object(conductor_api.API, 'get_client', Mock(return_value=Mock())) def test_backup_incremental_bad_metadata(self): diff --git a/trove/tests/unittests/guestagent/test_dbaas.py b/trove/tests/unittests/guestagent/test_dbaas.py index 556e71ca9f..c74462dae0 100644 --- a/trove/tests/unittests/guestagent/test_dbaas.py +++ b/trove/tests/unittests/guestagent/test_dbaas.py @@ -106,9 +106,6 @@ FAKE_DB_2 = {"_name": "testDB2", "_character_set": "latin2", FAKE_USER = [{"_name": "random", "_password": "guesswhat", "_host": "%", "_databases": [FAKE_DB]}] -conductor_api.API.get_client = Mock() -conductor_api.API.heartbeat = Mock() - class FakeTime: COUNTER = 0 @@ -811,6 +808,9 @@ class MySqlAdminTest(trove_testtools.TestCase): class MySqlAppTest(trove_testtools.TestCase): def setUp(self): + conductor_cli_patcher = patch.object(conductor_api.API, 'get_client') + self.addCleanup(conductor_cli_patcher.stop) + conductor_cli_patcher.start() super(MySqlAppTest, self).setUp() self.orig_utils_execute_with_timeout = \ mysql_common_service.utils.execute_with_timeout @@ -906,29 +906,35 @@ class MySqlAppTest(trove_testtools.TestCase): mysql_common_service.utils.execute_with_timeout = Mock() self.appStatus.set_next_status( rd_instance.ServiceStatuses.SHUTDOWN) + self.patch_conf_property('guest_id', self.FAKE_ID) with patch.object(BaseDbStatus, 'prepare_completed') as patch_pc: - patch_pc.__get__ = Mock(return_value=True) - self.mySqlApp.stop_db(True) - self.assertTrue(conductor_api.API.heartbeat.called_once_with( - self.FAKE_ID, - {'service_status': - rd_instance.ServiceStatuses.SHUTDOWN.description})) + with patch.object(conductor_api.API, 'heartbeat') as patch_hb: + patch_pc.__get__ = Mock(return_value=True) + self.mySqlApp.stop_db(True) + patch_hb.assert_called_once_with( + self.FAKE_ID, + {'service_status': + rd_instance.ServiceStatuses.SHUTDOWN.description}, + sent=ANY) @patch.object(utils, 'execute_with_timeout', return_value=('0', '')) def test_stop_mysql_do_not_start_on_reboot(self, mock_execute): self.appStatus.set_next_status( rd_instance.ServiceStatuses.SHUTDOWN) + self.patch_conf_property('guest_id', self.FAKE_ID) with patch.object(BaseDbStatus, 'prepare_completed') as patch_pc: - patch_pc.__get__ = Mock(return_value=True) - self.mySqlApp.stop_db(True, True) - self.assertTrue(conductor_api.API.heartbeat.called_once_with( - self.FAKE_ID, - {'service_status': - rd_instance.ServiceStatuses.SHUTDOWN.description})) - self.assertEqual(2, mock_execute.call_count) + with patch.object(conductor_api.API, 'heartbeat') as patch_hb: + patch_pc.__get__ = Mock(return_value=True) + self.mySqlApp.stop_db(True, True) + patch_hb.assert_called_once_with( + self.FAKE_ID, + {'service_status': + rd_instance.ServiceStatuses.SHUTDOWN.description}, + sent=ANY) + self.assertEqual(2, mock_execute.call_count) @patch('trove.guestagent.datastore.service.LOG') @patch('trove.guestagent.datastore.mysql_common.service.LOG') @@ -958,17 +964,20 @@ class MySqlAppTest(trove_testtools.TestCase): self.mySqlApp.stop_db = Mock() self.mysql_stops_successfully() self.mysql_starts_successfully() + self.patch_conf_property('guest_id', self.FAKE_ID) with patch.object(BaseDbStatus, 'prepare_completed') as patch_pc: - patch_pc.__get__ = Mock(return_value=True) - self.mySqlApp.restart() + with patch.object(conductor_api.API, 'heartbeat') as patch_hb: + patch_pc.__get__ = Mock(return_value=True) + self.mySqlApp.restart() - self.assertTrue(self.mySqlApp.stop_db.called) - self.assertTrue(self.mySqlApp.start_mysql.called) - self.assertTrue(conductor_api.API.heartbeat.called_once_with( - self.FAKE_ID, - {'service_status': - rd_instance.ServiceStatuses.RUNNING.description})) + self.assertTrue(self.mySqlApp.stop_db.called) + self.assertTrue(self.mySqlApp.start_mysql.called) + patch_hb.assert_called_once_with( + self.FAKE_ID, + {'service_status': + rd_instance.ServiceStatuses.RUNNING.description}, + sent=ANY) def test_restart_mysql_wont_start_up(self): @@ -1008,14 +1017,17 @@ class MySqlAppTest(trove_testtools.TestCase): mysql_common_service.utils.execute_with_timeout = Mock() self.mySqlApp._enable_mysql_on_boot = Mock() self.appStatus.set_next_status(rd_instance.ServiceStatuses.RUNNING) + self.patch_conf_property('guest_id', self.FAKE_ID) with patch.object(BaseDbStatus, 'prepare_completed') as patch_pc: - patch_pc.__get__ = Mock(return_value=True) - self.mySqlApp.start_mysql(update_db=True) - self.assertTrue(conductor_api.API.heartbeat.called_once_with( - self.FAKE_ID, - {'service_status': - rd_instance.ServiceStatuses.RUNNING.description})) + with patch.object(conductor_api.API, 'heartbeat') as patch_hb: + patch_pc.__get__ = Mock(return_value=True) + self.mySqlApp.start_mysql(update_db=True) + patch_hb.assert_called_once_with( + self.FAKE_ID, + {'service_status': + rd_instance.ServiceStatuses.RUNNING.description}, + sent=ANY) @patch('trove.guestagent.datastore.mysql_common.service.LOG') @patch('trove.guestagent.datastore.service.LOG') @@ -1025,14 +1037,17 @@ class MySqlAppTest(trove_testtools.TestCase): self.mySqlApp._enable_mysql_on_boot = Mock() self.mySqlApp.state_change_wait_time = 1 self.appStatus.set_next_status(rd_instance.ServiceStatuses.SHUTDOWN) + self.patch_conf_property('guest_id', self.FAKE_ID) with patch.object(BaseDbStatus, 'prepare_completed') as patch_pc: - patch_pc.__get__ = Mock(return_value=True) - self.assertRaises(RuntimeError, self.mySqlApp.start_mysql) - self.assertTrue(conductor_api.API.heartbeat.called_once_with( - self.FAKE_ID, - {'service_status': - rd_instance.ServiceStatuses.SHUTDOWN.description})) + with patch.object(conductor_api.API, 'heartbeat') as patch_hb: + patch_pc.__get__ = Mock(return_value=True) + self.assertRaises(RuntimeError, self.mySqlApp.start_mysql) + patch_hb.assert_called_once_with( + self.FAKE_ID, + {'service_status': + rd_instance.ServiceStatuses.SHUTDOWN.description}, + sent=ANY) @patch('trove.guestagent.datastore.service.LOG') @patch('trove.guestagent.datastore.mysql_common.service.LOG')