Merge "Alarm TimeConstraint display incorrect"

This commit is contained in:
Jenkins
2014-12-03 16:54:37 +00:00
committed by Gerrit Code Review
2 changed files with 38 additions and 4 deletions

View File

@@ -86,23 +86,25 @@ def format_nested_list_of_dict(l, column_names):
def print_dict(d, dict_property="Property", wrap=0):
pt = prettytable.PrettyTable([dict_property, 'Value'], print_empty=False)
pt.align = 'l'
for k, v in six.iteritems(d):
for k, v in sorted(six.iteritems(d)):
# convert dict to str to check length
if isinstance(v, dict):
v = str(v)
if wrap > 0:
v = textwrap.fill(str(v), wrap)
# if value has a newline, add in multiple rows
# e.g. fault with stacktrace
if v and isinstance(v, six.string_types) and r'\n' in v:
lines = v.strip().split(r'\n')
col1 = k
for line in lines:
if wrap > 0:
line = textwrap.fill(str(line), wrap)
pt.add_row([col1, line])
col1 = ''
else:
if wrap > 0:
v = textwrap.fill(str(v), wrap)
pt.add_row([k, v])
encoded = encodeutils.safe_encode(pt.get_string(sortby=dict_property))
encoded = encodeutils.safe_encode(pt.get_string())
# FIXME(gordc): https://bugs.launchpad.net/oslo-incubator/+bug/1370710
if six.PY3:
encoded = encoded.decode()

View File

@@ -39,6 +39,38 @@ class UtilsTest(test_utils.BaseTestCase):
| K | k |
| Key | Value |
+----------+-------+
''', stdout.getvalue())
with mock.patch('sys.stdout', new=six.StringIO()) as stdout:
utils.print_dict({'alarm_id': '262567fd-d79a-4bbb-a9d0-59d879b6',
'description': 'test alarm',
'state': 'insufficient data',
'repeat_actions': 'False',
'type': 'threshold',
'threshold': '1.0',
'statistic': 'avg',
'time_constraints': '[{name: c1,'
'\\n description: test,'
'\\n start: 0 18 * * *,'
'\\n duration: 1,'
'\\n timezone: US}]'})
self.assertEqual('''\
+------------------+----------------------------------+
| Property | Value |
+------------------+----------------------------------+
| alarm_id | 262567fd-d79a-4bbb-a9d0-59d879b6 |
| description | test alarm |
| repeat_actions | False |
| state | insufficient data |
| statistic | avg |
| threshold | 1.0 |
| time_constraints | [{name: c1, |
| | description: test, |
| | start: 0 18 * * *, |
| | duration: 1, |
| | timezone: US}] |
| type | threshold |
+------------------+----------------------------------+
''', stdout.getvalue())
def test_print_list(self):