From b738ea5cea487204b4853ef9c4362562e2c13f0e Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Thu, 26 Sep 2013 13:53:36 -0700 Subject: [PATCH] Fix unicode string values missing in previous patch Commit 61bedc9aa04b0bac21e5c4bd17d6d2c09e2d9e01 lost some code around the str_or_none() helper in objects/utils.py during a rebase which caused almost all of our values to not actually be coerced to unicode. This adds that. Also, the instance.uuid value can't (and shouldn't) be unicode, so add a cstring() helper and convert it to use that so that the above change doesn't try to make uuid be unicode. Also, make str_value() enforce non-None-ness like cstring() to avoid bugs where we get 'None' unexpectedly, and add a test for that whole function, which was missing before. Change-Id: I941545d38aee10f140610b268fbd9c744f6545f9 --- nova/objects/instance.py | 5 +++-- nova/objects/instance_group.py | 5 +++-- nova/objects/utils.py | 10 +++++++++- nova/tests/compute/test_resource_tracker.py | 1 + nova/tests/objects/test_objects.py | 12 ++++++++++++ 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/nova/objects/instance.py b/nova/objects/instance.py index 7a7528e411cc..e38eb0089fa1 100644 --- a/nova/objects/instance.py +++ b/nova/objects/instance.py @@ -68,7 +68,8 @@ class Instance(base.NovaPersistentObject, base.NovaObject): # Version 1.6: Added pci_devices # Version 1.7: String attributes updated to support unicode # Version 1.8: 'security_groups' and 'pci_devices' cannot be None - VERSION = '1.8' + # Version 1.9: Make uuid a non-None real string + VERSION = '1.9' fields = { 'id': int, @@ -122,7 +123,7 @@ class Instance(base.NovaPersistentObject, base.NovaObject): 'os_type': obj_utils.str_or_none, 'architecture': obj_utils.str_or_none, 'vm_mode': obj_utils.str_or_none, - 'uuid': obj_utils.str_or_none, + 'uuid': obj_utils.cstring, 'root_device_name': obj_utils.str_or_none, 'default_ephemeral_device': obj_utils.str_or_none, diff --git a/nova/objects/instance_group.py b/nova/objects/instance_group.py index ea6adf33c2de..dde06f44109a 100644 --- a/nova/objects/instance_group.py +++ b/nova/objects/instance_group.py @@ -24,7 +24,8 @@ class InstanceGroup(base.NovaPersistentObject, base.NovaObject): # Version 1.0: Initial version # Version 1.1: String attributes updated to support unicode # Version 1.2: Use list/dict helpers for policies, metadetails, members - VERSION = '1.2' + # Version 1.3: Make uuid a non-None real string + VERSION = '1.3' fields = { 'id': int, @@ -32,7 +33,7 @@ class InstanceGroup(base.NovaPersistentObject, base.NovaObject): 'user_id': obj_utils.str_or_none, 'project_id': obj_utils.str_or_none, - 'uuid': obj_utils.str_or_none, + 'uuid': obj_utils.cstring, 'name': obj_utils.str_or_none, 'policies': obj_utils.list_of_strings_or_none, diff --git a/nova/objects/utils.py b/nova/objects/utils.py index 33554fbb4afb..674576353eef 100644 --- a/nova/objects/utils.py +++ b/nova/objects/utils.py @@ -55,6 +55,8 @@ def int_or_none(val): def str_value(val): + if val is None: + raise ValueError(_('None is not valid here')) return unicode(val) @@ -63,7 +65,13 @@ def str_or_none(val): if val is None: return val else: - return str(val) + return str_value(val) + + +def cstring(val): + if val is None: + raise ValueError(_('None is not valid here')) + return str(val) def ip_or_none(version): diff --git a/nova/tests/compute/test_resource_tracker.py b/nova/tests/compute/test_resource_tracker.py index c38fd5d13c9a..b61f43d5f29e 100644 --- a/nova/tests/compute/test_resource_tracker.py +++ b/nova/tests/compute/test_resource_tracker.py @@ -89,6 +89,7 @@ class FakeVirtDriver(driver.ComputeDriver): if self.pci_support: d['pci_passthrough_devices'] = jsonutils.dumps([{ 'label': 'forza-napoli', + 'dev_type': 'foo', 'compute_node_id': 1, 'address': '0000:00:00.1', 'product_id': 'p1', diff --git a/nova/tests/objects/test_objects.py b/nova/tests/objects/test_objects.py index c9233edb0c2b..97d07e835ebb 100644 --- a/nova/tests/objects/test_objects.py +++ b/nova/tests/objects/test_objects.py @@ -162,6 +162,18 @@ class TestUtils(test.TestCase): self.assertEqual(utils.str_or_none('foo'), 'foo') self.assertEqual(utils.str_or_none(1), '1') self.assertEqual(utils.str_or_none(None), None) + self.assertTrue(isinstance(utils.str_or_none('foo'), unicode)) + + def test_str_value(self): + self.assertEqual('foo', utils.str_value('foo')) + self.assertEqual('1', utils.str_value(1)) + self.assertRaises(ValueError, utils.str_value, None) + self.assertTrue(isinstance(utils.str_value('foo'), unicode)) + + def test_cstring(self): + self.assertEqual('foo', utils.cstring('foo')) + self.assertEqual('1', utils.cstring(1)) + self.assertRaises(ValueError, utils.cstring, None) def test_ip_or_none(self): ip4 = netaddr.IPAddress('1.2.3.4', 4)