Statistics
| Branch: | Tag: | Revision:

root / test / py / ganeti.rapi.baserlib_unittest.py @ 61f8fda4

History | View | Annotate | Download (5.4 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
import itertools
26

    
27
from ganeti import errors
28
from ganeti import opcodes
29
from ganeti import ht
30
from ganeti import http
31
from ganeti import compat
32
from ganeti.rapi import baserlib
33

    
34
import testutils
35

    
36

    
37
class TestFillOpcode(unittest.TestCase):
38
  class OpTest(opcodes.OpCode):
39
    OP_PARAMS = [
40
      ("test", None, ht.TMaybeString, None),
41
      ]
42

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

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

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

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

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

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

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

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

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

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

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

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

    
101

    
102
class TestOpcodeResource(unittest.TestCase):
103
  @staticmethod
104
  def _MakeClass(method, attrs):
105
    return type("Test%s" % method, (baserlib.OpcodeResource, ), attrs)
106

    
107
  @staticmethod
108
  def _GetMethodAttributes(method):
109
    attrs = ["%s_OPCODE" % method, "%s_RENAME" % method,
110
             "Get%sOpInput" % method.capitalize()]
111
    assert attrs == dict((opattrs[0], list(opattrs[1:]))
112
                         for opattrs in baserlib.OPCODE_ATTRS)[method]
113
    return attrs
114

    
115
  def test(self):
116
    for method in baserlib._SUPPORTED_METHODS:
117
      # Empty handler
118
      obj = self._MakeClass(method, {})(None, {}, None)
119
      for attr in itertools.chain(*baserlib.OPCODE_ATTRS):
120
        self.assertFalse(hasattr(obj, attr))
121

    
122
      # Direct handler function
123
      obj = self._MakeClass(method, {
124
        method: lambda _: None,
125
        })(None, {}, None)
126
      self.assertFalse(compat.all(hasattr(obj, attr)
127
                                  for i in baserlib._SUPPORTED_METHODS
128
                                  for attr in self._GetMethodAttributes(i)))
129

    
130
      # Let metaclass define handler function
131
      for opcls in [None, object()]:
132
        obj = self._MakeClass(method, {
133
          "%s_OPCODE" % method: opcls,
134
          })(None, {}, None)
135
        self.assertTrue(callable(getattr(obj, method)))
136
        self.assertEqual(getattr(obj, "%s_OPCODE" % method), opcls)
137
        self.assertFalse(hasattr(obj, "%s_RENAME" % method))
138
        self.assertFalse(compat.any(hasattr(obj, attr)
139
                                    for i in baserlib._SUPPORTED_METHODS
140
                                      if i != method
141
                                    for attr in self._GetMethodAttributes(i)))
142

    
143
  def testIllegalRename(self):
144
    class _TClass(baserlib.OpcodeResource):
145
      PUT_RENAME = None
146
      def PUT(self): pass
147

    
148
    self.assertRaises(AssertionError, _TClass, None, None, None)
149

    
150
  def testEmpty(self):
151
    class _Empty(baserlib.OpcodeResource):
152
      pass
153

    
154
    obj = _Empty(None, {}, None)
155

    
156
    for attr in itertools.chain(*baserlib.OPCODE_ATTRS):
157
      self.assertFalse(hasattr(obj, attr))
158

    
159

    
160
if __name__ == "__main__":
161
  testutils.GanetiTestProgram()