From 8cc1a8a0244f06ac7010ed9f8b406f709d903ad2 Mon Sep 17 00:00:00 2001 From: ZhiQiang Fan Date: Tue, 15 Apr 2014 20:21:53 +0800 Subject: [PATCH] Avoid dead loop when token is string format The token needs to be a callable object in http.Client, so we assign a lambda expression to it when it is a string, but current implementation will cause dead loop since it is x = lambda: x. This patch fixes it and improves the corresponding test code. Change-Id: Id41edd705b46196404e5cb3483a290074bde12d0 Closes-Bug: #1301877 --- ceilometerclient/client.py | 4 ++-- ceilometerclient/tests/test_client.py | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/ceilometerclient/client.py b/ceilometerclient/client.py index f8c8ac0b..7bfe1a61 100644 --- a/ceilometerclient/client.py +++ b/ceilometerclient/client.py @@ -61,8 +61,8 @@ def get_client(api_version, **kwargs): * os_tenant_{name|id}: name or ID of tenant """ token = kwargs.get('os_auth_token') - if token: - token = (token if six.callable(token) else lambda: token) + if token and not six.callable(token): + token = lambda: kwargs.get('os_auth_token') if token and kwargs.get('ceilometer_url'): endpoint = kwargs.get('ceilometer_url') diff --git a/ceilometerclient/tests/test_client.py b/ceilometerclient/tests/test_client.py index fe9d5b0e..de46c9a0 100644 --- a/ceilometerclient/tests/test_client.py +++ b/ceilometerclient/tests/test_client.py @@ -41,15 +41,17 @@ class ClientTest(utils.BaseTestCase): c2 = self.create_client(api_version=2) self.assertIsInstance(c2, v2client.Client) - def test_client_auth_lambda(self): - FAKE_ENV['os_auth_token'] = lambda: FAKE_ENV['os_auth_token'] - self.assertIsInstance(FAKE_ENV['os_auth_token'], - types.FunctionType) - c2 = self.create_client() - self.assertIsInstance(c2, v2client.Client) + def test_client_auth_token_lambda(self): + FAKE_ENV['os_auth_token'] = lambda: '1234' + self._test_client_auth_token() - def test_client_auth_non_lambda(self): + def test_client_auth_token_non_lambda(self): FAKE_ENV['os_auth_token'] = "1234" - self.assertIsInstance(FAKE_ENV['os_auth_token'], str) + self._test_client_auth_token() + + def _test_client_auth_token(self): c2 = self.create_client() self.assertIsInstance(c2, v2client.Client) + self.assertIsInstance(c2.http_client.auth_token, + types.FunctionType) + self.assertEqual('1234', c2.http_client.auth_token())