Merge "Various changes in custom_image context"

This commit is contained in:
Jenkins 2017-05-30 14:15:32 +00:00 committed by Gerrit Code Review
commit 66b8573bcc
2 changed files with 39 additions and 109 deletions

View File

@ -23,17 +23,15 @@ from rally.common import logging
from rally.common import utils
from rally import consts
from rally import osclients
from rally.plugins.openstack.scenarios.nova import utils as nova_utils
from rally.plugins.openstack.scenarios.vm import vmtasks
from rally.plugins.openstack.services.image import image
from rally.plugins.openstack import types
from rally.plugins.openstack.wrappers import glance as glance_wrapper
from rally.task import context
LOG = logging.getLogger(__name__)
@six.add_metaclass(abc.ABCMeta)
@context.configure(name="custom_image", order=500, hidden=True)
class BaseCustomImageGenerator(context.Context):
"""Base class for the contexts providing customized image with.
@ -116,9 +114,12 @@ class BaseCustomImageGenerator(context.Context):
"""
if "admin" in self.context:
# NOTE(pboldin): Create by first user and make it public by
# the admin
user = self.context["users"][0]
if self.context["users"]:
# NOTE(pboldin): Create by first user and make it public by
# the admin
user = self.context["users"][0]
else:
user = self.context["admin"]
tenant = self.context["tenants"][user["tenant_id"]]
nics = None
@ -126,6 +127,9 @@ class BaseCustomImageGenerator(context.Context):
nics = [{"net-id": tenant["networks"][0]["id"]}]
custom_image = self.create_one_image(user, nics=nics)
glance_service = image.Image(
self.context["admin"]["credential"].clients())
glance_service.set_visibility(custom_image.id)
for tenant in self.context["tenants"].values():
tenant["custom_image"] = custom_image
@ -146,7 +150,6 @@ class BaseCustomImageGenerator(context.Context):
"""Create one image for the user."""
clients = osclients.Clients(user["credential"])
admin_clients = osclients.Clients(self.context["admin"]["credential"])
image_id = types.GlanceImage.transform(
clients=clients, resource_config=self.config["image"])
@ -156,8 +159,6 @@ class BaseCustomImageGenerator(context.Context):
vm_scenario = vmtasks.BootRuncommandDelete(self.context,
clients=clients)
glance_wrap = glance_wrapper.wrap(admin_clients.glance, self)
server, fip = vm_scenario._boot_server_with_fip(
image=image_id, flavor=flavor_id,
floating_network=self.config.get("floating_network"),
@ -175,16 +176,9 @@ class BaseCustomImageGenerator(context.Context):
LOG.debug("Creating snapshot for %r", server)
custom_image = vm_scenario._create_image(server)
glance_wrap.set_visibility(custom_image)
finally:
vm_scenario._delete_server_with_fip(server, fip)
if hasattr(custom_image, "to_dict"):
# NOTE(stpierre): Glance v1 images are objects that can be
# converted to dicts; Glance v2 images are already
# dict-like
custom_image = custom_image.to_dict()
return custom_image
@logging.log_task_wrapper(LOG.info, _("Exit context: `custom_image`"))
@ -215,17 +209,11 @@ class BaseCustomImageGenerator(context.Context):
def delete_one_image(self, user, custom_image):
"""Delete the image created for the user and tenant."""
clients = osclients.Clients(user["credential"])
nova_scenario = nova_utils.NovaScenario(
context=self.context, clients=clients)
with logging.ExceptionLogger(
LOG, _("Unable to delete image %s") % custom_image["id"]):
LOG, _("Unable to delete image %s") % custom_image.id):
custom_image = nova_scenario.clients("nova").images.get(
custom_image["id"])
nova_scenario._delete_image(custom_image)
glance_service = image.Image(user["credential"].clients())
glance_service.delete_image(custom_image.id)
@logging.log_task_wrapper(LOG.info,
_("Custom image context: customizing"))

