Add glance api v2 testing.
This commit adds initial support for the glance v2 api. It adds some basic image tests and a new v2 client. The client supports create, get metadata, store, get image, and delete. Change-Id: Ic8c79356d4dcadc35bb3a7aa0deac2336e370827
This commit is contained in:
parent
c1825107a9
commit
a62347f445
|
@ -53,6 +53,7 @@ from tempest.services.identity.json.identity_client import TokenClientJSON
|
|||
from tempest.services.identity.xml.identity_client import IdentityClientXML
|
||||
from tempest.services.identity.xml.identity_client import TokenClientXML
|
||||
from tempest.services.image.v1.json.image_client import ImageClientJSON
|
||||
from tempest.services.image.v2.json.image_client import ImageClientV2JSON
|
||||
from tempest.services.network.json.network_client import NetworkClient
|
||||
from tempest.services.object_storage.account_client import AccountClient
|
||||
from tempest.services.object_storage.account_client import \
|
||||
|
@ -215,6 +216,7 @@ class Manager(object):
|
|||
self.hosts_client = HostsClientJSON(*client_args)
|
||||
self.account_client = AccountClient(*client_args)
|
||||
self.image_client = ImageClientJSON(*client_args)
|
||||
self.image_client_v2 = ImageClientV2JSON(*client_args)
|
||||
self.container_client = ContainerClient(*client_args)
|
||||
self.object_client = ObjectClient(*client_args)
|
||||
self.ec2api_client = botoclients.APIClientEC2(*client_args)
|
||||
|
|
|
@ -0,0 +1,123 @@
|
|||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
#
|
||||
# Copyright 2013 IBM
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import json
|
||||
import urllib
|
||||
|
||||
import jsonschema
|
||||
|
||||
from tempest.common import glance_http
|
||||
from tempest.common import rest_client
|
||||
from tempest import exceptions
|
||||
|
||||
|
||||
class ImageClientV2JSON(rest_client.RestClient):
|
||||
|
||||
def __init__(self, config, username, password, auth_url, tenant_name=None):
|
||||
super(ImageClientV2JSON, self).__init__(config, username, password,
|
||||
auth_url, tenant_name)
|
||||
self.service = self.config.images.catalog_type
|
||||
self.http = self._get_http()
|
||||
|
||||
def _get_http(self):
|
||||
token, endpoint = self.keystone_auth(self.user, self.password,
|
||||
self.auth_url, self.service,
|
||||
self.tenant_name)
|
||||
dscv = self.config.identity.disable_ssl_certificate_validation
|
||||
return glance_http.HTTPClient(endpoint=endpoint, token=token,
|
||||
insecure=dscv)
|
||||
|
||||
def get_images_schema(self):
|
||||
url = 'v2/schemas/images'
|
||||
resp, body = self.get(url)
|
||||
body = json.loads(body)
|
||||
return resp, body
|
||||
|
||||
def get_image_schema(self):
|
||||
url = 'v2/schemas/image'
|
||||
resp, body = self.get(url)
|
||||
body = json.loads(body)
|
||||
return resp, body
|
||||
|
||||
def _validate_schema(self, body, type='image'):
|
||||
if type == 'image':
|
||||
resp, schema = self.get_image_schema()
|
||||
elif type == 'images':
|
||||
resp, schema = self.get_images_schema()
|
||||
else:
|
||||
raise ValueError("%s is not a valid schema type" % type)
|
||||
|
||||
jsonschema.validate(body, schema)
|
||||
|
||||
def create_image(self, name, container_format, disk_format, is_public=True,
|
||||
properties=None):
|
||||
params = {
|
||||
"name": name,
|
||||
"container_format": container_format,
|
||||
"disk_format": disk_format,
|
||||
}
|
||||
if is_public:
|
||||
params["visibility"] = "public"
|
||||
else:
|
||||
params["visibility"] = "private"
|
||||
|
||||
data = json.dumps(params)
|
||||
self._validate_schema(data)
|
||||
|
||||
resp, body = self.post('v2/images', data, self.headers)
|
||||
body = json.loads(body)
|
||||
return resp, body
|
||||
|
||||
def delete_image(self, image_id):
|
||||
url = 'v2/images/%s' % image_id
|
||||
self.delete(url)
|
||||
|
||||
def image_list(self, params=None):
|
||||
url = 'v2/images'
|
||||
|
||||
if params:
|
||||
url += '?%s' % urllib.urlencode(params)
|
||||
|
||||
resp, body = self.get(url)
|
||||
body = json.loads(body)
|
||||
self._validate_schema(body, type='images')
|
||||
return resp, body['images']
|
||||
|
||||
def get_image_metadata(self, image_id):
|
||||
url = 'v2/images/%s' % image_id
|
||||
resp, body = self.get(url)
|
||||
body = json.loads(body)
|
||||
return resp, body
|
||||
|
||||
def is_resource_deleted(self, id):
|
||||
try:
|
||||
self.get_image_metadata(id)
|
||||
except exceptions.NotFound:
|
||||
return True
|
||||
return False
|
||||
|
||||
def store_image(self, image_id, data):
|
||||
url = 'v2/images/%s/file' % image_id
|
||||
headers = {'Content-Type': 'application/octet-stream'}
|
||||
resp, body = self.http.raw_request('PUT', url, headers=headers,
|
||||
body=data)
|
||||
return resp, body
|
||||
|
||||
def get_image_file(self, image_id):
|
||||
url = 'v2/images/%s/file' % image_id
|
||||
resp, body = self.get(url)
|
||||
return resp, body
|
|
@ -0,0 +1,126 @@
|
|||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
# Copyright 2013 OpenStack, LLC
|
||||
# All Rights Reserved.
|
||||
# Copyright 2013 IBM Corp
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import cStringIO as StringIO
|
||||
import random
|
||||
|
||||
from tempest import clients
|
||||
from tempest import exceptions
|
||||
import tempest.test
|
||||
from tempest.test import attr
|
||||
|
||||
|
||||
class CreateRegisterImagesTest(tempest.test.BaseTestCase):
|
||||
|
||||
"""
|
||||
Here we test the registration and creation of images
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.os = clients.Manager()
|
||||
cls.client = cls.os.image_client_v2
|
||||
cls.created_images = []
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
for image_id in cls.created_images:
|
||||
cls.client.delete(image_id)
|
||||
|
||||
@attr(type='negative')
|
||||
def test_register_with_invalid_container_format(self):
|
||||
# Negative tests for invalid data supplied to POST /images
|
||||
self.assertRaises(exceptions.BadRequest, self.client.create_image,
|
||||
'test', 'wrong', 'vhd')
|
||||
|
||||
@attr(type='negative')
|
||||
def test_register_with_invalid_disk_format(self):
|
||||
self.assertRaises(exceptions.BadRequest, self.client.create_image,
|
||||
'test', 'bare', 'wrong')
|
||||
|
||||
@attr(type='image')
|
||||
def test_register_then_upload(self):
|
||||
# Register, then upload an image
|
||||
resp, body = self.client.create_image('New Name', 'bare', 'raw',
|
||||
is_public=True)
|
||||
self.assertTrue('id' in body)
|
||||
image_id = body.get('id')
|
||||
self.created_images.append(image_id)
|
||||
self.assertTrue('name' in body)
|
||||
self.assertEqual('New Name', body.get('name'))
|
||||
self.assertTrue('visibility' in body)
|
||||
self.assertTrue(body.get('visibility') == 'public')
|
||||
self.assertTrue('status' in body)
|
||||
self.assertEqual('queued', body.get('status'))
|
||||
|
||||
# Now try uploading an image file
|
||||
image_file = StringIO.StringIO(('*' * 1024))
|
||||
resp, body = self.client.store_image(image_id, image_file)
|
||||
self.assertEqual(resp.status, 204)
|
||||
resp, body = self.client.get_image_metadata(image_id)
|
||||
self.assertTrue('size' in body)
|
||||
self.assertEqual(1024, body.get('size'))
|
||||
|
||||
|
||||
class ListImagesTest(tempest.test.BaseTestCase):
|
||||
|
||||
"""
|
||||
Here we test the listing of image information
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.os = clients.Manager()
|
||||
cls.client = cls.os.image_client_v2
|
||||
cls.created_images = []
|
||||
|
||||
# We add a few images here to test the listing functionality of
|
||||
# the images API
|
||||
for x in xrange(0, 10):
|
||||
cls.created_images.append(cls._create_standard_image(x))
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
for image_id in cls.created_images:
|
||||
cls.client.delete_image(image_id)
|
||||
cls.client.wait_for_resource_deletion(image_id)
|
||||
|
||||
@classmethod
|
||||
def _create_standard_image(cls, number):
|
||||
"""
|
||||
Create a new standard image and return the ID of the newly-registered
|
||||
image. Note that the size of the new image is a random number between
|
||||
1024 and 4096
|
||||
"""
|
||||
image_file = StringIO.StringIO('*' * random.randint(1024, 4096))
|
||||
name = 'New Standard Image %s' % number
|
||||
resp, body = cls.client.create_image(name, 'bare', 'raw',
|
||||
is_public=True)
|
||||
image_id = body['id']
|
||||
resp, body = cls.client.store_image(image_id, data=image_file)
|
||||
|
||||
return image_id
|
||||
|
||||
@attr(type='image')
|
||||
def test_index_no_params(self):
|
||||
# Simple test to see all fixture images returned
|
||||
resp, images_list = self.client.image_list()
|
||||
self.assertEqual(resp['status'], '200')
|
||||
image_list = map(lambda x: x['id'], images_list)
|
||||
for image in self.created_images:
|
||||
self.assertTrue(image in image_list)
|
Loading…
Reference in New Issue