4 # Copyright (C) 2006, 2007, 2008 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Script for unittesting the objects module"""
27 from ganeti import constants
28 from ganeti import objects
33 class SimpleObject(objects.ConfigObject):
34 __slots__ = ['a', 'b']
37 class TestDictState(unittest.TestCase):
38 """Simple dict tansformation tests"""
40 def testSimpleObjectToDict(self):
41 o1 = SimpleObject(a='1')
42 self.assertEquals(o1.ToDict(), {'a': '1'})
43 self.assertEquals(o1.__getstate__(), {'a': '1'})
44 self.assertEquals(o1.__getstate__(), o1.ToDict())
47 self.assertEquals(o1.ToDict(), {'a': 2, 'b': 5})
48 o2 = SimpleObject.FromDict(o1.ToDict())
49 self.assertEquals(o1.ToDict(), {'a': 2, 'b': 5})
52 class TestClusterObject(unittest.TestCase):
53 """Tests done on a L{objects.Cluster}"""
71 constants.HT_XEN_PVM: {
72 "root_path": "/dev/sda5",
79 self.fake_cl = objects.Cluster(hvparams=hvparams, os_hvp=os_hvp)
80 self.fake_cl.UpgradeConfig()
82 def testGetHVDefaults(self):
84 self.failUnlessEqual(cl.GetHVDefaults(constants.HT_FAKE),
85 cl.hvparams[constants.HT_FAKE])
86 self.failUnlessEqual(cl.GetHVDefaults(None), {})
87 self.failUnlessEqual(cl.GetHVDefaults(constants.HT_XEN_PVM,
88 os_name="lenny-image"),
89 cl.os_hvp["lenny-image"][constants.HT_XEN_PVM])
92 def testFillHvFullMerge(self):
104 fake_inst = objects.Instance(name="foobar",
106 hypervisor=constants.HT_FAKE,
107 hvparams=inst_hvparams)
108 self.assertEqual(fake_dict, self.fake_cl.FillHV(fake_inst))
110 def testFillHvGlobalParams(self):
111 fake_inst = objects.Instance(name="foobar",
113 hypervisor=constants.HT_FAKE,
115 self.assertEqual(self.fake_cl.hvparams[constants.HT_FAKE],
116 self.fake_cl.FillHV(fake_inst))
118 def testFillHvInstParams(self):
122 fake_inst = objects.Instance(name="foobar",
124 hypervisor=constants.HT_XEN_PVM,
125 hvparams=inst_hvparams)
126 self.assertEqual(inst_hvparams, self.fake_cl.FillHV(fake_inst))
128 def testFillHvEmptyParams(self):
129 fake_inst = objects.Instance(name="foobar",
131 hypervisor=constants.HT_XEN_PVM,
133 self.assertEqual({}, self.fake_cl.FillHV(fake_inst))
135 def testFillHvPartialParams(self):
137 fake_inst = objects.Instance(name="foobar",
139 hypervisor=constants.HT_XEN_PVM,
141 self.assertEqual(self.fake_cl.os_hvp[os][constants.HT_XEN_PVM],
142 self.fake_cl.FillHV(fake_inst))
145 if __name__ == '__main__':
146 testutils.GanetiTestProgram()