Revision cfaeaaf7

b/Makefile.am
461 461
	test/ganeti.opcodes_unittest.py \
462 462
	test/ganeti.qlang_unittest.py \
463 463
	test/ganeti.query_unittest.py \
464
	test/ganeti.rapi.baserlib_unittest.py \
464 465
	test/ganeti.rapi.client_unittest.py \
465 466
	test/ganeti.rapi.resources_unittest.py \
466 467
	test/ganeti.rapi.rlib2_unittest.py \
b/lib/rapi/baserlib.py
170 170
  return result
171 171

  
172 172

  
173
def FillOpcode(opcls, body, static):
174
  """Fills an opcode with body parameters.
175

  
176
  Parameter types are checked.
177

  
178
  @type opcls: L{opcodes.OpCode}
179
  @param opcls: Opcode class
180
  @type body: dict
181
  @param body: Body parameters as received from client
182
  @type static: dict
183
  @param static: Static parameters which can't be modified by client
184
  @return: Opcode object
185

  
186
  """
187
  CheckType(body, dict, "Body contents")
188

  
189
  if static:
190
    overwritten = set(body.keys()) & set(static.keys())
191
    if overwritten:
192
      raise http.HttpBadRequest("Can't overwrite static parameters %r" %
193
                                overwritten)
194

  
195
  # Combine parameters
196
  params = body.copy()
197

  
198
  if static:
199
    params.update(static)
200

  
201
  # Convert keys to strings (simplejson decodes them as unicode)
202
  params = dict((str(key), value) for (key, value) in params.items())
203

  
204
  try:
205
    op = opcls(**params) # pylint: disable-msg=W0142
206
    op.Validate(False)
207
  except (errors.OpPrereqError, TypeError), err:
208
    raise http.HttpBadRequest("Invalid body parameters: %s" % err)
209

  
210
  return op
211

  
212

  
173 213
def SubmitJob(op, cl=None):
174 214
  """Generic wrapper for submit job, for better http compatibility.
175 215

  
b/test/ganeti.rapi.baserlib_unittest.py
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()

Also available in: Unified diff