root / test / py / ganeti.opcodes_unittest.py @ 14933c17
History | View | Annotate | Download (14 kB)
1 | c32b908e | Michael Hanselmann | #!/usr/bin/python
|
---|---|---|---|
2 | c32b908e | Michael Hanselmann | #
|
3 | c32b908e | Michael Hanselmann | |
4 | 08f31176 | Iustin Pop | # Copyright (C) 2010, 2011, 2012 Google Inc.
|
5 | c32b908e | Michael Hanselmann | #
|
6 | c32b908e | Michael Hanselmann | # This program is free software; you can redistribute it and/or modify
|
7 | c32b908e | Michael Hanselmann | # it under the terms of the GNU General Public License as published by
|
8 | c32b908e | Michael Hanselmann | # the Free Software Foundation; either version 2 of the License, or
|
9 | c32b908e | Michael Hanselmann | # (at your option) any later version.
|
10 | c32b908e | Michael Hanselmann | #
|
11 | c32b908e | Michael Hanselmann | # This program is distributed in the hope that it will be useful, but
|
12 | c32b908e | Michael Hanselmann | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | c32b908e | Michael Hanselmann | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | c32b908e | Michael Hanselmann | # General Public License for more details.
|
15 | c32b908e | Michael Hanselmann | #
|
16 | c32b908e | Michael Hanselmann | # You should have received a copy of the GNU General Public License
|
17 | c32b908e | Michael Hanselmann | # along with this program; if not, write to the Free Software
|
18 | c32b908e | Michael Hanselmann | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | c32b908e | Michael Hanselmann | # 02110-1301, USA.
|
20 | c32b908e | Michael Hanselmann | |
21 | c32b908e | Michael Hanselmann | |
22 | c32b908e | Michael Hanselmann | """Script for testing ganeti.backend"""
|
23 | c32b908e | Michael Hanselmann | |
24 | c32b908e | Michael Hanselmann | import os |
25 | c32b908e | Michael Hanselmann | import sys |
26 | c32b908e | Michael Hanselmann | import unittest |
27 | c32b908e | Michael Hanselmann | |
28 | c32b908e | Michael Hanselmann | from ganeti import utils |
29 | c32b908e | Michael Hanselmann | from ganeti import opcodes |
30 | 580b1fdd | Jose A. Lopes | from ganeti import opcodes_base |
31 | 65e183af | Michael Hanselmann | from ganeti import ht |
32 | 1cbef6d8 | Michael Hanselmann | from ganeti import constants |
33 | 1cbef6d8 | Michael Hanselmann | from ganeti import errors |
34 | 45d4c81c | Michael Hanselmann | from ganeti import compat |
35 | c32b908e | Michael Hanselmann | |
36 | c32b908e | Michael Hanselmann | import testutils |
37 | c32b908e | Michael Hanselmann | |
38 | c32b908e | Michael Hanselmann | |
39 | c32b908e | Michael Hanselmann | class TestOpcodes(unittest.TestCase): |
40 | c32b908e | Michael Hanselmann | def test(self): |
41 | c32b908e | Michael Hanselmann | self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, None) |
42 | c32b908e | Michael Hanselmann | self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, "") |
43 | c32b908e | Michael Hanselmann | self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, {}) |
44 | c32b908e | Michael Hanselmann | self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, {"OP_ID": ""}) |
45 | c32b908e | Michael Hanselmann | |
46 | c32b908e | Michael Hanselmann | for cls in opcodes.OP_MAPPING.values(): |
47 | c32b908e | Michael Hanselmann | self.assert_(cls.OP_ID.startswith("OP_")) |
48 | dbc96028 | Michael Hanselmann | self.assert_(len(cls.OP_ID) > 3) |
49 | c32b908e | Michael Hanselmann | self.assertEqual(cls.OP_ID, cls.OP_ID.upper())
|
50 | 580b1fdd | Jose A. Lopes | self.assertEqual(cls.OP_ID, opcodes_base._NameToId(cls.__name__))
|
51 | aea2f8d6 | Jose A. Lopes | self.assertFalse(
|
52 | aea2f8d6 | Jose A. Lopes | compat.any(cls.OP_ID.startswith(prefix) |
53 | aea2f8d6 | Jose A. Lopes | for prefix in opcodes_base.SUMMARY_PREFIX.keys())) |
54 | aea2f8d6 | Jose A. Lopes | self.assertTrue(callable(cls.OP_RESULT), |
55 | aea2f8d6 | Jose A. Lopes | msg=("%s should have a result check" % cls.OP_ID))
|
56 | c32b908e | Michael Hanselmann | |
57 | c32b908e | Michael Hanselmann | self.assertRaises(TypeError, cls, unsupported_parameter="some value") |
58 | c32b908e | Michael Hanselmann | |
59 | c32b908e | Michael Hanselmann | args = [ |
60 | c32b908e | Michael Hanselmann | # No variables
|
61 | c32b908e | Michael Hanselmann | {}, |
62 | c32b908e | Michael Hanselmann | |
63 | c32b908e | Michael Hanselmann | # Variables supported by all opcodes
|
64 | c32b908e | Michael Hanselmann | {"dry_run": False, "debug_level": 0, }, |
65 | c32b908e | Michael Hanselmann | |
66 | c32b908e | Michael Hanselmann | # All variables
|
67 | 12e62af5 | René Nussbaumer | dict([(name, []) for name in cls.GetAllSlots()]) |
68 | c32b908e | Michael Hanselmann | ] |
69 | c32b908e | Michael Hanselmann | |
70 | c32b908e | Michael Hanselmann | for i in args: |
71 | c32b908e | Michael Hanselmann | op = cls(**i) |
72 | c32b908e | Michael Hanselmann | |
73 | c32b908e | Michael Hanselmann | self.assertEqual(op.OP_ID, cls.OP_ID)
|
74 | c32b908e | Michael Hanselmann | self._checkSummary(op)
|
75 | c32b908e | Michael Hanselmann | |
76 | c32b908e | Michael Hanselmann | # Try a restore
|
77 | c32b908e | Michael Hanselmann | state = op.__getstate__() |
78 | c32b908e | Michael Hanselmann | self.assert_(isinstance(state, dict)) |
79 | c32b908e | Michael Hanselmann | |
80 | c32b908e | Michael Hanselmann | restored = opcodes.OpCode.LoadOpCode(state) |
81 | c32b908e | Michael Hanselmann | self.assert_(isinstance(restored, cls)) |
82 | c32b908e | Michael Hanselmann | self._checkSummary(restored)
|
83 | c32b908e | Michael Hanselmann | |
84 | 65e183af | Michael Hanselmann | for name in ["x_y_z", "hello_world"]: |
85 | 32683096 | René Nussbaumer | assert name not in cls.GetAllSlots() |
86 | 65e183af | Michael Hanselmann | for value in [None, True, False, [], "Hello World"]: |
87 | 65e183af | Michael Hanselmann | self.assertRaises(AttributeError, setattr, op, name, value) |
88 | 65e183af | Michael Hanselmann | |
89 | c32b908e | Michael Hanselmann | def _checkSummary(self, op): |
90 | c32b908e | Michael Hanselmann | summary = op.Summary() |
91 | c32b908e | Michael Hanselmann | |
92 | c32b908e | Michael Hanselmann | if hasattr(op, "OP_DSC_FIELD"): |
93 | c32b908e | Michael Hanselmann | self.assert_(("OP_%s" % summary).startswith("%s(" % op.OP_ID)) |
94 | c32b908e | Michael Hanselmann | self.assert_(summary.endswith(")")) |
95 | c32b908e | Michael Hanselmann | else:
|
96 | c32b908e | Michael Hanselmann | self.assertEqual("OP_%s" % summary, op.OP_ID) |
97 | c32b908e | Michael Hanselmann | |
98 | 65ffb373 | Michael Hanselmann | def testSummary(self): |
99 | ff0d18e6 | Iustin Pop | class OpTest(opcodes.OpCode): |
100 | 65ffb373 | Michael Hanselmann | OP_DSC_FIELD = "data"
|
101 | 65ffb373 | Michael Hanselmann | OP_PARAMS = [ |
102 | 197b323b | Michael Hanselmann | ("data", ht.NoDefault, ht.TString, None), |
103 | 65ffb373 | Michael Hanselmann | ] |
104 | 65ffb373 | Michael Hanselmann | |
105 | ff0d18e6 | Iustin Pop | self.assertEqual(OpTest(data="").Summary(), "TEST()") |
106 | ff0d18e6 | Iustin Pop | self.assertEqual(OpTest(data="Hello World").Summary(), |
107 | 65ffb373 | Michael Hanselmann | "TEST(Hello World)")
|
108 | ff0d18e6 | Iustin Pop | self.assertEqual(OpTest(data="node1.example.com").Summary(), |
109 | 65ffb373 | Michael Hanselmann | "TEST(node1.example.com)")
|
110 | 65ffb373 | Michael Hanselmann | |
111 | 8bc17ebb | Iustin Pop | def testSummaryFormatter(self): |
112 | 8bc17ebb | Iustin Pop | class OpTest(opcodes.OpCode): |
113 | 8bc17ebb | Iustin Pop | OP_DSC_FIELD = "data"
|
114 | 8bc17ebb | Iustin Pop | OP_DSC_FORMATTER = lambda _, v: "a" |
115 | 8bc17ebb | Iustin Pop | OP_PARAMS = [ |
116 | 8bc17ebb | Iustin Pop | ("data", ht.NoDefault, ht.TString, None), |
117 | 8bc17ebb | Iustin Pop | ] |
118 | 8bc17ebb | Iustin Pop | self.assertEqual(OpTest(data="").Summary(), "TEST(a)") |
119 | 8bc17ebb | Iustin Pop | self.assertEqual(OpTest(data="b").Summary(), "TEST(a)") |
120 | 8bc17ebb | Iustin Pop | |
121 | 3ce9a5e7 | Michael Hanselmann | def testTinySummary(self): |
122 | 580b1fdd | Jose A. Lopes | self.assertFalse(
|
123 | 580b1fdd | Jose A. Lopes | utils.FindDuplicates(opcodes_base.SUMMARY_PREFIX.values())) |
124 | 3ce9a5e7 | Michael Hanselmann | self.assertTrue(compat.all(prefix.endswith("_") and supplement.endswith("_") |
125 | 3ce9a5e7 | Michael Hanselmann | for (prefix, supplement) in |
126 | 580b1fdd | Jose A. Lopes | opcodes_base.SUMMARY_PREFIX.items())) |
127 | 3ce9a5e7 | Michael Hanselmann | |
128 | 3ce9a5e7 | Michael Hanselmann | self.assertEqual(opcodes.OpClusterPostInit().TinySummary(), "C_POST_INIT") |
129 | 3ce9a5e7 | Michael Hanselmann | self.assertEqual(opcodes.OpNodeRemove().TinySummary(), "N_REMOVE") |
130 | 3ce9a5e7 | Michael Hanselmann | self.assertEqual(opcodes.OpInstanceMigrate().TinySummary(), "I_MIGRATE") |
131 | 3ce9a5e7 | Michael Hanselmann | self.assertEqual(opcodes.OpGroupQuery().TinySummary(), "G_QUERY") |
132 | 3ce9a5e7 | Michael Hanselmann | self.assertEqual(opcodes.OpTestJqueue().TinySummary(), "TEST_JQUEUE") |
133 | 3ce9a5e7 | Michael Hanselmann | |
134 | 65ffb373 | Michael Hanselmann | def testListSummary(self): |
135 | ff0d18e6 | Iustin Pop | class OpTest(opcodes.OpCode): |
136 | 65ffb373 | Michael Hanselmann | OP_DSC_FIELD = "data"
|
137 | 65ffb373 | Michael Hanselmann | OP_PARAMS = [ |
138 | 197b323b | Michael Hanselmann | ("data", ht.NoDefault, ht.TList, None), |
139 | 65ffb373 | Michael Hanselmann | ] |
140 | 65ffb373 | Michael Hanselmann | |
141 | ff0d18e6 | Iustin Pop | self.assertEqual(OpTest(data=["a", "b", "c"]).Summary(), |
142 | 65ffb373 | Michael Hanselmann | "TEST(a,b,c)")
|
143 | ff0d18e6 | Iustin Pop | self.assertEqual(OpTest(data=["a", None, "c"]).Summary(), |
144 | 65ffb373 | Michael Hanselmann | "TEST(a,None,c)")
|
145 | ff0d18e6 | Iustin Pop | self.assertEqual(OpTest(data=[1, 2, 3, 4]).Summary(), "TEST(1,2,3,4)") |
146 | 65ffb373 | Michael Hanselmann | |
147 | dbc96028 | Michael Hanselmann | def testOpId(self): |
148 | dbc96028 | Michael Hanselmann | self.assertFalse(utils.FindDuplicates(cls.OP_ID
|
149 | dbc96028 | Michael Hanselmann | for cls in opcodes._GetOpList())) |
150 | dbc96028 | Michael Hanselmann | self.assertEqual(len(opcodes._GetOpList()), len(opcodes.OP_MAPPING)) |
151 | dbc96028 | Michael Hanselmann | |
152 | 65e183af | Michael Hanselmann | def testParams(self): |
153 | 65e183af | Michael Hanselmann | supported_by_all = set(["debug_level", "dry_run", "priority"]) |
154 | 65e183af | Michael Hanselmann | |
155 | 580b1fdd | Jose A. Lopes | self.assertTrue(opcodes_base.BaseOpCode not in opcodes.OP_MAPPING.values()) |
156 | 687c10d9 | Iustin Pop | self.assertTrue(opcodes.OpCode not in opcodes.OP_MAPPING.values()) |
157 | 65e183af | Michael Hanselmann | |
158 | 687c10d9 | Iustin Pop | for cls in opcodes.OP_MAPPING.values() + [opcodes.OpCode]: |
159 | 32683096 | René Nussbaumer | all_slots = cls.GetAllSlots() |
160 | 65e183af | Michael Hanselmann | |
161 | 65e183af | Michael Hanselmann | self.assertEqual(len(set(all_slots) & supported_by_all), 3, |
162 | 65e183af | Michael Hanselmann | msg=("Opcode %s doesn't support all base"
|
163 | 65e183af | Michael Hanselmann | " parameters (%r)" % (cls.OP_ID, supported_by_all)))
|
164 | 65e183af | Michael Hanselmann | |
165 | 65e183af | Michael Hanselmann | # All opcodes must have OP_PARAMS
|
166 | 65e183af | Michael Hanselmann | self.assert_(hasattr(cls, "OP_PARAMS"), |
167 | 65e183af | Michael Hanselmann | msg="%s doesn't have OP_PARAMS" % cls.OP_ID)
|
168 | 65e183af | Michael Hanselmann | |
169 | 197b323b | Michael Hanselmann | param_names = [name for (name, _, _, _) in cls.GetAllParams()] |
170 | 65e183af | Michael Hanselmann | |
171 | 65e183af | Michael Hanselmann | self.assertEqual(all_slots, param_names)
|
172 | 65e183af | Michael Hanselmann | |
173 | 65e183af | Michael Hanselmann | # Without inheritance
|
174 | 197b323b | Michael Hanselmann | self.assertEqual(cls.__slots__,
|
175 | 197b323b | Michael Hanselmann | [name for (name, _, _, _) in cls.OP_PARAMS]) |
176 | 65e183af | Michael Hanselmann | |
177 | 65e183af | Michael Hanselmann | # This won't work if parameters are converted to a dictionary
|
178 | 65e183af | Michael Hanselmann | duplicates = utils.FindDuplicates(param_names) |
179 | 65e183af | Michael Hanselmann | self.assertFalse(duplicates,
|
180 | 65e183af | Michael Hanselmann | msg=("Found duplicate parameters %r in %s" %
|
181 | 65e183af | Michael Hanselmann | (duplicates, cls.OP_ID))) |
182 | 65e183af | Michael Hanselmann | |
183 | 65e183af | Michael Hanselmann | # Check parameter definitions
|
184 | 197b323b | Michael Hanselmann | for attr_name, aval, test, doc in cls.GetAllParams(): |
185 | 65e183af | Michael Hanselmann | self.assert_(attr_name)
|
186 | 8e4968ca | Jose A. Lopes | self.assertTrue(callable(test), |
187 | 65e183af | Michael Hanselmann | msg=("Invalid type check for %s.%s" %
|
188 | 65e183af | Michael Hanselmann | (cls.OP_ID, attr_name))) |
189 | 45d4c81c | Michael Hanselmann | self.assertTrue(doc is None or isinstance(doc, basestring)) |
190 | 65e183af | Michael Hanselmann | |
191 | 65e183af | Michael Hanselmann | if callable(aval): |
192 | 6ae72286 | Michael Hanselmann | default_value = aval() |
193 | 6ae72286 | Michael Hanselmann | self.assertFalse(callable(default_value), |
194 | 08f31176 | Iustin Pop | msg=("Default value of %s.%s returned by function"
|
195 | 08f31176 | Iustin Pop | " is callable" % (cls.OP_ID, attr_name)))
|
196 | 6ae72286 | Michael Hanselmann | else:
|
197 | 6ae72286 | Michael Hanselmann | default_value = aval |
198 | 6ae72286 | Michael Hanselmann | |
199 | 8e4968ca | Jose A. Lopes | if aval is not ht.NoDefault and aval is not None: |
200 | 6ae72286 | Michael Hanselmann | self.assertTrue(test(default_value),
|
201 | 08f31176 | Iustin Pop | msg=("Default value of %s.%s does not verify" %
|
202 | 6ae72286 | Michael Hanselmann | (cls.OP_ID, attr_name))) |
203 | 65e183af | Michael Hanselmann | |
204 | 45d4c81c | Michael Hanselmann | # If any parameter has documentation, all others need to have it as well
|
205 | 45d4c81c | Michael Hanselmann | has_doc = [doc is not None for (_, _, _, doc) in cls.OP_PARAMS] |
206 | 45d4c81c | Michael Hanselmann | self.assertTrue(not compat.any(has_doc) or compat.all(has_doc), |
207 | 45d4c81c | Michael Hanselmann | msg="%s does not document all parameters" % cls)
|
208 | 45d4c81c | Michael Hanselmann | |
209 | 1cbef6d8 | Michael Hanselmann | def testValidateNoModification(self): |
210 | ff0d18e6 | Iustin Pop | class OpTest(opcodes.OpCode): |
211 | 1cbef6d8 | Michael Hanselmann | OP_PARAMS = [ |
212 | 6888a6ed | Jose A. Lopes | ("nodef", None, ht.TString, None), |
213 | 197b323b | Michael Hanselmann | ("wdef", "default", ht.TMaybeString, None), |
214 | 197b323b | Michael Hanselmann | ("number", 0, ht.TInt, None), |
215 | 8e4968ca | Jose A. Lopes | ("notype", None, ht.TAny, None), |
216 | 1cbef6d8 | Michael Hanselmann | ] |
217 | 1cbef6d8 | Michael Hanselmann | |
218 | 1cbef6d8 | Michael Hanselmann | # Missing required parameter "nodef"
|
219 | ff0d18e6 | Iustin Pop | op = OpTest() |
220 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
221 | 1cbef6d8 | Michael Hanselmann | self.assertRaises(errors.OpPrereqError, op.Validate, False) |
222 | a9e1819b | Klaus Aehlig | self.assertTrue(op.nodef is None) |
223 | a9e1819b | Klaus Aehlig | self.assertEqual(op.wdef, "default") |
224 | a9e1819b | Klaus Aehlig | self.assertEqual(op.number, 0) |
225 | a9e1819b | Klaus Aehlig | self.assertTrue(op.notype is None) |
226 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.__getstate__(), before, msg="Opcode was modified") |
227 | 1cbef6d8 | Michael Hanselmann | |
228 | 1cbef6d8 | Michael Hanselmann | # Required parameter "nodef" is provided
|
229 | ff0d18e6 | Iustin Pop | op = OpTest(nodef="foo")
|
230 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
231 | 1cbef6d8 | Michael Hanselmann | op.Validate(False)
|
232 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.__getstate__(), before, msg="Opcode was modified") |
233 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.nodef, "foo") |
234 | a9e1819b | Klaus Aehlig | self.assertEqual(op.wdef, "default") |
235 | a9e1819b | Klaus Aehlig | self.assertEqual(op.number, 0) |
236 | a9e1819b | Klaus Aehlig | self.assertTrue(op.notype is None) |
237 | 1cbef6d8 | Michael Hanselmann | |
238 | 1cbef6d8 | Michael Hanselmann | # Missing required parameter "nodef"
|
239 | ff0d18e6 | Iustin Pop | op = OpTest(wdef="hello", number=999) |
240 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
241 | 1cbef6d8 | Michael Hanselmann | self.assertRaises(errors.OpPrereqError, op.Validate, False) |
242 | a9e1819b | Klaus Aehlig | self.assertTrue(op.nodef is None) |
243 | a9e1819b | Klaus Aehlig | self.assertTrue(op.notype is None) |
244 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.__getstate__(), before, msg="Opcode was modified") |
245 | 1cbef6d8 | Michael Hanselmann | |
246 | 1cbef6d8 | Michael Hanselmann | # Wrong type for "nodef"
|
247 | ff0d18e6 | Iustin Pop | op = OpTest(nodef=987)
|
248 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
249 | 1cbef6d8 | Michael Hanselmann | self.assertRaises(errors.OpPrereqError, op.Validate, False) |
250 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.nodef, 987) |
251 | a9e1819b | Klaus Aehlig | self.assertTrue(op.notype is None) |
252 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.__getstate__(), before, msg="Opcode was modified") |
253 | 1cbef6d8 | Michael Hanselmann | |
254 | 1cbef6d8 | Michael Hanselmann | # Testing different types for "notype"
|
255 | ff0d18e6 | Iustin Pop | op = OpTest(nodef="foo", notype=[1, 2, 3]) |
256 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
257 | 1cbef6d8 | Michael Hanselmann | op.Validate(False)
|
258 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.nodef, "foo") |
259 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.notype, [1, 2, 3]) |
260 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.__getstate__(), before, msg="Opcode was modified") |
261 | 1cbef6d8 | Michael Hanselmann | |
262 | ff0d18e6 | Iustin Pop | op = OpTest(nodef="foo", notype="Hello World") |
263 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
264 | 1cbef6d8 | Michael Hanselmann | op.Validate(False)
|
265 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.nodef, "foo") |
266 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.notype, "Hello World") |
267 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.__getstate__(), before, msg="Opcode was modified") |
268 | 1cbef6d8 | Michael Hanselmann | |
269 | 1cbef6d8 | Michael Hanselmann | def testValidateSetDefaults(self): |
270 | ff0d18e6 | Iustin Pop | class OpTest(opcodes.OpCode): |
271 | 1cbef6d8 | Michael Hanselmann | OP_PARAMS = [ |
272 | 197b323b | Michael Hanselmann | ("value1", "default", ht.TMaybeString, None), |
273 | 6888a6ed | Jose A. Lopes | ("value2", "result", ht.TMaybeString, None), |
274 | 1cbef6d8 | Michael Hanselmann | ] |
275 | 1cbef6d8 | Michael Hanselmann | |
276 | ff0d18e6 | Iustin Pop | op = OpTest() |
277 | 1cbef6d8 | Michael Hanselmann | op.Validate(True)
|
278 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.value1, "default") |
279 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.value2, "result") |
280 | 1cbef6d8 | Michael Hanselmann | self.assert_(op.dry_run is None) |
281 | 1cbef6d8 | Michael Hanselmann | self.assert_(op.debug_level is None) |
282 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.priority, constants.OP_PRIO_DEFAULT)
|
283 | 1cbef6d8 | Michael Hanselmann | |
284 | ff0d18e6 | Iustin Pop | op = OpTest(value1="hello", value2="world", debug_level=123) |
285 | 1cbef6d8 | Michael Hanselmann | op.Validate(True)
|
286 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.value1, "hello") |
287 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.value2, "world") |
288 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.debug_level, 123) |
289 | 1cbef6d8 | Michael Hanselmann | |
290 | 12e62af5 | René Nussbaumer | def testOpInstanceMultiAlloc(self): |
291 | 12e62af5 | René Nussbaumer | inst = dict([(name, []) for name in opcodes.OpInstanceCreate.GetAllSlots()]) |
292 | 12e62af5 | René Nussbaumer | inst_op = opcodes.OpInstanceCreate(**inst) |
293 | 12e62af5 | René Nussbaumer | inst_state = inst_op.__getstate__() |
294 | 12e62af5 | René Nussbaumer | |
295 | 12e62af5 | René Nussbaumer | multialloc = opcodes.OpInstanceMultiAlloc(instances=[inst_op]) |
296 | 12e62af5 | René Nussbaumer | state = multialloc.__getstate__() |
297 | 12e62af5 | René Nussbaumer | self.assertEquals(state["instances"], [inst_state]) |
298 | 12e62af5 | René Nussbaumer | loaded_multialloc = opcodes.OpCode.LoadOpCode(state) |
299 | 12e62af5 | René Nussbaumer | (loaded_inst,) = loaded_multialloc.instances |
300 | 12e62af5 | René Nussbaumer | self.assertNotEquals(loaded_inst, inst_op)
|
301 | 12e62af5 | René Nussbaumer | self.assertEquals(loaded_inst.__getstate__(), inst_state)
|
302 | 12e62af5 | René Nussbaumer | |
303 | 12e62af5 | René Nussbaumer | |
304 | b247c6fc | Michael Hanselmann | class TestOpcodeDepends(unittest.TestCase): |
305 | b247c6fc | Michael Hanselmann | def test(self): |
306 | 580b1fdd | Jose A. Lopes | check_relative = opcodes_base.BuildJobDepCheck(True)
|
307 | 580b1fdd | Jose A. Lopes | check_norelative = opcodes_base.TNoRelativeJobDependencies |
308 | b247c6fc | Michael Hanselmann | |
309 | b247c6fc | Michael Hanselmann | for fn in [check_relative, check_norelative]: |
310 | b247c6fc | Michael Hanselmann | self.assertTrue(fn(None)) |
311 | b247c6fc | Michael Hanselmann | self.assertTrue(fn([]))
|
312 | b247c6fc | Michael Hanselmann | self.assertTrue(fn([(1, [])])) |
313 | b247c6fc | Michael Hanselmann | self.assertTrue(fn([(719833, [])])) |
314 | b247c6fc | Michael Hanselmann | self.assertTrue(fn([("24879", [])])) |
315 | b247c6fc | Michael Hanselmann | self.assertTrue(fn([(2028, [constants.JOB_STATUS_ERROR])])) |
316 | b247c6fc | Michael Hanselmann | self.assertTrue(fn([
|
317 | b247c6fc | Michael Hanselmann | (2028, [constants.JOB_STATUS_ERROR]),
|
318 | b247c6fc | Michael Hanselmann | (18750, []),
|
319 | b247c6fc | Michael Hanselmann | (5063, [constants.JOB_STATUS_SUCCESS, constants.JOB_STATUS_ERROR]),
|
320 | b247c6fc | Michael Hanselmann | ])) |
321 | b247c6fc | Michael Hanselmann | |
322 | b247c6fc | Michael Hanselmann | self.assertFalse(fn(1)) |
323 | b247c6fc | Michael Hanselmann | self.assertFalse(fn([(9, )])) |
324 | b247c6fc | Michael Hanselmann | self.assertFalse(fn([(15194, constants.JOB_STATUS_ERROR)])) |
325 | b247c6fc | Michael Hanselmann | |
326 | b247c6fc | Michael Hanselmann | for i in [ |
327 | b247c6fc | Michael Hanselmann | [(-1, [])],
|
328 | b247c6fc | Michael Hanselmann | [(-27740, [constants.JOB_STATUS_CANCELED, constants.JOB_STATUS_ERROR]),
|
329 | b247c6fc | Michael Hanselmann | (-1, [constants.JOB_STATUS_ERROR]),
|
330 | b247c6fc | Michael Hanselmann | (9921, [])],
|
331 | b247c6fc | Michael Hanselmann | ]: |
332 | b247c6fc | Michael Hanselmann | self.assertTrue(check_relative(i))
|
333 | b247c6fc | Michael Hanselmann | self.assertFalse(check_norelative(i))
|
334 | b247c6fc | Michael Hanselmann | |
335 | b247c6fc | Michael Hanselmann | |
336 | bdfd7802 | Michael Hanselmann | class TestResultChecks(unittest.TestCase): |
337 | bdfd7802 | Michael Hanselmann | def testJobIdList(self): |
338 | bdfd7802 | Michael Hanselmann | for i in [[], [(False, "error")], [(False, "")], |
339 | bdfd7802 | Michael Hanselmann | [(True, 123), (True, "999")]]: |
340 | 72cd5493 | Jose A. Lopes | self.assertTrue(ht.TJobIdList(i))
|
341 | bdfd7802 | Michael Hanselmann | |
342 | bdfd7802 | Michael Hanselmann | for i in ["", [("x", 1)], [[], []], [[False, "", None], [True, 123]]]: |
343 | 72cd5493 | Jose A. Lopes | self.assertFalse(ht.TJobIdList(i))
|
344 | bdfd7802 | Michael Hanselmann | |
345 | bdfd7802 | Michael Hanselmann | def testJobIdListOnly(self): |
346 | 72cd5493 | Jose A. Lopes | self.assertTrue(ht.TJobIdListOnly({
|
347 | bdfd7802 | Michael Hanselmann | constants.JOB_IDS_KEY: [], |
348 | bdfd7802 | Michael Hanselmann | })) |
349 | 72cd5493 | Jose A. Lopes | self.assertTrue(ht.TJobIdListOnly({
|
350 | bdfd7802 | Michael Hanselmann | constants.JOB_IDS_KEY: [(True, "9282")], |
351 | bdfd7802 | Michael Hanselmann | })) |
352 | bdfd7802 | Michael Hanselmann | |
353 | 72cd5493 | Jose A. Lopes | self.assertFalse(ht.TJobIdListOnly({
|
354 | bdfd7802 | Michael Hanselmann | "x": None, |
355 | bdfd7802 | Michael Hanselmann | })) |
356 | 72cd5493 | Jose A. Lopes | self.assertFalse(ht.TJobIdListOnly({
|
357 | bdfd7802 | Michael Hanselmann | constants.JOB_IDS_KEY: [], |
358 | bdfd7802 | Michael Hanselmann | "x": None, |
359 | bdfd7802 | Michael Hanselmann | })) |
360 | 72cd5493 | Jose A. Lopes | self.assertFalse(ht.TJobIdListOnly({
|
361 | bdfd7802 | Michael Hanselmann | constants.JOB_IDS_KEY: [("foo", "bar")], |
362 | bdfd7802 | Michael Hanselmann | })) |
363 | 72cd5493 | Jose A. Lopes | self.assertFalse(ht.TJobIdListOnly({
|
364 | bdfd7802 | Michael Hanselmann | constants.JOB_IDS_KEY: [("one", "two", "three")], |
365 | bdfd7802 | Michael Hanselmann | })) |
366 | bdfd7802 | Michael Hanselmann | |
367 | bdfd7802 | Michael Hanselmann | |
368 | ddc1de7c | Michael Hanselmann | class TestOpInstanceSetParams(unittest.TestCase): |
369 | ddc1de7c | Michael Hanselmann | def _GenericTests(self, fn): |
370 | ddc1de7c | Michael Hanselmann | self.assertTrue(fn([]))
|
371 | ddc1de7c | Michael Hanselmann | self.assertTrue(fn([(constants.DDM_ADD, {})]))
|
372 | ddc1de7c | Michael Hanselmann | self.assertTrue(fn([(constants.DDM_REMOVE, {})]))
|
373 | ddc1de7c | Michael Hanselmann | for i in [0, 1, 2, 3, 9, 10, 1024]: |
374 | ddc1de7c | Michael Hanselmann | self.assertTrue(fn([(i, {})]))
|
375 | ddc1de7c | Michael Hanselmann | |
376 | ddc1de7c | Michael Hanselmann | self.assertFalse(fn(None)) |
377 | ddc1de7c | Michael Hanselmann | self.assertFalse(fn({}))
|
378 | ddc1de7c | Michael Hanselmann | self.assertFalse(fn("")) |
379 | ddc1de7c | Michael Hanselmann | self.assertFalse(fn(0)) |
380 | ddc1de7c | Michael Hanselmann | self.assertFalse(fn([(-100, {})])) |
381 | ddc1de7c | Michael Hanselmann | self.assertFalse(fn([(constants.DDM_ADD, 2, 3)])) |
382 | ddc1de7c | Michael Hanselmann | self.assertFalse(fn([[constants.DDM_ADD]]))
|
383 | ddc1de7c | Michael Hanselmann | |
384 | ddc1de7c | Michael Hanselmann | def testNicModifications(self): |
385 | 72cd5493 | Jose A. Lopes | fn = ht.TSetParamsMods(ht.TINicParams) |
386 | ddc1de7c | Michael Hanselmann | self._GenericTests(fn)
|
387 | ddc1de7c | Michael Hanselmann | |
388 | ddc1de7c | Michael Hanselmann | for param in constants.INIC_PARAMS: |
389 | ddc1de7c | Michael Hanselmann | self.assertTrue(fn([[constants.DDM_ADD, {param: None}]])) |
390 | ddc1de7c | Michael Hanselmann | self.assertTrue(fn([[constants.DDM_ADD, {param: param}]]))
|
391 | ddc1de7c | Michael Hanselmann | |
392 | ddc1de7c | Michael Hanselmann | def testDiskModifications(self): |
393 | 72cd5493 | Jose A. Lopes | fn = ht.TSetParamsMods(ht.TIDiskParams) |
394 | ddc1de7c | Michael Hanselmann | self._GenericTests(fn)
|
395 | ddc1de7c | Michael Hanselmann | |
396 | ddc1de7c | Michael Hanselmann | for param in constants.IDISK_PARAMS: |
397 | ddc1de7c | Michael Hanselmann | self.assertTrue(fn([[constants.DDM_ADD, {param: 0}]])) |
398 | ddc1de7c | Michael Hanselmann | self.assertTrue(fn([[constants.DDM_ADD, {param: param}]]))
|
399 | ddc1de7c | Michael Hanselmann | |
400 | ddc1de7c | Michael Hanselmann | |
401 | c32b908e | Michael Hanselmann | if __name__ == "__main__": |
402 | c32b908e | Michael Hanselmann | testutils.GanetiTestProgram() |