commit da5f09ef60fae8766b298be6789c02b59073c280
Author: Bernardo Dal Seno <bdalseno@google.com>
Date:   Tue Feb 19 22:14:13 2013 +0100

    Refactor ispecs in ipolicy structures
    
    Minimum and maximum instance specs are put together into a single element
    of the instance policy. This is in preparation for introducing multiple
    min/max specs.
    
    Signed-off-by: Bernardo Dal Seno <bdalseno@google.com>
    Reviewed-by: Helga Velroyen <helgav@google.com>

diff --git a/test/py/ganeti.objects_unittest.py b/test/py/ganeti.objects_unittest.py
index a90afb2..720b22a 100755
--- a/test/py/ganeti.objects_unittest.py
+++ b/test/py/ganeti.objects_unittest.py
@@ -1,7 +1,7 @@
 #!/usr/bin/python
 #
 
-# Copyright (C) 2006, 2007, 2008, 2010, 2012 Google Inc.
+# Copyright (C) 2006, 2007, 2008, 2010, 2012, 2013 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -414,9 +414,13 @@ class TestInstancePolicy(unittest.TestCase):
 
   def _AssertIPolicyIsFull(self, policy):
     self.assertEqual(frozenset(policy.keys()), constants.IPOLICY_ALL_KEYS)
-    for key in constants.IPOLICY_ISPECS:
-      spec = policy[key]
-      self.assertEqual(frozenset(spec.keys()), constants.ISPECS_PARAMETERS)
+    minmax = policy[constants.ISPECS_MINMAX]
+    self.assertEqual(frozenset(minmax.keys()), constants.ISPECS_MINMAX_KEYS)
+    for key in constants.ISPECS_MINMAX_KEYS:
+      self.assertEqual(frozenset(minmax[key].keys()),
+                       constants.ISPECS_PARAMETERS)
+    self.assertEqual(frozenset(policy[constants.ISPECS_STD].keys()),
+                     constants.ISPECS_PARAMETERS)
 
   def testDefaultIPolicy(self):
     objects.InstancePolicy.CheckParameterSyntax(constants.IPOLICY_DEFAULTS,
@@ -426,28 +430,29 @@ class TestInstancePolicy(unittest.TestCase):
   def testCheckISpecSyntax(self):
     par = "my_parameter"
     for check_std in [True, False]:
-      if check_std:
-        allkeys = constants.IPOLICY_ISPECS
-      else:
-        allkeys = constants.IPOLICY_ISPECS - frozenset([constants.ISPECS_STD])
       # Only one policy limit
-      for key in allkeys:
-        policy = dict((k, {}) for k in allkeys)
-        policy[key][par] = 11
-        objects.InstancePolicy.CheckISpecSyntax(policy, par, check_std)
+      for key in constants.ISPECS_MINMAX_KEYS:
+        minmax = dict((k, {}) for k in constants.ISPECS_MINMAX_KEYS)
+        minmax[key][par] = 11
+        objects.InstancePolicy.CheckISpecSyntax(minmax, {}, par, check_std)
+      if check_std:
+        minmax = dict((k, {}) for k in constants.ISPECS_MINMAX_KEYS)
+        stdspec = {par: 11}
+        objects.InstancePolicy.CheckISpecSyntax(minmax, stdspec, par, check_std)
+
       # Min and max only
       good_values = [(11, 11), (11, 40), (0, 0)]
       for (mn, mx) in good_values:
-        policy = dict((k, {}) for k in allkeys)
-        policy[constants.ISPECS_MIN][par] = mn
-        policy[constants.ISPECS_MAX][par] = mx
-        objects.InstancePolicy.CheckISpecSyntax(policy, par, check_std)
-      policy = dict((k, {}) for k in allkeys)
-      policy[constants.ISPECS_MIN][par] = 11
-      policy[constants.ISPECS_MAX][par] = 5
+        minmax = dict((k, {}) for k in constants.ISPECS_MINMAX_KEYS)
+        minmax[constants.ISPECS_MIN][par] = mn
+        minmax[constants.ISPECS_MAX][par] = mx
+        objects.InstancePolicy.CheckISpecSyntax(minmax, {}, par, check_std)
+      minmax = dict((k, {}) for k in constants.ISPECS_MINMAX_KEYS)
+      minmax[constants.ISPECS_MIN][par] = 11
+      minmax[constants.ISPECS_MAX][par] = 5
       self.assertRaises(errors.ConfigurationError,
                         objects.InstancePolicy.CheckISpecSyntax,
-                        policy, par, check_std)
+                        minmax, {}, par, check_std)
     # Min, std, max
     good_values = [
       (11, 11, 11),
@@ -455,12 +460,12 @@ class TestInstancePolicy(unittest.TestCase):
       (11, 40, 40),
       ]
     for (mn, st, mx) in good_values:
