virt: handle unicode when logging LifecycleEvents

The repr on the LifecycleEvent object includes a translated
name, which blows up with a UnicodeEncodeError in the emit_event
method because of str(event) and non-English locales.

This change uses six.text_type on the object rather than str
and adds a test to recreate the bug and show the fix.

Change-Id: I9b7b52739883043b7aae9759f500e5e21cfe8b30
Closes-Bug: #1621392
This commit is contained in:
Matt Riedemann 2016-09-08 11:14:52 -04:00
parent c51af64dae
commit 2b57b3d867
2 changed files with 15 additions and 2 deletions

View File

@ -800,6 +800,18 @@ class _VirtDriverTestCase(_FakeDriverBackendTestCase):
self.connection.emit_event(event1)
def test_emit_unicode_event(self):
"""Tests that we do not fail on translated unicode events."""
started_event = virtevent.LifecycleEvent(
"cef19ce0-0ca2-11df-855d-b19fbce37686",
virtevent.EVENT_LIFECYCLE_STARTED)
callback = mock.Mock()
self.connection.register_event_listener(callback)
with mock.patch.object(started_event, 'get_name',
return_value=u'\xF0\x9F\x92\xA9'):
self.connection.emit_event(started_event)
callback.assert_called_once_with(started_event)
def test_set_bootable(self):
self.assertRaises(NotImplementedError, self.connection.set_bootable,
'instance', True)

View File

@ -24,6 +24,7 @@ import sys
from oslo_log import log as logging
from oslo_utils import importutils
import six
import nova.conf
from nova.i18n import _, _LE, _LI
@ -1428,7 +1429,7 @@ class ComputeDriver(object):
"""
if not self._compute_event_callback:
LOG.debug("Discarding event %s", str(event))
LOG.debug("Discarding event %s", six.text_type(event))
return
if not isinstance(event, virtevent.Event):
@ -1436,7 +1437,7 @@ class ComputeDriver(object):
_("Event must be an instance of nova.virt.event.Event"))
try:
LOG.debug("Emitting event %s", str(event))
LOG.debug("Emitting event %s", six.text_type(event))
self._compute_event_callback(event)
except Exception as ex:
LOG.error(_LE("Exception dispatching event %(event)s: %(ex)s"),