Merge "EventReporterStub"

This commit is contained in:
Jenkins 2016-11-10 15:54:38 +00:00 committed by Gerrit Code Review
commit b2db12d357
2 changed files with 44 additions and 78 deletions

View File

@ -791,6 +791,15 @@ class NoopConductorFixture(fixtures.Fixture):
'nova.conductor.API', _NoopConductor))
class EventReporterStub(fixtures.Fixture):
def setUp(self):
super(EventReporterStub, self).setUp()
self.useFixture(fixtures.MonkeyPatch(
'nova.compute.utils.EventReporter',
lambda *args, **kwargs: mock.MagicMock()))
class CinderFixture(fixtures.Fixture):
"""A fixture to volume operations"""

View File

@ -78,6 +78,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
fakes.FAKE_PROJECT_ID)
self.useFixture(fixtures.SpawnIsSynchronousFixture())
self.useFixture(fixtures.EventReporterStub())
@mock.patch.object(manager.ComputeManager, '_get_power_state')
@mock.patch.object(manager.ComputeManager, '_sync_instance_power_state')
@ -2521,12 +2522,11 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
mock.patch.object(compute_utils, 'notify_usage_exists'),
mock.patch.object(self.compute, '_get_power_state',
return_value=power_state.RUNNING),
mock.patch.object(instance, 'save'),
mock.patch.object(compute_utils, 'EventReporter')
mock.patch.object(instance, 'save')
) as (
elevated_context, get_nw_info, get_rescue_image,
notify_instance_usage, power_off_instance, driver_rescue,
notify_usage_exists, get_power_state, instance_save, event_reporter
notify_usage_exists, get_power_state, instance_save
):
self.compute.rescue_instance(
self.context, instance, rescue_password='verybadpass',
@ -2586,11 +2586,10 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
mock.patch.object(self.compute.driver, 'unrescue'),
mock.patch.object(self.compute, '_get_power_state',
return_value=power_state.RUNNING),
mock.patch.object(instance, 'save'),
mock.patch.object(compute_utils, 'EventReporter')
mock.patch.object(instance, 'save')
) as (
elevated_context, get_nw_info, notify_instance_usage,
driver_unrescue, get_power_state, instance_save, event_reporter
driver_unrescue, get_power_state, instance_save
):
self.compute.unrescue_instance(self.context, instance)
@ -2619,9 +2618,8 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
return_value=power_state.RUNNING)
@mock.patch.object(objects.Instance, 'save')
@mock.patch('nova.utils.generate_password', return_value='fake-pass')
@mock.patch.object(compute_utils, 'EventReporter')
def test_set_admin_password(self, event_mock, gen_password_mock,
instance_save_mock, power_state_mock):
def test_set_admin_password(self, gen_password_mock, instance_save_mock,
power_state_mock):
# Ensure instance can have its admin password set.
instance = fake_instance.fake_instance_obj(
self.context,
@ -2649,8 +2647,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
@mock.patch('nova.compute.manager.ComputeManager._instance_update')
@mock.patch.object(objects.Instance, 'save')
@mock.patch.object(compute_utils, 'add_instance_fault_from_exc')
@mock.patch.object(compute_utils, 'EventReporter')
def test_set_admin_password_bad_state(self, event_mock, add_fault_mock,
def test_set_admin_password_bad_state(self, add_fault_mock,
instance_save_mock, update_mock,
power_state_mock):
# Test setting password while instance is rebuilding.
@ -2669,7 +2666,6 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
add_fault_mock.assert_called_once_with(
self.context, instance, mock.ANY, mock.ANY)
@mock.patch.object(compute_utils, 'EventReporter')
@mock.patch('nova.utils.generate_password', return_value='fake-pass')
@mock.patch('nova.compute.manager.ComputeManager._get_power_state',
return_value=power_state.RUNNING)
@ -2684,8 +2680,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
instance_save_mock,
update_mock,
power_state_mock,
gen_password_mock,
event_mock):
gen_password_mock):
# Ensure expected exception is raised if set_admin_password fails.
instance = fake_instance.fake_instance_obj(
self.context,
@ -2931,8 +2926,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
@mock.patch.object(self.compute, '_notify_about_instance_usage')
@mock.patch.object(self.compute, '_power_off_instance')
@mock.patch.object(instance, 'save')
@mock.patch.object(compute_utils, 'EventReporter')
def do_test(event_mock, save_mock, power_off_mock, notify_mock,
def do_test(save_mock, power_off_mock, notify_mock,
notify_action_mock, get_state_mock):
# run the code
self.compute.stop_instance(self.context, instance, True)
@ -2986,9 +2980,8 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
mock.patch.object(self.compute, '_do_rebuild_instance_with_claim',
side_effect=ex),
mock.patch.object(self.compute, '_set_migration_status'),
mock.patch.object(self.compute, '_notify_about_instance_usage'),
mock.patch.object(compute_utils, 'EventReporter')
) as (mock_get, mock_rebuild, mock_set, mock_notify, mock_event):
mock.patch.object(self.compute, '_notify_about_instance_usage')
) as (mock_get, mock_rebuild, mock_set, mock_notify):
self.compute.rebuild_instance(self.context, instance, None, None,
None, None, None, None, None)
mock_set.assert_called_once_with(None, 'failed')
@ -3014,9 +3007,8 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
mock.patch.object(self.compute, '_get_compute_info'),
mock.patch.object(self.compute, '_do_rebuild_instance_with_claim'),
mock.patch.object(objects.Instance, 'save'),
mock.patch.object(self.compute, '_set_migration_status'),
mock.patch.object(compute_utils, 'EventReporter'),
) as (mock_get, mock_rebuild, mock_save, mock_set, mock_event):
mock.patch.object(self.compute, '_set_migration_status')
) as (mock_get, mock_rebuild, mock_save, mock_set):
self.compute.rebuild_instance(self.context, instance, None, None,
None, None, None, None, False)
self.assertFalse(mock_get.called)
@ -3032,9 +3024,8 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
mock.patch.object(self.compute, '_get_compute_info'),
mock.patch.object(self.compute, '_do_rebuild_instance_with_claim'),
mock.patch.object(objects.Instance, 'save'),
mock.patch.object(self.compute, '_set_migration_status'),
mock.patch.object(compute_utils, 'EventReporter'),
) as (mock_get, mock_rebuild, mock_save, mock_set, mock_event):
mock.patch.object(self.compute, '_set_migration_status')
) as (mock_get, mock_rebuild, mock_save, mock_set):
mock_get.return_value.hypervisor_hostname = 'new-node'
self.compute.rebuild_instance(self.context, instance, None, None,
None, None, None, None, True)
@ -3275,9 +3266,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
@mock.patch('nova.objects.BlockDeviceMappingList.get_by_instance_uuid')
@mock.patch('nova.compute.manager.ComputeManager._delete_instance')
@mock.patch.object(compute_utils, 'EventReporter')
def test_terminate_instance_no_bdm_volume_id(self, mock_event,
mock_delete_instance,
def test_terminate_instance_no_bdm_volume_id(self, mock_delete_instance,
mock_bdm_get_by_inst):
# Tests that we refresh the bdm list if a volume bdm does not have the
# volume_id set.
@ -3299,8 +3288,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
@mock.patch.object(nova.compute.manager.ComputeManager,
'_notify_about_instance_usage')
@mock.patch.object(compute_utils, 'EventReporter')
def test_trigger_crash_dump(self, event_mock, notify_mock):
def test_trigger_crash_dump(self, notify_mock):
instance = fake_instance.fake_instance_obj(
self.context, vm_state=vm_states.ACTIVE)
@ -3321,10 +3309,9 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
'notify_about_instance_action'),
mock.patch.object(self.compute, '_notify_about_instance_usage'),
mock.patch.object(objects.Instance, 'save'),
mock.patch.object(self.compute.driver, 'restore'),
mock.patch.object(compute_utils, 'EventReporter')
mock.patch.object(self.compute.driver, 'restore')
) as (
fake_notify, fake_usage, fake_save, fake_restore, fake_event
fake_notify, fake_usage, fake_save, fake_restore
):
self.compute.restore_instance(self.context, inst_obj)
fake_notify.assert_has_calls([
@ -4571,6 +4558,7 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
new_instance_type_id=7)
self.migration.status = 'migrating'
self.useFixture(fixtures.SpawnIsSynchronousFixture())
self.useFixture(fixtures.EventReporterStub())
@mock.patch.object(objects.Migration, 'save')
@mock.patch.object(objects.Migration, 'obj_as_admin')
@ -4607,10 +4595,9 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
mock.patch.object(self.instance, 'save'),
mock.patch.object(self.migration, 'save'),
mock.patch.object(self.migration, 'obj_as_admin',
return_value=mock.MagicMock()),
mock.patch.object(compute_utils, 'EventReporter')
return_value=mock.MagicMock())
) as (meth, fault_create, instance_update, instance_save,
migration_save, migration_obj_as_admin, event_reporter):
migration_save, migration_obj_as_admin):
fault_create.return_value = (
test_instance_fault.fake_faults['fake-uuid'][0])
self.assertRaises(
@ -4646,11 +4633,10 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
return_value=None),
mock.patch.object(objects.Flavor,
'get_by_id',
return_value=None),
mock.patch.object(compute_utils, 'EventReporter')
return_value=None)
) as (meth, fault_create, instance_update,
migration_save, migration_obj_as_admin, nw_info, save_inst,
notify, vol_block_info, bdm, flavor, event_reporter):
notify, vol_block_info, bdm, flavor):
fault_create.return_value = (
test_instance_fault.fake_faults['fake-uuid'][0])
self.assertRaises(
@ -4684,9 +4670,7 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
@mock.patch.object(self.migration, 'save')
@mock.patch.object(objects.BlockDeviceMappingList,
'get_by_instance_uuid')
@mock.patch.object(compute_utils, 'EventReporter')
def do_test(event_reporter,
get_by_instance_uuid,
def do_test(get_by_instance_uuid,
migration_save,
notify_usage_exists,
migrate_instance_start,
@ -4753,10 +4737,8 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
@mock.patch.object(db, 'instance_extra_update_by_uuid')
@mock.patch.object(objects.BlockDeviceMappingList,
'get_by_instance_uuid')
@mock.patch.object(compute_utils, 'EventReporter')
@mock.patch.object(compute_utils, 'notify_about_instance_usage')
def do_test(notify_about_instance_usage,
event_reporter,
get_by_instance_uuid,
extra_update,
fault_create,
@ -4800,7 +4782,6 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
@mock.patch.object(self.compute.network_api, 'setup_networks_on_host')
@mock.patch.object(self.compute.network_api, 'migrate_instance_start')
@mock.patch.object(compute_utils, 'notify_usage_exists')
@mock.patch.object(compute_utils, 'EventReporter')
@mock.patch.object(db, 'instance_extra_update_by_uuid')
@mock.patch.object(self.migration, 'save')
@mock.patch.object(objects.BlockDeviceMappingList,
@ -4808,7 +4789,6 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
def do_revert_resize(mock_get_by_instance_uuid,
mock_migration_save,
mock_extra_update,
mock_event,
mock_notify_usage_exists,
mock_migrate_instance_start,
mock_setup_networks_on_host,
@ -4835,7 +4815,6 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
@mock.patch.object(self.compute, "_set_instance_info")
@mock.patch.object(self.instance, 'save')
@mock.patch.object(self.migration, 'save')
@mock.patch.object(compute_utils, 'EventReporter')
@mock.patch.object(compute_utils, 'add_instance_fault_from_exc')
@mock.patch.object(db, 'instance_fault_create')
@mock.patch.object(db, 'instance_extra_update_by_uuid')
@ -4851,7 +4830,6 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
mock_extra_update,
mock_fault_create,
mock_fault_from_exc,
mock_event,
mock_mig_save,
mock_inst_save,
mock_set,
@ -4883,8 +4861,7 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
@mock.patch('nova.compute.manager.ComputeManager.'
'_do_live_migration')
@mock.patch.object(compute_utils, 'EventReporter')
def _test_max_concurrent_live(self, mock_event, mock_lm):
def _test_max_concurrent_live(self, mock_lm):
@mock.patch('nova.objects.Migration.save')
def _do_it(mock_mig_save):
@ -4942,8 +4919,7 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
@mock.patch.object(compute.driver, 'check_can_live_migrate_source')
@mock.patch.object(compute, '_get_instance_block_device_info')
@mock.patch.object(compute_utils, 'is_volume_backed_instance')
@mock.patch.object(compute_utils, 'EventReporter')
def _test(mock_event, mock_ivbi, mock_gibdi, mock_cclms):
def _test(mock_ivbi, mock_gibdi, mock_cclms):
mock_cclms.return_value = data
self.assertIsInstance(
compute.check_can_live_migrate_source(
@ -4957,14 +4933,12 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
def test_pre_live_migration_handles_dict(self):
compute = manager.ComputeManager()
@mock.patch.object(compute_utils, 'EventReporter')
@mock.patch.object(compute, '_notify_about_instance_usage')
@mock.patch.object(compute, 'network_api')
@mock.patch.object(compute.driver, 'pre_live_migration')
@mock.patch.object(compute, '_get_instance_block_device_info')
@mock.patch.object(compute_utils, 'is_volume_backed_instance')
def _test(mock_ivbi, mock_gibdi, mock_plm, mock_nwapi, mock_notify,
mock_event):
def _test(mock_ivbi, mock_gibdi, mock_plm, mock_nwapi, mock_notify):
migrate_data = migrate_data_obj.LiveMigrateData()
mock_plm.return_value = migrate_data
r = compute.pre_live_migration(self.context, {'uuid': 'foo'},
@ -5018,15 +4992,12 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
migration.status = 'running'
migration.id = 0
@mock.patch.object(compute_utils.EventReporter, '__exit__')
@mock.patch.object(compute_utils.EventReporter, '__enter__')
@mock.patch.object(self.compute, '_notify_about_instance_usage')
@mock.patch.object(objects.Migration, 'get_by_id',
return_value=migration)
@mock.patch.object(self.compute.driver,
'live_migration_force_complete')
def _do_test(force_complete, get_by_id, _notify_about_instance_usage,
enter_event_reporter, exit_event_reporter):
def _do_test(force_complete, get_by_id, _notify_about_instance_usage):
self.compute.live_migration_force_complete(
self.context, instance, migration.id)
@ -5040,13 +5011,11 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
]
_notify_about_instance_usage.assert_has_calls(_notify_usage_calls)
enter_event_reporter.assert_called_once_with()
_do_test()
def test_post_live_migration_at_destination_success(self):
@mock.patch.object(compute_utils, 'EventReporter')
@mock.patch.object(self.instance, 'save')
@mock.patch.object(self.compute.network_api, 'get_instance_nw_info',
return_value='test_network')
@ -5061,8 +5030,7 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
def _do_test(post_live_migration_at_destination, _get_compute_info,
_get_power_state, _get_instance_block_device_info,
_notify_about_instance_usage, migrate_instance_finish,
setup_networks_on_host, get_instance_nw_info, save,
event_reporter):
setup_networks_on_host, get_instance_nw_info, save):
cn = mock.Mock(spec_set=['hypervisor_hostname'])
cn.hypervisor_hostname = 'test_host'
@ -5116,7 +5084,6 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
def test_post_live_migration_at_destination_compute_not_found(self):
@mock.patch.object(compute_utils, 'EventReporter')
@mock.patch.object(self.instance, 'save')
@mock.patch.object(self.compute, 'network_api')
@mock.patch.object(self.compute, '_notify_about_instance_usage')
@ -5129,8 +5096,7 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
'post_live_migration_at_destination')
def _do_test(post_live_migration_at_destination, _get_compute_info,
_get_power_state, _get_instance_block_device_info,
_notify_about_instance_usage, network_api, save,
event_reporter):
_notify_about_instance_usage, network_api, save):
cn = mock.Mock(spec_set=['hypervisor_hostname'])
cn.hypervisor_hostname = 'test_host'
_get_compute_info.return_value = cn
@ -5143,7 +5109,6 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
def test_post_live_migration_at_destination_unexpected_exception(self):
@mock.patch.object(compute_utils, 'EventReporter')
@mock.patch.object(compute_utils, 'add_instance_fault_from_exc')
@mock.patch.object(self.instance, 'save')
@mock.patch.object(self.compute, 'network_api')
@ -5157,7 +5122,7 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
def _do_test(post_live_migration_at_destination, _get_compute_info,
_get_power_state, _get_instance_block_device_info,
_notify_about_instance_usage, network_api, save,
add_instance_fault_from_exc, event_reporter):
add_instance_fault_from_exc):
cn = mock.Mock(spec_set=['hypervisor_hostname'])
cn.hypervisor_hostname = 'test_host'
_get_compute_info.return_value = cn
@ -5179,10 +5144,7 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
@mock.patch.object(manager.ComputeManager, '_notify_about_instance_usage')
@mock.patch.object(objects.Migration, 'get_by_id')
@mock.patch.object(nova.virt.fake.SmallFakeDriver, 'live_migration_abort')
@mock.patch.object(compute_utils, 'EventReporter')
def test_live_migration_abort(self,
mock_event,
mock_driver,
def test_live_migration_abort(self, mock_driver,
mock_get_migration,
mock_notify):
instance = objects.Instance(id=123, uuid=uuids.instance)
@ -5204,10 +5166,7 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
@mock.patch.object(manager.ComputeManager, '_notify_about_instance_usage')
@mock.patch.object(objects.Migration, 'get_by_id')
@mock.patch.object(nova.virt.fake.SmallFakeDriver, 'live_migration_abort')
@mock.patch.object(compute_utils, 'EventReporter')
def test_live_migration_abort_not_supported(self,
mock_event,
mock_driver,
def test_live_migration_abort_not_supported(self, mock_driver,
mock_get_migration,
mock_notify,
mock_instance_fault):
@ -5223,9 +5182,7 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
@mock.patch.object(compute_utils, 'add_instance_fault_from_exc')
@mock.patch.object(objects.Migration, 'get_by_id')
@mock.patch.object(compute_utils, 'EventReporter')
def test_live_migration_abort_wrong_migration_state(self,
mock_event,
mock_get_migration,
mock_instance_fault):
instance = objects.Instance(id=123, uuid=uuids.instance)