Commit 24c09d5e authored by Petr Pudlak's avatar Petr Pudlak
Browse files

Rename CallLuxiMethod to CallRPCMethod



Also update error messages and testing code to refer to RPC instead of
LUXI.
Signed-off-by: default avatarPetr Pudlak <pudlak@google.com>
Reviewed-by: default avatarHelga Velroyen <helgav@google.com>
parent 912b2278
......@@ -47,21 +47,21 @@ def ParseRequest(msg):
try:
request = serializer.LoadJson(msg)
except ValueError, err:
raise ProtocolError("Invalid LUXI request (parsing error): %s" % err)
raise ProtocolError("Invalid RPC request (parsing error): %s" % err)
logging.debug("LUXI request: %s", request)
logging.debug("RPC request: %s", request)
if not isinstance(request, dict):
logging.error("LUXI request not a dict: %r", msg)
raise ProtocolError("Invalid LUXI request (not a dict)")
logging.error("RPC request not a dict: %r", msg)
raise ProtocolError("Invalid RPC request (not a dict)")
method = request.get(KEY_METHOD, None) # pylint: disable=E1103
args = request.get(KEY_ARGS, None) # pylint: disable=E1103
version = request.get(KEY_VERSION, None) # pylint: disable=E1103
if method is None or args is None:
logging.error("LUXI request missing method or arguments: %r", msg)
raise ProtocolError(("Invalid LUXI request (no method or arguments"
logging.error("RPC request missing method or arguments: %r", msg)
raise ProtocolError(("Invalid RPC request (no method or arguments"
" in request): %r") % msg)
return (method, args, version)
......@@ -101,7 +101,7 @@ def FormatResponse(success, result, version=None):
if version is not None:
response[KEY_VERSION] = version
logging.debug("LUXI response: %s", response)
logging.debug("RPC response: %s", response)
return serializer.DumpJson(response)
......@@ -123,8 +123,8 @@ def FormatRequest(method, args, version=None):
return serializer.DumpJson(request)
def CallLuxiMethod(transport_cb, method, args, version=None):
"""Send a LUXI request via a transport and return the response.
def CallRPCMethod(transport_cb, method, args, version=None):
"""Send a RPC request via a transport and return the response.
"""
assert callable(transport_cb)
......@@ -138,7 +138,7 @@ def CallLuxiMethod(transport_cb, method, args, version=None):
# Verify version if there was one in the response
if resp_version is not None and resp_version != version:
raise LuxiError("LUXI version mismatch, client %s, response %s" %
raise LuxiError("RPC version mismatch, client %s, response %s" %
(version, resp_version))
if success:
......@@ -227,5 +227,5 @@ class AbstractClient(object):
if not isinstance(args, (list, tuple)):
raise errors.ProgrammerError("Invalid parameter passed to CallMethod:"
" expected list, got %s" % type(args))
return CallLuxiMethod(self._SendMethodCall, method, args,
version=constants.LUXI_VERSION)
return CallRPCMethod(self._SendMethodCall, method, args,
version=constants.LUXI_VERSION)
......@@ -26,225 +26,225 @@ import unittest
from ganeti import constants
from ganeti import errors
import ganeti.rpc.client as luxi
from ganeti import serializer
from ganeti.rpc import client
import testutils
class TestLuxiParsing(testutils.GanetiTestCase):
class TextRPCParsing(testutils.GanetiTestCase):
def testParseRequest(self):
msg = serializer.DumpJson({
luxi.KEY_METHOD: "foo",
luxi.KEY_ARGS: ("bar", "baz", 123),
client.KEY_METHOD: "foo",
client.KEY_ARGS: ("bar", "baz", 123),
})
self.assertEqualValues(luxi.ParseRequest(msg),
self.assertEqualValues(client.ParseRequest(msg),
("foo", ["bar", "baz", 123], None))
self.assertRaises(luxi.ProtocolError, luxi.ParseRequest,
self.assertRaises(client.ProtocolError, client.ParseRequest,
"this\"is {invalid, ]json data")
# No dict
self.assertRaises(luxi.ProtocolError, luxi.ParseRequest,
self.assertRaises(client.ProtocolError, client.ParseRequest,
serializer.DumpJson(123))
# Empty dict
self.assertRaises(luxi.ProtocolError, luxi.ParseRequest,
self.assertRaises(client.ProtocolError, client.ParseRequest,
serializer.DumpJson({ }))
# No arguments
self.assertRaises(luxi.ProtocolError, luxi.ParseRequest,
serializer.DumpJson({ luxi.KEY_METHOD: "foo", }))
self.assertRaises(client.ProtocolError, client.ParseRequest,
serializer.DumpJson({ client.KEY_METHOD: "foo", }))
# No method
self.assertRaises(luxi.ProtocolError, luxi.ParseRequest,
serializer.DumpJson({ luxi.KEY_ARGS: [], }))
self.assertRaises(client.ProtocolError, client.ParseRequest,
serializer.DumpJson({ client.KEY_ARGS: [], }))
# No method or arguments
self.assertRaises(luxi.ProtocolError, luxi.ParseRequest,
serializer.DumpJson({ luxi.KEY_VERSION: 1, }))
self.assertRaises(client.ProtocolError, client.ParseRequest,
serializer.DumpJson({ client.KEY_VERSION: 1, }))
def testParseRequestWithVersion(self):
msg = serializer.DumpJson({
luxi.KEY_METHOD: "version",
luxi.KEY_ARGS: (["some"], "args", 0, "here"),
luxi.KEY_VERSION: 20100101,
client.KEY_METHOD: "version",
client.KEY_ARGS: (["some"], "args", 0, "here"),
client.KEY_VERSION: 20100101,
})
self.assertEqualValues(luxi.ParseRequest(msg),
self.assertEqualValues(client.ParseRequest(msg),
("version", [["some"], "args", 0, "here"], 20100101))
def testParseResponse(self):
msg = serializer.DumpJson({
luxi.KEY_SUCCESS: True,
luxi.KEY_RESULT: None,
client.KEY_SUCCESS: True,
client.KEY_RESULT: None,
})
self.assertEqual(luxi.ParseResponse(msg), (True, None, None))
self.assertEqual(client.ParseResponse(msg), (True, None, None))
self.assertRaises(luxi.ProtocolError, luxi.ParseResponse,
self.assertRaises(client.ProtocolError, client.ParseResponse,
"this\"is {invalid, ]json data")
# No dict
self.assertRaises(luxi.ProtocolError, luxi.ParseResponse,
self.assertRaises(client.ProtocolError, client.ParseResponse,
serializer.DumpJson(123))
# Empty dict
self.assertRaises(luxi.ProtocolError, luxi.ParseResponse,
self.assertRaises(client.ProtocolError, client.ParseResponse,
serializer.DumpJson({ }))
# No success
self.assertRaises(luxi.ProtocolError, luxi.ParseResponse,
serializer.DumpJson({ luxi.KEY_RESULT: True, }))
self.assertRaises(client.ProtocolError, client.ParseResponse,
serializer.DumpJson({ client.KEY_RESULT: True, }))
# No result
self.assertRaises(luxi.ProtocolError, luxi.ParseResponse,
serializer.DumpJson({ luxi.KEY_SUCCESS: True, }))
self.assertRaises(client.ProtocolError, client.ParseResponse,
serializer.DumpJson({ client.KEY_SUCCESS: True, }))
# No result or success
self.assertRaises(luxi.ProtocolError, luxi.ParseResponse,
serializer.DumpJson({ luxi.KEY_VERSION: 123, }))
self.assertRaises(client.ProtocolError, client.ParseResponse,
serializer.DumpJson({ client.KEY_VERSION: 123, }))
def testParseResponseWithVersion(self):
msg = serializer.DumpJson({
luxi.KEY_SUCCESS: True,
luxi.KEY_RESULT: "Hello World",
luxi.KEY_VERSION: 19991234,
client.KEY_SUCCESS: True,
client.KEY_RESULT: "Hello World",
client.KEY_VERSION: 19991234,
})
self.assertEqual(luxi.ParseResponse(msg), (True, "Hello World", 19991234))
self.assertEqual(client.ParseResponse(msg), (True, "Hello World", 19991234))
def testFormatResponse(self):
for success, result in [(False, "error"), (True, "abc"),
(True, { "a": 123, "b": None, })]:
msg = luxi.FormatResponse(success, result)
msg = client.FormatResponse(success, result)
msgdata = serializer.LoadJson(msg)
self.assert_(luxi.KEY_SUCCESS in msgdata)
self.assert_(luxi.KEY_RESULT in msgdata)
self.assert_(luxi.KEY_VERSION not in msgdata)
self.assert_(client.KEY_SUCCESS in msgdata)
self.assert_(client.KEY_RESULT in msgdata)
self.assert_(client.KEY_VERSION not in msgdata)
self.assertEqualValues(msgdata,
{ luxi.KEY_SUCCESS: success,
luxi.KEY_RESULT: result,
{ client.KEY_SUCCESS: success,
client.KEY_RESULT: result,
})
def testFormatResponseWithVersion(self):
for success, result, version in [(False, "error", 123), (True, "abc", 999),
(True, { "a": 123, "b": None, }, 2010)]:
msg = luxi.FormatResponse(success, result, version=version)
msg = client.FormatResponse(success, result, version=version)
msgdata = serializer.LoadJson(msg)
self.assert_(luxi.KEY_SUCCESS in msgdata)
self.assert_(luxi.KEY_RESULT in msgdata)
self.assert_(luxi.KEY_VERSION in msgdata)
self.assert_(client.KEY_SUCCESS in msgdata)
self.assert_(client.KEY_RESULT in msgdata)
self.assert_(client.KEY_VERSION in msgdata)
self.assertEqualValues(msgdata,
{ luxi.KEY_SUCCESS: success,
luxi.KEY_RESULT: result,
luxi.KEY_VERSION: version,
{ client.KEY_SUCCESS: success,
client.KEY_RESULT: result,
client.KEY_VERSION: version,
})
def testFormatRequest(self):
for method, args in [("a", []), ("b", [1, 2, 3])]:
msg = luxi.FormatRequest(method, args)
msg = client.FormatRequest(method, args)
msgdata = serializer.LoadJson(msg)
self.assert_(luxi.KEY_METHOD in msgdata)
self.assert_(luxi.KEY_ARGS in msgdata)
self.assert_(luxi.KEY_VERSION not in msgdata)
self.assert_(client.KEY_METHOD in msgdata)
self.assert_(client.KEY_ARGS in msgdata)
self.assert_(client.KEY_VERSION not in msgdata)
self.assertEqualValues(msgdata,
{ luxi.KEY_METHOD: method,
luxi.KEY_ARGS: args,
{ client.KEY_METHOD: method,
client.KEY_ARGS: args,
})
def testFormatRequestWithVersion(self):
for method, args, version in [("fn1", [], 123), ("fn2", [1, 2, 3], 999)]:
msg = luxi.FormatRequest(method, args, version=version)
msg = client.FormatRequest(method, args, version=version)
msgdata = serializer.LoadJson(msg)
self.assert_(luxi.KEY_METHOD in msgdata)
self.assert_(luxi.KEY_ARGS in msgdata)
self.assert_(luxi.KEY_VERSION in msgdata)
self.assert_(client.KEY_METHOD in msgdata)
self.assert_(client.KEY_ARGS in msgdata)
self.assert_(client.KEY_VERSION in msgdata)
self.assertEqualValues(msgdata,
{ luxi.KEY_METHOD: method,
luxi.KEY_ARGS: args,
luxi.KEY_VERSION: version,
{ client.KEY_METHOD: method,
client.KEY_ARGS: args,
client.KEY_VERSION: version,
})
class TestCallLuxiMethod(unittest.TestCase):
class TestCallRPCMethod(unittest.TestCase):
MY_LUXI_VERSION = 1234
assert constants.LUXI_VERSION != MY_LUXI_VERSION
def testSuccessNoVersion(self):
def _Cb(msg):
(method, args, version) = luxi.ParseRequest(msg)
(method, args, version) = client.ParseRequest(msg)
self.assertEqual(method, "fn1")
self.assertEqual(args, "Hello World")
return luxi.FormatResponse(True, "x")
return client.FormatResponse(True, "x")
result = luxi.CallLuxiMethod(_Cb, "fn1", "Hello World")
result = client.CallRPCMethod(_Cb, "fn1", "Hello World")
def testServerVersionOnly(self):
def _Cb(msg):
(method, args, version) = luxi.ParseRequest(msg)
(method, args, version) = client.ParseRequest(msg)
self.assertEqual(method, "fn1")
self.assertEqual(args, "Hello World")
return luxi.FormatResponse(True, "x", version=self.MY_LUXI_VERSION)
return client.FormatResponse(True, "x", version=self.MY_LUXI_VERSION)
self.assertRaises(errors.LuxiError, luxi.CallLuxiMethod,
self.assertRaises(errors.LuxiError, client.CallRPCMethod,
_Cb, "fn1", "Hello World")
def testWithVersion(self):
def _Cb(msg):
(method, args, version) = luxi.ParseRequest(msg)
(method, args, version) = client.ParseRequest(msg)
self.assertEqual(method, "fn99")
self.assertEqual(args, "xyz")
return luxi.FormatResponse(True, "y", version=self.MY_LUXI_VERSION)
return client.FormatResponse(True, "y", version=self.MY_LUXI_VERSION)
self.assertEqual("y", luxi.CallLuxiMethod(_Cb, "fn99", "xyz",
self.assertEqual("y", client.CallRPCMethod(_Cb, "fn99", "xyz",
version=self.MY_LUXI_VERSION))
def testVersionMismatch(self):
def _Cb(msg):
(method, args, version) = luxi.ParseRequest(msg)
(method, args, version) = client.ParseRequest(msg)
self.assertEqual(method, "fn5")
self.assertEqual(args, "xyz")
return luxi.FormatResponse(True, "F", version=self.MY_LUXI_VERSION * 2)
return client.FormatResponse(True, "F", version=self.MY_LUXI_VERSION * 2)
self.assertRaises(errors.LuxiError, luxi.CallLuxiMethod,
self.assertRaises(errors.LuxiError, client.CallRPCMethod,
_Cb, "fn5", "xyz", version=self.MY_LUXI_VERSION)
def testError(self):
def _Cb(msg):
(method, args, version) = luxi.ParseRequest(msg)
(method, args, version) = client.ParseRequest(msg)
self.assertEqual(method, "fnErr")
self.assertEqual(args, [])
err = errors.OpPrereqError("Test")
return luxi.FormatResponse(False, errors.EncodeException(err))
return client.FormatResponse(False, errors.EncodeException(err))
self.assertRaises(errors.OpPrereqError, luxi.CallLuxiMethod,
self.assertRaises(errors.OpPrereqError, client.CallRPCMethod,
_Cb, "fnErr", [])
def testErrorWithVersionMismatch(self):
def _Cb(msg):
(method, args, version) = luxi.ParseRequest(msg)
(method, args, version) = client.ParseRequest(msg)
self.assertEqual(method, "fnErr")
self.assertEqual(args, [])
err = errors.OpPrereqError("TestVer")
return luxi.FormatResponse(False, errors.EncodeException(err),
return client.FormatResponse(False, errors.EncodeException(err),
version=self.MY_LUXI_VERSION * 2)
self.assertRaises(errors.LuxiError, luxi.CallLuxiMethod,
self.assertRaises(errors.LuxiError, client.CallRPCMethod,
_Cb, "fnErr", [],
version=self.MY_LUXI_VERSION)
def testErrorWithVersion(self):
def _Cb(msg):
(method, args, version) = luxi.ParseRequest(msg)
(method, args, version) = client.ParseRequest(msg)
self.assertEqual(method, "fn9")
self.assertEqual(args, [])
err = errors.OpPrereqError("TestVer")
return luxi.FormatResponse(False, errors.EncodeException(err),
return client.FormatResponse(False, errors.EncodeException(err),
version=self.MY_LUXI_VERSION)
self.assertRaises(errors.OpPrereqError, luxi.CallLuxiMethod,
self.assertRaises(errors.OpPrereqError, client.CallRPCMethod,
_Cb, "fn9", [],
version=self.MY_LUXI_VERSION)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment