Revision 65978cb7

b/test/py/ganeti.cli_unittest.py
25 25
import testutils
26 26
import time
27 27
import unittest
28
import yaml
28 29
from cStringIO import StringIO
29 30

  
30 31
from ganeti import constants
......
1200 1201
    self._RunTest(dict(data), expected)
1201 1202

  
1202 1203

  
1204
class TestFormatPolicyInfo(unittest.TestCase):
1205
  """Test case for cli.FormatPolicyInfo.
1206

  
1207
  These tests rely on cli._SerializeGenericInfo (tested elsewhere).
1208

  
1209
  """
1210
  def setUp(self):
1211
    # Policies are big, and we want to see the difference in case of an error
1212
    self.maxDiff = None
1213

  
1214
  def _RenameDictItem(self, parsed, old, new):
1215
    self.assertTrue(old in parsed)
1216
    self.assertTrue(new not in parsed)
1217
    parsed[new] = parsed[old]
1218
    del parsed[old]
1219

  
1220
  def _TranslateParsedNames(self, parsed):
1221
    for (pretty, raw) in [
1222
      ("bounds specs", constants.ISPECS_MINMAX),
1223
      ("enabled disk templates", constants.IPOLICY_DTS)
1224
      ]:
1225
      self._RenameDictItem(parsed, pretty, raw)
1226
    for minmax in parsed[constants.ISPECS_MINMAX]:
1227
      for key in minmax:
1228
        keyparts = key.split("/", 1)
1229
        if len(keyparts) > 1:
1230
          self._RenameDictItem(minmax, key, keyparts[0])
1231
    self.assertTrue(constants.IPOLICY_DTS in parsed)
1232
    parsed[constants.IPOLICY_DTS] = yaml.load("[%s]" %
1233
                                              parsed[constants.IPOLICY_DTS])
1234

  
1235
  @staticmethod
1236
  def _PrintAndParsePolicy(custom, effective, iscluster):
1237
    formatted = cli.FormatPolicyInfo(custom, effective, iscluster)
1238
    buf = StringIO()
1239
    cli._SerializeGenericInfo(buf, formatted, 0)
1240
    return yaml.load(buf.getvalue())
1241

  
1242
  def _PrintAndCheckParsed(self, policy):
1243
    parsed = self._PrintAndParsePolicy(policy, NotImplemented, True)
1244
    self._TranslateParsedNames(parsed)
1245
    self.assertEqual(parsed, policy)
1246

  
1247
  def _CompareClusterGroupItems(self, cluster, group, skip=None):
1248
    if isinstance(group, dict):
1249
      self.assertTrue(isinstance(cluster, dict))
1250
      if skip is None:
1251
        skip = frozenset()
1252
      self.assertEqual(frozenset(cluster.keys()).difference(skip),
1253
                       frozenset(group.keys()))
1254
      for key in group:
1255
        self._CompareClusterGroupItems(cluster[key], group[key])
1256
    elif isinstance(group, list):
1257
      self.assertTrue(isinstance(cluster, list))
1258
      self.assertEqual(len(cluster), len(group))
1259
      for (cval, gval) in zip(cluster, group):
1260
        self._CompareClusterGroupItems(cval, gval)
1261
    else:
1262
      self.assertTrue(isinstance(group, basestring))
1263
      self.assertEqual("default (%s)" % cluster, group)
1264

  
1265
  def _TestClusterVsGroup(self, policy):
1266
    cluster = self._PrintAndParsePolicy(policy, NotImplemented, True)
1267
    group = self._PrintAndParsePolicy({}, policy, False)
1268
    self._CompareClusterGroupItems(cluster, group, ["std"])
1269

  
1270
  def testWithDefaults(self):
1271
    self._PrintAndCheckParsed(constants.IPOLICY_DEFAULTS)
1272
    self._TestClusterVsGroup(constants.IPOLICY_DEFAULTS)
1273

  
1274

  
1203 1275
class TestCreateIPolicyFromOpts(unittest.TestCase):
1204 1276
  """Test case for cli.CreateIPolicyFromOpts."""
1205 1277
  def setUp(self):

Also available in: Unified diff