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