From 85a307d3385ecfd641d7842c6450325410f2d3ba Mon Sep 17 00:00:00 2001 From: EdLeafe Date: Mon, 18 Apr 2016 21:36:09 +0000 Subject: [PATCH] Don't raise error when filtering on custom metadata Hosts can have custom metadata. There is no restriction on the key names used in this metadata, so we should not be raising an exception when checking for the existence of any metadata key. Originally worked on: https://review.openstack.org/#/c/271401 Co-Authored-By: Xiaowei Qian Closes-Bug: #1537062 Change-Id: Ie5ff3c1847e9c4533822a77d443e4ce1fcf047fe --- .../aggregate_image_properties_isolation.py | 11 ++++++- ...gate_image_properties_isolation_filters.py | 30 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/nova/scheduler/filters/aggregate_image_properties_isolation.py b/nova/scheduler/filters/aggregate_image_properties_isolation.py index bf83c6b50290..461f9120617e 100644 --- a/nova/scheduler/filters/aggregate_image_properties_isolation.py +++ b/nova/scheduler/filters/aggregate_image_properties_isolation.py @@ -17,6 +17,7 @@ from oslo_log import log as logging import six import nova.conf +from nova.i18n import _LW from nova.scheduler import filters from nova.scheduler.filters import utils @@ -45,7 +46,15 @@ class AggregateImagePropertiesIsolation(filters.BaseHostFilter): if (cfg_namespace and not key.startswith(cfg_namespace + cfg_separator)): continue - prop = image_props.get(key) + prop = None + try: + prop = image_props.get(key) + except AttributeError: + LOG.warning(_LW("Host '%(host)s' has a metadata key '%(key)s' " + "that is not present in the image metadata.") % + {"host": host_state.host, "key": key}) + continue + # NOTE(sbauza): Aggregate metadata is only strings, we need to # stringify the property to match with the option # TODO(sbauza): Fix that very ugly pattern matching diff --git a/nova/tests/unit/scheduler/filters/test_aggregate_image_properties_isolation_filters.py b/nova/tests/unit/scheduler/filters/test_aggregate_image_properties_isolation_filters.py index 025de7795abc..1a7ad672affe 100644 --- a/nova/tests/unit/scheduler/filters/test_aggregate_image_properties_isolation_filters.py +++ b/nova/tests/unit/scheduler/filters/test_aggregate_image_properties_isolation_filters.py @@ -103,3 +103,33 @@ class TestAggImagePropsIsolationFilter(test.NoDBTestCase): hw_vm_mode='hvm', img_owner_id='wrong'))) host = fakes.FakeHostState('host1', 'compute', {}) self.assertTrue(self.filt_cls.host_passes(host, spec_obj)) + + def test_aggregate_image_properties_iso_props_with_custom_meta(self, + agg_mock): + agg_mock.return_value = {'os': 'linux'} + spec_obj = objects.RequestSpec( + context=mock.sentinel.ctx, + image=objects.ImageMeta(properties=objects.ImageMetaProps( + os_type='linux'))) + host = fakes.FakeHostState('host1', 'compute', {}) + self.assertTrue(self.filt_cls.host_passes(host, spec_obj)) + + def test_aggregate_image_properties_iso_props_with_matching_meta_pass(self, + agg_mock): + agg_mock.return_value = {'os_type': 'linux'} + spec_obj = objects.RequestSpec( + context=mock.sentinel.ctx, + image=objects.ImageMeta(properties=objects.ImageMetaProps( + os_type='linux'))) + host = fakes.FakeHostState('host1', 'compute', {}) + self.assertTrue(self.filt_cls.host_passes(host, spec_obj)) + + def test_aggregate_image_properties_iso_props_with_matching_meta_fail( + self, agg_mock): + agg_mock.return_value = {'os_type': 'windows'} + spec_obj = objects.RequestSpec( + context=mock.sentinel.ctx, + image=objects.ImageMeta(properties=objects.ImageMetaProps( + os_type='linux'))) + host = fakes.FakeHostState('host1', 'compute', {}) + self.assertFalse(self.filt_cls.host_passes(host, spec_obj))