From e2f2d10e1a59a6688c6f9763fff0fd45578da5eb Mon Sep 17 00:00:00 2001 From: Russell Bryant Date: Wed, 23 Jan 2013 17:34:13 -0500 Subject: Avoid db lookup in info_from_instance(). The method info_from_instance() in nova.notifications was doing a db lookup for the system_metadata for an instance. This patch updates that code to get that data from the instance that's passed in instead. The rest of the patch are related changes to make that happen. metadata_to_dict() was needed here. It lived in nova.compute.utils. nova.compute.utils already imported nova.notifications, so using it from there would have created a circular import. Move the method to nova.utils instead and update the tree to use it from its new location. I also noticed that the xen driver had a copy of metdata_to_dict(). This patch removes it and uses the common implementation in nova.utils. 'system_metadata' was added to _extra_keys of the Instance db model so that it would show up in a serialized instance. Tests failed without it as the result of getting instances via the conductor API did not include system_metadata. Now it's there. Part of bp no-db-compute. Change-Id: I451355fb26ae29f13b71438f7896c448b59f97b0 --- nova/tests/compute/test_compute.py | 2 +- nova/tests/compute/test_compute_utils.py | 14 +++----------- nova/tests/test_utils.py | 11 +++++++++++ 3 files changed, 15 insertions(+), 12 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/compute/test_compute.py b/nova/tests/compute/test_compute.py index b8212848c..38dcd5521 100644 --- a/nova/tests/compute/test_compute.py +++ b/nova/tests/compute/test_compute.py @@ -5287,7 +5287,7 @@ class ComputeAPITestCase(BaseTestCase): _context = context.get_admin_context() instance = self._create_fake_instance({'metadata': {'key1': 'value1'}}) - instance = dict(instance) + instance = dict(instance.iteritems()) metadata = self.compute_api.get_instance_metadata(_context, instance) self.assertEqual(metadata, {'key1': 'value1'}) diff --git a/nova/tests/compute/test_compute_utils.py b/nova/tests/compute/test_compute_utils.py index 6e7227d4c..4372039e0 100644 --- a/nova/tests/compute/test_compute_utils.py +++ b/nova/tests/compute/test_compute_utils.py @@ -359,6 +359,9 @@ class UsageInfoTestCase(test.TestCase): extra_usage_info = {'image_name': 'fake_name'} db.instance_system_metadata_update(self.context, instance['uuid'], sys_metadata, False) + # NOTE(russellb) Make sure our instance has the latest system_metadata + # in it. + instance = db.instance_get(self.context, instance_id) compute_utils.notify_about_instance_usage(self.context, instance, 'create.start', extra_usage_info=extra_usage_info) self.assertEquals(len(test_notifier.NOTIFICATIONS), 1) @@ -382,14 +385,3 @@ class UsageInfoTestCase(test.TestCase): image_ref_url = "%s/images/1" % glance.generate_glance_url() self.assertEquals(payload['image_ref_url'], image_ref_url) self.compute.terminate_instance(self.context, instance) - - -class MetadataToDictTestCase(test.TestCase): - def test_metadata_to_dict(self): - self.assertEqual(compute_utils.metadata_to_dict( - [{'key': 'foo1', 'value': 'bar'}, - {'key': 'foo2', 'value': 'baz'}]), - {'foo1': 'bar', 'foo2': 'baz'}) - - def test_metadata_to_dict_empty(self): - self.assertEqual(compute_utils.metadata_to_dict([]), {}) diff --git a/nova/tests/test_utils.py b/nova/tests/test_utils.py index 9eab72c5b..84d56cadf 100644 --- a/nova/tests/test_utils.py +++ b/nova/tests/test_utils.py @@ -778,3 +778,14 @@ class IntLikeTestCase(test.TestCase): self.assertFalse( utils.is_int_like("0cc3346e-9fef-4445-abe6-5d2b2690ec64")) self.assertFalse(utils.is_int_like("a1")) + + +class MetadataToDictTestCase(test.TestCase): + def test_metadata_to_dict(self): + self.assertEqual(utils.metadata_to_dict( + [{'key': 'foo1', 'value': 'bar'}, + {'key': 'foo2', 'value': 'baz'}]), + {'foo1': 'bar', 'foo2': 'baz'}) + + def test_metadata_to_dict_empty(self): + self.assertEqual(utils.metadata_to_dict([]), {}) -- cgit