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"""
27 from ganeti import errors
28 from ganeti import opcodes
30 from ganeti import http
31 from ganeti import compat
32 from ganeti.rapi import baserlib
37 class TestFillOpcode(unittest.TestCase):
38 class OpTest(opcodes.OpCode):
40 ("test", None, ht.TMaybeString, None),
44 for static in [None, {}]:
45 op = baserlib.FillOpcode(self.OpTest, {}, static)
46 self.assertTrue(isinstance(op, self.OpTest))
47 self.assertFalse(hasattr(op, "test"))
50 op = baserlib.FillOpcode(self.OpTest, {}, {"test": "abc"})
51 self.assertTrue(isinstance(op, self.OpTest))
52 self.assertEqual(op.test, "abc")
54 # Overwrite static parameter
55 self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
56 self.OpTest, {"test": 123}, {"test": "abc"})
59 self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
60 self.OpTest, {"test": [1, 2, 3]}, {})
62 def testStaticType(self):
63 self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
64 self.OpTest, {}, {"test": [1, 2, 3]})
66 def testUnicode(self):
67 op = baserlib.FillOpcode(self.OpTest, {u"test": "abc"}, {})
68 self.assertTrue(isinstance(op, self.OpTest))
69 self.assertEqual(op.test, "abc")
71 op = baserlib.FillOpcode(self.OpTest, {}, {u"test": "abc"})
72 self.assertTrue(isinstance(op, self.OpTest))
73 self.assertEqual(op.test, "abc")
75 def testUnknownParameter(self):
76 self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
77 self.OpTest, {"othervalue": 123}, None)
79 def testInvalidBody(self):
80 self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
81 self.OpTest, "", None)
82 self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
83 self.OpTest, range(10), None)
85 def testRenameBothSpecified(self):
86 self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
87 self.OpTest, { "old": 123, "new": 999, }, None,
88 rename={ "old": "new", })
92 op = baserlib.FillOpcode(self.OpTest, { "data": value, }, None,
93 rename={ "data": "test", })
94 self.assertEqual(op.test, value)
96 def testRenameStatic(self):
97 self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
98 self.OpTest, { "data": 0, }, { "test": None, },
99 rename={ "data": "test", })
102 class TestOpcodeResource(unittest.TestCase):
104 def _MakeClass(method, attrs):
105 return type("Test%s" % method, (baserlib.OpcodeResource, ), attrs)
108 def _GetMethodAttributes(method):
109 attrs = ["%s_OPCODE" % method, "%s_RENAME" % method,
110 "Get%sOpInput" % method.capitalize()]
111 assert attrs == dict((opattrs[0], list(opattrs[1:]))
112 for opattrs in baserlib._OPCODE_ATTRS)[method]
116 for method in baserlib._SUPPORTED_METHODS:
118 obj = self._MakeClass(method, {})(None, None, None)
119 for attr in itertools.chain(*baserlib._OPCODE_ATTRS):
120 self.assertFalse(hasattr(obj, attr))
122 # Direct handler function
123 obj = self._MakeClass(method, {
124 method: lambda _: None,
126 self.assertFalse(compat.all(hasattr(obj, attr)
127 for i in baserlib._SUPPORTED_METHODS
128 for attr in self._GetMethodAttributes(i)))
130 # Let metaclass define handler function
131 for opcls in [None, object()]:
132 obj = self._MakeClass(method, {
133 "%s_OPCODE" % method: opcls,
135 self.assertTrue(callable(getattr(obj, method)))
136 self.assertEqual(getattr(obj, "%s_OPCODE" % method), opcls)
137 self.assertFalse(hasattr(obj, "%s_RENAME" % method))
138 self.assertFalse(compat.any(hasattr(obj, attr)
139 for i in baserlib._SUPPORTED_METHODS
141 for attr in self._GetMethodAttributes(i)))
143 def testIllegalRename(self):
144 class _TClass(baserlib.OpcodeResource):
148 self.assertRaises(AssertionError, _TClass, None, None, None)
151 class _Empty(baserlib.OpcodeResource):
154 obj = _Empty(None, None, None)
156 for attr in itertools.chain(*baserlib._OPCODE_ATTRS):
157 self.assertFalse(hasattr(obj, attr))
160 if __name__ == "__main__":
161 testutils.GanetiTestProgram()