Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.rapi.baserlib_unittest.py @ 7578ab0a

History | View | Annotate | Download (3.3 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 OpTest(opcodes.OpCode):
37
    OP_PARAMS = [
38
      ("test", None, ht.TMaybeString, None),
39
      ]
40

    
41
  def test(self):
42
    for static in [None, {}]:
43
      op = baserlib.FillOpcode(self.OpTest, {}, static)
44
      self.assertTrue(isinstance(op, self.OpTest))
45
      self.assertFalse(hasattr(op, "test"))
46

    
47
  def testStatic(self):
48
    op = baserlib.FillOpcode(self.OpTest, {}, {"test": "abc"})
49
    self.assertTrue(isinstance(op, self.OpTest))
50
    self.assertEqual(op.test, "abc")
51

    
52
    # Overwrite static parameter
53
    self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
54
                      self.OpTest, {"test": 123}, {"test": "abc"})
55

    
56
  def testType(self):
57
    self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
58
                      self.OpTest, {"test": [1, 2, 3]}, {})
59

    
60
  def testStaticType(self):
61
    self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
62
                      self.OpTest, {}, {"test": [1, 2, 3]})
63

    
64
  def testUnicode(self):
65
    op = baserlib.FillOpcode(self.OpTest, {u"test": "abc"}, {})
66
    self.assertTrue(isinstance(op, self.OpTest))
67
    self.assertEqual(op.test, "abc")
68

    
69
    op = baserlib.FillOpcode(self.OpTest, {}, {u"test": "abc"})
70
    self.assertTrue(isinstance(op, self.OpTest))
71
    self.assertEqual(op.test, "abc")
72

    
73
  def testUnknownParameter(self):
74
    self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
75
                      self.OpTest, {"othervalue": 123}, None)
76

    
77
  def testInvalidBody(self):
78
    self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
79
                      self.OpTest, "", None)
80
    self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
81
                      self.OpTest, range(10), None)
82

    
83
  def testRenameBothSpecified(self):
84
    self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
85
                      self.OpTest, { "old": 123, "new": 999, }, None,
86
                      rename={ "old": "new", })
87

    
88
  def testRename(self):
89
    value = "Hello World"
90
    op = baserlib.FillOpcode(self.OpTest, { "data": value, }, None,
91
                             rename={ "data": "test", })
92
    self.assertEqual(op.test, value)
93

    
94
  def testRenameStatic(self):
95
    self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
96
                      self.OpTest, { "data": 0, }, { "test": None, },
97
                      rename={ "data": "test", })
98

    
99

    
100
if __name__ == "__main__":
101
  testutils.GanetiTestProgram()