Commit 63b883de authored by Michael Hanselmann's avatar Michael Hanselmann Committed by Guido Trotter

Add utility to format dictionary as key=value strings

This will be used in QA to format network interface parameters.

This is a cherry-pick of master commit eac9b7b8Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarGuido Trotter <ultrotter@google.com>
parent c12e30ca
......@@ -588,3 +588,15 @@ def Truncate(text, length):
return text
else:
return text[:length - len(_ASCII_ELLIPSIS)] + _ASCII_ELLIPSIS
def FormatKeyValue(data):
"""Formats a dictionary as "key=value" parameters.
The keys are sorted to have a stable order.
@type data: dict
@rtype: list of string
"""
return ["%s=%s" % (key, value) for (key, value) in sorted(data.items())]
......@@ -576,5 +576,16 @@ class TestTruncate(unittest.TestCase):
self.assertRaises(AssertionError, utils.Truncate, "", i)
class TestFormatKeyValue(unittest.TestCase):
def test(self):
self.assertEqual(utils.FormatKeyValue({}), [])
self.assertEqual(utils.FormatKeyValue({1: 2}), ["1=2"])
self.assertEqual(utils.FormatKeyValue({
"zzz": "0",
"aaa": "1",
}),
["aaa=1", "zzz=0"])
if __name__ == "__main__":
testutils.GanetiTestProgram()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment