Add v2 support for optional project_id in version discovery urls

This is part of the work to allow Nova and Novaclient to handle endpoints both
with and without project id's. v2 version information returns a different data
structure than later versions. This patch updates the mock tests to return a
different data structure corresponding to v2 or v2.x depending on the
endpoint_type. A caller using the mock test class can set the attribute
endpoint_type to a version number, and the method get_endpoint, will return the
appropriate data structure. This is more of a workaround given the current state
of mocking Nova api calls in our unit tests.

Part of bp:service-catalog-tng

Change-Id: I61984ee592f7a5d7f1a91c9e2ff3038c0245f797
This commit is contained in:
Augustina Ragwitz 2015-10-21 22:16:10 -07:00
parent 4f16fe65c4
commit 5f5ec354be
2 changed files with 70 additions and 11 deletions

View File

@ -29,10 +29,16 @@ from novaclient.tests.unit import utils
from novaclient.v2 import client
# regex to compare callback to result of get_endpoint()
# checks version number (vX or vX.X where X is a number)
# and also checks if the id is on the end
ENDPOINT_RE = re.compile(
r"^(get_http:__nova_api:8774_)(v\d(_\d)?)_(\w{32})$")
r"^get_http:__nova_api:8774_v\d(_\d)?_\w{32}$")
ENDPOINT_TYPE_RE = re.compile(r"^(v\d(\.\d)?)$")
# accepts formats like v2 or v2.1
ENDPOINT_TYPE_RE = re.compile(r"^v\d(\.\d)?$")
# accepts formats like v2 or v2_1
CALLBACK_RE = re.compile(r"^get_http:__nova_api:8774_v\d(_\d)?$")
class FakeClient(fakes.FakeClient, client.Client):
@ -58,6 +64,8 @@ class FakeHTTPClient(base_client.HTTPClient):
self.region_name = 'region_name'
# determines which endpoint to return in get_endpoint()
# NOTE(augustina): this is a hacky workaround, ultimately
# we need to fix our whole mocking architecture (fixtures?)
if 'endpoint_type' in kwargs:
self.endpoint_type = kwargs['endpoint_type']
else:
@ -97,7 +105,7 @@ class FakeHTTPClient(base_client.HTTPClient):
# To get API version information, it is necessary to GET
# a nova endpoint directly without "v2/<tenant-id>".
callback = "get_versions"
elif callback == "get_http:__nova_api:8774_v2_1":
elif CALLBACK_RE.search(callback):
callback = "get_current_version"
elif ENDPOINT_RE.search(callback):
# compare callback to result of get_endpoint()
@ -149,14 +157,47 @@ class FakeHTTPClient(base_client.HTTPClient):
]})
def get_current_version(self):
return (200, {}, {
"version": {"status": "CURRENT",
"updated": "2013-07-23T11:33:21Z",
"links": [{"href": "http://nova-api:8774/v2.1/",
"rel": "self"}],
"min_version": "2.1",
"version": "2.3",
"id": "v2.1"}})
versions = {
# v2 doesn't contain version or min_version fields
"v2": {
"version": {
"status": "SUPPORTED",
"updated": "2011-01-21T11:33:21Z",
"links": [{
"href": "http://nova-api:8774/v2/",
"rel": "self"
}],
"id": "v2.0"
}
},
"v2.1": {
"version": {
"status": "CURRENT",
"updated": "2013-07-23T11:33:21Z",
"links": [{
"href": "http://nova-api:8774/v2.1/",
"rel": "self"
}],
"min_version": "2.1",
"version": "2.3",
"id": "v2.1"
}
}
}
# if an endpoint_type that matches a version wasn't initialized,
# default to v2.1
endpoint = 'v2.1'
if hasattr(self, 'endpoint_type'):
if ENDPOINT_TYPE_RE.search(self.endpoint_type):
if self.endpoint_type in versions:
endpoint = self.endpoint_type
else:
raise AssertionError(
"Unknown endpoint_type:%s" % self.endpoint_type)
return (200, {}, versions[endpoint])
#
# agents

View File

@ -99,3 +99,21 @@ class VersionsTest(utils.TestCase):
# check that the full request works as expected
cs_2_1.assert_called('GET', 'http://nova-api:8774/v2.1/')
@mock.patch.object(versions.VersionManager, '_is_session_client',
return_value=True)
def test_v2_get_endpoint_without_project_id(self, mock_is_session_client):
# create a fake client such that get_endpoint()
# doesn't return uuid in url
endpoint_type = 'v2'
expected_endpoint = 'http://nova-api:8774/v2/'
cs_2 = fakes.FakeClient(endpoint_type=endpoint_type)
result = cs_2.versions.get_current()
self.assertEqual(result.manager.api.client.endpoint_type,
endpoint_type, "Check v2 endpoint_type was set")
self.assertEqual(result.manager.api.client.management_url,
expected_endpoint, "Check v2 endpoint without uuid")
# check that the full request works as expected
cs_2.assert_called('GET', 'http://nova-api:8774/v2/')