All the hypervisors are supposed to exist in the config file, but it might not be so after upgrades from old versions. This patch ensures that all the missing hypervisors are added with their default values to the config file.
Also, some tests are adapted, because now they receive the default values instead of an empty dictionary when they are working using a minimal cluster config as their input. Fixes Issue 640. Signed-off-by: Michele Tartara <[email protected]> --- lib/objects.py | 8 ++++++-- test/py/ganeti.objects_unittest.py | 23 +++++++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/objects.py b/lib/objects.py index 6a2fe36..4c986d4 100644 --- a/lib/objects.py +++ b/lib/objects.py @@ -1597,9 +1597,13 @@ class Cluster(TaggableObject): if self.hvparams is None: self.hvparams = constants.HVC_DEFAULTS else: - for hypervisor in self.hvparams: + for hypervisor in constants.HYPER_TYPES: + try: + existing_params = self.hvparams[hypervisor] + except KeyError: + existing_params = {} self.hvparams[hypervisor] = FillDict( - constants.HVC_DEFAULTS[hypervisor], self.hvparams[hypervisor]) + constants.HVC_DEFAULTS[hypervisor], existing_params) if self.os_hvp is None: self.os_hvp = {} diff --git a/test/py/ganeti.objects_unittest.py b/test/py/ganeti.objects_unittest.py index db80e07..97594f8 100755 --- a/test/py/ganeti.objects_unittest.py +++ b/test/py/ganeti.objects_unittest.py @@ -93,9 +93,10 @@ class TestClusterObject(unittest.TestCase): self.failUnlessEqual(cl.GetHVDefaults(constants.HT_FAKE), cl.hvparams[constants.HT_FAKE]) self.failUnlessEqual(cl.GetHVDefaults(None), {}) - self.failUnlessEqual(cl.GetHVDefaults(constants.HT_XEN_PVM, - os_name="lenny-image"), - cl.os_hvp["lenny-image"][constants.HT_XEN_PVM]) + defaults = cl.GetHVDefaults(constants.HT_XEN_PVM, + os_name="lenny-image") + for param, value in cl.os_hvp["lenny-image"][constants.HT_XEN_PVM].items(): + self.assertEqual(value, defaults[param]) def testFillHvFullMerge(self): inst_hvparams = { @@ -132,14 +133,19 @@ class TestClusterObject(unittest.TestCase): os="ubuntu-hardy", hypervisor=constants.HT_XEN_PVM, hvparams=inst_hvparams) - self.assertEqual(inst_hvparams, self.fake_cl.FillHV(fake_inst)) + filled_conf = self.fake_cl.FillHV(fake_inst) + for param, value in constants.HVC_DEFAULTS[constants.HT_XEN_PVM].items(): + if param == "blah": + value = "blubb" + self.assertEqual(value, filled_conf[param]) - def testFillHvEmptyParams(self): + def testFillHvDefaultParams(self): fake_inst = objects.Instance(name="foobar", os="ubuntu-hardy", hypervisor=constants.HT_XEN_PVM, hvparams={}) - self.assertEqual({}, self.fake_cl.FillHV(fake_inst)) + self.assertEqual(constants.HVC_DEFAULTS[constants.HT_XEN_PVM], + self.fake_cl.FillHV(fake_inst)) def testFillHvPartialParams(self): os = "lenny-image" @@ -147,8 +153,9 @@ class TestClusterObject(unittest.TestCase): os=os, hypervisor=constants.HT_XEN_PVM, hvparams={}) - self.assertEqual(self.fake_cl.os_hvp[os][constants.HT_XEN_PVM], - self.fake_cl.FillHV(fake_inst)) + filled_conf = self.fake_cl.FillHV(fake_inst) + for param, value in self.fake_cl.os_hvp[os][constants.HT_XEN_PVM].items(): + self.assertEqual(value, filled_conf[param]) def testFillNdParamsCluster(self): fake_node = objects.Node(name="test", -- 1.8.5.1
