4 # Copyright (C) 2010 Google Inc.
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.
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.
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
22 """Script for testing ganeti.backend"""
28 from ganeti import utils
29 from ganeti import opcodes
34 class TestOpcodes(unittest.TestCase):
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": ""})
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())
45 self.assertRaises(TypeError, cls, unsupported_parameter="some value")
51 # Variables supported by all opcodes
52 {"dry_run": False, "debug_level": 0, },
55 dict([(name, False) for name in cls._all_slots()])
61 self.assertEqual(op.OP_ID, cls.OP_ID)
62 self._checkSummary(op)
65 state = op.__getstate__()
66 self.assert_(isinstance(state, dict))
68 restored = opcodes.OpCode.LoadOpCode(state)
69 self.assert_(isinstance(restored, cls))
70 self._checkSummary(restored)
72 def _checkSummary(self, op):
73 summary = op.Summary()
75 if hasattr(op, "OP_DSC_FIELD"):
76 self.assert_(("OP_%s" % summary).startswith("%s(" % op.OP_ID))
77 self.assert_(summary.endswith(")"))
79 self.assertEqual("OP_%s" % summary, op.OP_ID)
82 if __name__ == "__main__":
83 testutils.GanetiTestProgram()