From f0cc3be2ac2cb6474ba117da323f6369c1e6c791 Mon Sep 17 00:00:00 2001
From: Alistair Coles <alistair.coles@hp.com>
Date: Tue, 2 Jun 2015 21:02:42 +0100
Subject: [PATCH] Make default get_auth timeout be None

Setting timeout to a default of False in get_auth()
results in a requests timeout of 0.0 in keystoneclient,
which causes a connection failure.

This bug will cause func tests to not run when using
keystoneauth.

Added unit tests to verify correct default timeout is set
in get_auth().

Drive-by: remove what seems like a stale TODO comment

Change-Id: I17c781ce160a682b1768d315422ade0cdd2df198
---
 swiftclient/client.py          |  2 +-
 tests/unit/test_swiftclient.py | 55 ++++++++++++++++++++++++++++++++--
 2 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/swiftclient/client.py b/swiftclient/client.py
index c4429f72..a51c93aa 100644
--- a/swiftclient/client.py
+++ b/swiftclient/client.py
@@ -394,7 +394,7 @@ def get_auth(auth_url, user, key, **kwargs):
     storage_url, token = None, None
     cacert = kwargs.get('cacert', None)
     insecure = kwargs.get('insecure', False)
-    timeout = kwargs.get('timeout', False)
+    timeout = kwargs.get('timeout', None)
     if auth_version in AUTH_VERSIONS_V1:
         storage_url, token = get_auth_1_0(auth_url,
                                           user,
diff --git a/tests/unit/test_swiftclient.py b/tests/unit/test_swiftclient.py
index 413da815..b90d99c4 100644
--- a/tests/unit/test_swiftclient.py
+++ b/tests/unit/test_swiftclient.py
@@ -200,8 +200,6 @@ class TestHttpHelpers(MockHttpTest):
         ua = req_headers.get('user-agent', 'XXX-MISSING-XXX')
         self.assertEqual(ua, 'a-new-default')
 
-# TODO: following tests are placeholders, need more tests, better coverage
-
 
 class TestGetAuth(MockHttpTest):
 
@@ -239,6 +237,59 @@ class TestGetAuth(MockHttpTest):
         # the full plumbing into the requests's 'verify' option
         self.assertIn('invalid_certificate', str(e))
 
+    def test_auth_v1_timeout(self):
+        # this test has some overlap with
+        # TestConnection.test_timeout_passed_down but is required to check that
+        # get_auth does the right thing when it is not passed a timeout arg
+        orig_http_connection = c.http_connection
+        timeouts = []
+
+        def fake_request_handler(*a, **kw):
+            if 'timeout' in kw:
+                timeouts.append(kw['timeout'])
+            else:
+                timeouts.append(None)
+            return MockHttpResponse(
+                status=200,
+                headers={
+                    'x-auth-token': 'a_token',
+                    'x-storage-url': 'http://files.example.com/v1/AUTH_user'})
+
+        def fake_connection(*a, **kw):
+            url, conn = orig_http_connection(*a, **kw)
+            conn._request = fake_request_handler
+            return url, conn
+
+        with mock.patch('swiftclient.client.http_connection', fake_connection):
+            c.get_auth('http://www.test.com', 'asdf', 'asdf',
+                       auth_version="1.0", timeout=42.0)
+            c.get_auth('http://www.test.com', 'asdf', 'asdf',
+                       auth_version="1.0", timeout=None)
+            c.get_auth('http://www.test.com', 'asdf', 'asdf',
+                       auth_version="1.0")
+
+        self.assertEqual(timeouts, [42.0, None, None])
+
+    def test_auth_v2_timeout(self):
+        # this test has some overlap with
+        # TestConnection.test_timeout_passed_down but is required to check that
+        # get_auth does the right thing when it is not passed a timeout arg
+        fake_ks = FakeKeystone(endpoint='http://some_url', token='secret')
+        with mock.patch('swiftclient.client._import_keystone_client',
+                        _make_fake_import_keystone_client(fake_ks)):
+            c.get_auth('http://www.test.com', 'asdf', 'asdf',
+                       os_options=dict(tenant_name='tenant'),
+                       auth_version="2.0", timeout=42.0)
+            c.get_auth('http://www.test.com', 'asdf', 'asdf',
+                       os_options=dict(tenant_name='tenant'),
+                       auth_version="2.0", timeout=None)
+            c.get_auth('http://www.test.com', 'asdf', 'asdf',
+                       os_options=dict(tenant_name='tenant'),
+                       auth_version="2.0")
+        self.assertEqual(3, len(fake_ks.calls))
+        timeouts = [call['timeout'] for call in fake_ks.calls]
+        self.assertEqual([42.0, None, None], timeouts)
+
     def test_auth_v2_with_tenant_name(self):
         os_options = {'tenant_name': 'asdf'}
         req_args = {'auth_version': '2.0'}