root / test / ganeti.objects_unittest.py @ e9f7d26a
History | View | Annotate | Download (9.6 kB)
1 | 4c14965f | Guido Trotter | #!/usr/bin/python
|
---|---|---|---|
2 | 4c14965f | Guido Trotter | #
|
3 | 4c14965f | Guido Trotter | |
4 | 870dc44c | Iustin Pop | # Copyright (C) 2006, 2007, 2008, 2010 Google Inc.
|
5 | 4c14965f | Guido Trotter | #
|
6 | 4c14965f | Guido Trotter | # This program is free software; you can redistribute it and/or modify
|
7 | 4c14965f | Guido Trotter | # it under the terms of the GNU General Public License as published by
|
8 | 4c14965f | Guido Trotter | # the Free Software Foundation; either version 2 of the License, or
|
9 | 4c14965f | Guido Trotter | # (at your option) any later version.
|
10 | 4c14965f | Guido Trotter | #
|
11 | 4c14965f | Guido Trotter | # This program is distributed in the hope that it will be useful, but
|
12 | 4c14965f | Guido Trotter | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 4c14965f | Guido Trotter | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 4c14965f | Guido Trotter | # General Public License for more details.
|
15 | 4c14965f | Guido Trotter | #
|
16 | 4c14965f | Guido Trotter | # You should have received a copy of the GNU General Public License
|
17 | 4c14965f | Guido Trotter | # along with this program; if not, write to the Free Software
|
18 | 4c14965f | Guido Trotter | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 4c14965f | Guido Trotter | # 0.0510-1301, USA.
|
20 | 4c14965f | Guido Trotter | |
21 | 4c14965f | Guido Trotter | |
22 | 4c14965f | Guido Trotter | """Script for unittesting the objects module"""
|
23 | 4c14965f | Guido Trotter | |
24 | 4c14965f | Guido Trotter | |
25 | 4c14965f | Guido Trotter | import unittest |
26 | 4c14965f | Guido Trotter | |
27 | 7b64b9ea | René Nussbaumer | from ganeti import constants |
28 | 4c14965f | Guido Trotter | from ganeti import objects |
29 | 6a050007 | Michael Hanselmann | from ganeti import errors |
30 | 4c14965f | Guido Trotter | |
31 | 25231ec5 | Michael Hanselmann | import testutils |
32 | 25231ec5 | Michael Hanselmann | |
33 | 25231ec5 | Michael Hanselmann | |
34 | 4c14965f | Guido Trotter | class SimpleObject(objects.ConfigObject): |
35 | 4c14965f | Guido Trotter | __slots__ = ['a', 'b'] |
36 | 4c14965f | Guido Trotter | |
37 | 25231ec5 | Michael Hanselmann | |
38 | 4c14965f | Guido Trotter | class TestDictState(unittest.TestCase): |
39 | 4c14965f | Guido Trotter | """Simple dict tansformation tests"""
|
40 | 4c14965f | Guido Trotter | |
41 | 4c14965f | Guido Trotter | def testSimpleObjectToDict(self): |
42 | 4c14965f | Guido Trotter | o1 = SimpleObject(a='1')
|
43 | 4c14965f | Guido Trotter | self.assertEquals(o1.ToDict(), {'a': '1'}) |
44 | 4c14965f | Guido Trotter | self.assertEquals(o1.__getstate__(), {'a': '1'}) |
45 | 4c14965f | Guido Trotter | self.assertEquals(o1.__getstate__(), o1.ToDict())
|
46 | 4c14965f | Guido Trotter | o1.a = 2
|
47 | 4c14965f | Guido Trotter | o1.b = 5
|
48 | 4c14965f | Guido Trotter | self.assertEquals(o1.ToDict(), {'a': 2, 'b': 5}) |
49 | 4c14965f | Guido Trotter | o2 = SimpleObject.FromDict(o1.ToDict()) |
50 | 4c14965f | Guido Trotter | self.assertEquals(o1.ToDict(), {'a': 2, 'b': 5}) |
51 | 4c14965f | Guido Trotter | |
52 | 4c14965f | Guido Trotter | |
53 | 7b64b9ea | René Nussbaumer | class TestClusterObject(unittest.TestCase): |
54 | 7b64b9ea | René Nussbaumer | """Tests done on a L{objects.Cluster}"""
|
55 | 7b64b9ea | René Nussbaumer | |
56 | 7b64b9ea | René Nussbaumer | def setUp(self): |
57 | 7b64b9ea | René Nussbaumer | hvparams = { |
58 | 7b64b9ea | René Nussbaumer | constants.HT_FAKE: { |
59 | 7b64b9ea | René Nussbaumer | "foo": "bar", |
60 | 7b64b9ea | René Nussbaumer | "bar": "foo", |
61 | 7b64b9ea | René Nussbaumer | "foobar": "barfoo", |
62 | 7b64b9ea | René Nussbaumer | }, |
63 | 7b64b9ea | René Nussbaumer | } |
64 | 7b64b9ea | René Nussbaumer | os_hvp = { |
65 | 7b64b9ea | René Nussbaumer | "lenny-image": {
|
66 | 7b64b9ea | René Nussbaumer | constants.HT_FAKE: { |
67 | 7b64b9ea | René Nussbaumer | "foo": "baz", |
68 | 7b64b9ea | René Nussbaumer | "foobar": "foobar", |
69 | 7b64b9ea | René Nussbaumer | "blah": "blibb", |
70 | 7b64b9ea | René Nussbaumer | "blubb": "blah", |
71 | 7b64b9ea | René Nussbaumer | }, |
72 | 7b64b9ea | René Nussbaumer | constants.HT_XEN_PVM: { |
73 | 7b64b9ea | René Nussbaumer | "root_path": "/dev/sda5", |
74 | 7b64b9ea | René Nussbaumer | "foo": "foobar", |
75 | 7b64b9ea | René Nussbaumer | }, |
76 | 7b64b9ea | René Nussbaumer | }, |
77 | 7b64b9ea | René Nussbaumer | "ubuntu-hardy": {
|
78 | 7b64b9ea | René Nussbaumer | }, |
79 | 7b64b9ea | René Nussbaumer | } |
80 | 095e71aa | René Nussbaumer | ndparams = { |
81 | 095e71aa | René Nussbaumer | constants.ND_OOB_PROGRAM: "/bin/cluster-oob"
|
82 | 095e71aa | René Nussbaumer | } |
83 | 095e71aa | René Nussbaumer | |
84 | 095e71aa | René Nussbaumer | self.fake_cl = objects.Cluster(hvparams=hvparams, os_hvp=os_hvp,
|
85 | 095e71aa | René Nussbaumer | ndparams=ndparams) |
86 | 7b64b9ea | René Nussbaumer | self.fake_cl.UpgradeConfig()
|
87 | 7b64b9ea | René Nussbaumer | |
88 | d63479b5 | Iustin Pop | def testGetHVDefaults(self): |
89 | d63479b5 | Iustin Pop | cl = self.fake_cl
|
90 | d63479b5 | Iustin Pop | self.failUnlessEqual(cl.GetHVDefaults(constants.HT_FAKE),
|
91 | d63479b5 | Iustin Pop | cl.hvparams[constants.HT_FAKE]) |
92 | d63479b5 | Iustin Pop | self.failUnlessEqual(cl.GetHVDefaults(None), {}) |
93 | d63479b5 | Iustin Pop | self.failUnlessEqual(cl.GetHVDefaults(constants.HT_XEN_PVM,
|
94 | d63479b5 | Iustin Pop | os_name="lenny-image"),
|
95 | d63479b5 | Iustin Pop | cl.os_hvp["lenny-image"][constants.HT_XEN_PVM])
|
96 | d63479b5 | Iustin Pop | |
97 | d63479b5 | Iustin Pop | |
98 | 7b64b9ea | René Nussbaumer | def testFillHvFullMerge(self): |
99 | 7b64b9ea | René Nussbaumer | inst_hvparams = { |
100 | 7b64b9ea | René Nussbaumer | "blah": "blubb", |
101 | 7b64b9ea | René Nussbaumer | } |
102 | 7b64b9ea | René Nussbaumer | |
103 | 7b64b9ea | René Nussbaumer | fake_dict = { |
104 | 7b64b9ea | René Nussbaumer | "foo": "baz", |
105 | 7b64b9ea | René Nussbaumer | "bar": "foo", |
106 | 7b64b9ea | René Nussbaumer | "foobar": "foobar", |
107 | 7b64b9ea | René Nussbaumer | "blah": "blubb", |
108 | 7b64b9ea | René Nussbaumer | "blubb": "blah", |
109 | 7b64b9ea | René Nussbaumer | } |
110 | 7b64b9ea | René Nussbaumer | fake_inst = objects.Instance(name="foobar",
|
111 | 7b64b9ea | René Nussbaumer | os="lenny-image",
|
112 | 7b64b9ea | René Nussbaumer | hypervisor=constants.HT_FAKE, |
113 | 7b64b9ea | René Nussbaumer | hvparams=inst_hvparams) |
114 | 7b64b9ea | René Nussbaumer | self.assertEqual(fake_dict, self.fake_cl.FillHV(fake_inst)) |
115 | 7b64b9ea | René Nussbaumer | |
116 | 7b64b9ea | René Nussbaumer | def testFillHvGlobalParams(self): |
117 | 7b64b9ea | René Nussbaumer | fake_inst = objects.Instance(name="foobar",
|
118 | 7b64b9ea | René Nussbaumer | os="ubuntu-hardy",
|
119 | 7b64b9ea | René Nussbaumer | hypervisor=constants.HT_FAKE, |
120 | 7b64b9ea | René Nussbaumer | hvparams={}) |
121 | 7b64b9ea | René Nussbaumer | self.assertEqual(self.fake_cl.hvparams[constants.HT_FAKE], |
122 | 7b64b9ea | René Nussbaumer | self.fake_cl.FillHV(fake_inst))
|
123 | 7b64b9ea | René Nussbaumer | |
124 | 7b64b9ea | René Nussbaumer | def testFillHvInstParams(self): |
125 | 7b64b9ea | René Nussbaumer | inst_hvparams = { |
126 | 7b64b9ea | René Nussbaumer | "blah": "blubb", |
127 | 7b64b9ea | René Nussbaumer | } |
128 | 7b64b9ea | René Nussbaumer | fake_inst = objects.Instance(name="foobar",
|
129 | 7b64b9ea | René Nussbaumer | os="ubuntu-hardy",
|
130 | 7b64b9ea | René Nussbaumer | hypervisor=constants.HT_XEN_PVM, |
131 | 7b64b9ea | René Nussbaumer | hvparams=inst_hvparams) |
132 | 7b64b9ea | René Nussbaumer | self.assertEqual(inst_hvparams, self.fake_cl.FillHV(fake_inst)) |
133 | 7b64b9ea | René Nussbaumer | |
134 | 7b64b9ea | René Nussbaumer | def testFillHvEmptyParams(self): |
135 | 7b64b9ea | René Nussbaumer | fake_inst = objects.Instance(name="foobar",
|
136 | 7b64b9ea | René Nussbaumer | os="ubuntu-hardy",
|
137 | 7b64b9ea | René Nussbaumer | hypervisor=constants.HT_XEN_PVM, |
138 | 7b64b9ea | René Nussbaumer | hvparams={}) |
139 | 7b64b9ea | René Nussbaumer | self.assertEqual({}, self.fake_cl.FillHV(fake_inst)) |
140 | 7b64b9ea | René Nussbaumer | |
141 | 7b64b9ea | René Nussbaumer | def testFillHvPartialParams(self): |
142 | 7b64b9ea | René Nussbaumer | os = "lenny-image"
|
143 | 7b64b9ea | René Nussbaumer | fake_inst = objects.Instance(name="foobar",
|
144 | 7b64b9ea | René Nussbaumer | os=os, |
145 | 7b64b9ea | René Nussbaumer | hypervisor=constants.HT_XEN_PVM, |
146 | 7b64b9ea | René Nussbaumer | hvparams={}) |
147 | 7b64b9ea | René Nussbaumer | self.assertEqual(self.fake_cl.os_hvp[os][constants.HT_XEN_PVM], |
148 | 7b64b9ea | René Nussbaumer | self.fake_cl.FillHV(fake_inst))
|
149 | 7b64b9ea | René Nussbaumer | |
150 | 095e71aa | René Nussbaumer | def testFillNdParamsCluster(self): |
151 | 095e71aa | René Nussbaumer | fake_node = objects.Node(name="test",
|
152 | 095e71aa | René Nussbaumer | ndparams={}, |
153 | 095e71aa | René Nussbaumer | group="testgroup")
|
154 | 095e71aa | René Nussbaumer | fake_group = objects.NodeGroup(name="testgroup",
|
155 | 095e71aa | René Nussbaumer | ndparams={}) |
156 | 095e71aa | René Nussbaumer | self.assertEqual(self.fake_cl.ndparams, |
157 | 095e71aa | René Nussbaumer | self.fake_cl.FillND(fake_node, fake_group))
|
158 | 095e71aa | René Nussbaumer | |
159 | 095e71aa | René Nussbaumer | def testFillNdParamsNodeGroup(self): |
160 | 095e71aa | René Nussbaumer | fake_node = objects.Node(name="test",
|
161 | 095e71aa | René Nussbaumer | ndparams={}, |
162 | 095e71aa | René Nussbaumer | group="testgroup")
|
163 | 095e71aa | René Nussbaumer | group_ndparams = { |
164 | 095e71aa | René Nussbaumer | constants.ND_OOB_PROGRAM: "/bin/group-oob"
|
165 | 095e71aa | René Nussbaumer | } |
166 | 095e71aa | René Nussbaumer | fake_group = objects.NodeGroup(name="testgroup",
|
167 | 095e71aa | René Nussbaumer | ndparams=group_ndparams) |
168 | 095e71aa | René Nussbaumer | self.assertEqual(group_ndparams,
|
169 | 095e71aa | René Nussbaumer | self.fake_cl.FillND(fake_node, fake_group))
|
170 | 095e71aa | René Nussbaumer | |
171 | 095e71aa | René Nussbaumer | def testFillNdParamsNode(self): |
172 | 095e71aa | René Nussbaumer | node_ndparams = { |
173 | 095e71aa | René Nussbaumer | constants.ND_OOB_PROGRAM: "/bin/node-oob"
|
174 | 095e71aa | René Nussbaumer | } |
175 | 095e71aa | René Nussbaumer | fake_node = objects.Node(name="test",
|
176 | 095e71aa | René Nussbaumer | ndparams=node_ndparams, |
177 | 095e71aa | René Nussbaumer | group="testgroup")
|
178 | 095e71aa | René Nussbaumer | fake_group = objects.NodeGroup(name="testgroup",
|
179 | 095e71aa | René Nussbaumer | ndparams={}) |
180 | 095e71aa | René Nussbaumer | self.assertEqual(node_ndparams,
|
181 | 095e71aa | René Nussbaumer | self.fake_cl.FillND(fake_node, fake_group))
|
182 | 095e71aa | René Nussbaumer | |
183 | 095e71aa | René Nussbaumer | def testFillNdParamsAll(self): |
184 | 095e71aa | René Nussbaumer | node_ndparams = { |
185 | 095e71aa | René Nussbaumer | constants.ND_OOB_PROGRAM: "/bin/node-oob"
|
186 | 095e71aa | René Nussbaumer | } |
187 | 095e71aa | René Nussbaumer | fake_node = objects.Node(name="test",
|
188 | 095e71aa | René Nussbaumer | ndparams=node_ndparams, |
189 | 095e71aa | René Nussbaumer | group="testgroup")
|
190 | 095e71aa | René Nussbaumer | group_ndparams = { |
191 | 095e71aa | René Nussbaumer | constants.ND_OOB_PROGRAM: "/bin/group-oob"
|
192 | 095e71aa | René Nussbaumer | } |
193 | 095e71aa | René Nussbaumer | fake_group = objects.NodeGroup(name="testgroup",
|
194 | 095e71aa | René Nussbaumer | ndparams=group_ndparams) |
195 | 095e71aa | René Nussbaumer | self.assertEqual(node_ndparams,
|
196 | 095e71aa | René Nussbaumer | self.fake_cl.FillND(fake_node, fake_group))
|
197 | 095e71aa | René Nussbaumer | |
198 | 7b64b9ea | René Nussbaumer | |
199 | 870dc44c | Iustin Pop | class TestOS(unittest.TestCase): |
200 | 870dc44c | Iustin Pop | ALL_DATA = [ |
201 | 870dc44c | Iustin Pop | "debootstrap",
|
202 | 870dc44c | Iustin Pop | "debootstrap+default",
|
203 | 870dc44c | Iustin Pop | "debootstrap++default",
|
204 | 870dc44c | Iustin Pop | ] |
205 | 870dc44c | Iustin Pop | |
206 | 870dc44c | Iustin Pop | def testSplitNameVariant(self): |
207 | 870dc44c | Iustin Pop | for name in self.ALL_DATA: |
208 | 870dc44c | Iustin Pop | self.assertEqual(len(objects.OS.SplitNameVariant(name)), 2) |
209 | 870dc44c | Iustin Pop | |
210 | 870dc44c | Iustin Pop | def testVariant(self): |
211 | 870dc44c | Iustin Pop | self.assertEqual(objects.OS.GetVariant("debootstrap"), "") |
212 | 870dc44c | Iustin Pop | self.assertEqual(objects.OS.GetVariant("debootstrap+default"), "default") |
213 | 870dc44c | Iustin Pop | |
214 | 870dc44c | Iustin Pop | |
215 | 6a050007 | Michael Hanselmann | class TestInstance(unittest.TestCase): |
216 | 6a050007 | Michael Hanselmann | def _GenericCheck(self, inst): |
217 | 6a050007 | Michael Hanselmann | for i in [inst.all_nodes, inst.secondary_nodes]: |
218 | 6a050007 | Michael Hanselmann | self.assertTrue(isinstance(inst.all_nodes, (list, tuple)), |
219 | 6a050007 | Michael Hanselmann | msg="Data type doesn't guarantee order")
|
220 | 6a050007 | Michael Hanselmann | |
221 | 6a050007 | Michael Hanselmann | self.assertTrue(inst.primary_node not in inst.secondary_nodes) |
222 | 6a050007 | Michael Hanselmann | self.assertEqual(inst.all_nodes[0], inst.primary_node, |
223 | 6a050007 | Michael Hanselmann | msg="Primary node not first node in list")
|
224 | 6a050007 | Michael Hanselmann | |
225 | 6a050007 | Michael Hanselmann | def testNodesNoDisks(self): |
226 | 6a050007 | Michael Hanselmann | inst = objects.Instance(name="fakeinst.example.com",
|
227 | 6a050007 | Michael Hanselmann | primary_node="pnode.example.com",
|
228 | 6a050007 | Michael Hanselmann | disks=[ |
229 | 6a050007 | Michael Hanselmann | ]) |
230 | 6a050007 | Michael Hanselmann | |
231 | 6a050007 | Michael Hanselmann | self._GenericCheck(inst)
|
232 | 6a050007 | Michael Hanselmann | self.assertEqual(len(inst.secondary_nodes), 0) |
233 | 6a050007 | Michael Hanselmann | self.assertEqual(set(inst.all_nodes), set([inst.primary_node])) |
234 | 6a050007 | Michael Hanselmann | self.assertEqual(inst.MapLVsByNode(), {
|
235 | 6a050007 | Michael Hanselmann | inst.primary_node: [], |
236 | 6a050007 | Michael Hanselmann | }) |
237 | 6a050007 | Michael Hanselmann | |
238 | 6a050007 | Michael Hanselmann | def testNodesPlainDisks(self): |
239 | 6a050007 | Michael Hanselmann | inst = objects.Instance(name="fakeinstplain.example.com",
|
240 | 6a050007 | Michael Hanselmann | primary_node="node3.example.com",
|
241 | 6a050007 | Michael Hanselmann | disks=[ |
242 | 6a050007 | Michael Hanselmann | objects.Disk(dev_type=constants.LD_LV, size=128,
|
243 | 6a050007 | Michael Hanselmann | logical_id=("myxenvg", "disk25494")), |
244 | 6a050007 | Michael Hanselmann | objects.Disk(dev_type=constants.LD_LV, size=512,
|
245 | 6a050007 | Michael Hanselmann | logical_id=("myxenvg", "disk29071")), |
246 | 6a050007 | Michael Hanselmann | ]) |
247 | 6a050007 | Michael Hanselmann | |
248 | 6a050007 | Michael Hanselmann | self._GenericCheck(inst)
|
249 | 6a050007 | Michael Hanselmann | self.assertEqual(len(inst.secondary_nodes), 0) |
250 | 6a050007 | Michael Hanselmann | self.assertEqual(set(inst.all_nodes), set([inst.primary_node])) |
251 | 6a050007 | Michael Hanselmann | self.assertEqual(inst.MapLVsByNode(), {
|
252 | 6a050007 | Michael Hanselmann | inst.primary_node: ["myxenvg/disk25494", "myxenvg/disk29071"], |
253 | 6a050007 | Michael Hanselmann | }) |
254 | 6a050007 | Michael Hanselmann | |
255 | 6a050007 | Michael Hanselmann | def testNodesDrbdDisks(self): |
256 | 6a050007 | Michael Hanselmann | inst = objects.Instance(name="fakeinstdrbd.example.com",
|
257 | 6a050007 | Michael Hanselmann | primary_node="node10.example.com",
|
258 | 6a050007 | Michael Hanselmann | disks=[ |
259 | 6a050007 | Michael Hanselmann | objects.Disk(dev_type=constants.LD_DRBD8, size=786432,
|
260 | 6a050007 | Michael Hanselmann | logical_id=("node10.example.com", "node15.example.com", |
261 | 6a050007 | Michael Hanselmann | 12300, 0, 0, "secret"), |
262 | 6a050007 | Michael Hanselmann | children=[ |
263 | 6a050007 | Michael Hanselmann | objects.Disk(dev_type=constants.LD_LV, size=786432,
|
264 | 6a050007 | Michael Hanselmann | logical_id=("myxenvg", "disk0")), |
265 | 6a050007 | Michael Hanselmann | objects.Disk(dev_type=constants.LD_LV, size=128,
|
266 | 6a050007 | Michael Hanselmann | logical_id=("myxenvg", "meta0")) |
267 | 6a050007 | Michael Hanselmann | ], |
268 | 6a050007 | Michael Hanselmann | iv_name="disk/0")
|
269 | 6a050007 | Michael Hanselmann | ]) |
270 | 6a050007 | Michael Hanselmann | |
271 | 6a050007 | Michael Hanselmann | self._GenericCheck(inst)
|
272 | 6a050007 | Michael Hanselmann | self.assertEqual(set(inst.secondary_nodes), set(["node15.example.com"])) |
273 | 6a050007 | Michael Hanselmann | self.assertEqual(set(inst.all_nodes), |
274 | 6a050007 | Michael Hanselmann | set([inst.primary_node, "node15.example.com"])) |
275 | 6a050007 | Michael Hanselmann | self.assertEqual(inst.MapLVsByNode(), {
|
276 | 6a050007 | Michael Hanselmann | inst.primary_node: ["myxenvg/disk0", "myxenvg/meta0"], |
277 | 6a050007 | Michael Hanselmann | "node15.example.com": ["myxenvg/disk0", "myxenvg/meta0"], |
278 | 6a050007 | Michael Hanselmann | }) |
279 | 6a050007 | Michael Hanselmann | |
280 | 6a050007 | Michael Hanselmann | self.assertEqual(inst.FindDisk(0), inst.disks[0]) |
281 | 6a050007 | Michael Hanselmann | self.assertRaises(errors.OpPrereqError, inst.FindDisk, "hello") |
282 | 6a050007 | Michael Hanselmann | self.assertRaises(errors.OpPrereqError, inst.FindDisk, 100) |
283 | 6a050007 | Michael Hanselmann | self.assertRaises(errors.OpPrereqError, inst.FindDisk, 1) |
284 | 6a050007 | Michael Hanselmann | |
285 | 6a050007 | Michael Hanselmann | |
286 | 4c14965f | Guido Trotter | if __name__ == '__main__': |
287 | 25231ec5 | Michael Hanselmann | testutils.GanetiTestProgram() |