4 # Copyright (C) 2011 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.rapi.baserlib"""
26 from ganeti import errors
27 from ganeti import opcodes
29 from ganeti import http
30 from ganeti.rapi import baserlib
35 class TestFillOpcode(unittest.TestCase):
36 class OpTest(opcodes.OpCode):
38 ("test", None, ht.TMaybeString, None),
42 for static in [None, {}]:
43 op = baserlib.FillOpcode(self.OpTest, {}, static)
44 self.assertTrue(isinstance(op, self.OpTest))
45 self.assertFalse(hasattr(op, "test"))
48 op = baserlib.FillOpcode(self.OpTest, {}, {"test": "abc"})
49 self.assertTrue(isinstance(op, self.OpTest))
50 self.assertEqual(op.test, "abc")
52 # Overwrite static parameter
53 self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
54 self.OpTest, {"test": 123}, {"test": "abc"})
57 self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
58 self.OpTest, {"test": [1, 2, 3]}, {})
60 def testStaticType(self):
61 self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
62 self.OpTest, {}, {"test": [1, 2, 3]})
64 def testUnicode(self):
65 op = baserlib.FillOpcode(self.OpTest, {u"test": "abc"}, {})
66 self.assertTrue(isinstance(op, self.OpTest))
67 self.assertEqual(op.test, "abc")
69 op = baserlib.FillOpcode(self.OpTest, {}, {u"test": "abc"})
70 self.assertTrue(isinstance(op, self.OpTest))
71 self.assertEqual(op.test, "abc")
73 def testUnknownParameter(self):
74 self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
75 self.OpTest, {"othervalue": 123}, None)
77 def testInvalidBody(self):
78 self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
79 self.OpTest, "", None)
80 self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
81 self.OpTest, range(10), None)
84 if __name__ == "__main__":
85 testutils.GanetiTestProgram()