root / test / ganeti.luxi_unittest.py @ 37e62cb9
History | View | Annotate | Download (3.7 kB)
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 unittesting the luxi module"""
|
23 |
|
24 |
|
25 |
import unittest |
26 |
|
27 |
from ganeti import luxi |
28 |
from ganeti import serializer |
29 |
|
30 |
import testutils |
31 |
|
32 |
|
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), |
38 |
}) |
39 |
|
40 |
self.assertEqualValues(luxi.ParseRequest(msg),
|
41 |
("foo", ["bar", "baz", 123])) |
42 |
|
43 |
self.assertRaises(luxi.ProtocolError, luxi.ParseRequest,
|
44 |
"this\"is {invalid, ]json data")
|
45 |
|
46 |
# No dict
|
47 |
self.assertRaises(luxi.ProtocolError, luxi.ParseRequest,
|
48 |
serializer.DumpJson(123))
|
49 |
|
50 |
# Empty dict
|
51 |
self.assertRaises(luxi.ProtocolError, luxi.ParseRequest,
|
52 |
serializer.DumpJson({ })) |
53 |
|
54 |
# No arguments
|
55 |
self.assertRaises(luxi.ProtocolError, luxi.ParseRequest,
|
56 |
serializer.DumpJson({ luxi.KEY_METHOD: "foo", }))
|
57 |
|
58 |
# No method
|
59 |
self.assertRaises(luxi.ProtocolError, luxi.ParseRequest,
|
60 |
serializer.DumpJson({ luxi.KEY_ARGS: [], })) |
61 |
|
62 |
def testParseResponse(self): |
63 |
msg = serializer.DumpJson({ |
64 |
luxi.KEY_SUCCESS: True,
|
65 |
luxi.KEY_RESULT: None,
|
66 |
}) |
67 |
|
68 |
self.assertEqual(luxi.ParseResponse(msg), (True, None)) |
69 |
|
70 |
self.assertRaises(luxi.ProtocolError, luxi.ParseResponse,
|
71 |
"this\"is {invalid, ]json data")
|
72 |
|
73 |
# No dict
|
74 |
self.assertRaises(luxi.ProtocolError, luxi.ParseResponse,
|
75 |
serializer.DumpJson(123))
|
76 |
|
77 |
# Empty dict
|
78 |
self.assertRaises(luxi.ProtocolError, luxi.ParseResponse,
|
79 |
serializer.DumpJson({ })) |
80 |
|
81 |
# No success
|
82 |
self.assertRaises(luxi.ProtocolError, luxi.ParseResponse,
|
83 |
serializer.DumpJson({ luxi.KEY_RESULT: True, }))
|
84 |
|
85 |
# No result
|
86 |
self.assertRaises(luxi.ProtocolError, luxi.ParseResponse,
|
87 |
serializer.DumpJson({ luxi.KEY_SUCCESS: True, }))
|
88 |
|
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, |
99 |
}) |
100 |
|
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, |
109 |
luxi.KEY_ARGS: args, |
110 |
}) |
111 |
|
112 |
|
113 |
if __name__ == "__main__": |
114 |
testutils.GanetiTestProgram() |