Clean up ComputeManager._get_instance_nw_info

The private method '_get_instance_nw_info' of ComputeManager
make no sense, it just call self.network_api.get_instance_nw_info
again. Remove it, unify all methods of ComputeManager to use
self.network_api.get_instance_nw_info and update the related
test cases.

Remove the asserting admin context logic, because it come from
I5bc35632ca382d011eb42e738140ba54d9cdc93f, it's useless any more.

Change-Id: Ica58122c60c9e2d3e6740d458559ddf4b5a652be
This commit is contained in:
Rui Chen 2015-04-15 13:05:20 +08:00
parent 0b23bce359
commit f065a76de5
4 changed files with 70 additions and 90 deletions

View File

@ -840,8 +840,8 @@ class ComputeManager(manager.Manager):
{'instance_host': instance.host,
'our_host': our_host}, instance=instance)
try:
network_info = self._get_instance_nw_info(context,
instance)
network_info = self.network_api.get_instance_nw_info(
context, instance)
bdi = self._get_instance_block_device_info(context,
instance)
destroy_disks = not (self._is_instance_storage_shared(
@ -1380,10 +1380,6 @@ class ComputeManager(manager.Manager):
"""This call passes straight through to the virtualization driver."""
return self.driver.refresh_provider_fw_rules()
def _get_instance_nw_info(self, context, instance):
"""Get a list of dictionaries of network data of an instance."""
return self.network_api.get_instance_nw_info(context, instance)
def _await_block_device_map_created(self, context, vol_id):
# TODO(yamahata): creating volume simultaneously
# reduces creation time?
@ -1838,7 +1834,7 @@ class ComputeManager(manager.Manager):
# network resource need setup on the new host.
self.network_api.setup_instance_network_on_host(
context, instance, instance.host)
return self._get_instance_nw_info(context, instance)
return self.network_api.get_instance_nw_info(context, instance)
if not self.is_neutron_security_groups:
security_groups = []
@ -2819,7 +2815,7 @@ class ComputeManager(manager.Manager):
do_stop_instance()
def _power_on(self, context, instance):
network_info = self._get_instance_nw_info(context, instance)
network_info = self.network_api.get_instance_nw_info(context, instance)
block_device_info = self._get_instance_block_device_info(context,
instance)
self.driver.power_on(context, instance,
@ -3134,7 +3130,7 @@ class ComputeManager(manager.Manager):
block_device_info = self._get_instance_block_device_info(context,
instance)
network_info = self._get_instance_nw_info(context, instance)
network_info = self.network_api.get_instance_nw_info(context, instance)
self._notify_about_instance_usage(context, instance, "reboot.start")
@ -3495,7 +3491,7 @@ class ComputeManager(manager.Manager):
admin_password = (rescue_password if rescue_password else
utils.generate_password())
network_info = self._get_instance_nw_info(context, instance)
network_info = self.network_api.get_instance_nw_info(context, instance)
rescue_image_meta = self._get_rescue_image(context, instance,
rescue_image_ref)
@ -3541,7 +3537,7 @@ class ComputeManager(manager.Manager):
context = context.elevated()
LOG.info(_LI('Unrescuing'), context=context, instance=instance)
network_info = self._get_instance_nw_info(context, instance)
network_info = self.network_api.get_instance_nw_info(context, instance)
self._notify_about_instance_usage(context, instance,
"unrescue.start", network_info=network_info)
with self._error_out_instance_on_exception(context, instance):
@ -3668,7 +3664,8 @@ class ComputeManager(manager.Manager):
self.network_api.setup_networks_on_host(context, instance,
migration.source_compute, teardown=True)
network_info = self._get_instance_nw_info(context, instance)
network_info = self.network_api.get_instance_nw_info(context,
instance)
self.driver.confirm_migration(migration, instance,
network_info)
@ -3737,7 +3734,8 @@ class ComputeManager(manager.Manager):
instance,
migration_p)
network_info = self._get_instance_nw_info(context, instance)
network_info = self.network_api.get_instance_nw_info(context,
instance)
bdms = objects.BlockDeviceMappingList.get_by_instance_uuid(
context, instance.uuid)
block_device_info = self._get_instance_block_device_info(
@ -3779,7 +3777,8 @@ class ComputeManager(manager.Manager):
with self._error_out_instance_on_exception(context, instance,
quotas=quotas):
network_info = self._get_instance_nw_info(context, instance)
network_info = self.network_api.get_instance_nw_info(context,
instance)
self._notify_about_instance_usage(
context, instance, "resize.revert.start")
@ -4001,7 +4000,8 @@ class ComputeManager(manager.Manager):
instance_type = objects.Flavor.get_by_id(
context, migration['new_instance_type_id'])
network_info = self._get_instance_nw_info(context, instance)
network_info = self.network_api.get_instance_nw_info(context,
instance)
migration.status = 'migrating'
with migration.obj_as_admin():
@ -4093,7 +4093,7 @@ class ComputeManager(manager.Manager):
instance,
migration_p)
network_info = self._get_instance_nw_info(context, instance)
network_info = self.network_api.get_instance_nw_info(context, instance)
instance.task_state = task_states.RESIZE_FINISH
instance.save(expected_task_state=task_states.RESIZE_MIGRATED)
@ -4335,7 +4335,7 @@ class ComputeManager(manager.Manager):
LOG.info(_LI('Resuming'), context=context, instance=instance)
self._notify_about_instance_usage(context, instance, 'resume.start')
network_info = self._get_instance_nw_info(context, instance)
network_info = self.network_api.get_instance_nw_info(context, instance)
block_device_info = self._get_instance_block_device_info(
context, instance)
@ -4431,7 +4431,7 @@ class ComputeManager(manager.Manager):
self.network_api.cleanup_instance_network_on_host(context, instance,
instance.host)
network_info = self._get_instance_nw_info(context, instance)
network_info = self.network_api.get_instance_nw_info(context, instance)
block_device_info = self._get_instance_block_device_info(context,
instance)
self.driver.destroy(context, instance, network_info,
@ -4511,7 +4511,7 @@ class ComputeManager(manager.Manager):
self.network_api.setup_instance_network_on_host(context, instance,
self.host)
network_info = self._get_instance_nw_info(context, instance)
network_info = self.network_api.get_instance_nw_info(context, instance)
try:
with rt.instance_claim(context, instance, limits):
self.driver.spawn(context, instance, image, injected_files=[],
@ -4552,7 +4552,7 @@ class ComputeManager(manager.Manager):
@wrap_instance_fault
def inject_network_info(self, context, instance):
"""Inject network info, but don't return the info."""
network_info = self._get_instance_nw_info(context, instance)
network_info = self.network_api.get_instance_nw_info(context, instance)
self._inject_network_info(context, instance, network_info)
@object_compat
@ -5189,7 +5189,7 @@ class ComputeManager(manager.Manager):
block_device_info = self._get_instance_block_device_info(
context, instance, refresh_conn_info=True)
network_info = self._get_instance_nw_info(context, instance)
network_info = self.network_api.get_instance_nw_info(context, instance)
self._notify_about_instance_usage(
context, instance, "live_migration.pre.start",
network_info=network_info)
@ -5353,7 +5353,7 @@ class ComputeManager(manager.Manager):
# Releasing vlan.
# (not necessary in current implementation?)
network_info = self._get_instance_nw_info(ctxt, instance)
network_info = self.network_api.get_instance_nw_info(ctxt, instance)
self._notify_about_instance_usage(ctxt, instance,
"live_migration._post.start",
@ -5449,7 +5449,7 @@ class ComputeManager(manager.Manager):
instance,
migration)
network_info = self._get_instance_nw_info(context, instance)
network_info = self.network_api.get_instance_nw_info(context, instance)
self._notify_about_instance_usage(
context, instance, "live_migration.post.dest.start",
network_info=network_info)
@ -5538,7 +5538,7 @@ class ComputeManager(manager.Manager):
:param context: security context
:param instance: a nova.objects.instance.Instance object sent over rpc
"""
network_info = self._get_instance_nw_info(context, instance)
network_info = self.network_api.get_instance_nw_info(context, instance)
self._notify_about_instance_usage(
context, instance, "live_migration.rollback.dest.start",
network_info=network_info)
@ -5637,7 +5637,7 @@ class ComputeManager(manager.Manager):
try:
# Call to network API to get instance info.. this will
# force an update to the instance's info_cache
self._get_instance_nw_info(context, instance)
self.network_api.get_instance_nw_info(context, instance)
LOG.debug('Updated the network info_cache for instance',
instance=instance)
except exception.InstanceNotFound:

View File

@ -254,8 +254,7 @@ class BaseTestCase(test.TestCase):
fake_server_actions.stub_out_action_events(self.stubs)
def fake_get_nw_info(cls, ctxt, instance, *args, **kwargs):
self.assertTrue(ctxt.is_admin)
return fake_network.fake_get_instance_nw_info(self.stubs, 1, 1)
return network_model.NetworkInfo()
self.stubs.Set(network_api.API, 'get_instance_nw_info',
fake_get_nw_info)
@ -2599,7 +2598,8 @@ class ComputeTestCase(BaseTestCase):
self.mox.StubOutWithMock(self.compute,
'_get_instance_block_device_info')
self.mox.StubOutWithMock(self.compute, '_get_instance_nw_info')
self.mox.StubOutWithMock(self.compute.network_api,
'get_instance_nw_info')
self.mox.StubOutWithMock(self.compute, '_notify_about_instance_usage')
self.mox.StubOutWithMock(self.compute, '_instance_update')
self.mox.StubOutWithMock(db, 'instance_update_and_get_original')
@ -2655,9 +2655,8 @@ class ComputeTestCase(BaseTestCase):
self.compute._get_instance_block_device_info(
econtext, instance).AndReturn(fake_block_dev_info)
self.compute._get_instance_nw_info(econtext,
instance).AndReturn(
fake_nw_model)
self.compute.network_api.get_instance_nw_info(
econtext, instance).AndReturn(fake_nw_model)
self.compute._notify_about_instance_usage(econtext,
instance,
'reboot.start')
@ -4420,7 +4419,8 @@ class ComputeTestCase(BaseTestCase):
self.mox.StubOutWithMock(network_api, 'setup_networks_on_host')
self.mox.StubOutWithMock(network_api,
'migrate_instance_finish')
self.mox.StubOutWithMock(self.compute, '_get_instance_nw_info')
self.mox.StubOutWithMock(self.compute.network_api,
'get_instance_nw_info')
self.mox.StubOutWithMock(self.compute,
'_notify_about_instance_usage')
self.mox.StubOutWithMock(self.compute.driver, 'finish_migration')
@ -4470,7 +4470,7 @@ class ComputeTestCase(BaseTestCase):
mox.IsA(objects.Instance),
mox.IsA(dict))
self.compute._get_instance_nw_info(
self.compute.network_api.get_instance_nw_info(
self.context, instance).AndReturn('fake-nwinfo1')
# Third save to update task state
@ -5427,8 +5427,8 @@ class ComputeTestCase(BaseTestCase):
# Confirm setup_compute_volume is called when volume is mounted.
def stupid(*args, **kwargs):
return fake_network.fake_get_instance_nw_info(self.stubs)
self.stubs.Set(nova.compute.manager.ComputeManager,
'_get_instance_nw_info', stupid)
self.stubs.Set(self.compute.network_api,
'get_instance_nw_info', stupid)
# creating instance testdata
instance = self._create_fake_instance_obj({'host': 'dummy'})
@ -6159,26 +6159,6 @@ class ComputeTestCase(BaseTestCase):
val = self.compute._running_deleted_instances(admin_context)
self.assertEqual(val, [instance1])
def test_get_instance_nw_info(self):
fake_network.unset_stub_network_methods(self.stubs)
fake_inst = fake_instance.fake_db_instance(uuid='fake-instance')
fake_nw_info = network_model.NetworkInfo()
self.mox.StubOutWithMock(self.compute.network_api,
'get_instance_nw_info')
self.compute.network_api.get_instance_nw_info(self.context,
mox.IsA(objects.Instance)).AndReturn(fake_nw_info)
self.mox.ReplayAll()
fake_inst_obj = objects.Instance._from_db_object(
self.context, objects.Instance(), fake_inst, [])
result = self.compute._get_instance_nw_info(self.context,
fake_inst_obj)
self.assertEqual(fake_nw_info, result)
def _heal_instance_info_cache(self, _get_instance_nw_info_raise=False):
# Update on every call for the test
self.flags(heal_instance_info_cache_interval=-1)
@ -6228,7 +6208,7 @@ class ComputeTestCase(BaseTestCase):
fake_instance_get_all_by_host)
self.stubs.Set(db, 'instance_get_by_uuid',
fake_instance_get_by_uuid)
self.stubs.Set(self.compute, '_get_instance_nw_info',
self.stubs.Set(self.compute.network_api, 'get_instance_nw_info',
fake_get_instance_nw_info)
# Make an instance appear to be still Building
@ -6564,8 +6544,8 @@ class ComputeTestCase(BaseTestCase):
self.mox.StubOutWithMock(self.compute,
'_get_instances_on_driver')
self.mox.StubOutWithMock(self.compute,
'_get_instance_nw_info')
self.mox.StubOutWithMock(self.compute.network_api,
'get_instance_nw_info')
self.mox.StubOutWithMock(self.compute,
'_get_instance_block_device_info')
self.mox.StubOutWithMock(self.compute,
@ -6574,9 +6554,8 @@ class ComputeTestCase(BaseTestCase):
self.compute._get_instances_on_driver(
fake_context, {'deleted': False}).AndReturn(instances)
self.compute._get_instance_nw_info(fake_context,
evacuated_instance).AndReturn(
'fake_network_info')
self.compute.network_api.get_instance_nw_info(
fake_context, evacuated_instance).AndReturn('fake_network_info')
self.compute._get_instance_block_device_info(
fake_context, evacuated_instance).AndReturn('fake_bdi')
self.compute._is_instance_storage_shared(fake_context,
@ -6611,8 +6590,8 @@ class ComputeTestCase(BaseTestCase):
self.mox.StubOutWithMock(self.compute,
'_get_instances_on_driver')
self.mox.StubOutWithMock(self.compute,
'_get_instance_nw_info')
self.mox.StubOutWithMock(self.compute.network_api,
'get_instance_nw_info')
self.mox.StubOutWithMock(self.compute,
'_get_instance_block_device_info')
self.mox.StubOutWithMock(self.compute.driver,
@ -6625,9 +6604,8 @@ class ComputeTestCase(BaseTestCase):
self.compute._get_instances_on_driver(
fake_context, {'deleted': False}).AndReturn(instances)
self.compute._get_instance_nw_info(fake_context,
evacuated_instance).AndReturn(
'fake_network_info')
self.compute.network_api.get_instance_nw_info(
fake_context, evacuated_instance).AndReturn('fake_network_info')
self.compute._get_instance_block_device_info(
fake_context, evacuated_instance).AndReturn('fake_bdi')
self.compute.driver.check_instance_shared_storage_local(fake_context,
@ -6667,8 +6645,8 @@ class ComputeTestCase(BaseTestCase):
self.mox.StubOutWithMock(self.compute,
'_get_instances_on_driver')
self.mox.StubOutWithMock(self.compute,
'_get_instance_nw_info')
self.mox.StubOutWithMock(self.compute.network_api,
'get_instance_nw_info')
self.mox.StubOutWithMock(self.compute,
'_get_instance_block_device_info')
self.mox.StubOutWithMock(self.compute.driver,
@ -6681,9 +6659,8 @@ class ComputeTestCase(BaseTestCase):
self.compute._get_instances_on_driver(
fake_context, {'deleted': False}).AndReturn(instances)
self.compute._get_instance_nw_info(fake_context,
evacuated_instance).AndReturn(
'fake_network_info')
self.compute.network_api.get_instance_nw_info(
fake_context, evacuated_instance).AndReturn('fake_network_info')
self.compute._get_instance_block_device_info(
fake_context, evacuated_instance).AndReturn('fake_bdi')
self.compute.driver.check_instance_shared_storage_local(fake_context,
@ -10987,7 +10964,7 @@ class ComputeRescheduleOrErrorTestCase(BaseTestCase):
with contextlib.nested(
mock.patch.object(self.context, 'elevated',
return_value=elevated_context),
mock.patch.object(self.compute, '_get_instance_nw_info',
mock.patch.object(self.compute.network_api, 'get_instance_nw_info',
side_effect=error),
mock.patch.object(self.compute,
'_get_instance_block_device_info'),

View File

@ -546,7 +546,8 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
self.mox.StubOutWithMock(self.compute, 'init_virt_events')
self.mox.StubOutWithMock(self.compute, '_get_instances_on_driver')
self.mox.StubOutWithMock(self.compute, '_init_instance')
self.mox.StubOutWithMock(self.compute, '_get_instance_nw_info')
self.mox.StubOutWithMock(self.compute.network_api,
'get_instance_nw_info')
self.compute.driver.init_host(host=our_host)
context.get_admin_context().AndReturn(self.context)
@ -559,9 +560,9 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
# simulate failed instance
self.compute._get_instances_on_driver(
self.context, {'deleted': False}).AndReturn([deleted_instance])
self.compute._get_instance_nw_info(self.context, deleted_instance
).AndRaise(exception.InstanceNotFound(
instance_id=deleted_instance['uuid']))
self.compute.network_api.get_instance_nw_info(
self.context, deleted_instance).AndRaise(
exception.InstanceNotFound(instance_id=deleted_instance['uuid']))
# ensure driver.destroy is called so that driver may
# clean up any dangling files
self.compute.driver.destroy(self.context, deleted_instance,
@ -1870,7 +1871,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
with contextlib.nested(
mock.patch.object(self.context, 'elevated',
return_value=self.context),
mock.patch.object(self.compute, '_get_instance_nw_info',
mock.patch.object(self.compute.network_api, 'get_instance_nw_info',
return_value=fake_nw_info),
mock.patch.object(self.compute, '_get_rescue_image',
return_value=rescue_image_meta),
@ -1938,7 +1939,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
with contextlib.nested(
mock.patch.object(self.context, 'elevated',
return_value=self.context),
mock.patch.object(self.compute, '_get_instance_nw_info',
mock.patch.object(self.compute.network_api, 'get_instance_nw_info',
return_value=fake_nw_info),
mock.patch.object(self.compute, '_notify_about_instance_usage'),
mock.patch.object(self.compute.driver, 'unrescue'),
@ -2111,14 +2112,14 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
mock.patch.object(self.compute, '_get_instances_on_driver',
return_value=[instance_1,
instance_2]),
mock.patch.object(self.compute, '_get_instance_nw_info',
mock.patch.object(self.compute.network_api, 'get_instance_nw_info',
return_value=None),
mock.patch.object(self.compute, '_get_instance_block_device_info',
return_value={}),
mock.patch.object(self.compute, '_is_instance_storage_shared',
return_value=False),
mock.patch.object(self.compute.driver, 'destroy')
) as (_get_instances_on_driver, _get_instance_nw_info,
) as (_get_instances_on_driver, get_instance_nw_info,
_get_instance_block_device_info, _is_instance_storage_shared,
destroy):
self.compute._destroy_evacuated_instances(self.context)
@ -3463,7 +3464,8 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
system_metadata={},
expected_attrs=['system_metadata'])
self.mox.StubOutWithMock(self.compute, '_get_instance_nw_info')
self.mox.StubOutWithMock(self.compute.network_api,
'get_instance_nw_info')
self.mox.StubOutWithMock(self.compute, '_allocate_network')
self.compute._allocate_network(self.context, instance,
self.requested_networks, None, self.security_groups, None)
@ -3477,7 +3479,8 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
system_metadata=dict(network_allocated='False'),
expected_attrs=['system_metadata'])
self.mox.StubOutWithMock(self.compute, '_get_instance_nw_info')
self.mox.StubOutWithMock(self.compute.network_api,
'get_instance_nw_info')
self.mox.StubOutWithMock(self.compute, '_allocate_network')
self.compute._allocate_network(self.context, instance,
self.requested_networks, None, self.security_groups, None)
@ -3494,14 +3497,16 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
def fake_network_info():
return network_model.NetworkInfo([{'address': '123.123.123.123'}])
self.mox.StubOutWithMock(self.compute, '_get_instance_nw_info')
self.mox.StubOutWithMock(self.compute.network_api,
'get_instance_nw_info')
self.mox.StubOutWithMock(self.compute, '_allocate_network')
self.mox.StubOutWithMock(self.compute.network_api,
'setup_instance_network_on_host')
self.compute.network_api.setup_instance_network_on_host(
self.context, instance, instance.host)
self.compute._get_instance_nw_info(self.context, instance).AndReturn(
network_model.NetworkInfoAsyncWrapper(fake_network_info))
self.compute.network_api.get_instance_nw_info(
self.context, instance).AndReturn(
network_model.NetworkInfoAsyncWrapper(fake_network_info))
self.mox.ReplayAll()
self.compute._build_networks_for_instance(self.context, instance,
@ -3657,7 +3662,7 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
mock.patch.object(self.migration, 'save'),
mock.patch.object(self.migration, 'obj_as_admin',
return_value=mock.MagicMock()),
mock.patch.object(self.compute, '_get_instance_nw_info',
mock.patch.object(self.compute.network_api, 'get_instance_nw_info',
return_value=None),
mock.patch.object(self.instance, 'save'),
mock.patch.object(self.compute, '_notify_about_instance_usage'),
@ -3692,7 +3697,7 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
# revert_resize() and the return value is passed to driver.destroy().
# Otherwise we could regress this.
@mock.patch.object(self.compute, '_get_instance_nw_info')
@mock.patch.object(self.compute.network_api, 'get_instance_nw_info')
@mock.patch.object(self.compute, '_is_instance_storage_shared')
@mock.patch.object(self.compute, 'finish_revert_resize')
@mock.patch.object(self.compute, '_instance_update')
@ -3714,7 +3719,7 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
_instance_update,
finish_revert_resize,
_is_instance_storage_shared,
_get_instance_nw_info):
get_instance_nw_info):
self.migration.source_compute = self.instance['host']

View File

@ -352,7 +352,6 @@ def set_stub_network_methods(stubs):
cm = compute_manager.ComputeManager
if not _real_functions:
_real_functions = {
'_get_instance_nw_info': cm._get_instance_nw_info,
'_allocate_network': cm._allocate_network,
'_deallocate_network': cm._deallocate_network}
@ -362,7 +361,6 @@ def set_stub_network_methods(stubs):
def fake_async_networkinfo(*args, **kwargs):
return network_model.NetworkInfoAsyncWrapper(fake_networkinfo)
stubs.Set(cm, '_get_instance_nw_info', fake_networkinfo)
stubs.Set(cm, '_allocate_network', fake_async_networkinfo)
stubs.Set(cm, '_deallocate_network', lambda *args, **kwargs: None)