From 3f02ca85654aac8b528e5ddfaaac62762d818684 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Tue, 16 Jun 2015 09:45:15 -0700 Subject: [PATCH] Fix up instance flavor usage in compute and network tests The change to fake_instance here has a couple impacts in the vmware driver, the test_server_password, and test_nova_manage tests. Change-Id: I48c4fbfcac66e9a50b29349cce6824e8985e7205 --- .../compute/contrib/test_server_password.py | 5 +++- nova/tests/unit/compute/test_compute.py | 12 ++++---- nova/tests/unit/fake_instance.py | 21 +++++++++++++- nova/tests/unit/network/test_api.py | 29 +++++-------------- nova/tests/unit/test_nova_manage.py | 8 ++--- .../unit/virt/vmwareapi/test_configdrive.py | 3 +- .../unit/virt/vmwareapi/test_driver_api.py | 3 +- 7 files changed, 45 insertions(+), 36 deletions(-) diff --git a/nova/tests/unit/api/openstack/compute/contrib/test_server_password.py b/nova/tests/unit/api/openstack/compute/contrib/test_server_password.py index 26e9f18c6232..7dbbaab765af 100644 --- a/nova/tests/unit/api/openstack/compute/contrib/test_server_password.py +++ b/nova/tests/unit/api/openstack/compute/contrib/test_server_password.py @@ -13,6 +13,8 @@ # License for the specific language governing permissions and limitations # under the License. +import mock + from oslo_config import cfg from nova.api.metadata import password @@ -65,7 +67,8 @@ class ServerPasswordTestV21(test.NoDBTestCase): self.assertEqual(res['password'], 'fakepass') def test_reset_password(self): - eval(self.delete_call)(self.fake_req, 'fake') + with mock.patch('nova.objects.Instance._save_flavor'): + eval(self.delete_call)(self.fake_req, 'fake') self.assertEqual(eval(self.delete_call).wsgi_code, 204) res = self.controller.index(self.fake_req, 'fake') diff --git a/nova/tests/unit/compute/test_compute.py b/nova/tests/unit/compute/test_compute.py index 1289eecea8bd..f205d1574e33 100644 --- a/nova/tests/unit/compute/test_compute.py +++ b/nova/tests/unit/compute/test_compute.py @@ -295,7 +295,9 @@ class BaseTestCase(test.TestCase): inst.updated_at = timeutils.utcnow() inst.launched_at = timeutils.utcnow() inst.security_groups = objects.SecurityGroupList(objects=[]) - flavors.save_flavor_info(inst.system_metadata, flavor) + inst.flavor = flavor + inst.old_flavor = None + inst.new_flavor = None if params: inst.update(params) if services: @@ -9085,10 +9087,10 @@ class ComputeAPITestCase(BaseTestCase): def test_attach_interface(self): new_type = flavors.get_flavor_by_flavor_id('4') - sys_meta = flavors.save_flavor_info({}, new_type) instance = objects.Instance(image_ref='foo', - system_metadata=sys_meta) + system_metadata={}, + flavor=new_type) self.mox.StubOutWithMock(self.compute.network_api, 'allocate_port_for_instance') nwinfo = [fake_network_cache_model.new_vif()] @@ -9109,9 +9111,9 @@ class ComputeAPITestCase(BaseTestCase): def test_attach_interface_failed(self): new_type = flavors.get_flavor_by_flavor_id('4') - sys_meta = flavors.save_flavor_info({}, new_type) instance = objects.Instance(uuid='fake_id', image_ref='foo', - system_metadata=sys_meta) + system_metadata={}, + flavor=new_type) nwinfo = [fake_network_cache_model.new_vif()] network_id = nwinfo[0]['network']['id'] port_id = nwinfo[0]['id'] diff --git a/nova/tests/unit/fake_instance.py b/nova/tests/unit/fake_instance.py index b57c3e55ae71..a675991e5bbb 100644 --- a/nova/tests/unit/fake_instance.py +++ b/nova/tests/unit/fake_instance.py @@ -101,9 +101,28 @@ def fake_db_instance(**updates): def fake_instance_obj(context, **updates): expected_attrs = updates.pop('expected_attrs', None) - return objects.Instance._from_db_object(context, + flavor = updates.pop('flavor', None) + if not flavor: + flavor = objects.Flavor(id=1, name='flavor1', + memory_mb=256, vcpus=1, + root_gb=1, ephemeral_gb=1, + flavorid='1', + swap=0, rxtx_factor=1.0, + vcpu_weight=1, + disabled=False, + is_public=True, + extra_specs={}, + projects=[]) + flavor.obj_reset_changes() + inst = objects.Instance._from_db_object(context, objects.Instance(), fake_db_instance(**updates), expected_attrs=expected_attrs) + if flavor: + inst.flavor = flavor + inst.old_flavor = None + inst.new_flavor = None + inst.obj_reset_changes() + return inst def fake_fault_obj(context, instance_uuid, code=404, diff --git a/nova/tests/unit/network/test_api.py b/nova/tests/unit/network/test_api.py index eea02024057b..8dc815b5a99d 100644 --- a/nova/tests/unit/network/test_api.py +++ b/nova/tests/unit/network/test_api.py @@ -38,9 +38,7 @@ from nova import policy from nova import test from nova.tests.unit import fake_instance from nova.tests.unit.objects import test_fixed_ip -from nova.tests.unit.objects import test_flavor from nova.tests.unit.objects import test_virtual_interface -from nova import utils FAKE_UUID = 'a47ae74e-ab08-547f-9eee-ffd23fc46c16' @@ -179,11 +177,9 @@ class ApiTestCase(test.TestCase): self.mox.ReplayAll() flavor = flavors.get_default_flavor() flavor['rxtx_factor'] = 0 - sys_meta = flavors.save_flavor_info({}, flavor) - instance = dict(id=1, uuid='uuid', project_id='project_id', - host='host', system_metadata=utils.dict_to_metadata(sys_meta)) - instance = fake_instance.fake_instance_obj( - self.context, expected_attrs=['system_metadata'], **instance) + instance = objects.Instance(id=1, uuid='uuid', project_id='project_id', + host='host', system_metadata={}, + flavor=flavor) self.network_api.allocate_for_instance( self.context, instance, 'vpn', 'requested_networks', macs=macs) @@ -292,12 +288,12 @@ class ApiTestCase(test.TestCase): def _stub_migrate_instance_calls(self, method, multi_host, info): fake_flavor = flavors.get_default_flavor() fake_flavor['rxtx_factor'] = 1.21 - sys_meta = flavors.save_flavor_info({}, fake_flavor) fake_instance = objects.Instance( uuid=uuid.uuid4().hex, project_id='fake_project_id', instance_type_id=fake_flavor['id'], - system_metadata=sys_meta) + flavor=fake_flavor, + system_metadata={}) fake_migration = {'source_compute': 'fake_compute_source', 'dest_compute': 'fake_compute_dest'} @@ -469,29 +465,20 @@ class ApiTestCase(test.TestCase): self.assertFalse(nwinfo_mock.called) def test_allocate_for_instance_refresh_cache(self): - sys_meta = flavors.save_flavor_info({}, test_flavor.fake_flavor) - instance = fake_instance.fake_instance_obj( - self.context, expected_attrs=['system_metadata'], - system_metadata=sys_meta) + instance = fake_instance.fake_instance_obj(self.context) vpn = 'fake-vpn' requested_networks = 'fake-networks' self._test_refresh_cache('allocate_for_instance', self.context, instance, vpn, requested_networks) def test_add_fixed_ip_to_instance_refresh_cache(self): - sys_meta = flavors.save_flavor_info({}, test_flavor.fake_flavor) - instance = fake_instance.fake_instance_obj( - self.context, expected_attrs=['system_metadata'], - system_metadata=sys_meta) + instance = fake_instance.fake_instance_obj(self.context) network_id = 'fake-network-id' self._test_refresh_cache('add_fixed_ip_to_instance', self.context, instance, network_id) def test_remove_fixed_ip_from_instance_refresh_cache(self): - sys_meta = flavors.save_flavor_info({}, test_flavor.fake_flavor) - instance = fake_instance.fake_instance_obj( - self.context, expected_attrs=['system_metadata'], - system_metadata=sys_meta) + instance = fake_instance.fake_instance_obj(self.context) address = 'fake-address' self._test_refresh_cache('remove_fixed_ip_from_instance', self.context, instance, address) diff --git a/nova/tests/unit/test_nova_manage.py b/nova/tests/unit/test_nova_manage.py index cb9986f04958..1edbfa5c2182 100644 --- a/nova/tests/unit/test_nova_manage.py +++ b/nova/tests/unit/test_nova_manage.py @@ -345,8 +345,8 @@ class VmCommandsTestCase(test.TestCase): get.return_value = objects.InstanceList( objects=[fake_instance.fake_instance_obj( context.get_admin_context(), host='foo-host', - instance_type=self.fake_flavor, - expected_attrs=('flavor'))]) + flavor=self.fake_flavor, + system_metadata={})]) self.commands.list() sys.stdout = sys.__stdout__ @@ -363,8 +363,8 @@ class VmCommandsTestCase(test.TestCase): get.return_value = objects.InstanceList( objects=[fake_instance.fake_instance_obj( context.get_admin_context(), - instance_type=self.fake_flavor, - expected_attrs=('flavor'))]) + flavor=self.fake_flavor, + system_metadata={})]) self.commands.list(host='fake-host') sys.stdout = sys.__stdout__ diff --git a/nova/tests/unit/virt/vmwareapi/test_configdrive.py b/nova/tests/unit/virt/vmwareapi/test_configdrive.py index 998e2d429f6c..20bec76ebbb5 100644 --- a/nova/tests/unit/virt/vmwareapi/test_configdrive.py +++ b/nova/tests/unit/virt/vmwareapi/test_configdrive.py @@ -66,7 +66,7 @@ class ConfigDriveTestCase(test.NoDBTestCase): 'ramdisk_id': '1', 'mac_addresses': [{'address': 'de:ad:be:ef:be:ef'}], 'memory_mb': 8192, - 'flavor': 'm1.large', + 'flavor': objects.Flavor(extra_specs={}), 'instance_type_id': 0, 'vcpus': 4, 'root_gb': 80, @@ -82,7 +82,6 @@ class ConfigDriveTestCase(test.NoDBTestCase): } self.test_instance = fake_instance.fake_instance_obj(self.context, **instance_values) - self.test_instance.flavor = objects.Flavor(extra_specs={}) (image_service, image_id) = glance.get_remote_image_service(context, image_ref) diff --git a/nova/tests/unit/virt/vmwareapi/test_driver_api.py b/nova/tests/unit/virt/vmwareapi/test_driver_api.py index c29135e76a35..0c309ae5514d 100644 --- a/nova/tests/unit/virt/vmwareapi/test_driver_api.py +++ b/nova/tests/unit/virt/vmwareapi/test_driver_api.py @@ -350,7 +350,7 @@ class VMwareAPIVMTestCase(test.NoDBTestCase): 'kernel_id': "fake_kernel_uuid", 'ramdisk_id': "fake_ramdisk_uuid", 'mac_address': "de:ad:be:ef:be:ef", - 'flavor': instance_type, + 'flavor': objects.Flavor(**self.type_data), 'node': node, 'memory_mb': self.type_data['memory_mb'], 'root_gb': self.type_data['root_gb'], @@ -365,7 +365,6 @@ class VMwareAPIVMTestCase(test.NoDBTestCase): self.uuid = uuid self.instance = fake_instance.fake_instance_obj( self.context, **values) - self.instance.flavor = objects.Flavor(**self.type_data) def _create_vm(self, node=None, num_instances=1, uuid=None, instance_type='m1.large', powered_on=True,