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
(cherry picked from commit 2b57b3d867
)
This commit is contained in:
parent
7cc81c0e1d
commit
53a5d12ad5
@ -805,6 +805,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)
|
||||
|
@ -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
|
||||
@ -1432,7 +1433,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):
|
||||
@ -1440,7 +1441,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"),
|
||||
|
Loading…
Reference in New Issue
Block a user