From 12932b0caceec99ebb1f717ff323c237c24d895e Mon Sep 17 00:00:00 2001 From: Julie Pichon Date: Mon, 12 Sep 2016 11:17:06 +0100 Subject: [PATCH] Display error message when socket is closed Give the user some additional information when Zaqar is down. Also, move the websocket import to the third-party import list. Change-Id: I8d4caf2ba08bceba1f014555ccd6ae3ca1a4a6c0 Closes-Bug: #1619712 --- tripleoclient/plugin.py | 11 +++++++++-- tripleoclient/tests/test_plugin.py | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/tripleoclient/plugin.py b/tripleoclient/plugin.py index 035d0b609..8bb981767 100644 --- a/tripleoclient/plugin.py +++ b/tripleoclient/plugin.py @@ -17,11 +17,12 @@ import json import logging +import socket import uuid -import websocket from openstackclient.common import utils from swiftclient import client as swift_client +import websocket LOG = logging.getLogger(__name__) @@ -79,7 +80,13 @@ class WebsocketClient(object): self._websocket_client_id = str(uuid.uuid4()) LOG.debug('Instantiating messaging websocket client: %s', endpoint) - self._ws = websocket.create_connection(endpoint) + try: + self._ws = websocket.create_connection(endpoint) + except socket.error: + LOG.error("Could not establish a connection to the Zaqar " + "websocket. The command was sent but the answer " + "could not be read.") + raise self.send('authenticate', extra_headers={'X-Auth-Token': token}) diff --git a/tripleoclient/tests/test_plugin.py b/tripleoclient/tests/test_plugin.py index e5d29d9ba..56da5f063 100644 --- a/tripleoclient/tests/test_plugin.py +++ b/tripleoclient/tests/test_plugin.py @@ -14,6 +14,7 @@ import json import mock +import socket from tripleoclient import plugin from tripleoclient.tests import base @@ -83,3 +84,21 @@ class TestPlugin(base.TestCase): "message": "Result for IDID", "execution": {"id": "IDID"}, }) + + @mock.patch("websocket.create_connection") + def test_websocket_creation_error(self, ws_create_connection): + + ws_create_connection.side_effect = socket.error + + clientmgr = mock.MagicMock() + clientmgr.get_endpoint_for_service_type.return_value = fakes.WS_URL + clientmgr.auth.get_token.return_value = "TOKEN" + clientmgr.auth_ref.project_id = "ID" + + client = plugin.make_client(clientmgr) + + msg = ("Could not establish a connection to the Zaqar websocket. The " + "command was sent but the answer could not be read.") + with mock.patch('tripleoclient.plugin.LOG') as mock_log: + self.assertRaises(socket.error, client.messaging_websocket) + mock_log.error.assert_called_once_with(msg)