From 902184f5dad8334c07e5bee0050376cdfc26f62c Mon Sep 17 00:00:00 2001 From: Andrea Rosa Date: Wed, 11 Sep 2013 16:24:09 +0100 Subject: [PATCH] Fix empty selector XML bug The XML serialization fails when it's trying to create a datum selector with no values. In that case the obj is an empty string so the selection of datum from it just fails. Instead of raising an exception the fix is returning an empty string, that allows to serialize Selector with an empty object. Fixes bug 1223358 Change-Id: If5d564c9b037eb0a107f4688ffa1aceb5281dc42 --- nova/api/openstack/xmlutil.py | 2 ++ nova/tests/api/openstack/test_xmlutil.py | 26 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/nova/api/openstack/xmlutil.py b/nova/api/openstack/xmlutil.py index 6e6962e3b6b5..e99f37c84d08 100644 --- a/nova/api/openstack/xmlutil.py +++ b/nova/api/openstack/xmlutil.py @@ -81,6 +81,8 @@ class Selector(object): if callable(elem): obj = elem(obj) else: + if obj == '': + return '' # Use indexing try: obj = obj[elem] diff --git a/nova/tests/api/openstack/test_xmlutil.py b/nova/tests/api/openstack/test_xmlutil.py index 864704901f6a..bd432d399270 100644 --- a/nova/tests/api/openstack/test_xmlutil.py +++ b/nova/tests/api/openstack/test_xmlutil.py @@ -724,6 +724,32 @@ class TemplateTest(test.NoDBTestCase): result = master.serialize(obj) self.assertEqual(expected_xml, result) + def test__serialize_with_empty_datum_selector(self): + # Our test object to serialize + obj = { + 'test': { + 'name': 'foobar', + 'image': '' + }, + } + + root = xmlutil.TemplateElement('test', selector='test', + name='name') + master = xmlutil.MasterTemplate(root, 1) + root_slave = xmlutil.TemplateElement('test', selector='test') + image = xmlutil.SubTemplateElement(root_slave, 'image', + selector='image') + image.set('id') + xmlutil.make_links(image, 'links') + slave = xmlutil.SlaveTemplate(root_slave, 1) + master.attach(slave) + + siblings = master._siblings() + result = master._serialize(None, obj, siblings) + self.assertEqual(result.tag, 'test') + self.assertEqual(result[0].tag, 'image') + self.assertEqual(result[0].get('id'), str(obj['test']['image'])) + class MasterTemplateBuilder(xmlutil.TemplateBuilder): def construct(self):