Fix unicode string values missing in previous patch

Commit 61bedc9aa0 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
This commit is contained in:
Dan Smith
2013-09-26 13:53:36 -07:00
parent 1c854b230e
commit b738ea5cea
5 changed files with 28 additions and 5 deletions

View File

@@ -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,

View File

@@ -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,

View File

@@ -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):

View File

@@ -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',

View File

@@ -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)