Skip to content

Commit

Permalink
Adds api sample testing for versions
Browse files Browse the repository at this point in the history
This patch tests api samples for the versions resource in json and
xml. It makes a small modification to the integrated api client to
support making requests above /v2/tenant_id.

This also fixes an uncovered bug in the link handling in the versions
resource.

Fixes bug 1041426

Change-Id: Ie898ef3822fa250def2e0fa407a0968275262260
  • Loading branch information
vishvananda committed Aug 28, 2012
1 parent 31f6244 commit 9e96a92
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 8 deletions.
14 changes: 11 additions & 3 deletions nova/api/openstack/compute/views/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@
import copy
import os

from nova.api.openstack import common
from nova import flags


FLAGS = flags.FLAGS


def get_view_builder(req):
base_url = req.application_url
return ViewBuilder(base_url)


class ViewBuilder(object):
class ViewBuilder(common.ViewBuilder):

def __init__(self, base_url):
"""
Expand Down Expand Up @@ -86,9 +92,11 @@ def _build_links(self, version_data):

def generate_href(self, path=None):
"""Create an url that refers to a specific version_number."""
prefix = self._update_link_prefix(self.base_url,
FLAGS.osapi_compute_link_prefix)
version_number = 'v2'
if path:
path = path.strip('/')
return os.path.join(self.base_url, version_number, path)
return os.path.join(prefix, version_number, path)
else:
return os.path.join(self.base_url, version_number) + '/'
return os.path.join(prefix, version_number) + '/'
6 changes: 5 additions & 1 deletion nova/tests/integrated/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,15 @@ def _authenticate(self):
self.auth_result = auth_headers
return self.auth_result

def api_request(self, relative_uri, check_response_status=None, **kwargs):
def api_request(self, relative_uri, check_response_status=None,
strip_version=False, **kwargs):
auth_result = self._authenticate()

# NOTE(justinsb): httplib 'helpfully' converts headers to lower case
base_uri = auth_result['x-server-management-url']
if strip_version:
# NOTE(vish): cut out version number and tenant_id
base_uri = '/'.join(base_uri.split('/', 3)[:-1])

full_uri = '%s/%s' % (base_uri, relative_uri)

Expand Down
15 changes: 15 additions & 0 deletions nova/tests/integrated/api_samples/versions-get-resp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"versions": [
{
"id": "v2.0",
"links": [
{
"href": "http://openstack.example.com/v2/",
"rel": "self"
}
],
"status": "CURRENT",
"updated": "2011-01-21T11:33:21Z"
}
]
}
15 changes: 15 additions & 0 deletions nova/tests/integrated/api_samples/versions-get-resp.json.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"versions": [
{
"id": "v2.0",
"links": [
{
"href": "%(host)s/v2/",
"rel": "self"
}
],
"status": "CURRENT",
"updated": "2011-01-21T11:33:21Z"
}
]
}
6 changes: 6 additions & 0 deletions nova/tests/integrated/api_samples/versions-get-resp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version='1.0' encoding='UTF-8'?>
<versions xmlns:atom="http://www.w3.org/2005/Atom" xmlns="http://docs.openstack.org/common/api/v1.0">
<version status="CURRENT" updated="2011-01-21T11:33:21Z" id="v2.0">
<atom:link href="http://openstack.example.com/v2/" rel="self"/>
</version>
</versions>
6 changes: 6 additions & 0 deletions nova/tests/integrated/api_samples/versions-get-resp.xml.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version='1.0' encoding='UTF-8'?>
<versions xmlns:atom="http://www.w3.org/2005/Atom" xmlns="http://docs.openstack.org/common/api/v1.0">
<version status="CURRENT" updated="2011-01-21T11:33:21Z" id="v2.0">
<atom:link href="http://openstack.example.com/v2/" rel="self"/>
</version>
</versions>
19 changes: 15 additions & 4 deletions nova/tests/integrated/test_api_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ def _get_regexes(self):
'compute_host': self.compute.host,
}

def _get_response(self, url, method, body=None):
def _get_response(self, url, method, body=None, strip_version=False):
headers = {}
headers['Content-Type'] = 'application/' + self.ctype
headers['Accept'] = 'application/' + self.ctype
return self.api.api_request(url, body=body, method=method,
headers=headers)
headers=headers, strip_version=strip_version)

def _do_get(self, url):
return self._get_response(url, 'GET')
def _do_get(self, url, strip_version=False):
return self._get_response(url, 'GET', strip_version=strip_version)

def _do_post(self, url, name, subs):
body = self._read_template(name) % subs
Expand All @@ -143,6 +143,17 @@ def _do_post(self, url, name, subs):
return self._get_response(url, 'POST', body)


class VersionsSampleJsonTest(ApiSampleTestBase):
def test_servers_get(self):
response = self._do_get('', strip_version=True)
subs = self._get_regexes()
return self._verify_response('versions-get-resp', subs, response)


class VersionsSampleXmlTest(VersionsSampleJsonTest):
ctype = 'xml'


class ServersSampleJsonTest(ApiSampleTestBase):
def test_servers_post(self):
subs = {
Expand Down

0 comments on commit 9e96a92

Please sign in to comment.