View File

@ -26,7 +26,7 @@ BASE = "rally.plugins.openstack.context.vm.custom_image"
@context.configure(name="test_custom_image", order=500)
class TestImageGenerator(custom_image.BaseCustomImageGenerator):
class FakeImageGenerator(custom_image.BaseCustomImageGenerator):
def _customize_image(self, *args):
pass
@ -48,7 +48,7 @@ class BaseCustomImageContextVMTestCase(test.TestCase):
}
},
"admin": {
"credential": "credential",
"credential": mock.Mock(),
},
"users": [
{"tenant_id": "tenant_id0"},
@ -65,24 +65,21 @@ class BaseCustomImageContextVMTestCase(test.TestCase):
@mock.patch("%s.osclients.Clients" % BASE)
@mock.patch("%s.types.GlanceImage.transform" % BASE, return_value="image")
@mock.patch("%s.types.Flavor.transform" % BASE, return_value="flavor")
@mock.patch("rally.plugins.openstack.wrappers.glance.wrap")
@mock.patch("%s.vmtasks.BootRuncommandDelete" % BASE)
def test_create_one_image(
self, mock_boot_runcommand_delete, mock_glance_wrap,
mock_flavor_transform, mock_glance_image_transform, mock_clients
):
self, mock_boot_runcommand_delete, mock_flavor_transform,
mock_glance_image_transform, mock_clients):
ip = {"ip": "foo_ip", "id": "foo_id", "is_floating": True}
fake_server = mock.Mock()
fake_image = mock.MagicMock(
to_dict=mock.MagicMock(return_value={"id": "image"}))
fake_image = {"id": "image"}
scenario = mock_boot_runcommand_delete.return_value = mock.MagicMock(
_create_image=mock.MagicMock(return_value=fake_image),
_boot_server_with_fip=mock.MagicMock(
return_value=(fake_server, ip))
)
generator_ctx = TestImageGenerator(self.context)
generator_ctx = FakeImageGenerator(self.context)
generator_ctx._customize_image = mock.MagicMock()
user = {
@ -93,9 +90,7 @@ class BaseCustomImageContextVMTestCase(test.TestCase):
custom_image = generator_ctx.create_one_image(user,
foo_arg="foo_value")
mock_glance_wrap.assert_called_once_with(
mock_clients.return_value.glance, generator_ctx)
self.assertEqual({"id": "image"}, custom_image)
mock_flavor_transform.assert_called_once_with(
clients=mock_clients.return_value,
@ -118,92 +113,39 @@ class BaseCustomImageContextVMTestCase(test.TestCase):
fake_server, ip, user)
scenario._create_image.assert_called_once_with(fake_server)
mock_glance_wrap.return_value.set_visibility.assert_called_once_with(
fake_image)
scenario._delete_server_with_fip.assert_called_once_with(
fake_server, ip)
self.assertEqual({"id": "image"}, custom_image)
@mock.patch("%s.image.Image" % BASE)
def test_delete_one_image(self, mock_image):
generator_ctx = FakeImageGenerator(self.context)
@mock.patch("%s.osclients.Clients" % BASE)
@mock.patch("%s.types.GlanceImage.transform" % BASE,
return_value="image")
@mock.patch("%s.types.Flavor.transform" % BASE,
return_value="flavor")
@mock.patch("rally.plugins.openstack.wrappers.glance.wrap")
@mock.patch("%s.vmtasks.BootRuncommandDelete" % BASE)
def test_create_one_image_cleanup(
self, mock_boot_runcommand_delete, mock_glance_wrap,
mock_flavor_transform, mock_glance_image_transform, mock_clients
):
ip = {"ip": "foo_ip", "id": "foo_id", "is_floating": True}
fake_server = mock.Mock()
fake_image = mock.MagicMock(
to_dict=mock.MagicMock(return_value={"id": "image"}))
scenario = mock_boot_runcommand_delete.return_value = mock.MagicMock(
_create_image=mock.MagicMock(return_value=fake_image),
_boot_server_with_fip=mock.MagicMock(
return_value=(fake_server, ip)),
_generate_random_name=mock.MagicMock(return_value="foo_name"),
)
generator_ctx = TestImageGenerator(self.context)
generator_ctx._customize_image = mock.MagicMock(
side_effect=ValueError())
user = {
"credential": "credential",
"keypair": {"name": "keypair_name"},
"secgroup": {"name": "secgroup_name"}
}
self.assertRaises(
ValueError,
generator_ctx.create_one_image, user, foo_arg="foo_value")
generator_ctx._customize_image.assert_called_once_with(
fake_server, ip, user)
scenario._delete_server_with_fip.assert_called_once_with(
fake_server, ip)
@mock.patch("%s.nova_utils.NovaScenario" % BASE)
@mock.patch("%s.osclients.Clients" % BASE)
def test_delete_one_image(self, mock_clients, mock_nova_scenario):
nova_scenario = mock_nova_scenario.return_value = mock.MagicMock()
nova_client = nova_scenario.clients.return_value
nova_client.images.get.return_value = "image_obj"
generator_ctx = TestImageGenerator(self.context)
user = {"credential": "credential",
credential = mock.Mock()
user = {"credential": credential,
"keypair": {"name": "keypair_name"}}
custom_image = {"id": "image"}
custom_image = mock.Mock(id="image")
generator_ctx.delete_one_image(user, custom_image)
mock_nova_scenario.assert_called_once_with(
context=self.context, clients=mock_clients.return_value)
mock_image.return_value.delete_image.assert_called_once_with("image")
nova_scenario.clients.assert_called_once_with("nova")
nova_client.images.get.assert_called_once_with("image")
nova_scenario._delete_image.assert_called_once_with("image_obj")
def test_setup_admin(self):
@mock.patch("%s.image.Image" % BASE)
def test_setup_admin(self, mock_image):
self.context["tenants"]["tenant_id0"]["networks"] = [
{"id": "network_id"}]
generator_ctx = TestImageGenerator(self.context)
generator_ctx = FakeImageGenerator(self.context)
generator_ctx.create_one_image = mock.Mock(
return_value="custom_image")
generator_ctx.make_image_public = mock.Mock()
image = mock.Mock(id="custom_image")
generator_ctx.create_one_image = mock.Mock(return_value=image)
generator_ctx.setup()
mock_image.return_value.set_visibility.assert_called_once_with(
image.id)
generator_ctx.create_one_image.assert_called_once_with(
self.context["users"][0], nics=[{"net-id": "network_id"}])
@ -211,7 +153,7 @@ class BaseCustomImageContextVMTestCase(test.TestCase):
tenant = self.context["tenants"]["tenant_id0"]
custom_image = tenant["custom_image"] = {"id": "image"}
generator_ctx = TestImageGenerator(self.context)
generator_ctx = FakeImageGenerator(self.context)
generator_ctx.delete_one_image = mock.Mock()
@ -223,7 +165,7 @@ class BaseCustomImageContextVMTestCase(test.TestCase):
def test_setup(self):
self.context.pop("admin")
generator_ctx = TestImageGenerator(self.context)
generator_ctx = FakeImageGenerator(self.context)
generator_ctx.create_one_image = mock.Mock(
side_effect=["custom_image0", "custom_image1", "custom_image2"])
@ -247,7 +189,7 @@ class BaseCustomImageContextVMTestCase(test.TestCase):
self.context["tenants"]["tenant_id%d" % i]["custom_image"] = {
"id": "custom_image%d" % i}
generator_ctx = TestImageGenerator(self.context)
generator_ctx = FakeImageGenerator(self.context)
generator_ctx.delete_one_image = mock.Mock()
generator_ctx.cleanup()