From 2cb7e9054f7af59b4091b84f009d6d8de1eb2ed4 Mon Sep 17 00:00:00 2001 From: dynarro Date: Thu, 13 Nov 2014 10:14:26 +0100 Subject: [PATCH] Makes health return True or False API returns a 204 for the health check with no body content. The function should have returned True if http result was a 204, or else a False. I solved the problem by making the function return false in case of an error. Change-Id: I02771f5aa61db4ed83da952245744cb3a6163a26 Closes-bug: #1288986 --- tests/unit/queues/v1/test_client.py | 21 ++++++++++++++++----- zaqarclient/queues/v1/client.py | 7 ++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/tests/unit/queues/v1/test_client.py b/tests/unit/queues/v1/test_client.py index 2af3935c..51a3f4b8 100644 --- a/tests/unit/queues/v1/test_client.py +++ b/tests/unit/queues/v1/test_client.py @@ -20,7 +20,7 @@ import ddt from zaqarclient.queues import client from zaqarclient.queues.v1 import core from zaqarclient.tests import base -from zaqarclient.transport import response +from zaqarclient.transport import errors VERSIONS = [1, 1.1] @@ -35,10 +35,21 @@ class TestClient(base.TestBase): self.assertIsNotNone(cli.transport()) @ddt.data(*VERSIONS) - def test_health(self, version): + def test_health_ok(self, version): cli = client.Client('http://example.com', version, {}) with mock.patch.object(core, 'health', autospec=True) as core_health: - resp = response.Response(None, None) - core_health.return_value = resp - self.assertIsNotNone(cli.health()) + core_health.return_value = None + self.assertTrue(cli.health()) + + @ddt.data(*VERSIONS) + def test_health_bad(self, version): + cli = client.Client('http://example.com', + version, {}) + + def raise_error(*args, **kwargs): + raise errors.ServiceUnavailableError() + + with mock.patch.object(core, 'health', autospec=True) as core_health: + core_health.side_effect = raise_error + self.assertFalse(cli.health()) diff --git a/zaqarclient/queues/v1/client.py b/zaqarclient/queues/v1/client.py index 31cf6106..f1069955 100644 --- a/zaqarclient/queues/v1/client.py +++ b/zaqarclient/queues/v1/client.py @@ -23,6 +23,7 @@ from zaqarclient.queues.v1 import iterator from zaqarclient.queues.v1 import pool from zaqarclient.queues.v1 import queues from zaqarclient import transport +from zaqarclient.transport import errors from zaqarclient.transport import request @@ -160,4 +161,8 @@ class Client(object): def health(self): """Gets the health status of Zaqar server.""" req, trans = self._request_and_transport() - return core.health(trans, req) + try: + core.health(trans, req) + return True + except errors.ServiceUnavailableError: + return False