Piotr Kliczewski has uploaded a new change for review.
Change subject: tests: JsonRpcServer tests suite ......................................................................
tests: JsonRpcServer tests suite
Change-Id: I1090764c7289544abe331a13ec765ceed2a53afe Signed-off-by: pkliczewski piotr.kliczewski@gmail.com --- M lib/yajsonrpc/__init__.py M tests/Makefile.am A tests/jsonrpcServerTests.py 3 files changed, 133 insertions(+), 1 deletion(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/81/43581/1
diff --git a/lib/yajsonrpc/__init__.py b/lib/yajsonrpc/__init__.py index 68af242..a307c7d 100644 --- a/lib/yajsonrpc/__init__.py +++ b/lib/yajsonrpc/__init__.py @@ -293,7 +293,12 @@ self._responses.append(response)
def requestDone(self, response): - del self._requests[response.id] + try: + del self._requests[response.id] + except KeyError: + # ignore when request had no id + pass + self.addResponse(response) self.sendReply()
diff --git a/tests/Makefile.am b/tests/Makefile.am index 15dbd7b..9a7f81f 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -57,6 +57,7 @@ iproute2Tests.py \ ipwrapperTests.py \ iscsiTests.py \ + jsonrpcServerTests.py \ libvirtconnectionTests.py \ lvmTests.py \ main.py \ diff --git a/tests/jsonrpcServerTests.py b/tests/jsonrpcServerTests.py new file mode 100644 index 0000000..6c508c2 --- /dev/null +++ b/tests/jsonrpcServerTests.py @@ -0,0 +1,126 @@ +# +# Copyright 2015 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# +# Refer to the README and COPYING files for full details of the license +# +import threading +from contextlib import contextmanager +from Queue import Queue + +from testlib import VdsmTestCase as TestCaseBase +from yajsonrpc import JsonRpcServer, JsonRpcResponse, JsonRpcRequest + + +ID = 'e8a936a6-d886-4cfa-97b9-2d54209053ff' + + +class FakeBridge(object): + + def get_text(self): + return 'my text' + + def register_server_address(self, server_address): + self.server_address = server_address + + def unregister_server_address(self): + self.server_address = None + + +class TestClient(object): + + def __init__(self): + self._queue = Queue() + + def send(self, data): + self._queue.put_nowait(data) + + def pop_message(self): + return self._queue.get(True, 3) + + @property + def has_outgoing_messages(self): + return (self._queue.qsize() > 0) + + +class TestConnection(object): + + def get_local_address(self): + return 'localhost' + + +@contextmanager +def create_server(bridge): + server = JsonRpcServer(bridge, 3600) + t = threading.Thread(target=server.serve_requests, + name='JsonRpcServer') + t.setDaemon(True) + t.start() + + try: + yield server + finally: + server.stop() + + +class JsonrpcServerTest(TestCaseBase): + + def test_method_call(self): + request = JsonRpcRequest(method='get_text', params=(), reqId=ID) + client = TestClient() + + with create_server(FakeBridge()) as server: + server.queueRequest((client, TestConnection(), request.encode())) + + response = client.pop_message() + self.assertIsNot(response, None) + res = JsonRpcResponse.decode(response) + self.assertEquals(res.result, 'my text') + + def test_not_existing_method(self): + request = JsonRpcRequest(method='abcd', params=(), reqId=ID) + client = TestClient() + + with create_server(FakeBridge()) as server: + server.queueRequest((client, TestConnection(), request.encode())) + + response = client.pop_message() + self.assertIsNot(response, None) + res = JsonRpcResponse.decode(response) + self.assertEquals(res.error['code'], -32601) + + def test_wrong_param(self): + request = JsonRpcRequest(method='get_text', params=('param',), + reqId=ID) + client = TestClient() + + with create_server(FakeBridge()) as server: + server.queueRequest((client, TestConnection(), request.encode())) + + response = client.pop_message() + self.assertIsNot(response, None) + res = JsonRpcResponse.decode(response) + self.assertEquals(res.error['code'], -32603) + + def test_no_request_id(self): + request = JsonRpcRequest(method='get_text', params=()) + client = TestClient() + + with create_server(FakeBridge()) as server: + server.queueRequest((client, TestConnection(), request.encode())) + + response = client.pop_message() + self.assertIsNot(response, None)
automation@ovirt.org has posted comments on this change.
Change subject: tests: JsonRpcServer tests suite ......................................................................
Patch Set 1:
* Update tracker::IGNORE, no Bug-Url found * Check Bug-Url::WARN, no bug url found, make sure header matches 'Bug-Url: ' and is a valid url. * Check merged to previous::IGNORE, Not in stable branch (['ovirt-3.5', 'ovirt-3.4', 'ovirt-3.3'])
Piotr Kliczewski has posted comments on this change.
Change subject: tests: JsonRpcServer tests suite ......................................................................
Patch Set 1: Verified+1
Verified by running local build.
automation@ovirt.org has posted comments on this change.
Change subject: tests: JsonRpcServer tests suite ......................................................................
Patch Set 2:
* Update tracker::IGNORE, no Bug-Url found * Check Bug-Url::WARN, no bug url found, make sure header matches 'Bug-Url: ' and is a valid url. * Check merged to previous::IGNORE, Not in stable branch (['ovirt-3.5', 'ovirt-3.4', 'ovirt-3.3'])
Piotr Kliczewski has posted comments on this change.
Change subject: tests: JsonRpcServer tests suite ......................................................................
Patch Set 2: Verified+1
Verified by running local build.
automation@ovirt.org has posted comments on this change.
Change subject: tests: JsonRpcServer tests suite ......................................................................
Patch Set 3:
* Update tracker::IGNORE, no Bug-Url found * Check Bug-Url::WARN, no bug url found, make sure header matches 'Bug-Url: ' and is a valid url. * Check merged to previous::IGNORE, Not in stable branch (['ovirt-3.5', 'ovirt-3.4', 'ovirt-3.3'])
automation@ovirt.org has posted comments on this change.
Change subject: tests: JsonRpcServer tests suite ......................................................................
Patch Set 4:
* Update tracker::IGNORE, no Bug-Url found * Check Bug-Url::WARN, no bug url found, make sure header matches 'Bug-Url: ' and is a valid url. * Check merged to previous::IGNORE, Not in stable branch (['ovirt-3.5', 'ovirt-3.4', 'ovirt-3.3'])
Piotr Kliczewski has posted comments on this change.
Change subject: tests: JsonRpcServer tests suite ......................................................................
Patch Set 4: Verified+1
Rebased only.
Piotr Kliczewski has posted comments on this change.
Change subject: tests: JsonRpcServer tests suite ......................................................................
Patch Set 4:
We still want it in
vdsm-patches@lists.fedorahosted.org