root / test / py / ganeti.opcodes_unittest.py @ 91c17910
History | View | Annotate | Download (15.7 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 | 65e183af | Michael Hanselmann | from ganeti import ht |
31 | 1cbef6d8 | Michael Hanselmann | from ganeti import constants |
32 | 1cbef6d8 | Michael Hanselmann | from ganeti import errors |
33 | 45d4c81c | Michael Hanselmann | from ganeti import compat |
34 | c32b908e | Michael Hanselmann | |
35 | c32b908e | Michael Hanselmann | import testutils |
36 | c32b908e | Michael Hanselmann | |
37 | c32b908e | Michael Hanselmann | |
38 | 5fa3d337 | Michael Hanselmann | #: Unless an opcode is included in the following list it must have a result
|
39 | 5fa3d337 | Michael Hanselmann | #: check of some sort
|
40 | b8028dcf | Michael Hanselmann | MISSING_RESULT_CHECK = compat.UniqueFrozenset([ |
41 | 5fa3d337 | Michael Hanselmann | opcodes.OpTestAllocator, |
42 | 5fa3d337 | Michael Hanselmann | opcodes.OpTestDelay, |
43 | 5fa3d337 | Michael Hanselmann | opcodes.OpTestDummy, |
44 | 5fa3d337 | Michael Hanselmann | opcodes.OpTestJqueue, |
45 | 5fa3d337 | Michael Hanselmann | ]) |
46 | 5fa3d337 | Michael Hanselmann | |
47 | 5fa3d337 | Michael Hanselmann | |
48 | c32b908e | Michael Hanselmann | class TestOpcodes(unittest.TestCase): |
49 | c32b908e | Michael Hanselmann | def test(self): |
50 | c32b908e | Michael Hanselmann | self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, None) |
51 | c32b908e | Michael Hanselmann | self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, "") |
52 | c32b908e | Michael Hanselmann | self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, {}) |
53 | c32b908e | Michael Hanselmann | self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, {"OP_ID": ""}) |
54 | c32b908e | Michael Hanselmann | |
55 | c32b908e | Michael Hanselmann | for cls in opcodes.OP_MAPPING.values(): |
56 | c32b908e | Michael Hanselmann | self.assert_(cls.OP_ID.startswith("OP_")) |
57 | dbc96028 | Michael Hanselmann | self.assert_(len(cls.OP_ID) > 3) |
58 | c32b908e | Michael Hanselmann | self.assertEqual(cls.OP_ID, cls.OP_ID.upper())
|
59 | ff0d18e6 | Iustin Pop | self.assertEqual(cls.OP_ID, opcodes._NameToId(cls.__name__))
|
60 | 3ce9a5e7 | Michael Hanselmann | self.assertFalse(compat.any(cls.OP_ID.startswith(prefix)
|
61 | 3ce9a5e7 | Michael Hanselmann | for prefix in opcodes._SUMMARY_PREFIX.keys())) |
62 | 5fa3d337 | Michael Hanselmann | if cls in MISSING_RESULT_CHECK: |
63 | 5fa3d337 | Michael Hanselmann | self.assertTrue(cls.OP_RESULT is None, |
64 | 5fa3d337 | Michael Hanselmann | msg=("%s is listed to not have a result check" %
|
65 | 5fa3d337 | Michael Hanselmann | cls.OP_ID)) |
66 | 5fa3d337 | Michael Hanselmann | else:
|
67 | 5fa3d337 | Michael Hanselmann | self.assertTrue(callable(cls.OP_RESULT), |
68 | 5fa3d337 | Michael Hanselmann | msg=("%s should have a result check" % cls.OP_ID))
|
69 | c32b908e | Michael Hanselmann | |
70 | c32b908e | Michael Hanselmann | self.assertRaises(TypeError, cls, unsupported_parameter="some value") |
71 | c32b908e | Michael Hanselmann | |
72 | c32b908e | Michael Hanselmann | args = [ |
73 | c32b908e | Michael Hanselmann | # No variables
|
74 | c32b908e | Michael Hanselmann | {}, |
75 | c32b908e | Michael Hanselmann | |
76 | c32b908e | Michael Hanselmann | # Variables supported by all opcodes
|
77 | c32b908e | Michael Hanselmann | {"dry_run": False, "debug_level": 0, }, |
78 | c32b908e | Michael Hanselmann | |
79 | c32b908e | Michael Hanselmann | # All variables
|
80 | 12e62af5 | René Nussbaumer | dict([(name, []) for name in cls.GetAllSlots()]) |
81 | c32b908e | Michael Hanselmann | ] |
82 | c32b908e | Michael Hanselmann | |
83 | c32b908e | Michael Hanselmann | for i in args: |
84 | c32b908e | Michael Hanselmann | op = cls(**i) |
85 | c32b908e | Michael Hanselmann | |
86 | c32b908e | Michael Hanselmann | self.assertEqual(op.OP_ID, cls.OP_ID)
|
87 | c32b908e | Michael Hanselmann | self._checkSummary(op)
|
88 | c32b908e | Michael Hanselmann | |
89 | c32b908e | Michael Hanselmann | # Try a restore
|
90 | c32b908e | Michael Hanselmann | state = op.__getstate__() |
91 | c32b908e | Michael Hanselmann | self.assert_(isinstance(state, dict)) |
92 | c32b908e | Michael Hanselmann | |
93 | c32b908e | Michael Hanselmann | restored = opcodes.OpCode.LoadOpCode(state) |
94 | c32b908e | Michael Hanselmann | self.assert_(isinstance(restored, cls)) |
95 | c32b908e | Michael Hanselmann | self._checkSummary(restored)
|
96 | c32b908e | Michael Hanselmann | |
97 | 65e183af | Michael Hanselmann | for name in ["x_y_z", "hello_world"]: |
98 | 32683096 | René Nussbaumer | assert name not in cls.GetAllSlots() |
99 | 65e183af | Michael Hanselmann | for value in [None, True, False, [], "Hello World"]: |
100 | 65e183af | Michael Hanselmann | self.assertRaises(AttributeError, setattr, op, name, value) |
101 | 65e183af | Michael Hanselmann | |
102 | c32b908e | Michael Hanselmann | def _checkSummary(self, op): |
103 | c32b908e | Michael Hanselmann | summary = op.Summary() |
104 | c32b908e | Michael Hanselmann | |
105 | c32b908e | Michael Hanselmann | if hasattr(op, "OP_DSC_FIELD"): |
106 | c32b908e | Michael Hanselmann | self.assert_(("OP_%s" % summary).startswith("%s(" % op.OP_ID)) |
107 | c32b908e | Michael Hanselmann | self.assert_(summary.endswith(")")) |
108 | c32b908e | Michael Hanselmann | else:
|
109 | c32b908e | Michael Hanselmann | self.assertEqual("OP_%s" % summary, op.OP_ID) |
110 | c32b908e | Michael Hanselmann | |
111 | 65ffb373 | Michael Hanselmann | def testSummary(self): |
112 | ff0d18e6 | Iustin Pop | class OpTest(opcodes.OpCode): |
113 | 65ffb373 | Michael Hanselmann | OP_DSC_FIELD = "data"
|
114 | 65ffb373 | Michael Hanselmann | OP_PARAMS = [ |
115 | 197b323b | Michael Hanselmann | ("data", ht.NoDefault, ht.TString, None), |
116 | 65ffb373 | Michael Hanselmann | ] |
117 | 65ffb373 | Michael Hanselmann | |
118 | ff0d18e6 | Iustin Pop | self.assertEqual(OpTest(data="").Summary(), "TEST()") |
119 | ff0d18e6 | Iustin Pop | self.assertEqual(OpTest(data="Hello World").Summary(), |
120 | 65ffb373 | Michael Hanselmann | "TEST(Hello World)")
|
121 | ff0d18e6 | Iustin Pop | self.assertEqual(OpTest(data="node1.example.com").Summary(), |
122 | 65ffb373 | Michael Hanselmann | "TEST(node1.example.com)")
|
123 | 65ffb373 | Michael Hanselmann | |
124 | 8bc17ebb | Iustin Pop | def testSummaryFormatter(self): |
125 | 8bc17ebb | Iustin Pop | class OpTest(opcodes.OpCode): |
126 | 8bc17ebb | Iustin Pop | OP_DSC_FIELD = "data"
|
127 | 8bc17ebb | Iustin Pop | OP_DSC_FORMATTER = lambda _, v: "a" |
128 | 8bc17ebb | Iustin Pop | OP_PARAMS = [ |
129 | 8bc17ebb | Iustin Pop | ("data", ht.NoDefault, ht.TString, None), |
130 | 8bc17ebb | Iustin Pop | ] |
131 | 8bc17ebb | Iustin Pop | self.assertEqual(OpTest(data="").Summary(), "TEST(a)") |
132 | 8bc17ebb | Iustin Pop | self.assertEqual(OpTest(data="b").Summary(), "TEST(a)") |
133 | 8bc17ebb | Iustin Pop | |
134 | 3ce9a5e7 | Michael Hanselmann | def testTinySummary(self): |
135 | 3ce9a5e7 | Michael Hanselmann | self.assertFalse(utils.FindDuplicates(opcodes._SUMMARY_PREFIX.values()))
|
136 | 3ce9a5e7 | Michael Hanselmann | self.assertTrue(compat.all(prefix.endswith("_") and supplement.endswith("_") |
137 | 3ce9a5e7 | Michael Hanselmann | for (prefix, supplement) in |
138 | 3ce9a5e7 | Michael Hanselmann | opcodes._SUMMARY_PREFIX.items())) |
139 | 3ce9a5e7 | Michael Hanselmann | |
140 | 3ce9a5e7 | Michael Hanselmann | self.assertEqual(opcodes.OpClusterPostInit().TinySummary(), "C_POST_INIT") |
141 | 3ce9a5e7 | Michael Hanselmann | self.assertEqual(opcodes.OpNodeRemove().TinySummary(), "N_REMOVE") |
142 | 3ce9a5e7 | Michael Hanselmann | self.assertEqual(opcodes.OpInstanceMigrate().TinySummary(), "I_MIGRATE") |
143 | 3ce9a5e7 | Michael Hanselmann | self.assertEqual(opcodes.OpGroupQuery().TinySummary(), "G_QUERY") |
144 | 3ce9a5e7 | Michael Hanselmann | self.assertEqual(opcodes.OpTestJqueue().TinySummary(), "TEST_JQUEUE") |
145 | 3ce9a5e7 | Michael Hanselmann | |
146 | 65ffb373 | Michael Hanselmann | def testListSummary(self): |
147 | ff0d18e6 | Iustin Pop | class OpTest(opcodes.OpCode): |
148 | 65ffb373 | Michael Hanselmann | OP_DSC_FIELD = "data"
|
149 | 65ffb373 | Michael Hanselmann | OP_PARAMS = [ |
150 | 197b323b | Michael Hanselmann | ("data", ht.NoDefault, ht.TList, None), |
151 | 65ffb373 | Michael Hanselmann | ] |
152 | 65ffb373 | Michael Hanselmann | |
153 | ff0d18e6 | Iustin Pop | self.assertEqual(OpTest(data=["a", "b", "c"]).Summary(), |
154 | 65ffb373 | Michael Hanselmann | "TEST(a,b,c)")
|
155 | ff0d18e6 | Iustin Pop | self.assertEqual(OpTest(data=["a", None, "c"]).Summary(), |
156 | 65ffb373 | Michael Hanselmann | "TEST(a,None,c)")
|
157 | ff0d18e6 | Iustin Pop | self.assertEqual(OpTest(data=[1, 2, 3, 4]).Summary(), "TEST(1,2,3,4)") |
158 | 65ffb373 | Michael Hanselmann | |
159 | dbc96028 | Michael Hanselmann | def testOpId(self): |
160 | dbc96028 | Michael Hanselmann | self.assertFalse(utils.FindDuplicates(cls.OP_ID
|
161 | dbc96028 | Michael Hanselmann | for cls in opcodes._GetOpList())) |
162 | dbc96028 | Michael Hanselmann | self.assertEqual(len(opcodes._GetOpList()), len(opcodes.OP_MAPPING)) |
163 | dbc96028 | Michael Hanselmann | |
164 | 65e183af | Michael Hanselmann | def testParams(self): |
165 | 65e183af | Michael Hanselmann | supported_by_all = set(["debug_level", "dry_run", "priority"]) |
166 | 65e183af | Michael Hanselmann | |
167 | 687c10d9 | Iustin Pop | self.assertTrue(opcodes.BaseOpCode not in opcodes.OP_MAPPING.values()) |
168 | 687c10d9 | Iustin Pop | self.assertTrue(opcodes.OpCode not in opcodes.OP_MAPPING.values()) |
169 | 65e183af | Michael Hanselmann | |
170 | 687c10d9 | Iustin Pop | for cls in opcodes.OP_MAPPING.values() + [opcodes.OpCode]: |
171 | 32683096 | René Nussbaumer | all_slots = cls.GetAllSlots() |
172 | 65e183af | Michael Hanselmann | |
173 | 65e183af | Michael Hanselmann | self.assertEqual(len(set(all_slots) & supported_by_all), 3, |
174 | 65e183af | Michael Hanselmann | msg=("Opcode %s doesn't support all base"
|
175 | 65e183af | Michael Hanselmann | " parameters (%r)" % (cls.OP_ID, supported_by_all)))
|
176 | 65e183af | Michael Hanselmann | |
177 | 65e183af | Michael Hanselmann | # All opcodes must have OP_PARAMS
|
178 | 65e183af | Michael Hanselmann | self.assert_(hasattr(cls, "OP_PARAMS"), |
179 | 65e183af | Michael Hanselmann | msg="%s doesn't have OP_PARAMS" % cls.OP_ID)
|
180 | 65e183af | Michael Hanselmann | |
181 | 197b323b | Michael Hanselmann | param_names = [name for (name, _, _, _) in cls.GetAllParams()] |
182 | 65e183af | Michael Hanselmann | |
183 | 65e183af | Michael Hanselmann | self.assertEqual(all_slots, param_names)
|
184 | 65e183af | Michael Hanselmann | |
185 | 65e183af | Michael Hanselmann | # Without inheritance
|
186 | 197b323b | Michael Hanselmann | self.assertEqual(cls.__slots__,
|
187 | 197b323b | Michael Hanselmann | [name for (name, _, _, _) in cls.OP_PARAMS]) |
188 | 65e183af | Michael Hanselmann | |
189 | 65e183af | Michael Hanselmann | # This won't work if parameters are converted to a dictionary
|
190 | 65e183af | Michael Hanselmann | duplicates = utils.FindDuplicates(param_names) |
191 | 65e183af | Michael Hanselmann | self.assertFalse(duplicates,
|
192 | 65e183af | Michael Hanselmann | msg=("Found duplicate parameters %r in %s" %
|
193 | 65e183af | Michael Hanselmann | (duplicates, cls.OP_ID))) |
194 | 65e183af | Michael Hanselmann | |
195 | 65e183af | Michael Hanselmann | # Check parameter definitions
|
196 | 197b323b | Michael Hanselmann | for attr_name, aval, test, doc in cls.GetAllParams(): |
197 | 65e183af | Michael Hanselmann | self.assert_(attr_name)
|
198 | 65e183af | Michael Hanselmann | self.assert_(test is None or test is ht.NoType or callable(test), |
199 | 65e183af | Michael Hanselmann | msg=("Invalid type check for %s.%s" %
|
200 | 65e183af | Michael Hanselmann | (cls.OP_ID, attr_name))) |
201 | 45d4c81c | Michael Hanselmann | self.assertTrue(doc is None or isinstance(doc, basestring)) |
202 | 65e183af | Michael Hanselmann | |
203 | 65e183af | Michael Hanselmann | if callable(aval): |
204 | 6ae72286 | Michael Hanselmann | default_value = aval() |
205 | 6ae72286 | Michael Hanselmann | self.assertFalse(callable(default_value), |
206 | 08f31176 | Iustin Pop | msg=("Default value of %s.%s returned by function"
|
207 | 08f31176 | Iustin Pop | " is callable" % (cls.OP_ID, attr_name)))
|
208 | 6ae72286 | Michael Hanselmann | else:
|
209 | 08f31176 | Iustin Pop | self.assertFalse(isinstance(aval, (list, dict, set)), |
210 | 08f31176 | Iustin Pop | msg=("Default value of %s.%s is mutable (%s)" %
|
211 | 08f31176 | Iustin Pop | (cls.OP_ID, attr_name, repr(aval))))
|
212 | 08f31176 | Iustin Pop | |
213 | 6ae72286 | Michael Hanselmann | default_value = aval |
214 | 6ae72286 | Michael Hanselmann | |
215 | 6ae72286 | Michael Hanselmann | if aval is not ht.NoDefault and test is not ht.NoType: |
216 | 6ae72286 | Michael Hanselmann | self.assertTrue(test(default_value),
|
217 | 08f31176 | Iustin Pop | msg=("Default value of %s.%s does not verify" %
|
218 | 6ae72286 | Michael Hanselmann | (cls.OP_ID, attr_name))) |
219 | 65e183af | Michael Hanselmann | |
220 | 45d4c81c | Michael Hanselmann | # If any parameter has documentation, all others need to have it as well
|
221 | 45d4c81c | Michael Hanselmann | has_doc = [doc is not None for (_, _, _, doc) in cls.OP_PARAMS] |
222 | 45d4c81c | Michael Hanselmann | self.assertTrue(not compat.any(has_doc) or compat.all(has_doc), |
223 | 45d4c81c | Michael Hanselmann | msg="%s does not document all parameters" % cls)
|
224 | 45d4c81c | Michael Hanselmann | |
225 | 1cbef6d8 | Michael Hanselmann | def testValidateNoModification(self): |
226 | ff0d18e6 | Iustin Pop | class OpTest(opcodes.OpCode): |
227 | 1cbef6d8 | Michael Hanselmann | OP_PARAMS = [ |
228 | 197b323b | Michael Hanselmann | ("nodef", ht.NoDefault, ht.TMaybeString, None), |
229 | 197b323b | Michael Hanselmann | ("wdef", "default", ht.TMaybeString, None), |
230 | 197b323b | Michael Hanselmann | ("number", 0, ht.TInt, None), |
231 | 197b323b | Michael Hanselmann | ("notype", None, ht.NoType, None), |
232 | 1cbef6d8 | Michael Hanselmann | ] |
233 | 1cbef6d8 | Michael Hanselmann | |
234 | 1cbef6d8 | Michael Hanselmann | # Missing required parameter "nodef"
|
235 | ff0d18e6 | Iustin Pop | op = OpTest() |
236 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
237 | 1cbef6d8 | Michael Hanselmann | self.assertRaises(errors.OpPrereqError, op.Validate, False) |
238 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "nodef")) |
239 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "wdef")) |
240 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "number")) |
241 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "notype")) |
242 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.__getstate__(), before, msg="Opcode was modified") |
243 | 1cbef6d8 | Michael Hanselmann | |
244 | 1cbef6d8 | Michael Hanselmann | # Required parameter "nodef" is provided
|
245 | ff0d18e6 | Iustin Pop | op = OpTest(nodef="foo")
|
246 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
247 | 1cbef6d8 | Michael Hanselmann | op.Validate(False)
|
248 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.__getstate__(), before, msg="Opcode was modified") |
249 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.nodef, "foo") |
250 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "wdef")) |
251 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "number")) |
252 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "notype")) |
253 | 1cbef6d8 | Michael Hanselmann | |
254 | 1cbef6d8 | Michael Hanselmann | # Missing required parameter "nodef"
|
255 | ff0d18e6 | Iustin Pop | op = OpTest(wdef="hello", number=999) |
256 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
257 | 1cbef6d8 | Michael Hanselmann | self.assertRaises(errors.OpPrereqError, op.Validate, False) |
258 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "nodef")) |
259 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "notype")) |
260 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.__getstate__(), before, msg="Opcode was modified") |
261 | 1cbef6d8 | Michael Hanselmann | |
262 | 1cbef6d8 | Michael Hanselmann | # Wrong type for "nodef"
|
263 | ff0d18e6 | Iustin Pop | op = OpTest(nodef=987)
|
264 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
265 | 1cbef6d8 | Michael Hanselmann | self.assertRaises(errors.OpPrereqError, op.Validate, False) |
266 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.nodef, 987) |
267 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "notype")) |
268 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.__getstate__(), before, msg="Opcode was modified") |
269 | 1cbef6d8 | Michael Hanselmann | |
270 | 1cbef6d8 | Michael Hanselmann | # Testing different types for "notype"
|
271 | ff0d18e6 | Iustin Pop | op = OpTest(nodef="foo", notype=[1, 2, 3]) |
272 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
273 | 1cbef6d8 | Michael Hanselmann | op.Validate(False)
|
274 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.nodef, "foo") |
275 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.notype, [1, 2, 3]) |
276 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.__getstate__(), before, msg="Opcode was modified") |
277 | 1cbef6d8 | Michael Hanselmann | |
278 | ff0d18e6 | Iustin Pop | op = OpTest(nodef="foo", notype="Hello World") |
279 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
280 | 1cbef6d8 | Michael Hanselmann | op.Validate(False)
|
281 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.nodef, "foo") |
282 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.notype, "Hello World") |
283 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.__getstate__(), before, msg="Opcode was modified") |
284 | 1cbef6d8 | Michael Hanselmann | |
285 | 1cbef6d8 | Michael Hanselmann | def testValidateSetDefaults(self): |
286 | ff0d18e6 | Iustin Pop | class OpTest(opcodes.OpCode): |
287 | 1cbef6d8 | Michael Hanselmann | OP_PARAMS = [ |
288 | 1cbef6d8 | Michael Hanselmann | # Static default value
|
289 | 197b323b | Michael Hanselmann | ("value1", "default", ht.TMaybeString, None), |
290 | 1cbef6d8 | Michael Hanselmann | |
291 | 1cbef6d8 | Michael Hanselmann | # Default value callback
|
292 | 197b323b | Michael Hanselmann | ("value2", lambda: "result", ht.TMaybeString, None), |
293 | 1cbef6d8 | Michael Hanselmann | ] |
294 | 1cbef6d8 | Michael Hanselmann | |
295 | ff0d18e6 | Iustin Pop | op = OpTest() |
296 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
297 | 1cbef6d8 | Michael Hanselmann | op.Validate(True)
|
298 | 1cbef6d8 | Michael Hanselmann | self.assertNotEqual(op.__getstate__(), before,
|
299 | 1cbef6d8 | Michael Hanselmann | msg="Opcode was not modified")
|
300 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.value1, "default") |
301 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.value2, "result") |
302 | 1cbef6d8 | Michael Hanselmann | self.assert_(op.dry_run is None) |
303 | 1cbef6d8 | Michael Hanselmann | self.assert_(op.debug_level is None) |
304 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.priority, constants.OP_PRIO_DEFAULT)
|
305 | 1cbef6d8 | Michael Hanselmann | |
306 | ff0d18e6 | Iustin Pop | op = OpTest(value1="hello", value2="world", debug_level=123) |
307 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
308 | 1cbef6d8 | Michael Hanselmann | op.Validate(True)
|
309 | 1cbef6d8 | Michael Hanselmann | self.assertNotEqual(op.__getstate__(), before,
|
310 | 1cbef6d8 | Michael Hanselmann | msg="Opcode was not modified")
|
311 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.value1, "hello") |
312 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.value2, "world") |
313 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.debug_level, 123) |
314 | 1cbef6d8 | Michael Hanselmann | |
315 | 12e62af5 | René Nussbaumer | def testOpInstanceMultiAlloc(self): |
316 | 12e62af5 | René Nussbaumer | inst = dict([(name, []) for name in opcodes.OpInstanceCreate.GetAllSlots()]) |
317 | 12e62af5 | René Nussbaumer | inst_op = opcodes.OpInstanceCreate(**inst) |
318 | 12e62af5 | René Nussbaumer | inst_state = inst_op.__getstate__() |
319 | 12e62af5 | René Nussbaumer | |
320 | 12e62af5 | René Nussbaumer | multialloc = opcodes.OpInstanceMultiAlloc(instances=[inst_op]) |
321 | 12e62af5 | René Nussbaumer | state = multialloc.__getstate__() |
322 | 12e62af5 | René Nussbaumer | self.assertEquals(state["instances"], [inst_state]) |
323 | 12e62af5 | René Nussbaumer | loaded_multialloc = opcodes.OpCode.LoadOpCode(state) |
324 | 12e62af5 | René Nussbaumer | (loaded_inst,) = loaded_multialloc.instances |
325 | 12e62af5 | René Nussbaumer | self.assertNotEquals(loaded_inst, inst_op)
|
326 | 12e62af5 | René Nussbaumer | self.assertEquals(loaded_inst.__getstate__(), inst_state)
|
327 | 12e62af5 | René Nussbaumer | |
328 | 12e62af5 | René Nussbaumer | |
329 | b247c6fc | Michael Hanselmann | class TestOpcodeDepends(unittest.TestCase): |
330 | b247c6fc | Michael Hanselmann | def test(self): |
331 | b247c6fc | Michael Hanselmann | check_relative = opcodes._BuildJobDepCheck(True)
|
332 | b247c6fc | Michael Hanselmann | check_norelative = opcodes.TNoRelativeJobDependencies |
333 | b247c6fc | Michael Hanselmann | |
334 | b247c6fc | Michael Hanselmann | for fn in [check_relative, check_norelative]: |
335 | b247c6fc | Michael Hanselmann | self.assertTrue(fn(None)) |
336 | b247c6fc | Michael Hanselmann | self.assertTrue(fn([]))
|
337 | b247c6fc | Michael Hanselmann | self.assertTrue(fn([(1, [])])) |
338 | b247c6fc | Michael Hanselmann | self.assertTrue(fn([(719833, [])])) |
339 | b247c6fc | Michael Hanselmann | self.assertTrue(fn([("24879", [])])) |
340 | b247c6fc | Michael Hanselmann | self.assertTrue(fn([(2028, [constants.JOB_STATUS_ERROR])])) |
341 | b247c6fc | Michael Hanselmann | self.assertTrue(fn([
|
342 | b247c6fc | Michael Hanselmann | (2028, [constants.JOB_STATUS_ERROR]),
|
343 | b247c6fc | Michael Hanselmann | (18750, []),
|
344 | b247c6fc | Michael Hanselmann | (5063, [constants.JOB_STATUS_SUCCESS, constants.JOB_STATUS_ERROR]),
|
345 | b247c6fc | Michael Hanselmann | ])) |
346 | b247c6fc | Michael Hanselmann | |
347 | b247c6fc | Michael Hanselmann | self.assertFalse(fn(1)) |
348 | b247c6fc | Michael Hanselmann | self.assertFalse(fn([(9, )])) |
349 | b247c6fc | Michael Hanselmann | self.assertFalse(fn([(15194, constants.JOB_STATUS_ERROR)])) |
350 | b247c6fc | Michael Hanselmann | |
351 | b247c6fc | Michael Hanselmann | for i in [ |
352 | b247c6fc | Michael Hanselmann | [(-1, [])],
|
353 | b247c6fc | Michael Hanselmann | [(-27740, [constants.JOB_STATUS_CANCELED, constants.JOB_STATUS_ERROR]),
|
354 | b247c6fc | Michael Hanselmann | (-1, [constants.JOB_STATUS_ERROR]),
|
355 | b247c6fc | Michael Hanselmann | (9921, [])],
|
356 | b247c6fc | Michael Hanselmann | ]: |
357 | b247c6fc | Michael Hanselmann | self.assertTrue(check_relative(i))
|
358 | b247c6fc | Michael Hanselmann | self.assertFalse(check_norelative(i))
|
359 | b247c6fc | Michael Hanselmann | |
360 | b247c6fc | Michael Hanselmann | |
361 | bdfd7802 | Michael Hanselmann | class TestResultChecks(unittest.TestCase): |
362 | bdfd7802 | Michael Hanselmann | def testJobIdList(self): |
363 | bdfd7802 | Michael Hanselmann | for i in [[], [(False, "error")], [(False, "")], |
364 | bdfd7802 | Michael Hanselmann | [(True, 123), (True, "999")]]: |
365 | bdfd7802 | Michael Hanselmann | self.assertTrue(opcodes.TJobIdList(i))
|
366 | bdfd7802 | Michael Hanselmann | |
367 | bdfd7802 | Michael Hanselmann | for i in ["", [("x", 1)], [[], []], [[False, "", None], [True, 123]]]: |
368 | bdfd7802 | Michael Hanselmann | self.assertFalse(opcodes.TJobIdList(i))
|
369 | bdfd7802 | Michael Hanselmann | |
370 | bdfd7802 | Michael Hanselmann | def testJobIdListOnly(self): |
371 | bdfd7802 | Michael Hanselmann | self.assertTrue(opcodes.TJobIdListOnly({
|
372 | bdfd7802 | Michael Hanselmann | constants.JOB_IDS_KEY: [], |
373 | bdfd7802 | Michael Hanselmann | })) |
374 | bdfd7802 | Michael Hanselmann | self.assertTrue(opcodes.TJobIdListOnly({
|
375 | bdfd7802 | Michael Hanselmann | constants.JOB_IDS_KEY: [(True, "9282")], |
376 | bdfd7802 | Michael Hanselmann | })) |
377 | bdfd7802 | Michael Hanselmann | |
378 | bdfd7802 | Michael Hanselmann | self.assertFalse(opcodes.TJobIdListOnly({
|
379 | bdfd7802 | Michael Hanselmann | "x": None, |
380 | bdfd7802 | Michael Hanselmann | })) |
381 | bdfd7802 | Michael Hanselmann | self.assertFalse(opcodes.TJobIdListOnly({
|
382 | bdfd7802 | Michael Hanselmann | constants.JOB_IDS_KEY: [], |
383 | bdfd7802 | Michael Hanselmann | "x": None, |
384 | bdfd7802 | Michael Hanselmann | })) |
385 | bdfd7802 | Michael Hanselmann | self.assertFalse(opcodes.TJobIdListOnly({
|
386 | bdfd7802 | Michael Hanselmann | constants.JOB_IDS_KEY: [("foo", "bar")], |
387 | bdfd7802 | Michael Hanselmann | })) |
388 | bdfd7802 | Michael Hanselmann | self.assertFalse(opcodes.TJobIdListOnly({
|
389 | bdfd7802 | Michael Hanselmann | constants.JOB_IDS_KEY: [("one", "two", "three")], |
390 | bdfd7802 | Michael Hanselmann | })) |
391 | bdfd7802 | Michael Hanselmann | |
392 | bdfd7802 | Michael Hanselmann | |
393 | 8f227489 | Michael Hanselmann | class TestClusterOsList(unittest.TestCase): |
394 | 8f227489 | Michael Hanselmann | def test(self): |
395 | 8f227489 | Michael Hanselmann | good = [ |
396 | 8f227489 | Michael Hanselmann | None,
|
397 | 8f227489 | Michael Hanselmann | [], |
398 | 8f227489 | Michael Hanselmann | [(constants.DDM_ADD, "dos"),
|
399 | 8f227489 | Michael Hanselmann | (constants.DDM_REMOVE, "linux")],
|
400 | 8f227489 | Michael Hanselmann | ] |
401 | 8f227489 | Michael Hanselmann | |
402 | 8f227489 | Michael Hanselmann | for i in good: |
403 | 8f227489 | Michael Hanselmann | self.assertTrue(opcodes._TestClusterOsList(i))
|
404 | 8f227489 | Michael Hanselmann | |
405 | 8f227489 | Michael Hanselmann | wrong = ["", 0, "xy", ["Hello World"], object(), |
406 | 8f227489 | Michael Hanselmann | [("foo", "bar")], |
407 | 8f227489 | Michael Hanselmann | [("", "")], |
408 | 8f227489 | Michael Hanselmann | [[constants.DDM_ADD]], |
409 | 8f227489 | Michael Hanselmann | [(constants.DDM_ADD, "")],
|
410 | 8f227489 | Michael Hanselmann | [(constants.DDM_REMOVE, "")],
|
411 | 8f227489 | Michael Hanselmann | [(constants.DDM_ADD, None)],
|
412 | 8f227489 | Michael Hanselmann | [(constants.DDM_REMOVE, None)],
|
413 | 8f227489 | Michael Hanselmann | ] |
414 | 8f227489 | Michael Hanselmann | |
415 | 8f227489 | Michael Hanselmann | for i in wrong: |
416 | 8f227489 | Michael Hanselmann | self.assertFalse(opcodes._TestClusterOsList(i))
|
417 | 8f227489 | Michael Hanselmann | |
418 | 8f227489 | Michael Hanselmann | |
419 | ddc1de7c | Michael Hanselmann | class TestOpInstanceSetParams(unittest.TestCase): |
420 | ddc1de7c | Michael Hanselmann | def _GenericTests(self, fn): |
421 | ddc1de7c | Michael Hanselmann | self.assertTrue(fn([]))
|
422 | ddc1de7c | Michael Hanselmann | self.assertTrue(fn([(constants.DDM_ADD, {})]))
|
423 | ddc1de7c | Michael Hanselmann | self.assertTrue(fn([(constants.DDM_REMOVE, {})]))
|
424 | ddc1de7c | Michael Hanselmann | for i in [0, 1, 2, 3, 9, 10, 1024]: |
425 | ddc1de7c | Michael Hanselmann | self.assertTrue(fn([(i, {})]))
|
426 | ddc1de7c | Michael Hanselmann | |
427 | ddc1de7c | Michael Hanselmann | self.assertFalse(fn(None)) |
428 | ddc1de7c | Michael Hanselmann | self.assertFalse(fn({}))
|
429 | ddc1de7c | Michael Hanselmann | self.assertFalse(fn("")) |
430 | ddc1de7c | Michael Hanselmann | self.assertFalse(fn(0)) |
431 | ddc1de7c | Michael Hanselmann | self.assertFalse(fn([(-100, {})])) |
432 | ddc1de7c | Michael Hanselmann | self.assertFalse(fn([(constants.DDM_ADD, 2, 3)])) |
433 | ddc1de7c | Michael Hanselmann | self.assertFalse(fn([[constants.DDM_ADD]]))
|
434 | ddc1de7c | Michael Hanselmann | |
435 | ddc1de7c | Michael Hanselmann | def testNicModifications(self): |
436 | 0813170b | Michael Hanselmann | fn = opcodes.OpInstanceSetParams.TestNicModifications |
437 | ddc1de7c | Michael Hanselmann | self._GenericTests(fn)
|
438 | ddc1de7c | Michael Hanselmann | |
439 | ddc1de7c | Michael Hanselmann | for param in constants.INIC_PARAMS: |
440 | ddc1de7c | Michael Hanselmann | self.assertTrue(fn([[constants.DDM_ADD, {param: None}]])) |
441 | ddc1de7c | Michael Hanselmann | self.assertTrue(fn([[constants.DDM_ADD, {param: param}]]))
|
442 | ddc1de7c | Michael Hanselmann | |
443 | ddc1de7c | Michael Hanselmann | def testDiskModifications(self): |
444 | 0813170b | Michael Hanselmann | fn = opcodes.OpInstanceSetParams.TestDiskModifications |
445 | ddc1de7c | Michael Hanselmann | self._GenericTests(fn)
|
446 | ddc1de7c | Michael Hanselmann | |
447 | ddc1de7c | Michael Hanselmann | for param in constants.IDISK_PARAMS: |
448 | ddc1de7c | Michael Hanselmann | self.assertTrue(fn([[constants.DDM_ADD, {param: 0}]])) |
449 | ddc1de7c | Michael Hanselmann | self.assertTrue(fn([[constants.DDM_ADD, {param: param}]]))
|
450 | ddc1de7c | Michael Hanselmann | |
451 | ddc1de7c | Michael Hanselmann | |
452 | c32b908e | Michael Hanselmann | if __name__ == "__main__": |
453 | c32b908e | Michael Hanselmann | testutils.GanetiTestProgram() |