root / test / ganeti.opcodes_unittest.py @ ef9fa5b9
History | View | Annotate | Download (12 kB)
1 | c32b908e | Michael Hanselmann | #!/usr/bin/python
|
---|---|---|---|
2 | c32b908e | Michael Hanselmann | #
|
3 | c32b908e | Michael Hanselmann | |
4 | 687c10d9 | Iustin Pop | # Copyright (C) 2010, 2011 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 | c32b908e | Michael Hanselmann | class TestOpcodes(unittest.TestCase): |
39 | c32b908e | Michael Hanselmann | def test(self): |
40 | c32b908e | Michael Hanselmann | self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, None) |
41 | c32b908e | Michael Hanselmann | self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, "") |
42 | c32b908e | Michael Hanselmann | self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, {}) |
43 | c32b908e | Michael Hanselmann | self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, {"OP_ID": ""}) |
44 | c32b908e | Michael Hanselmann | |
45 | c32b908e | Michael Hanselmann | for cls in opcodes.OP_MAPPING.values(): |
46 | c32b908e | Michael Hanselmann | self.assert_(cls.OP_ID.startswith("OP_")) |
47 | dbc96028 | Michael Hanselmann | self.assert_(len(cls.OP_ID) > 3) |
48 | c32b908e | Michael Hanselmann | self.assertEqual(cls.OP_ID, cls.OP_ID.upper())
|
49 | ff0d18e6 | Iustin Pop | self.assertEqual(cls.OP_ID, opcodes._NameToId(cls.__name__))
|
50 | 3ce9a5e7 | Michael Hanselmann | self.assertFalse(compat.any(cls.OP_ID.startswith(prefix)
|
51 | 3ce9a5e7 | Michael Hanselmann | for prefix in opcodes._SUMMARY_PREFIX.keys())) |
52 | 1ce03fb1 | Michael Hanselmann | self.assertTrue(cls.OP_RESULT is None or callable(cls.OP_RESULT)) |
53 | c32b908e | Michael Hanselmann | |
54 | c32b908e | Michael Hanselmann | self.assertRaises(TypeError, cls, unsupported_parameter="some value") |
55 | c32b908e | Michael Hanselmann | |
56 | c32b908e | Michael Hanselmann | args = [ |
57 | c32b908e | Michael Hanselmann | # No variables
|
58 | c32b908e | Michael Hanselmann | {}, |
59 | c32b908e | Michael Hanselmann | |
60 | c32b908e | Michael Hanselmann | # Variables supported by all opcodes
|
61 | c32b908e | Michael Hanselmann | {"dry_run": False, "debug_level": 0, }, |
62 | c32b908e | Michael Hanselmann | |
63 | c32b908e | Michael Hanselmann | # All variables
|
64 | c32b908e | Michael Hanselmann | dict([(name, False) for name in cls._all_slots()]) |
65 | c32b908e | Michael Hanselmann | ] |
66 | c32b908e | Michael Hanselmann | |
67 | c32b908e | Michael Hanselmann | for i in args: |
68 | c32b908e | Michael Hanselmann | op = cls(**i) |
69 | c32b908e | Michael Hanselmann | |
70 | c32b908e | Michael Hanselmann | self.assertEqual(op.OP_ID, cls.OP_ID)
|
71 | c32b908e | Michael Hanselmann | self._checkSummary(op)
|
72 | c32b908e | Michael Hanselmann | |
73 | c32b908e | Michael Hanselmann | # Try a restore
|
74 | c32b908e | Michael Hanselmann | state = op.__getstate__() |
75 | c32b908e | Michael Hanselmann | self.assert_(isinstance(state, dict)) |
76 | c32b908e | Michael Hanselmann | |
77 | c32b908e | Michael Hanselmann | restored = opcodes.OpCode.LoadOpCode(state) |
78 | c32b908e | Michael Hanselmann | self.assert_(isinstance(restored, cls)) |
79 | c32b908e | Michael Hanselmann | self._checkSummary(restored)
|
80 | c32b908e | Michael Hanselmann | |
81 | 65e183af | Michael Hanselmann | for name in ["x_y_z", "hello_world"]: |
82 | 65e183af | Michael Hanselmann | assert name not in cls._all_slots() |
83 | 65e183af | Michael Hanselmann | for value in [None, True, False, [], "Hello World"]: |
84 | 65e183af | Michael Hanselmann | self.assertRaises(AttributeError, setattr, op, name, value) |
85 | 65e183af | Michael Hanselmann | |
86 | c32b908e | Michael Hanselmann | def _checkSummary(self, op): |
87 | c32b908e | Michael Hanselmann | summary = op.Summary() |
88 | c32b908e | Michael Hanselmann | |
89 | c32b908e | Michael Hanselmann | if hasattr(op, "OP_DSC_FIELD"): |
90 | c32b908e | Michael Hanselmann | self.assert_(("OP_%s" % summary).startswith("%s(" % op.OP_ID)) |
91 | c32b908e | Michael Hanselmann | self.assert_(summary.endswith(")")) |
92 | c32b908e | Michael Hanselmann | else:
|
93 | c32b908e | Michael Hanselmann | self.assertEqual("OP_%s" % summary, op.OP_ID) |
94 | c32b908e | Michael Hanselmann | |
95 | 65ffb373 | Michael Hanselmann | def testSummary(self): |
96 | ff0d18e6 | Iustin Pop | class OpTest(opcodes.OpCode): |
97 | 65ffb373 | Michael Hanselmann | OP_DSC_FIELD = "data"
|
98 | 65ffb373 | Michael Hanselmann | OP_PARAMS = [ |
99 | 197b323b | Michael Hanselmann | ("data", ht.NoDefault, ht.TString, None), |
100 | 65ffb373 | Michael Hanselmann | ] |
101 | 65ffb373 | Michael Hanselmann | |
102 | ff0d18e6 | Iustin Pop | self.assertEqual(OpTest(data="").Summary(), "TEST()") |
103 | ff0d18e6 | Iustin Pop | self.assertEqual(OpTest(data="Hello World").Summary(), |
104 | 65ffb373 | Michael Hanselmann | "TEST(Hello World)")
|
105 | ff0d18e6 | Iustin Pop | self.assertEqual(OpTest(data="node1.example.com").Summary(), |
106 | 65ffb373 | Michael Hanselmann | "TEST(node1.example.com)")
|
107 | 65ffb373 | Michael Hanselmann | |
108 | 3ce9a5e7 | Michael Hanselmann | def testTinySummary(self): |
109 | 3ce9a5e7 | Michael Hanselmann | self.assertFalse(utils.FindDuplicates(opcodes._SUMMARY_PREFIX.values()))
|
110 | 3ce9a5e7 | Michael Hanselmann | self.assertTrue(compat.all(prefix.endswith("_") and supplement.endswith("_") |
111 | 3ce9a5e7 | Michael Hanselmann | for (prefix, supplement) in |
112 | 3ce9a5e7 | Michael Hanselmann | opcodes._SUMMARY_PREFIX.items())) |
113 | 3ce9a5e7 | Michael Hanselmann | |
114 | 3ce9a5e7 | Michael Hanselmann | self.assertEqual(opcodes.OpClusterPostInit().TinySummary(), "C_POST_INIT") |
115 | 3ce9a5e7 | Michael Hanselmann | self.assertEqual(opcodes.OpNodeRemove().TinySummary(), "N_REMOVE") |
116 | 3ce9a5e7 | Michael Hanselmann | self.assertEqual(opcodes.OpInstanceMigrate().TinySummary(), "I_MIGRATE") |
117 | 3ce9a5e7 | Michael Hanselmann | self.assertEqual(opcodes.OpGroupQuery().TinySummary(), "G_QUERY") |
118 | 3ce9a5e7 | Michael Hanselmann | self.assertEqual(opcodes.OpTestJqueue().TinySummary(), "TEST_JQUEUE") |
119 | 3ce9a5e7 | Michael Hanselmann | |
120 | 65ffb373 | Michael Hanselmann | def testListSummary(self): |
121 | ff0d18e6 | Iustin Pop | class OpTest(opcodes.OpCode): |
122 | 65ffb373 | Michael Hanselmann | OP_DSC_FIELD = "data"
|
123 | 65ffb373 | Michael Hanselmann | OP_PARAMS = [ |
124 | 197b323b | Michael Hanselmann | ("data", ht.NoDefault, ht.TList, None), |
125 | 65ffb373 | Michael Hanselmann | ] |
126 | 65ffb373 | Michael Hanselmann | |
127 | ff0d18e6 | Iustin Pop | self.assertEqual(OpTest(data=["a", "b", "c"]).Summary(), |
128 | 65ffb373 | Michael Hanselmann | "TEST(a,b,c)")
|
129 | ff0d18e6 | Iustin Pop | self.assertEqual(OpTest(data=["a", None, "c"]).Summary(), |
130 | 65ffb373 | Michael Hanselmann | "TEST(a,None,c)")
|
131 | ff0d18e6 | Iustin Pop | self.assertEqual(OpTest(data=[1, 2, 3, 4]).Summary(), "TEST(1,2,3,4)") |
132 | 65ffb373 | Michael Hanselmann | |
133 | dbc96028 | Michael Hanselmann | def testOpId(self): |
134 | dbc96028 | Michael Hanselmann | self.assertFalse(utils.FindDuplicates(cls.OP_ID
|
135 | dbc96028 | Michael Hanselmann | for cls in opcodes._GetOpList())) |
136 | dbc96028 | Michael Hanselmann | self.assertEqual(len(opcodes._GetOpList()), len(opcodes.OP_MAPPING)) |
137 | dbc96028 | Michael Hanselmann | |
138 | 65e183af | Michael Hanselmann | def testParams(self): |
139 | 65e183af | Michael Hanselmann | supported_by_all = set(["debug_level", "dry_run", "priority"]) |
140 | 65e183af | Michael Hanselmann | |
141 | 687c10d9 | Iustin Pop | self.assertTrue(opcodes.BaseOpCode not in opcodes.OP_MAPPING.values()) |
142 | 687c10d9 | Iustin Pop | self.assertTrue(opcodes.OpCode not in opcodes.OP_MAPPING.values()) |
143 | 65e183af | Michael Hanselmann | |
144 | 687c10d9 | Iustin Pop | for cls in opcodes.OP_MAPPING.values() + [opcodes.OpCode]: |
145 | 65e183af | Michael Hanselmann | all_slots = cls._all_slots() |
146 | 65e183af | Michael Hanselmann | |
147 | 65e183af | Michael Hanselmann | self.assertEqual(len(set(all_slots) & supported_by_all), 3, |
148 | 65e183af | Michael Hanselmann | msg=("Opcode %s doesn't support all base"
|
149 | 65e183af | Michael Hanselmann | " parameters (%r)" % (cls.OP_ID, supported_by_all)))
|
150 | 65e183af | Michael Hanselmann | |
151 | 65e183af | Michael Hanselmann | # All opcodes must have OP_PARAMS
|
152 | 65e183af | Michael Hanselmann | self.assert_(hasattr(cls, "OP_PARAMS"), |
153 | 65e183af | Michael Hanselmann | msg="%s doesn't have OP_PARAMS" % cls.OP_ID)
|
154 | 65e183af | Michael Hanselmann | |
155 | 197b323b | Michael Hanselmann | param_names = [name for (name, _, _, _) in cls.GetAllParams()] |
156 | 65e183af | Michael Hanselmann | |
157 | 65e183af | Michael Hanselmann | self.assertEqual(all_slots, param_names)
|
158 | 65e183af | Michael Hanselmann | |
159 | 65e183af | Michael Hanselmann | # Without inheritance
|
160 | 197b323b | Michael Hanselmann | self.assertEqual(cls.__slots__,
|
161 | 197b323b | Michael Hanselmann | [name for (name, _, _, _) in cls.OP_PARAMS]) |
162 | 65e183af | Michael Hanselmann | |
163 | 65e183af | Michael Hanselmann | # This won't work if parameters are converted to a dictionary
|
164 | 65e183af | Michael Hanselmann | duplicates = utils.FindDuplicates(param_names) |
165 | 65e183af | Michael Hanselmann | self.assertFalse(duplicates,
|
166 | 65e183af | Michael Hanselmann | msg=("Found duplicate parameters %r in %s" %
|
167 | 65e183af | Michael Hanselmann | (duplicates, cls.OP_ID))) |
168 | 65e183af | Michael Hanselmann | |
169 | 65e183af | Michael Hanselmann | # Check parameter definitions
|
170 | 197b323b | Michael Hanselmann | for attr_name, aval, test, doc in cls.GetAllParams(): |
171 | 65e183af | Michael Hanselmann | self.assert_(attr_name)
|
172 | 65e183af | Michael Hanselmann | self.assert_(test is None or test is ht.NoType or callable(test), |
173 | 65e183af | Michael Hanselmann | msg=("Invalid type check for %s.%s" %
|
174 | 65e183af | Michael Hanselmann | (cls.OP_ID, attr_name))) |
175 | 45d4c81c | Michael Hanselmann | self.assertTrue(doc is None or isinstance(doc, basestring)) |
176 | 65e183af | Michael Hanselmann | |
177 | 65e183af | Michael Hanselmann | if callable(aval): |
178 | 65e183af | Michael Hanselmann | self.assertFalse(callable(aval()), |
179 | 65e183af | Michael Hanselmann | msg="Default value returned by function is callable")
|
180 | 65e183af | Michael Hanselmann | |
181 | 45d4c81c | Michael Hanselmann | # If any parameter has documentation, all others need to have it as well
|
182 | 45d4c81c | Michael Hanselmann | has_doc = [doc is not None for (_, _, _, doc) in cls.OP_PARAMS] |
183 | 45d4c81c | Michael Hanselmann | self.assertTrue(not compat.any(has_doc) or compat.all(has_doc), |
184 | 45d4c81c | Michael Hanselmann | msg="%s does not document all parameters" % cls)
|
185 | 45d4c81c | Michael Hanselmann | |
186 | 1cbef6d8 | Michael Hanselmann | def testValidateNoModification(self): |
187 | ff0d18e6 | Iustin Pop | class OpTest(opcodes.OpCode): |
188 | 1cbef6d8 | Michael Hanselmann | OP_PARAMS = [ |
189 | 197b323b | Michael Hanselmann | ("nodef", ht.NoDefault, ht.TMaybeString, None), |
190 | 197b323b | Michael Hanselmann | ("wdef", "default", ht.TMaybeString, None), |
191 | 197b323b | Michael Hanselmann | ("number", 0, ht.TInt, None), |
192 | 197b323b | Michael Hanselmann | ("notype", None, ht.NoType, None), |
193 | 1cbef6d8 | Michael Hanselmann | ] |
194 | 1cbef6d8 | Michael Hanselmann | |
195 | 1cbef6d8 | Michael Hanselmann | # Missing required parameter "nodef"
|
196 | ff0d18e6 | Iustin Pop | op = OpTest() |
197 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
198 | 1cbef6d8 | Michael Hanselmann | self.assertRaises(errors.OpPrereqError, op.Validate, False) |
199 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "nodef")) |
200 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "wdef")) |
201 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "number")) |
202 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "notype")) |
203 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.__getstate__(), before, msg="Opcode was modified") |
204 | 1cbef6d8 | Michael Hanselmann | |
205 | 1cbef6d8 | Michael Hanselmann | # Required parameter "nodef" is provided
|
206 | ff0d18e6 | Iustin Pop | op = OpTest(nodef="foo")
|
207 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
208 | 1cbef6d8 | Michael Hanselmann | op.Validate(False)
|
209 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.__getstate__(), before, msg="Opcode was modified") |
210 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.nodef, "foo") |
211 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "wdef")) |
212 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "number")) |
213 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "notype")) |
214 | 1cbef6d8 | Michael Hanselmann | |
215 | 1cbef6d8 | Michael Hanselmann | # Missing required parameter "nodef"
|
216 | ff0d18e6 | Iustin Pop | op = OpTest(wdef="hello", number=999) |
217 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
218 | 1cbef6d8 | Michael Hanselmann | self.assertRaises(errors.OpPrereqError, op.Validate, False) |
219 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "nodef")) |
220 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "notype")) |
221 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.__getstate__(), before, msg="Opcode was modified") |
222 | 1cbef6d8 | Michael Hanselmann | |
223 | 1cbef6d8 | Michael Hanselmann | # Wrong type for "nodef"
|
224 | ff0d18e6 | Iustin Pop | op = OpTest(nodef=987)
|
225 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
226 | 1cbef6d8 | Michael Hanselmann | self.assertRaises(errors.OpPrereqError, op.Validate, False) |
227 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.nodef, 987) |
228 | 1cbef6d8 | Michael Hanselmann | self.assertFalse(hasattr(op, "notype")) |
229 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.__getstate__(), before, msg="Opcode was modified") |
230 | 1cbef6d8 | Michael Hanselmann | |
231 | 1cbef6d8 | Michael Hanselmann | # Testing different types for "notype"
|
232 | ff0d18e6 | Iustin Pop | op = OpTest(nodef="foo", notype=[1, 2, 3]) |
233 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
234 | 1cbef6d8 | Michael Hanselmann | op.Validate(False)
|
235 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.nodef, "foo") |
236 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.notype, [1, 2, 3]) |
237 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.__getstate__(), before, msg="Opcode was modified") |
238 | 1cbef6d8 | Michael Hanselmann | |
239 | ff0d18e6 | Iustin Pop | op = OpTest(nodef="foo", notype="Hello World") |
240 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
241 | 1cbef6d8 | Michael Hanselmann | op.Validate(False)
|
242 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.nodef, "foo") |
243 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.notype, "Hello World") |
244 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.__getstate__(), before, msg="Opcode was modified") |
245 | 1cbef6d8 | Michael Hanselmann | |
246 | 1cbef6d8 | Michael Hanselmann | def testValidateSetDefaults(self): |
247 | ff0d18e6 | Iustin Pop | class OpTest(opcodes.OpCode): |
248 | 1cbef6d8 | Michael Hanselmann | OP_PARAMS = [ |
249 | 1cbef6d8 | Michael Hanselmann | # Static default value
|
250 | 197b323b | Michael Hanselmann | ("value1", "default", ht.TMaybeString, None), |
251 | 1cbef6d8 | Michael Hanselmann | |
252 | 1cbef6d8 | Michael Hanselmann | # Default value callback
|
253 | 197b323b | Michael Hanselmann | ("value2", lambda: "result", ht.TMaybeString, None), |
254 | 1cbef6d8 | Michael Hanselmann | ] |
255 | 1cbef6d8 | Michael Hanselmann | |
256 | ff0d18e6 | Iustin Pop | op = OpTest() |
257 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
258 | 1cbef6d8 | Michael Hanselmann | op.Validate(True)
|
259 | 1cbef6d8 | Michael Hanselmann | self.assertNotEqual(op.__getstate__(), before,
|
260 | 1cbef6d8 | Michael Hanselmann | msg="Opcode was not modified")
|
261 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.value1, "default") |
262 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.value2, "result") |
263 | 1cbef6d8 | Michael Hanselmann | self.assert_(op.dry_run is None) |
264 | 1cbef6d8 | Michael Hanselmann | self.assert_(op.debug_level is None) |
265 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.priority, constants.OP_PRIO_DEFAULT)
|
266 | 1cbef6d8 | Michael Hanselmann | |
267 | ff0d18e6 | Iustin Pop | op = OpTest(value1="hello", value2="world", debug_level=123) |
268 | 1cbef6d8 | Michael Hanselmann | before = op.__getstate__() |
269 | 1cbef6d8 | Michael Hanselmann | op.Validate(True)
|
270 | 1cbef6d8 | Michael Hanselmann | self.assertNotEqual(op.__getstate__(), before,
|
271 | 1cbef6d8 | Michael Hanselmann | msg="Opcode was not modified")
|
272 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.value1, "hello") |
273 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.value2, "world") |
274 | 1cbef6d8 | Michael Hanselmann | self.assertEqual(op.debug_level, 123) |
275 | 1cbef6d8 | Michael Hanselmann | |
276 | c32b908e | Michael Hanselmann | |
277 | b247c6fc | Michael Hanselmann | class TestOpcodeDepends(unittest.TestCase): |
278 | b247c6fc | Michael Hanselmann | def test(self): |
279 | b247c6fc | Michael Hanselmann | check_relative = opcodes._BuildJobDepCheck(True)
|
280 | b247c6fc | Michael Hanselmann | check_norelative = opcodes.TNoRelativeJobDependencies |
281 | b247c6fc | Michael Hanselmann | |
282 | b247c6fc | Michael Hanselmann | for fn in [check_relative, check_norelative]: |
283 | b247c6fc | Michael Hanselmann | self.assertTrue(fn(None)) |
284 | b247c6fc | Michael Hanselmann | self.assertTrue(fn([]))
|
285 | b247c6fc | Michael Hanselmann | self.assertTrue(fn([(1, [])])) |
286 | b247c6fc | Michael Hanselmann | self.assertTrue(fn([(719833, [])])) |
287 | b247c6fc | Michael Hanselmann | self.assertTrue(fn([("24879", [])])) |
288 | b247c6fc | Michael Hanselmann | self.assertTrue(fn([(2028, [constants.JOB_STATUS_ERROR])])) |
289 | b247c6fc | Michael Hanselmann | self.assertTrue(fn([
|
290 | b247c6fc | Michael Hanselmann | (2028, [constants.JOB_STATUS_ERROR]),
|
291 | b247c6fc | Michael Hanselmann | (18750, []),
|
292 | b247c6fc | Michael Hanselmann | (5063, [constants.JOB_STATUS_SUCCESS, constants.JOB_STATUS_ERROR]),
|
293 | b247c6fc | Michael Hanselmann | ])) |
294 | b247c6fc | Michael Hanselmann | |
295 | b247c6fc | Michael Hanselmann | self.assertFalse(fn(1)) |
296 | b247c6fc | Michael Hanselmann | self.assertFalse(fn([(9, )])) |
297 | b247c6fc | Michael Hanselmann | self.assertFalse(fn([(15194, constants.JOB_STATUS_ERROR)])) |
298 | b247c6fc | Michael Hanselmann | |
299 | b247c6fc | Michael Hanselmann | for i in [ |
300 | b247c6fc | Michael Hanselmann | [(-1, [])],
|
301 | b247c6fc | Michael Hanselmann | [(-27740, [constants.JOB_STATUS_CANCELED, constants.JOB_STATUS_ERROR]),
|
302 | b247c6fc | Michael Hanselmann | (-1, [constants.JOB_STATUS_ERROR]),
|
303 | b247c6fc | Michael Hanselmann | (9921, [])],
|
304 | b247c6fc | Michael Hanselmann | ]: |
305 | b247c6fc | Michael Hanselmann | self.assertTrue(check_relative(i))
|
306 | b247c6fc | Michael Hanselmann | self.assertFalse(check_norelative(i))
|
307 | b247c6fc | Michael Hanselmann | |
308 | b247c6fc | Michael Hanselmann | |
309 | bdfd7802 | Michael Hanselmann | class TestResultChecks(unittest.TestCase): |
310 | bdfd7802 | Michael Hanselmann | def testJobIdList(self): |
311 | bdfd7802 | Michael Hanselmann | for i in [[], [(False, "error")], [(False, "")], |
312 | bdfd7802 | Michael Hanselmann | [(True, 123), (True, "999")]]: |
313 | bdfd7802 | Michael Hanselmann | self.assertTrue(opcodes.TJobIdList(i))
|
314 | bdfd7802 | Michael Hanselmann | |
315 | bdfd7802 | Michael Hanselmann | for i in ["", [("x", 1)], [[], []], [[False, "", None], [True, 123]]]: |
316 | bdfd7802 | Michael Hanselmann | self.assertFalse(opcodes.TJobIdList(i))
|
317 | bdfd7802 | Michael Hanselmann | |
318 | bdfd7802 | Michael Hanselmann | def testJobIdListOnly(self): |
319 | bdfd7802 | Michael Hanselmann | self.assertTrue(opcodes.TJobIdListOnly({
|
320 | bdfd7802 | Michael Hanselmann | constants.JOB_IDS_KEY: [], |
321 | bdfd7802 | Michael Hanselmann | })) |
322 | bdfd7802 | Michael Hanselmann | self.assertTrue(opcodes.TJobIdListOnly({
|
323 | bdfd7802 | Michael Hanselmann | constants.JOB_IDS_KEY: [(True, "9282")], |
324 | bdfd7802 | Michael Hanselmann | })) |
325 | bdfd7802 | Michael Hanselmann | |
326 | bdfd7802 | Michael Hanselmann | self.assertFalse(opcodes.TJobIdListOnly({
|
327 | bdfd7802 | Michael Hanselmann | "x": None, |
328 | bdfd7802 | Michael Hanselmann | })) |
329 | bdfd7802 | Michael Hanselmann | self.assertFalse(opcodes.TJobIdListOnly({
|
330 | bdfd7802 | Michael Hanselmann | constants.JOB_IDS_KEY: [], |
331 | bdfd7802 | Michael Hanselmann | "x": None, |
332 | bdfd7802 | Michael Hanselmann | })) |
333 | bdfd7802 | Michael Hanselmann | self.assertFalse(opcodes.TJobIdListOnly({
|
334 | bdfd7802 | Michael Hanselmann | constants.JOB_IDS_KEY: [("foo", "bar")], |
335 | bdfd7802 | Michael Hanselmann | })) |
336 | bdfd7802 | Michael Hanselmann | self.assertFalse(opcodes.TJobIdListOnly({
|
337 | bdfd7802 | Michael Hanselmann | constants.JOB_IDS_KEY: [("one", "two", "three")], |
338 | bdfd7802 | Michael Hanselmann | })) |
339 | bdfd7802 | Michael Hanselmann | |
340 | bdfd7802 | Michael Hanselmann | |
341 | c32b908e | Michael Hanselmann | if __name__ == "__main__": |
342 | c32b908e | Michael Hanselmann | testutils.GanetiTestProgram() |