Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.rapi.baserlib_unittest.py @ b166ef84

History | View | Annotate | Download (3.3 kB)

1 cfaeaaf7 Michael Hanselmann
#!/usr/bin/python
2 cfaeaaf7 Michael Hanselmann
#
3 cfaeaaf7 Michael Hanselmann
4 cfaeaaf7 Michael Hanselmann
# Copyright (C) 2011 Google Inc.
5 cfaeaaf7 Michael Hanselmann
#
6 cfaeaaf7 Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 cfaeaaf7 Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 cfaeaaf7 Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 cfaeaaf7 Michael Hanselmann
# (at your option) any later version.
10 cfaeaaf7 Michael Hanselmann
#
11 cfaeaaf7 Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 cfaeaaf7 Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 cfaeaaf7 Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 cfaeaaf7 Michael Hanselmann
# General Public License for more details.
15 cfaeaaf7 Michael Hanselmann
#
16 cfaeaaf7 Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 cfaeaaf7 Michael Hanselmann
# along with this program; if not, write to the Free Software
18 cfaeaaf7 Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 cfaeaaf7 Michael Hanselmann
# 02110-1301, USA.
20 cfaeaaf7 Michael Hanselmann
21 cfaeaaf7 Michael Hanselmann
22 cfaeaaf7 Michael Hanselmann
"""Script for testing ganeti.rapi.baserlib"""
23 cfaeaaf7 Michael Hanselmann
24 cfaeaaf7 Michael Hanselmann
import unittest
25 cfaeaaf7 Michael Hanselmann
26 cfaeaaf7 Michael Hanselmann
from ganeti import errors
27 cfaeaaf7 Michael Hanselmann
from ganeti import opcodes
28 cfaeaaf7 Michael Hanselmann
from ganeti import ht
29 cfaeaaf7 Michael Hanselmann
from ganeti import http
30 cfaeaaf7 Michael Hanselmann
from ganeti.rapi import baserlib
31 cfaeaaf7 Michael Hanselmann
32 cfaeaaf7 Michael Hanselmann
import testutils
33 cfaeaaf7 Michael Hanselmann
34 cfaeaaf7 Michael Hanselmann
35 cfaeaaf7 Michael Hanselmann
class TestFillOpcode(unittest.TestCase):
36 ff0d18e6 Iustin Pop
  class OpTest(opcodes.OpCode):
37 cfaeaaf7 Michael Hanselmann
    OP_PARAMS = [
38 197b323b Michael Hanselmann
      ("test", None, ht.TMaybeString, None),
39 cfaeaaf7 Michael Hanselmann
      ]
40 cfaeaaf7 Michael Hanselmann
41 cfaeaaf7 Michael Hanselmann
  def test(self):
42 cfaeaaf7 Michael Hanselmann
    for static in [None, {}]:
43 ff0d18e6 Iustin Pop
      op = baserlib.FillOpcode(self.OpTest, {}, static)
44 ff0d18e6 Iustin Pop
      self.assertTrue(isinstance(op, self.OpTest))
45 cfaeaaf7 Michael Hanselmann
      self.assertFalse(hasattr(op, "test"))
46 cfaeaaf7 Michael Hanselmann
47 cfaeaaf7 Michael Hanselmann
  def testStatic(self):
48 ff0d18e6 Iustin Pop
    op = baserlib.FillOpcode(self.OpTest, {}, {"test": "abc"})
49 ff0d18e6 Iustin Pop
    self.assertTrue(isinstance(op, self.OpTest))
50 cfaeaaf7 Michael Hanselmann
    self.assertEqual(op.test, "abc")
51 cfaeaaf7 Michael Hanselmann
52 cfaeaaf7 Michael Hanselmann
    # Overwrite static parameter
53 cfaeaaf7 Michael Hanselmann
    self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
54 ff0d18e6 Iustin Pop
                      self.OpTest, {"test": 123}, {"test": "abc"})
55 cfaeaaf7 Michael Hanselmann
56 cfaeaaf7 Michael Hanselmann
  def testType(self):
57 cfaeaaf7 Michael Hanselmann
    self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
58 ff0d18e6 Iustin Pop
                      self.OpTest, {"test": [1, 2, 3]}, {})
59 cfaeaaf7 Michael Hanselmann
60 cfaeaaf7 Michael Hanselmann
  def testStaticType(self):
61 cfaeaaf7 Michael Hanselmann
    self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
62 ff0d18e6 Iustin Pop
                      self.OpTest, {}, {"test": [1, 2, 3]})
63 cfaeaaf7 Michael Hanselmann
64 cfaeaaf7 Michael Hanselmann
  def testUnicode(self):
65 ff0d18e6 Iustin Pop
    op = baserlib.FillOpcode(self.OpTest, {u"test": "abc"}, {})
66 ff0d18e6 Iustin Pop
    self.assertTrue(isinstance(op, self.OpTest))
67 cfaeaaf7 Michael Hanselmann
    self.assertEqual(op.test, "abc")
68 cfaeaaf7 Michael Hanselmann
69 ff0d18e6 Iustin Pop
    op = baserlib.FillOpcode(self.OpTest, {}, {u"test": "abc"})
70 ff0d18e6 Iustin Pop
    self.assertTrue(isinstance(op, self.OpTest))
71 cfaeaaf7 Michael Hanselmann
    self.assertEqual(op.test, "abc")
72 cfaeaaf7 Michael Hanselmann
73 cfaeaaf7 Michael Hanselmann
  def testUnknownParameter(self):
74 cfaeaaf7 Michael Hanselmann
    self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
75 ff0d18e6 Iustin Pop
                      self.OpTest, {"othervalue": 123}, None)
76 cfaeaaf7 Michael Hanselmann
77 cfaeaaf7 Michael Hanselmann
  def testInvalidBody(self):
78 cfaeaaf7 Michael Hanselmann
    self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
79 ff0d18e6 Iustin Pop
                      self.OpTest, "", None)
80 cfaeaaf7 Michael Hanselmann
    self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
81 ff0d18e6 Iustin Pop
                      self.OpTest, range(10), None)
82 cfaeaaf7 Michael Hanselmann
83 b166ef84 Michael Hanselmann
  def testRenameBothSpecified(self):
84 b166ef84 Michael Hanselmann
    self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
85 b166ef84 Michael Hanselmann
                      self.OpTest, { "old": 123, "new": 999, }, None,
86 b166ef84 Michael Hanselmann
                      rename={ "old": "new", })
87 b166ef84 Michael Hanselmann
88 b166ef84 Michael Hanselmann
  def testRename(self):
89 b166ef84 Michael Hanselmann
    value = "Hello World"
90 b166ef84 Michael Hanselmann
    op = baserlib.FillOpcode(self.OpTest, { "data": value, }, None,
91 b166ef84 Michael Hanselmann
                             rename={ "data": "test", })
92 b166ef84 Michael Hanselmann
    self.assertEqual(op.test, value)
93 b166ef84 Michael Hanselmann
94 b166ef84 Michael Hanselmann
  def testRenameStatic(self):
95 b166ef84 Michael Hanselmann
    self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
96 b166ef84 Michael Hanselmann
                      self.OpTest, { "data": 0, }, { "test": None, },
97 b166ef84 Michael Hanselmann
                      rename={ "data": "test", })
98 b166ef84 Michael Hanselmann
99 cfaeaaf7 Michael Hanselmann
100 cfaeaaf7 Michael Hanselmann
if __name__ == "__main__":
101 cfaeaaf7 Michael Hanselmann
  testutils.GanetiTestProgram()