Handle pathless URLs

Among the zillion permutations being tested, there was no test case for
discovery URLs lacking a path component (e.g. 'http://foo:123' as
opposed to 'http://foo:123/v2').  A new discovery code path was indexing
the second-to-last part of the path component, resulting in IndexError
when the discovery data contained a pathless URL.

This change set fixes the IndexError and adds unit test coverage for
pathless URLs in discovery data.

Change-Id: I990a24ea32d0f7123566053046c80e48bce0536b
Partial-Bug: #1705770
This commit is contained in:
Eric Fried 2017-07-21 16:32:04 -05:00
parent 5715035f42
commit f6c9d042e3
2 changed files with 27 additions and 1 deletions

View File

@ -946,7 +946,7 @@ class EndpointData(object):
try:
normalize_version_number(url_parts[-2])
self._saved_project_id = url_parts.pop()
except TypeError:
except (IndexError, TypeError):
pass
catalog_discovery = versioned_discovery = None

View File

@ -854,6 +854,32 @@ class CommonIdentityTests(object):
self.assertEqual(a_token, b_token)
self.assertAccessInfoEqual(a.auth_ref, b.auth_ref)
def test_pathless_url(self):
disc = fixture.DiscoveryList(v2=False, v3=False)
url = 'http://path.less.url:1234'
disc.add_microversion(href=url, id='v2.1')
self.stub_url('GET', base_url=url, status_code=200, json=disc)
token = fixture.V2Token()
service = token.add_service('network')
service.add_endpoint(public=url, admin=url, internal=url)
self.stub_url('POST', ['tokens'], base_url=url, json=token)
v2_auth = identity.V2Password(url, username='u', password='p')
sess = session.Session(auth=v2_auth)
data = sess.get_endpoint_data(service_type='network')
# Discovery ran and returned the URL
self.assertEqual(url, data.url)
# Run with a project_id to ensure that path is covered
self.assertEqual(
3, len(list(data._get_discovery_url_choices(project_id='42'))))
class V3(CommonIdentityTests, utils.TestCase):