Truncate encoded instance message to 255

In nova/compute/utils.py function exception_to_dict truncates unicode
message to 255. However, in non-English locales, instance message may
be longer than 255 after encoding the unicode message to byte message.
Need to truncate the encoded byte message to 255, in order to ensure
db insert operation succeed.

Change-Id: I70cd2d6e4590ea95510c1ea3d51287f0dea14956
Closes-Bug: 1389102
This commit is contained in:
Qin Zhao 2014-11-04 16:57:45 +08:00
parent 89cd6a0c49
commit bd9c32289f
2 changed files with 13 additions and 1 deletions

View File

@ -19,6 +19,7 @@ import string
import traceback
from oslo.config import cfg
from oslo.utils import encodeutils
from nova import block_device
from nova.compute import flavors
@ -64,7 +65,7 @@ def exception_to_dict(fault):
# NOTE(dripton) The message field in the database is limited to 255 chars.
# MySQL silently truncates overly long messages, but PostgreSQL throws an
# error if we don't truncate it.
u_message = unicode(message)[:255]
u_message = encodeutils.safe_decode(encodeutils.safe_encode(message)[:255])
fault_dict = dict(exception=fault)
fault_dict["message"] = u_message

View File

@ -22,6 +22,7 @@ import string
import mock
from oslo.config import cfg
from oslo.serialization import jsonutils
from oslo.utils import encodeutils
from oslo.utils import importutils
import six
import testtools
@ -825,3 +826,13 @@ class ComputeUtilsGetRebootTypes(test.TestCase):
def test_get_reboot_not_running_hard(self):
reboot_type = compute_utils.get_reboot_type('foo', 'bar')
self.assertEqual(reboot_type, 'HARD')
class ComputeUtilsTestCase(test.NoDBTestCase):
def test_exception_to_dict_with_long_message(self):
# Generate Russian byte message whose length is 300
msg = encodeutils.safe_decode(' \xd0\xb2' * 100)
exc = exception.NovaException(message=msg)
fault_dict = compute_utils.exception_to_dict(exc)
byte_message = encodeutils.safe_encode(fault_dict["message"])
self.assertEqual(255, len(byte_message))