Adam Litke has uploaded a new change for review. Change subject: tests: Cleanup apiTests exception handling ......................................................................
tests: Cleanup apiTests exception handling As Dan reported in http://gerrit.ovirt.org/#/c/9442/ the behavior of SocketServer differs between versions of python which causes the exception raised by sendMessage() to change. Rather than key the expected exception based on the Python version, clean up the flow so it will behave the same across Python versions. Change-Id: I818e5e7b8dd0a1abad94deb65e32c1d76225b839 Signed-off-by: Adam Litke <[email protected]> --- M tests/apiTests.py 1 file changed, 22 insertions(+), 7 deletions(-) git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/99/9499/1 diff --git a/tests/apiTests.py b/tests/apiTests.py index ff0a785..3e4be6a 100644 --- a/tests/apiTests.py +++ b/tests/apiTests.py @@ -176,6 +176,10 @@ _fakeret = {} +class ProtocolError(Exception): + pass + + class JsonRawTest(APITest): _Size = struct.Struct("!Q") @@ -191,12 +195,23 @@ try: sock.connect((ip, port)) sock.sendall(msg) + except socket.error, e: + raise ProtocolError("Unable to send request: %s", e) + try: data = sock.recv(JsonRawTest._Size.size) - msgLen = JsonRawTest._Size.unpack(data)[0] + except socket.error, e: + raise ProtocolError("Unable to read response length: %s", e) + if not data: + raise ProtocolError("No data received") + msgLen = JsonRawTest._Size.unpack(data)[0] + try: data = sock.recv(msgLen) - return json.loads(data) - finally: - sock.close() + except socket.error, e: + raise ProtocolError("Unable to read response body: %s", e) + if len(data) != msgLen: + raise ProtocolError("Response body length mismatch") + return json.loads(data) + sock.close() def testPing(self): self.clearAPI() @@ -227,14 +242,14 @@ self.assertEquals(4, reply['error']['code']) def testMissingSize(self): - self.assertRaises(struct.error, self.sendMessage, + self.assertRaises(ProtocolError, self.sendMessage, "malformed message") def testClientNotJson(self): msg = "malformed message" msize = JsonRawTest._Size.pack(len(msg)) msg = msize + msg - self.assertRaises(struct.error, self.sendMessage, msg) + self.assertRaises(ProtocolError, self.sendMessage, msg) def testSynchronization(self): def doPing(msg): @@ -245,7 +260,7 @@ msg = self.buildMessage({'id': 1, 'method': 'Host.ping'}) # Send Truncated message - self.assertRaises(struct.error, doPing, msg[:-1]) + self.assertRaises(ProtocolError, doPing, msg[:-1]) # Test that the server recovers doPing(msg) -- To view, visit http://gerrit.ovirt.org/9499 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I818e5e7b8dd0a1abad94deb65e32c1d76225b839 Gerrit-PatchSet: 1 Gerrit-Project: vdsm Gerrit-Branch: master Gerrit-Owner: Adam Litke <[email protected]> _______________________________________________ vdsm-patches mailing list [email protected] https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches
