root / test / ganeti.rapi.baserlib_unittest.py @ cfaeaaf7
History | View | Annotate | Download (2.7 kB)
1 |
#!/usr/bin/python
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2011 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 testing ganeti.rapi.baserlib"""
|
23 |
|
24 |
import unittest |
25 |
|
26 |
from ganeti import errors |
27 |
from ganeti import opcodes |
28 |
from ganeti import ht |
29 |
from ganeti import http |
30 |
from ganeti.rapi import baserlib |
31 |
|
32 |
import testutils |
33 |
|
34 |
|
35 |
class TestFillOpcode(unittest.TestCase): |
36 |
class _TestOp(opcodes.OpCode): |
37 |
OP_ID = "RAPI_TEST_OP"
|
38 |
OP_PARAMS = [ |
39 |
("test", None, ht.TMaybeString), |
40 |
] |
41 |
|
42 |
def test(self): |
43 |
for static in [None, {}]: |
44 |
op = baserlib.FillOpcode(self._TestOp, {}, static)
|
45 |
self.assertTrue(isinstance(op, self._TestOp)) |
46 |
self.assertFalse(hasattr(op, "test")) |
47 |
|
48 |
def testStatic(self): |
49 |
op = baserlib.FillOpcode(self._TestOp, {}, {"test": "abc"}) |
50 |
self.assertTrue(isinstance(op, self._TestOp)) |
51 |
self.assertEqual(op.test, "abc") |
52 |
|
53 |
# Overwrite static parameter
|
54 |
self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
|
55 |
self._TestOp, {"test": 123}, {"test": "abc"}) |
56 |
|
57 |
def testType(self): |
58 |
self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
|
59 |
self._TestOp, {"test": [1, 2, 3]}, {}) |
60 |
|
61 |
def testStaticType(self): |
62 |
self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
|
63 |
self._TestOp, {}, {"test": [1, 2, 3]}) |
64 |
|
65 |
def testUnicode(self): |
66 |
op = baserlib.FillOpcode(self._TestOp, {u"test": "abc"}, {}) |
67 |
self.assertTrue(isinstance(op, self._TestOp)) |
68 |
self.assertEqual(op.test, "abc") |
69 |
|
70 |
op = baserlib.FillOpcode(self._TestOp, {}, {u"test": "abc"}) |
71 |
self.assertTrue(isinstance(op, self._TestOp)) |
72 |
self.assertEqual(op.test, "abc") |
73 |
|
74 |
def testUnknownParameter(self): |
75 |
self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
|
76 |
self._TestOp, {"othervalue": 123}, None) |
77 |
|
78 |
def testInvalidBody(self): |
79 |
self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
|
80 |
self._TestOp, "", None) |
81 |
self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
|
82 |
self._TestOp, range(10), None) |
83 |
|
84 |
|
85 |
if __name__ == "__main__": |
86 |
testutils.GanetiTestProgram() |