Fix retrieve_auth_url in case Keystone URL does not contain port.

Sahara calls retrieve_auth_url() to retrieve auth_url from input
uri. However, this function does not work correctly if input uri
does not contain port.

Change-Id: Ic7a65cd36a237d68151936404567c3bd7d068117
Closes-Bug: #1473434
This commit is contained in:
Khanh-Toan Tran 2015-07-10 15:17:19 +00:00
parent a1c95bca60
commit 710ee9b8ad
4 changed files with 71 additions and 5 deletions

View File

@ -44,10 +44,16 @@ def retrieve_auth_url():
else:
url = 'v2.0'
return '{scheme}://{hostname}:{port}/{url}/'.format(scheme=info.scheme,
hostname=info.hostname,
port=info.port,
url=url)
if info.port:
returned_url = '{scheme}://{hostname}:{port}/{url}/'
return returned_url.format(scheme=info.scheme,
hostname=info.hostname,
port=info.port,
url=url)
else:
return '{scheme}://{hostname}/{url}/'.format(scheme=info.scheme,
hostname=info.hostname,
url=url)
def retrieve_preauth_url():

View File

@ -40,3 +40,19 @@ class SwiftUtilsTest(testbase.SaharaTestCase):
_assert("https://127.0.0.1:8080/v2.0/")
_assert("https://127.0.0.1:8080/v42/")
_assert("https://127.0.0.1:8080/foo")
@mock.patch('sahara.utils.openstack.base.url_for')
def test_retrieve_auth_url_without_port(self, url_for_mock):
correct = "https://127.0.0.1/v2.0/"
def _assert(uri):
url_for_mock.return_value = uri
self.assertEqual(correct, utils.retrieve_auth_url())
_assert("%s/" % correct)
_assert("https://127.0.0.1")
_assert("https://127.0.0.1/")
_assert("https://127.0.0.1/v2.0")
_assert("https://127.0.0.1/v2.0/")
_assert("https://127.0.0.1/v42/")
_assert("https://127.0.0.1/foo")

View File

@ -74,6 +74,24 @@ class AuthUrlTest(testbase.SaharaTestCase):
_assert("https://127.0.0.1:8080/v42")
_assert("https://127.0.0.1:8080/v42/")
def test_retrieve_auth_url_api_v3_without_port(self):
self.override_config('use_identity_api_v3', True)
correct = "https://127.0.0.1/v3/"
def _assert(uri):
self.setup_context(auth_uri=uri)
self.assertEqual(correct, base.retrieve_auth_url())
_assert("%s/" % correct)
_assert("https://127.0.0.1")
_assert("https://127.0.0.1/")
_assert("https://127.0.0.1/v2.0")
_assert("https://127.0.0.1/v2.0/")
_assert("https://127.0.0.1/v3")
_assert("https://127.0.0.1/v3/")
_assert("https://127.0.0.1/v42")
_assert("https://127.0.0.1/v42/")
def test_retrieve_auth_url_api_v20(self):
self.override_config('use_identity_api_v3', False)
correct = "https://127.0.0.1:8080/v2.0/"
@ -92,6 +110,24 @@ class AuthUrlTest(testbase.SaharaTestCase):
_assert("https://127.0.0.1:8080/v42")
_assert("https://127.0.0.1:8080/v42/")
def test_retrieve_auth_url_api_v20_without_port(self):
self.override_config('use_identity_api_v3', False)
correct = "https://127.0.0.1/v2.0/"
def _assert(uri):
self.setup_context(auth_uri=uri)
self.assertEqual(correct, base.retrieve_auth_url())
_assert("%s/" % correct)
_assert("https://127.0.0.1")
_assert("https://127.0.0.1/")
_assert("https://127.0.0.1/v2.0")
_assert("https://127.0.0.1/v2.0/")
_assert("https://127.0.0.1/v3")
_assert("https://127.0.0.1/v3/")
_assert("https://127.0.0.1/v42")
_assert("https://127.0.0.1/v42/")
class ExecuteWithRetryTest(testbase.SaharaTestCase):

View File

@ -106,7 +106,15 @@ def retrieve_auth_url():
info = urlparse.urlparse(context.current().auth_uri)
version = 'v3' if CONF.use_identity_api_v3 else 'v2.0'
return "%s://%s:%s/%s/" % (info.scheme, info.hostname, info.port, version)
if info.port:
return "%s://%s:%s/%s/" % (info.scheme,
info.hostname,
info.port,
version)
else:
return "%s://%s/%s/" % (info.scheme,
info.hostname,
version)
def execute_with_retries(method, *args, **kwargs):