Handle unicode characters in migration params

There can be unicode characters in the params for live migration, for
example, the guest domain name in the destination XML. We need to
convert those to bytes when we call migrateToURI3 under python2.

The existing code was just calling str() for this, but that will fail
with the error:

  UnicodeEncodeError: 'ascii' codec can't encode characters...

We need to encode the unicode characters to do conversion.

The existing unit test wasn't using any unicode characters in its test
data, so this scenario wasn't covered.

Closes-Bug: #1768807

Change-Id: I4b34139a3c5e3e2b7cf7cbe50bdf3da3131b9b1c
This commit is contained in:
melanie witt 2018-08-10 20:28:41 +00:00
parent b2ff372535
commit f046d8b540
2 changed files with 8 additions and 4 deletions

View File

@ -646,15 +646,19 @@ class GuestTestCase(test.NoDBTestCase):
@testtools.skipIf(not six.PY2, 'libvirt python3 bindings accept unicode')
def test_migrate_v3_unicode(self):
dest_xml_template = "<domain type='qemu'><name>%s</name></domain>"
name = u'\u00CD\u00F1st\u00E1\u00F1c\u00E9'
dest_xml = dest_xml_template % name
expect_dest_xml = dest_xml_template % encodeutils.to_utf8(name)
self.guest.migrate('an-uri', flags=1, migrate_uri='dest-uri',
migrate_disks=[u"disk1", u"disk2"],
destination_xml='</xml>',
destination_xml=dest_xml,
bandwidth=2)
self.domain.migrateToURI3.assert_called_once_with(
'an-uri', flags=1, params={'migrate_uri': 'dest-uri',
'migrate_disks': ['disk1',
'disk2'],
'destination_xml': '</xml>',
'destination_xml': expect_dest_xml,
'bandwidth': 2})
def test_abort_job(self):

View File

@ -665,8 +665,8 @@ class Guest(object):
# In the Python2 libvirt bindings, strings passed to
# migrateToURI3 via params must not be unicode.
if six.PY2:
params = {key: str(value) if isinstance(value, unicode) # noqa
else value
params = {key: encodeutils.to_utf8(value)
if isinstance(value, six.text_type) else value
for key, value in params.items()}
self._domain.migrateToURI3(