root / test / ganeti.opcodes_unittest.py @ 6873a52a
History | View | Annotate | Download (2.3 kB)
1 | c32b908e | Michael Hanselmann | #!/usr/bin/python
|
---|---|---|---|
2 | c32b908e | Michael Hanselmann | #
|
3 | c32b908e | Michael Hanselmann | |
4 | c32b908e | Michael Hanselmann | # Copyright (C) 2010 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 | c32b908e | Michael Hanselmann | |
31 | c32b908e | Michael Hanselmann | import testutils |
32 | c32b908e | Michael Hanselmann | |
33 | c32b908e | Michael Hanselmann | |
34 | c32b908e | Michael Hanselmann | class TestOpcodes(unittest.TestCase): |
35 | c32b908e | Michael Hanselmann | def test(self): |
36 | c32b908e | Michael Hanselmann | self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, None) |
37 | c32b908e | Michael Hanselmann | self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, "") |
38 | c32b908e | Michael Hanselmann | self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, {}) |
39 | c32b908e | Michael Hanselmann | self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, {"OP_ID": ""}) |
40 | c32b908e | Michael Hanselmann | |
41 | c32b908e | Michael Hanselmann | for cls in opcodes.OP_MAPPING.values(): |
42 | c32b908e | Michael Hanselmann | self.assert_(cls.OP_ID.startswith("OP_")) |
43 | c32b908e | Michael Hanselmann | self.assertEqual(cls.OP_ID, cls.OP_ID.upper())
|
44 | c32b908e | Michael Hanselmann | |
45 | c32b908e | Michael Hanselmann | self.assertRaises(TypeError, cls, unsupported_parameter="some value") |
46 | c32b908e | Michael Hanselmann | |
47 | c32b908e | Michael Hanselmann | args = [ |
48 | c32b908e | Michael Hanselmann | # No variables
|
49 | c32b908e | Michael Hanselmann | {}, |
50 | c32b908e | Michael Hanselmann | |
51 | c32b908e | Michael Hanselmann | # Variables supported by all opcodes
|
52 | c32b908e | Michael Hanselmann | {"dry_run": False, "debug_level": 0, }, |
53 | c32b908e | Michael Hanselmann | |
54 | c32b908e | Michael Hanselmann | # All variables
|
55 | c32b908e | Michael Hanselmann | dict([(name, False) for name in cls._all_slots()]) |
56 | c32b908e | Michael Hanselmann | ] |
57 | c32b908e | Michael Hanselmann | |
58 | c32b908e | Michael Hanselmann | for i in args: |
59 | c32b908e | Michael Hanselmann | op = cls(**i) |
60 | c32b908e | Michael Hanselmann | |
61 | c32b908e | Michael Hanselmann | self.assertEqual(op.OP_ID, cls.OP_ID)
|
62 | c32b908e | Michael Hanselmann | self._checkSummary(op)
|
63 | c32b908e | Michael Hanselmann | |
64 | c32b908e | Michael Hanselmann | # Try a restore
|
65 | c32b908e | Michael Hanselmann | state = op.__getstate__() |
66 | c32b908e | Michael Hanselmann | self.assert_(isinstance(state, dict)) |
67 | c32b908e | Michael Hanselmann | |
68 | c32b908e | Michael Hanselmann | restored = opcodes.OpCode.LoadOpCode(state) |
69 | c32b908e | Michael Hanselmann | self.assert_(isinstance(restored, cls)) |
70 | c32b908e | Michael Hanselmann | self._checkSummary(restored)
|
71 | c32b908e | Michael Hanselmann | |
72 | c32b908e | Michael Hanselmann | def _checkSummary(self, op): |
73 | c32b908e | Michael Hanselmann | summary = op.Summary() |
74 | c32b908e | Michael Hanselmann | |
75 | c32b908e | Michael Hanselmann | if hasattr(op, "OP_DSC_FIELD"): |
76 | c32b908e | Michael Hanselmann | self.assert_(("OP_%s" % summary).startswith("%s(" % op.OP_ID)) |
77 | c32b908e | Michael Hanselmann | self.assert_(summary.endswith(")")) |
78 | c32b908e | Michael Hanselmann | else:
|
79 | c32b908e | Michael Hanselmann | self.assertEqual("OP_%s" % summary, op.OP_ID) |
80 | c32b908e | Michael Hanselmann | |
81 | c32b908e | Michael Hanselmann | |
82 | c32b908e | Michael Hanselmann | if __name__ == "__main__": |
83 | c32b908e | Michael Hanselmann | testutils.GanetiTestProgram() |