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 unittesting the luxi module"""
27 from ganeti import luxi
28 from ganeti import serializer
33 class TestLuxiParsing(testutils.GanetiTestCase):
34 def testParseRequest(self):
35 msg = serializer.DumpJson({
36 luxi.KEY_METHOD: "foo",
37 luxi.KEY_ARGS: ("bar", "baz", 123),
40 self.assertEqualValues(luxi.ParseRequest(msg),
41 ("foo", ["bar", "baz", 123]))
43 self.assertRaises(luxi.ProtocolError, luxi.ParseRequest,
44 "this\"is {invalid, ]json data")
47 self.assertRaises(luxi.ProtocolError, luxi.ParseRequest,
48 serializer.DumpJson(123))
51 self.assertRaises(luxi.ProtocolError, luxi.ParseRequest,
52 serializer.DumpJson({ }))
55 self.assertRaises(luxi.ProtocolError, luxi.ParseRequest,
56 serializer.DumpJson({ luxi.KEY_METHOD: "foo", }))
59 self.assertRaises(luxi.ProtocolError, luxi.ParseRequest,
60 serializer.DumpJson({ luxi.KEY_ARGS: [], }))
62 def testParseResponse(self):
63 msg = serializer.DumpJson({
64 luxi.KEY_SUCCESS: True,
65 luxi.KEY_RESULT: None,
68 self.assertEqual(luxi.ParseResponse(msg), (True, None))
70 self.assertRaises(luxi.ProtocolError, luxi.ParseResponse,
71 "this\"is {invalid, ]json data")
74 self.assertRaises(luxi.ProtocolError, luxi.ParseResponse,
75 serializer.DumpJson(123))
78 self.assertRaises(luxi.ProtocolError, luxi.ParseResponse,
79 serializer.DumpJson({ }))
82 self.assertRaises(luxi.ProtocolError, luxi.ParseResponse,
83 serializer.DumpJson({ luxi.KEY_RESULT: True, }))
86 self.assertRaises(luxi.ProtocolError, luxi.ParseResponse,
87 serializer.DumpJson({ luxi.KEY_SUCCESS: True, }))
89 def testFormatResponse(self):
90 for success, result in [(False, "error"), (True, "abc"),
91 (True, { "a": 123, "b": None, })]:
92 msg = luxi.FormatResponse(success, result)
93 msgdata = serializer.LoadJson(msg)
94 self.assert_(luxi.KEY_SUCCESS in msgdata)
95 self.assert_(luxi.KEY_RESULT in msgdata)
96 self.assertEqualValues(msgdata,
97 { luxi.KEY_SUCCESS: success,
98 luxi.KEY_RESULT: result,
101 def testFormatRequest(self):
102 for method, args in [("a", []), ("b", [1, 2, 3])]:
103 msg = luxi.FormatRequest(method, args)
104 msgdata = serializer.LoadJson(msg)
105 self.assert_(luxi.KEY_METHOD in msgdata)
106 self.assert_(luxi.KEY_ARGS in msgdata)
107 self.assertEqualValues(msgdata,
108 { luxi.KEY_METHOD: method,
113 if __name__ == "__main__":
114 testutils.GanetiTestProgram()