-      policy = {
+      minmax = {
         constants.ISPECS_MIN: {par: mn},
-        constants.ISPECS_STD: {par: st},
         constants.ISPECS_MAX: {par: mx},
         }
-      objects.InstancePolicy.CheckISpecSyntax(policy, par, True)
+      stdspec = {par: st}
+      objects.InstancePolicy.CheckISpecSyntax(minmax, stdspec, par, True)
     bad_values = [
       (11, 11,  5),
       (40, 11, 11),
@@ -470,14 +475,14 @@ class TestInstancePolicy(unittest.TestCase):
       (40, 40, 11),
       ]
     for (mn, st, mx) in bad_values:
-      policy = {
+      minmax = {
         constants.ISPECS_MIN: {par: mn},
-        constants.ISPECS_STD: {par: st},
         constants.ISPECS_MAX: {par: mx},
         }
+      stdspec = {par: st}
       self.assertRaises(errors.ConfigurationError,
                         objects.InstancePolicy.CheckISpecSyntax,
-                        policy, par, True)
+                        minmax, stdspec, par, True)
 
   def testCheckDiskTemplates(self):
     invalid = "this_is_not_a_good_template"
@@ -499,18 +504,14 @@ class TestInstancePolicy(unittest.TestCase):
   def testCheckParameterSyntax(self):
     invalid = "this_key_shouldnt_be_here"
     for check_std in [True, False]:
-      self.assertRaises(KeyError,
-                        objects.InstancePolicy.CheckParameterSyntax,
-                        {}, check_std)
-      policy = objects.MakeEmptyIPolicy()
-      policy[invalid] = None
+      objects.InstancePolicy.CheckParameterSyntax({}, check_std)
+      policy = {invalid: None}
       self.assertRaises(errors.ConfigurationError,
                         objects.InstancePolicy.CheckParameterSyntax,
                         policy, check_std)
       for par in constants.IPOLICY_PARAMETERS:
-        policy = objects.MakeEmptyIPolicy()
         for val in ("blah", None, {}, [42]):
-          policy[par] = val
+          policy = {par: val}
           self.assertRaises(errors.ConfigurationError,
                             objects.InstancePolicy.CheckParameterSyntax,
                             policy, check_std)
@@ -530,7 +531,12 @@ class TestInstancePolicy(unittest.TestCase):
   def _AssertIPolicyMerged(self, default_pol, diff_pol, merged_pol):
     for (key, value) in merged_pol.items():
       if key in diff_pol:
-        if key in constants.IPOLICY_ISPECS:
+        if key == constants.ISPECS_MINMAX:
+          self.assertEqual(frozenset(value), constants.ISPECS_MINMAX_KEYS)
+          for k in constants.ISPECS_MINMAX_KEYS:
+            self._AssertISpecsMerged(default_pol[key][k], diff_pol[key][k],
+                                     value[k])
+        elif key == constants.ISPECS_STD:
           self._AssertISpecsMerged(default_pol[key], diff_pol[key], value)
         else:
           self.assertEqual(value, diff_pol[key])
@@ -550,17 +556,28 @@ class TestInstancePolicy(unittest.TestCase):
       self._AssertIPolicyMerged(constants.IPOLICY_DEFAULTS, diff_pol, policy)
 
   def testFillIPolicySpecs(self):
-    partial_policies = [
-      {constants.ISPECS_MIN: {constants.ISPEC_MEM_SIZE: 32},
-       constants.ISPECS_MAX: {constants.ISPEC_CPU_COUNT: 1024}},
-      {constants.ISPECS_STD: {constants.ISPEC_DISK_SIZE: 2048},
-       constants.ISPECS_MAX: {
-          constants.ISPEC_DISK_COUNT: constants.MAX_DISKS - 1,
-          constants.ISPEC_NIC_COUNT: constants.MAX_NICS - 1,
-          }},
-      {constants.ISPECS_STD: {constants.ISPEC_SPINDLE_USE: 3}},
+    partial_ipolicies = [
+      {
+        constants.ISPECS_MINMAX: {
+          constants.ISPECS_MIN: {constants.ISPEC_MEM_SIZE: 32},
+          constants.ISPECS_MAX: {constants.ISPEC_CPU_COUNT: 1024}
+          },
+        },
+      {
+        constants.ISPECS_MINMAX: {
+          constants.ISPECS_MAX: {
+            constants.ISPEC_DISK_COUNT: constants.MAX_DISKS - 1,
+            constants.ISPEC_NIC_COUNT: constants.MAX_NICS - 1,
+            },
+          constants.ISPECS_MIN: {},
+          },
+          constants.ISPECS_STD: {constants.ISPEC_DISK_SIZE: 2048},
+        },
+      {
+        constants.ISPECS_STD: {constants.ISPEC_SPINDLE_USE: 3},
+        },
       ]
-    for diff_pol in partial_policies:
+    for diff_pol in partial_ipolicies:
       policy = objects.FillIPolicy(constants.IPOLICY_DEFAULTS, diff_pol)
       objects.InstancePolicy.CheckParameterSyntax(policy, True)
       self._AssertIPolicyIsFull(policy)
