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.common import utils
from rally import consts from rally import consts
from rally import osclients 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.scenarios.vm import vmtasks
from rally.plugins.openstack.services.image import image
from rally.plugins.openstack import types from rally.plugins.openstack import types
from rally.plugins.openstack.wrappers import glance as glance_wrapper
from rally.task import context from rally.task import context
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@six.add_metaclass(abc.ABCMeta) @six.add_metaclass(abc.ABCMeta)
@context.configure(name="custom_image", order=500, hidden=True)
class BaseCustomImageGenerator(context.Context): class BaseCustomImageGenerator(context.Context):
"""Base class for the contexts providing customized image with. """Base class for the contexts providing customized image with.
@ -116,9 +114,12 @@ class BaseCustomImageGenerator(context.Context):
""" """
if "admin" in self.context: if "admin" in self.context:
# NOTE(pboldin): Create by first user and make it public by if self.context["users"]:
# the admin # NOTE(pboldin): Create by first user and make it public by
user = self.context["users"][0] # the admin
user = self.context["users"][0]
else:
user = self.context["admin"]
tenant = self.context["tenants"][user["tenant_id"]] tenant = self.context["tenants"][user["tenant_id"]]
nics = None nics = None
@ -126,6 +127,9 @@ class BaseCustomImageGenerator(context.Context):
nics = [{"net-id": tenant["networks"][0]["id"]}] nics = [{"net-id": tenant["networks"][0]["id"]}]
custom_image = self.create_one_image(user, nics=nics) 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(): for tenant in self.context["tenants"].values():
tenant["custom_image"] = custom_image tenant["custom_image"] = custom_image
@ -146,7 +150,6 @@ class BaseCustomImageGenerator(context.Context):
"""Create one image for the user.""" """Create one image for the user."""
clients = osclients.Clients(user["credential"]) clients = osclients.Clients(user["credential"])
admin_clients = osclients.Clients(self.context["admin"]["credential"])
image_id = types.GlanceImage.transform( image_id = types.GlanceImage.transform(
clients=clients, resource_config=self.config["image"]) clients=clients, resource_config=self.config["image"])
@ -156,8 +159,6 @@ class BaseCustomImageGenerator(context.Context):
vm_scenario = vmtasks.BootRuncommandDelete(self.context, vm_scenario = vmtasks.BootRuncommandDelete(self.context,
clients=clients) clients=clients)
glance_wrap = glance_wrapper.wrap(admin_clients.glance, self)
server, fip = vm_scenario._boot_server_with_fip( server, fip = vm_scenario._boot_server_with_fip(
image=image_id, flavor=flavor_id, image=image_id, flavor=flavor_id,
floating_network=self.config.get("floating_network"), floating_network=self.config.get("floating_network"),
@ -175,16 +176,9 @@ class BaseCustomImageGenerator(context.Context):
LOG.debug("Creating snapshot for %r", server) LOG.debug("Creating snapshot for %r", server)
custom_image = vm_scenario._create_image(server) custom_image = vm_scenario._create_image(server)
glance_wrap.set_visibility(custom_image)
finally: finally:
vm_scenario._delete_server_with_fip(server, fip) 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 return custom_image
@logging.log_task_wrapper(LOG.info, _("Exit context: `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): def delete_one_image(self, user, custom_image):
"""Delete the image created for the user and tenant.""" """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( 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( glance_service = image.Image(user["credential"].clients())
custom_image["id"]) glance_service.delete_image(custom_image.id)
nova_scenario._delete_image(custom_image)
@logging.log_task_wrapper(LOG.info, @logging.log_task_wrapper(LOG.info,
_("Custom image context: customizing")) _("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) @context.configure(name="test_custom_image", order=500)
class TestImageGenerator(custom_image.BaseCustomImageGenerator): class FakeImageGenerator(custom_image.BaseCustomImageGenerator):
def _customize_image(self, *args): def _customize_image(self, *args):
pass pass
@ -48,7 +48,7 @@ class BaseCustomImageContextVMTestCase(test.TestCase):
} }
}, },
"admin": { "admin": {
"credential": "credential", "credential": mock.Mock(),
}, },
"users": [ "users": [
{"tenant_id": "tenant_id0"}, {"tenant_id": "tenant_id0"},
@ -65,24 +65,21 @@ class BaseCustomImageContextVMTestCase(test.TestCase):
@mock.patch("%s.osclients.Clients" % BASE) @mock.patch("%s.osclients.Clients" % BASE)
@mock.patch("%s.types.GlanceImage.transform" % BASE, return_value="image") @mock.patch("%s.types.GlanceImage.transform" % BASE, return_value="image")
@mock.patch("%s.types.Flavor.transform" % BASE, return_value="flavor") @mock.patch("%s.types.Flavor.transform" % BASE, return_value="flavor")
@mock.patch("rally.plugins.openstack.wrappers.glance.wrap")
@mock.patch("%s.vmtasks.BootRuncommandDelete" % BASE) @mock.patch("%s.vmtasks.BootRuncommandDelete" % BASE)
def test_create_one_image( def test_create_one_image(
self, mock_boot_runcommand_delete, mock_glance_wrap, self, mock_boot_runcommand_delete, mock_flavor_transform,
mock_flavor_transform, mock_glance_image_transform, mock_clients mock_glance_image_transform, mock_clients):
):
ip = {"ip": "foo_ip", "id": "foo_id", "is_floating": True} ip = {"ip": "foo_ip", "id": "foo_id", "is_floating": True}
fake_server = mock.Mock() fake_server = mock.Mock()
fake_image = mock.MagicMock( fake_image = {"id": "image"}
to_dict=mock.MagicMock(return_value={"id": "image"}))
scenario = mock_boot_runcommand_delete.return_value = mock.MagicMock( scenario = mock_boot_runcommand_delete.return_value = mock.MagicMock(
_create_image=mock.MagicMock(return_value=fake_image), _create_image=mock.MagicMock(return_value=fake_image),
_boot_server_with_fip=mock.MagicMock( _boot_server_with_fip=mock.MagicMock(
return_value=(fake_server, ip)) return_value=(fake_server, ip))
) )
generator_ctx = TestImageGenerator(self.context) generator_ctx = FakeImageGenerator(self.context)
generator_ctx._customize_image = mock.MagicMock() generator_ctx._customize_image = mock.MagicMock()
user = { user = {
@ -93,9 +90,7 @@ class BaseCustomImageContextVMTestCase(test.TestCase):
custom_image = generator_ctx.create_one_image(user, custom_image = generator_ctx.create_one_image(user,
foo_arg="foo_value") foo_arg="foo_value")
self.assertEqual({"id": "image"}, custom_image)
mock_glance_wrap.assert_called_once_with(
mock_clients.return_value.glance, generator_ctx)
mock_flavor_transform.assert_called_once_with( mock_flavor_transform.assert_called_once_with(
clients=mock_clients.return_value, clients=mock_clients.return_value,
@ -118,92 +113,39 @@ class BaseCustomImageContextVMTestCase(test.TestCase):
fake_server, ip, user) fake_server, ip, user)
scenario._create_image.assert_called_once_with(fake_server) 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( scenario._delete_server_with_fip.assert_called_once_with(
fake_server, ip) 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) credential = mock.Mock()
@mock.patch("%s.types.GlanceImage.transform" % BASE, user = {"credential": credential,
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",
"keypair": {"name": "keypair_name"}} "keypair": {"name": "keypair_name"}}
custom_image = {"id": "image"} custom_image = mock.Mock(id="image")
generator_ctx.delete_one_image(user, custom_image) generator_ctx.delete_one_image(user, custom_image)
mock_nova_scenario.assert_called_once_with( mock_image.return_value.delete_image.assert_called_once_with("image")
context=self.context, clients=mock_clients.return_value)
nova_scenario.clients.assert_called_once_with("nova") @mock.patch("%s.image.Image" % BASE)
nova_client.images.get.assert_called_once_with("image") def test_setup_admin(self, mock_image):
nova_scenario._delete_image.assert_called_once_with("image_obj")
def test_setup_admin(self):
self.context["tenants"]["tenant_id0"]["networks"] = [ self.context["tenants"]["tenant_id0"]["networks"] = [
{"id": "network_id"}] {"id": "network_id"}]
generator_ctx = TestImageGenerator(self.context) generator_ctx = FakeImageGenerator(self.context)
generator_ctx.create_one_image = mock.Mock( image = mock.Mock(id="custom_image")
return_value="custom_image")
generator_ctx.make_image_public = mock.Mock() generator_ctx.create_one_image = mock.Mock(return_value=image)
generator_ctx.setup() generator_ctx.setup()
mock_image.return_value.set_visibility.assert_called_once_with(
image.id)
generator_ctx.create_one_image.assert_called_once_with( generator_ctx.create_one_image.assert_called_once_with(
self.context["users"][0], nics=[{"net-id": "network_id"}]) self.context["users"][0], nics=[{"net-id": "network_id"}])
@ -211,7 +153,7 @@ class BaseCustomImageContextVMTestCase(test.TestCase):
tenant = self.context["tenants"]["tenant_id0"] tenant = self.context["tenants"]["tenant_id0"]
custom_image = tenant["custom_image"] = {"id": "image"} custom_image = tenant["custom_image"] = {"id": "image"}
generator_ctx = TestImageGenerator(self.context) generator_ctx = FakeImageGenerator(self.context)
generator_ctx.delete_one_image = mock.Mock() generator_ctx.delete_one_image = mock.Mock()
@ -223,7 +165,7 @@ class BaseCustomImageContextVMTestCase(test.TestCase):
def test_setup(self): def test_setup(self):
self.context.pop("admin") self.context.pop("admin")
generator_ctx = TestImageGenerator(self.context) generator_ctx = FakeImageGenerator(self.context)
generator_ctx.create_one_image = mock.Mock( generator_ctx.create_one_image = mock.Mock(
side_effect=["custom_image0", "custom_image1", "custom_image2"]) 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"] = { self.context["tenants"]["tenant_id%d" % i]["custom_image"] = {
"id": "custom_image%d" % i} "id": "custom_image%d" % i}
generator_ctx = TestImageGenerator(self.context) generator_ctx = FakeImageGenerator(self.context)
generator_ctx.delete_one_image = mock.Mock() generator_ctx.delete_one_image = mock.Mock()
generator_ctx.cleanup() generator_ctx.cleanup()