Commit 65978cb7 authored by Bernardo Dal Seno's avatar Bernardo Dal Seno

Unit test for cli.FormatPolicyInfo()

We try to parse the string obtained by formatting a policy, and see if we
get the same policy.
Signed-off-by: default avatarBernardo Dal Seno <>
Reviewed-by: default avatarHelga Velroyen <>
parent ef99e3e8
......@@ -25,6 +25,7 @@ import copy
import testutils
import time
import unittest
import yaml
from cStringIO import StringIO
from ganeti import constants
......@@ -1200,6 +1201,77 @@ class TestSerializeGenericInfo(unittest.TestCase):
self._RunTest(dict(data), expected)
class TestFormatPolicyInfo(unittest.TestCase):
"""Test case for cli.FormatPolicyInfo.
These tests rely on cli._SerializeGenericInfo (tested elsewhere).
def setUp(self):
# Policies are big, and we want to see the difference in case of an error
self.maxDiff = None
def _RenameDictItem(self, parsed, old, new):
self.assertTrue(old in parsed)
self.assertTrue(new not in parsed)
parsed[new] = parsed[old]
del parsed[old]
def _TranslateParsedNames(self, parsed):
for (pretty, raw) in [
("bounds specs", constants.ISPECS_MINMAX),
("enabled disk templates", constants.IPOLICY_DTS)
self._RenameDictItem(parsed, pretty, raw)
for minmax in parsed[constants.ISPECS_MINMAX]:
for key in minmax:
keyparts = key.split("/", 1)
if len(keyparts) > 1:
self._RenameDictItem(minmax, key, keyparts[0])
self.assertTrue(constants.IPOLICY_DTS in parsed)
parsed[constants.IPOLICY_DTS] = yaml.load("[%s]" %
def _PrintAndParsePolicy(custom, effective, iscluster):
formatted = cli.FormatPolicyInfo(custom, effective, iscluster)
buf = StringIO()
cli._SerializeGenericInfo(buf, formatted, 0)
return yaml.load(buf.getvalue())
def _PrintAndCheckParsed(self, policy):
parsed = self._PrintAndParsePolicy(policy, NotImplemented, True)
self.assertEqual(parsed, policy)
def _CompareClusterGroupItems(self, cluster, group, skip=None):
if isinstance(group, dict):
self.assertTrue(isinstance(cluster, dict))
if skip is None:
skip = frozenset()
for key in group:
self._CompareClusterGroupItems(cluster[key], group[key])
elif isinstance(group, list):
self.assertTrue(isinstance(cluster, list))
self.assertEqual(len(cluster), len(group))
for (cval, gval) in zip(cluster, group):
self._CompareClusterGroupItems(cval, gval)
self.assertTrue(isinstance(group, basestring))
self.assertEqual("default (%s)" % cluster, group)
def _TestClusterVsGroup(self, policy):
cluster = self._PrintAndParsePolicy(policy, NotImplemented, True)
group = self._PrintAndParsePolicy({}, policy, False)
self._CompareClusterGroupItems(cluster, group, ["std"])
def testWithDefaults(self):
class TestCreateIPolicyFromOpts(unittest.TestCase):
"""Test case for cli.CreateIPolicyFromOpts."""
def setUp(self):
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