Revision 5401c39d

b/lib/mcpu.py
404 404
    if not (resultcheck_fn is None or resultcheck_fn(result)):
405 405
      logging.error("Expected opcode result matching %s, got %s",
406 406
                    resultcheck_fn, result)
407
      raise errors.OpResultError("Opcode result does not match %s, got %s" %
408
                                 (resultcheck_fn, result[:80]))
407
      raise errors.OpResultError("Opcode result does not match %s: %s" %
408
                                 (resultcheck_fn, utils.Truncate(result, 80)))
409 409

  
410 410
    return result
411 411

  
b/lib/utils/text.py
43 43
#: Shell param checker regexp
44 44
_SHELLPARAM_REGEX = re.compile(r"^[-a-zA-Z0-9._+/:%@]+$")
45 45

  
46
#: ASCII equivalent of unicode character 'HORIZONTAL ELLIPSIS' (U+2026)
47
_ASCII_ELLIPSIS = "..."
48

  
46 49

  
47 50
def MatchNameComponent(key, name_list, case_sensitive=True):
48 51
  """Try to match a name against a list.
......
556 559
    suffix = "th"
557 560

  
558 561
  return "%s%s" % (value, suffix)
562

  
563

  
564
def Truncate(text, length):
565
  """Truncate string and add ellipsis if needed.
566

  
567
  @type text: string
568
  @param text: Text
569
  @type length: integer
570
  @param length: Desired length
571
  @rtype: string
572
  @return: Truncated text
573

  
574
  """
575
  assert length > len(_ASCII_ELLIPSIS)
576

  
577
  # Serialize if necessary
578
  if not isinstance(text, basestring):
579
    text = str(text)
580

  
581
  if len(text) <= length:
582
    return text
583
  else:
584
    return text[:length - len(_ASCII_ELLIPSIS)] + _ASCII_ELLIPSIS
b/test/ganeti.utils.text_unittest.py
545 545
      self.assertEqual(utils.FormatOrdinal(value), ordinal)
546 546

  
547 547

  
548
class TestTruncate(unittest.TestCase):
549
  def _Test(self, text, length):
550
    result = utils.Truncate(text, length)
551
    self.assertTrue(len(result) <= length)
552
    return result
553

  
554
  def test(self):
555
    self.assertEqual(self._Test("", 80), "")
556
    self.assertEqual(self._Test("abc", 4), "abc")
557
    self.assertEqual(self._Test("Hello World", 80), "Hello World")
558
    self.assertEqual(self._Test("Hello World", 4), "H...")
559
    self.assertEqual(self._Test("Hello World", 5), "He...")
560

  
561
    for i in [4, 10, 100]:
562
      data = i * "FooBarBaz"
563
      self.assertEqual(self._Test(data, len(data)), data)
564

  
565
    for (length, exp) in [(8, u"T\u00e4st\u2026xyz"), (7, u"T\u00e4st...")]:
566
      self.assertEqual(self._Test(u"T\u00e4st\u2026xyz", length), exp)
567

  
568
    self.assertEqual(self._Test(range(100), 20), "[0, 1, 2, 3, 4, 5...")
569

  
570
  def testError(self):
571
    for i in range(4):
572
      self.assertRaises(AssertionError, utils.Truncate, "", i)
573

  
574

  
548 575
if __name__ == "__main__":
549 576
  testutils.GanetiTestProgram()

Also available in: Unified diff