root / test / ganeti.objects_unittest.py @ 5f06ce5e
History | View | Annotate | Download (11.2 kB)
1 |
#!/usr/bin/python
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2006, 2007, 2008, 2010 Google Inc.
|
5 |
#
|
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.
|
10 |
#
|
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.
|
15 |
#
|
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
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Script for unittesting the objects module"""
|
23 |
|
24 |
|
25 |
import unittest |
26 |
|
27 |
from ganeti import constants |
28 |
from ganeti import objects |
29 |
from ganeti import errors |
30 |
|
31 |
import testutils |
32 |
|
33 |
|
34 |
class SimpleObject(objects.ConfigObject): |
35 |
__slots__ = ['a', 'b'] |
36 |
|
37 |
|
38 |
class TestDictState(unittest.TestCase): |
39 |
"""Simple dict tansformation tests"""
|
40 |
|
41 |
def testSimpleObjectToDict(self): |
42 |
o1 = SimpleObject(a='1')
|
43 |
self.assertEquals(o1.ToDict(), {'a': '1'}) |
44 |
self.assertEquals(o1.__getstate__(), {'a': '1'}) |
45 |
self.assertEquals(o1.__getstate__(), o1.ToDict())
|
46 |
o1.a = 2
|
47 |
o1.b = 5
|
48 |
self.assertEquals(o1.ToDict(), {'a': 2, 'b': 5}) |
49 |
o2 = SimpleObject.FromDict(o1.ToDict()) |
50 |
self.assertEquals(o1.ToDict(), {'a': 2, 'b': 5}) |
51 |
|
52 |
|
53 |
class TestClusterObject(unittest.TestCase): |
54 |
"""Tests done on a L{objects.Cluster}"""
|
55 |
|
56 |
def setUp(self): |
57 |
hvparams = { |
58 |
constants.HT_FAKE: { |
59 |
"foo": "bar", |
60 |
"bar": "foo", |
61 |
"foobar": "barfoo", |
62 |
}, |
63 |
} |
64 |
os_hvp = { |
65 |
"lenny-image": {
|
66 |
constants.HT_FAKE: { |
67 |
"foo": "baz", |
68 |
"foobar": "foobar", |
69 |
"blah": "blibb", |
70 |
"blubb": "blah", |
71 |
}, |
72 |
constants.HT_XEN_PVM: { |
73 |
"root_path": "/dev/sda5", |
74 |
"foo": "foobar", |
75 |
}, |
76 |
}, |
77 |
"ubuntu-hardy": {
|
78 |
}, |
79 |
} |
80 |
ndparams = { |
81 |
constants.ND_OOB_PROGRAM: "/bin/cluster-oob"
|
82 |
} |
83 |
|
84 |
self.fake_cl = objects.Cluster(hvparams=hvparams, os_hvp=os_hvp,
|
85 |
ndparams=ndparams) |
86 |
self.fake_cl.UpgradeConfig()
|
87 |
|
88 |
def testGetHVDefaults(self): |
89 |
cl = self.fake_cl
|
90 |
self.failUnlessEqual(cl.GetHVDefaults(constants.HT_FAKE),
|
91 |
cl.hvparams[constants.HT_FAKE]) |
92 |
self.failUnlessEqual(cl.GetHVDefaults(None), {}) |
93 |
self.failUnlessEqual(cl.GetHVDefaults(constants.HT_XEN_PVM,
|
94 |
os_name="lenny-image"),
|
95 |
cl.os_hvp["lenny-image"][constants.HT_XEN_PVM])
|
96 |
|
97 |
|
98 |
def testFillHvFullMerge(self): |
99 |
inst_hvparams = { |
100 |
"blah": "blubb", |
101 |
} |
102 |
|
103 |
fake_dict = { |
104 |
"foo": "baz", |
105 |
"bar": "foo", |
106 |
"foobar": "foobar", |
107 |
"blah": "blubb", |
108 |
"blubb": "blah", |
109 |
} |
110 |
fake_inst = objects.Instance(name="foobar",
|
111 |
os="lenny-image",
|
112 |
hypervisor=constants.HT_FAKE, |
113 |
hvparams=inst_hvparams) |
114 |
self.assertEqual(fake_dict, self.fake_cl.FillHV(fake_inst)) |
115 |
|
116 |
def testFillHvGlobalParams(self): |
117 |
fake_inst = objects.Instance(name="foobar",
|
118 |
os="ubuntu-hardy",
|
119 |
hypervisor=constants.HT_FAKE, |
120 |
hvparams={}) |
121 |
self.assertEqual(self.fake_cl.hvparams[constants.HT_FAKE], |
122 |
self.fake_cl.FillHV(fake_inst))
|
123 |
|
124 |
def testFillHvInstParams(self): |
125 |
inst_hvparams = { |
126 |
"blah": "blubb", |
127 |
} |
128 |
fake_inst = objects.Instance(name="foobar",
|
129 |
os="ubuntu-hardy",
|
130 |
hypervisor=constants.HT_XEN_PVM, |
131 |
hvparams=inst_hvparams) |
132 |
self.assertEqual(inst_hvparams, self.fake_cl.FillHV(fake_inst)) |
133 |
|
134 |
def testFillHvEmptyParams(self): |
135 |
fake_inst = objects.Instance(name="foobar",
|
136 |
os="ubuntu-hardy",
|
137 |
hypervisor=constants.HT_XEN_PVM, |
138 |
hvparams={}) |
139 |
self.assertEqual({}, self.fake_cl.FillHV(fake_inst)) |
140 |
|
141 |
def testFillHvPartialParams(self): |
142 |
os = "lenny-image"
|
143 |
fake_inst = objects.Instance(name="foobar",
|
144 |
os=os, |
145 |
hypervisor=constants.HT_XEN_PVM, |
146 |
hvparams={}) |
147 |
self.assertEqual(self.fake_cl.os_hvp[os][constants.HT_XEN_PVM], |
148 |
self.fake_cl.FillHV(fake_inst))
|
149 |
|
150 |
def testFillNdParamsCluster(self): |
151 |
fake_node = objects.Node(name="test",
|
152 |
ndparams={}, |
153 |
group="testgroup")
|
154 |
fake_group = objects.NodeGroup(name="testgroup",
|
155 |
ndparams={}) |
156 |
self.assertEqual(self.fake_cl.ndparams, |
157 |
self.fake_cl.FillND(fake_node, fake_group))
|
158 |
|
159 |
def testFillNdParamsNodeGroup(self): |
160 |
fake_node = objects.Node(name="test",
|
161 |
ndparams={}, |
162 |
group="testgroup")
|
163 |
group_ndparams = { |
164 |
constants.ND_OOB_PROGRAM: "/bin/group-oob"
|
165 |
} |
166 |
fake_group = objects.NodeGroup(name="testgroup",
|
167 |
ndparams=group_ndparams) |
168 |
self.assertEqual(group_ndparams,
|
169 |
self.fake_cl.FillND(fake_node, fake_group))
|
170 |
|
171 |
def testFillNdParamsNode(self): |
172 |
node_ndparams = { |
173 |
constants.ND_OOB_PROGRAM: "/bin/node-oob"
|
174 |
} |
175 |
fake_node = objects.Node(name="test",
|
176 |
ndparams=node_ndparams, |
177 |
group="testgroup")
|
178 |
fake_group = objects.NodeGroup(name="testgroup",
|
179 |
ndparams={}) |
180 |
self.assertEqual(node_ndparams,
|
181 |
self.fake_cl.FillND(fake_node, fake_group))
|
182 |
|
183 |
def testFillNdParamsAll(self): |
184 |
node_ndparams = { |
185 |
constants.ND_OOB_PROGRAM: "/bin/node-oob"
|
186 |
} |
187 |
fake_node = objects.Node(name="test",
|
188 |
ndparams=node_ndparams, |
189 |
group="testgroup")
|
190 |
group_ndparams = { |
191 |
constants.ND_OOB_PROGRAM: "/bin/group-oob"
|
192 |
} |
193 |
fake_group = objects.NodeGroup(name="testgroup",
|
194 |
ndparams=group_ndparams) |
195 |
self.assertEqual(node_ndparams,
|
196 |
self.fake_cl.FillND(fake_node, fake_group))
|
197 |
|
198 |
|
199 |
class TestOS(unittest.TestCase): |
200 |
ALL_DATA = [ |
201 |
"debootstrap",
|
202 |
"debootstrap+default",
|
203 |
"debootstrap++default",
|
204 |
] |
205 |
|
206 |
def testSplitNameVariant(self): |
207 |
for name in self.ALL_DATA: |
208 |
self.assertEqual(len(objects.OS.SplitNameVariant(name)), 2) |
209 |
|
210 |
def testVariant(self): |
211 |
self.assertEqual(objects.OS.GetVariant("debootstrap"), "") |
212 |
self.assertEqual(objects.OS.GetVariant("debootstrap+default"), "default") |
213 |
|
214 |
|
215 |
class TestInstance(unittest.TestCase): |
216 |
def _GenericCheck(self, inst): |
217 |
for i in [inst.all_nodes, inst.secondary_nodes]: |
218 |
self.assertTrue(isinstance(inst.all_nodes, (list, tuple)), |
219 |
msg="Data type doesn't guarantee order")
|
220 |
|
221 |
self.assertTrue(inst.primary_node not in inst.secondary_nodes) |
222 |
self.assertEqual(inst.all_nodes[0], inst.primary_node, |
223 |
msg="Primary node not first node in list")
|
224 |
|
225 |
def testNodesNoDisks(self): |
226 |
inst = objects.Instance(name="fakeinst.example.com",
|
227 |
primary_node="pnode.example.com",
|
228 |
disks=[ |
229 |
]) |
230 |
|
231 |
self._GenericCheck(inst)
|
232 |
self.assertEqual(len(inst.secondary_nodes), 0) |
233 |
self.assertEqual(set(inst.all_nodes), set([inst.primary_node])) |
234 |
self.assertEqual(inst.MapLVsByNode(), {
|
235 |
inst.primary_node: [], |
236 |
}) |
237 |
|
238 |
def testNodesPlainDisks(self): |
239 |
inst = objects.Instance(name="fakeinstplain.example.com",
|
240 |
primary_node="node3.example.com",
|
241 |
disks=[ |
242 |
objects.Disk(dev_type=constants.LD_LV, size=128,
|
243 |
logical_id=("myxenvg", "disk25494")), |
244 |
objects.Disk(dev_type=constants.LD_LV, size=512,
|
245 |
logical_id=("myxenvg", "disk29071")), |
246 |
]) |
247 |
|
248 |
self._GenericCheck(inst)
|
249 |
self.assertEqual(len(inst.secondary_nodes), 0) |
250 |
self.assertEqual(set(inst.all_nodes), set([inst.primary_node])) |
251 |
self.assertEqual(inst.MapLVsByNode(), {
|
252 |
inst.primary_node: ["myxenvg/disk25494", "myxenvg/disk29071"], |
253 |
}) |
254 |
|
255 |
def testNodesDrbdDisks(self): |
256 |
inst = objects.Instance(name="fakeinstdrbd.example.com",
|
257 |
primary_node="node10.example.com",
|
258 |
disks=[ |
259 |
objects.Disk(dev_type=constants.LD_DRBD8, size=786432,
|
260 |
logical_id=("node10.example.com", "node15.example.com", |
261 |
12300, 0, 0, "secret"), |
262 |
children=[ |
263 |
objects.Disk(dev_type=constants.LD_LV, size=786432,
|
264 |
logical_id=("myxenvg", "disk0")), |
265 |
objects.Disk(dev_type=constants.LD_LV, size=128,
|
266 |
logical_id=("myxenvg", "meta0")) |
267 |
], |
268 |
iv_name="disk/0")
|
269 |
]) |
270 |
|
271 |
self._GenericCheck(inst)
|
272 |
self.assertEqual(set(inst.secondary_nodes), set(["node15.example.com"])) |
273 |
self.assertEqual(set(inst.all_nodes), |
274 |
set([inst.primary_node, "node15.example.com"])) |
275 |
self.assertEqual(inst.MapLVsByNode(), {
|
276 |
inst.primary_node: ["myxenvg/disk0", "myxenvg/meta0"], |
277 |
"node15.example.com": ["myxenvg/disk0", "myxenvg/meta0"], |
278 |
}) |
279 |
|
280 |
self.assertEqual(inst.FindDisk(0), inst.disks[0]) |
281 |
self.assertRaises(errors.OpPrereqError, inst.FindDisk, "hello") |
282 |
self.assertRaises(errors.OpPrereqError, inst.FindDisk, 100) |
283 |
self.assertRaises(errors.OpPrereqError, inst.FindDisk, 1) |
284 |
|
285 |
|
286 |
class TestNode(unittest.TestCase): |
287 |
def testEmpty(self): |
288 |
self.assertEqual(objects.Node().ToDict(), {})
|
289 |
self.assertTrue(isinstance(objects.Node.FromDict({}), objects.Node)) |
290 |
|
291 |
def testHvState(self): |
292 |
node = objects.Node(name="node18157.example.com", hv_state={
|
293 |
constants.HT_XEN_HVM: objects.NodeHvState(cpu_total=64),
|
294 |
constants.HT_KVM: objects.NodeHvState(cpu_node=1),
|
295 |
}) |
296 |
|
297 |
node2 = objects.Node.FromDict(node.ToDict()) |
298 |
|
299 |
# Make sure nothing can reference it anymore
|
300 |
del node
|
301 |
|
302 |
self.assertEqual(node2.name, "node18157.example.com") |
303 |
self.assertEqual(frozenset(node2.hv_state), frozenset([ |
304 |
constants.HT_XEN_HVM, |
305 |
constants.HT_KVM, |
306 |
])) |
307 |
self.assertEqual(node2.hv_state[constants.HT_KVM].cpu_node, 1) |
308 |
self.assertEqual(node2.hv_state[constants.HT_XEN_HVM].cpu_total, 64) |
309 |
|
310 |
def testDiskState(self): |
311 |
node = objects.Node(name="node32087.example.com", disk_state={
|
312 |
constants.LD_LV: { |
313 |
"lv32352": objects.NodeDiskState(total=128), |
314 |
"lv2082": objects.NodeDiskState(total=512), |
315 |
}, |
316 |
}) |
317 |
|
318 |
node2 = objects.Node.FromDict(node.ToDict()) |
319 |
|
320 |
# Make sure nothing can reference it anymore
|
321 |
del node
|
322 |
|
323 |
self.assertEqual(node2.name, "node32087.example.com") |
324 |
self.assertEqual(frozenset(node2.disk_state), frozenset([ |
325 |
constants.LD_LV, |
326 |
])) |
327 |
self.assertEqual(frozenset(node2.disk_state[constants.LD_LV]), frozenset([ |
328 |
"lv32352",
|
329 |
"lv2082",
|
330 |
])) |
331 |
self.assertEqual(node2.disk_state[constants.LD_LV]["lv2082"].total, 512) |
332 |
self.assertEqual(node2.disk_state[constants.LD_LV]["lv32352"].total, 128) |
333 |
|
334 |
|
335 |
if __name__ == '__main__': |
336 |
testutils.GanetiTestProgram() |