Merge "Adding Image Context Class"
This commit is contained in:
commit
ec6cd401e0
23
doc/samples/tasks/glance/list_images.json
Normal file
23
doc/samples/tasks/glance/list_images.json
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"GlanceImages.list_images": [
|
||||
{
|
||||
"runner": {
|
||||
"type": "constant",
|
||||
"times": 10,
|
||||
"concurrency": 1
|
||||
},
|
||||
"context": {
|
||||
"users": {
|
||||
"tenants": 2,
|
||||
"users_per_tenant": 2
|
||||
},
|
||||
"images": {
|
||||
"image_url": "http://download.cirros-cloud.net/0.3.1/cirros-0.3.1-x86_64-disk.img",
|
||||
"image_type": "qcow2",
|
||||
"image_container": "bare",
|
||||
"images_per_tenant": 4
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
16
doc/samples/tasks/glance/list_images.yaml
Normal file
16
doc/samples/tasks/glance/list_images.yaml
Normal file
@ -0,0 +1,16 @@
|
||||
---
|
||||
GlanceImages.list_images:
|
||||
-
|
||||
runner:
|
||||
type: "constant"
|
||||
times: 10
|
||||
concurrency: 1
|
||||
context:
|
||||
users:
|
||||
tenants: 2
|
||||
users_per_tenant: 2
|
||||
images:
|
||||
image_url: "http://download.cirros-cloud.net/0.3.1/cirros-0.3.1-x86_64-disk.img"
|
||||
image_type: "qcow2"
|
||||
image_container: "bare"
|
||||
images_per_tenant: 4
|
102
rally/benchmark/context/images.py
Normal file
102
rally/benchmark/context/images.py
Normal file
@ -0,0 +1,102 @@
|
||||
# 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.
|
||||
|
||||
|
||||
from rally.benchmark.context import base
|
||||
from rally.benchmark.context.cleanup import utils as cleanup_utils
|
||||
from rally.benchmark.scenarios import base as scenarios_base
|
||||
from rally.benchmark.scenarios.glance import utils as glance_utils
|
||||
from rally import exceptions
|
||||
from rally.openstack.common import log as logging
|
||||
from rally import osclients
|
||||
from rally import utils as rutils
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ImageGenerator(base.Context):
|
||||
"""Context class for adding images to each user for benchmarks."""
|
||||
|
||||
__ctx_name__ = "images"
|
||||
__ctx_order__ = 411
|
||||
__ctx_hidden__ = False
|
||||
|
||||
CONFIG_SCHEMA = {
|
||||
"type": "object",
|
||||
"$schema": rutils.JSON_SCHEMA,
|
||||
"properties": {
|
||||
"image_url": {
|
||||
"type": "string",
|
||||
},
|
||||
"image_type": {
|
||||
"enum": ["qcow2", "raw", "vhd", "vmdk", "vdi", "iso", "aki",
|
||||
"ari", "ami"],
|
||||
},
|
||||
"image_container": {
|
||||
"type": "string",
|
||||
},
|
||||
"images_per_tenant": {
|
||||
"type": "integer",
|
||||
"minimum": 1
|
||||
},
|
||||
},
|
||||
"additionalProperties": False
|
||||
}
|
||||
|
||||
def __init__(self, context):
|
||||
super(ImageGenerator, self).__init__(context)
|
||||
self.config.setdefault("images_per_tenant", 1)
|
||||
self.config.setdefault("image_type", "qcow2")
|
||||
self.config.setdefault("image_container", "bare")
|
||||
self.context["images"] = []
|
||||
|
||||
@rutils.log_task_wrapper(LOG.info, _("Enter context: `Images`"))
|
||||
def setup(self):
|
||||
image_url = self.config["image_url"]
|
||||
image_type = self.config["image_type"]
|
||||
image_container = self.config["image_container"]
|
||||
images_per_tenant = self.config["images_per_tenant"]
|
||||
current_tenants = []
|
||||
|
||||
for user in self.context["users"]:
|
||||
if user["tenant_id"] not in current_tenants:
|
||||
current_tenants.append(user["tenant_id"])
|
||||
current_images = []
|
||||
|
||||
clients = osclients.Clients(user["endpoint"])
|
||||
glance_util_class = glance_utils.GlanceScenario(clients=
|
||||
clients)
|
||||
for i in range(images_per_tenant):
|
||||
rnd_name = scenarios_base.Scenario._generate_random_name()
|
||||
|
||||
image = glance_util_class._create_image(rnd_name,
|
||||
image_container,
|
||||
image_url,
|
||||
image_type)
|
||||
current_images.append(image.id)
|
||||
|
||||
self.context["images"].append({"image_id": current_images,
|
||||
"endpoint": user["endpoint"],
|
||||
"tenant_id": user["tenant_id"]})
|
||||
|
||||
@rutils.log_task_wrapper(LOG.info, _("Exit context: `Images`"))
|
||||
def cleanup(self):
|
||||
for images in self.context["images"]:
|
||||
try:
|
||||
glance = osclients.Clients(images["endpoint"]).glance()
|
||||
cleanup_utils.delete_glance_resources(glance,
|
||||
images["tenant_id"])
|
||||
except Exception:
|
||||
raise exceptions.ImageCleanUpException()
|
@ -46,6 +46,20 @@ class GlanceImages(utils.GlanceScenario, nova_utils.NovaScenario):
|
||||
**kwargs)
|
||||
self._list_images()
|
||||
|
||||
@base.scenario(context={"cleanup": ["glance"]})
|
||||
def list_images(self):
|
||||
"""Test the glance image-list command.
|
||||
|
||||
This simple scenario tests the glance image-list command by listing
|
||||
all the images.
|
||||
|
||||
Suppose if we have 2 users in context and each has 2 images
|
||||
uploaded for them we will be able to test the performance of
|
||||
glance image-list command in this case.
|
||||
"""
|
||||
|
||||
self._list_images()
|
||||
|
||||
@base.scenario(context={"cleanup": ["glance"]})
|
||||
def create_and_delete_image(self, container_format,
|
||||
image_location, disk_format, **kwargs):
|
||||
|
@ -242,3 +242,7 @@ class NoNodesFound(RallyException):
|
||||
|
||||
class UnknownRelease(RallyException):
|
||||
msg_fmt = _("Unknown release '%(release)s'")
|
||||
|
||||
|
||||
class ImageCleanUpException(RallyException):
|
||||
msg_fmt = _("Image Deletion Failed")
|
||||
|
99
tests/benchmark/context/test_images.py
Normal file
99
tests/benchmark/context/test_images.py
Normal file
@ -0,0 +1,99 @@
|
||||
# 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 copy
|
||||
import mock
|
||||
|
||||
from rally.benchmark.context import images
|
||||
from rally import exceptions
|
||||
from tests import test
|
||||
|
||||
CTX = "rally.benchmark.context"
|
||||
SCN = "rally.benchmark.scenarios"
|
||||
|
||||
|
||||
class ImageGeneratorTestCase(test.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(ImageGeneratorTestCase, self).setUp()
|
||||
self.image = mock.MagicMock()
|
||||
self.image1 = mock.MagicMock()
|
||||
self.tenants_num = 2
|
||||
self.users_per_tenant = 5
|
||||
self.users = self.tenants_num * self.users_per_tenant
|
||||
self.concurrent = 10
|
||||
self.image_type = "qcow2"
|
||||
self.image_container = "bare"
|
||||
self.images_per_tenant = 5
|
||||
self.task = mock.MagicMock()
|
||||
self.image_list = ["uuid" for i in range(self.images_per_tenant)]
|
||||
self.users_key_with_image_id = [{'image_id': self.image_list,
|
||||
'endpoint': 'endpoint',
|
||||
'tenant_id': i}
|
||||
for i in range(self.tenants_num)]
|
||||
self.user_key = [{'id': i, 'tenant_id': j, 'endpoint': 'endpoint'}
|
||||
for j in range(self.tenants_num)
|
||||
for i in range(self.users_per_tenant)]
|
||||
|
||||
@property
|
||||
def context_without_images_key(self):
|
||||
return {
|
||||
"config": {
|
||||
"users": {
|
||||
"tenants": self.tenants_num,
|
||||
"users_per_tenant": self.users_per_tenant,
|
||||
"concurrent": self.concurrent,
|
||||
},
|
||||
"images": {
|
||||
"image_url": "mock_url",
|
||||
"image_type": self.image_type,
|
||||
"image_container": self.image_container,
|
||||
"images_per_tenant": self.images_per_tenant,
|
||||
}
|
||||
},
|
||||
"admin": {"endpoint": mock.MagicMock()},
|
||||
"task": mock.MagicMock(),
|
||||
"users": self.user_key,
|
||||
}
|
||||
|
||||
@mock.patch("%s.glance.utils.GlanceScenario._create_image" % SCN)
|
||||
@mock.patch("%s.images.osclients" % CTX)
|
||||
@mock.patch("%s.cleanup.utils.delete_glance_resources" % CTX)
|
||||
def test_setup_and_cleanup(self, mock_image_remover, mock_osclients,
|
||||
mock_image_generator):
|
||||
|
||||
class FakeImage(object):
|
||||
def __init__(self):
|
||||
self.id = "uuid"
|
||||
fake_image = FakeImage()
|
||||
|
||||
endpoint = mock.MagicMock()
|
||||
mock_osclients.Clients(endpoint).glance().images.get.\
|
||||
return_value = self.image1
|
||||
mock_image_generator.return_value = fake_image
|
||||
|
||||
real_context = self.context_without_images_key
|
||||
new_context = copy.deepcopy(real_context)
|
||||
new_context["images"] = self.users_key_with_image_id
|
||||
|
||||
images_ctx = images.ImageGenerator(real_context)
|
||||
images_ctx.setup()
|
||||
self.assertEqual(new_context, real_context)
|
||||
images_ctx.cleanup()
|
||||
|
||||
self.assertEqual(self.tenants_num, len(mock_image_remover.mock_calls))
|
||||
|
||||
mock_image_remover.side_effect = Exception('failed_deletion')
|
||||
self.assertRaises(exceptions.ImageCleanUpException, images_ctx.cleanup)
|
@ -40,6 +40,12 @@ class GlanceImagesTestCase(test.TestCase):
|
||||
"url", "df", fakearg="f")
|
||||
mock_list.assert_called_once_with()
|
||||
|
||||
@mock.patch(GLANCE_IMAGES + "._list_images")
|
||||
def test_list_images(self, mock_list):
|
||||
glance_scenario = images.GlanceImages()
|
||||
glance_scenario.list_images()
|
||||
mock_list.assert_called_once_with()
|
||||
|
||||
@mock.patch(GLANCE_IMAGES + "._generate_random_name")
|
||||
@mock.patch(GLANCE_IMAGES + "._delete_image")
|
||||
@mock.patch(GLANCE_IMAGES + "._create_image")
|
||||
|
Loading…
Reference in New Issue
Block a user