From 272349796c36a682183aac9ef51dc45299a2f946 Mon Sep 17 00:00:00 2001 From: Andrey Kurilin Date: Mon, 23 Apr 2018 16:36:32 +0300 Subject: [PATCH] Redesign ResourceTypes Rally 0.12 is comming with introducing a new interface of rally.task.ResourceType. It also ports in-tree OpenStack plugins to the interface. Despite the fact that Rally 0.12 is not released yet. Let's be prepared to it. This patch is a cherry-pick of an original commit to rally core repo. Thanks to git, also complicts ard movements were fixed automatically. Change-Id: Iea95450892f229886c86697d632013e868b6cb31 --- CHANGELOG.rst | 5 + rally_openstack/contexts/ec2/servers.py | 6 +- rally_openstack/contexts/nova/servers.py | 10 +- rally_openstack/contexts/vm/custom_image.py | 8 +- .../contexts/watcher/audit_templates.py | 13 +- rally_openstack/types.py | 260 ++++++------- rally_openstack/validators.py | 18 +- tests/unit/contexts/ec2/test_servers.py | 10 +- tests/unit/contexts/nova/test_servers.py | 16 +- tests/unit/contexts/vm/test_custom_image.py | 22 +- .../contexts/watcher/test_audit_templates.py | 17 +- tests/unit/doc/test_docstrings.py | 2 - tests/unit/test_types.py | 361 ++++++++++-------- tests/unit/test_validators.py | 87 +++-- 14 files changed, 425 insertions(+), 410 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 8ab670fb..e9025ebc 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -42,6 +42,11 @@ Changed rally framework repository. Also, the CI is extended to check ability to build Docker image for any of changes. +* An interface of ResourceType plugins is changed since Rally 0.12. All our + plugins ared adpoted to support it. + The port is done in a backward compatible way, so the minimum required + version of Rally still is 0.11.0, but we suggest you to use the latest + release of Rally. Removed ~~~~~~~ diff --git a/rally_openstack/contexts/ec2/servers.py b/rally_openstack/contexts/ec2/servers.py index 0354fa29..06917067 100644 --- a/rally_openstack/contexts/ec2/servers.py +++ b/rally_openstack/contexts/ec2/servers.py @@ -18,7 +18,6 @@ from rally.task import context from rally_openstack.cleanup import manager as resource_manager from rally_openstack import consts -from rally_openstack import osclients from rally_openstack.scenarios.ec2 import utils as ec2_utils from rally_openstack import types @@ -65,9 +64,8 @@ class EC2ServerGenerator(context.Context): image = self.config["image"] flavor = self.config["flavor"] - clients = osclients.Clients(self.context["users"][0]["credential"]) - image_id = types.EC2Image.transform(clients=clients, - resource_config=image) + image_id = types.EC2Image(self.context).pre_process( + resource_spec=image, config={}) for user, tenant_id in rutils.iterate_per_tenants( self.context["users"]): diff --git a/rally_openstack/contexts/nova/servers.py b/rally_openstack/contexts/nova/servers.py index 8654503c..15aa6757 100755 --- a/rally_openstack/contexts/nova/servers.py +++ b/rally_openstack/contexts/nova/servers.py @@ -18,7 +18,6 @@ from rally.common import validation from rally.task import context from rally_openstack.cleanup import manager as resource_manager -from rally_openstack import osclients from rally_openstack.scenarios.nova import utils as nova_utils from rally_openstack import types @@ -101,11 +100,10 @@ class ServerGenerator(context.Context): kwargs["nics"] = [{"net-id": nic} for nic in self.config["nics"]] - clients = osclients.Clients(self.context["users"][0]["credential"]) - image_id = types.GlanceImage.transform(clients=clients, - resource_config=image) - flavor_id = types.Flavor.transform(clients=clients, - resource_config=flavor) + image_id = types.GlanceImage(self.context).pre_process( + resource_spec=image, config={}) + flavor_id = types.Flavor(self.context).pre_process( + resource_spec=flavor, config={}) for iter_, (user, tenant_id) in enumerate(rutils.iterate_per_tenants( self.context["users"])): diff --git a/rally_openstack/contexts/vm/custom_image.py b/rally_openstack/contexts/vm/custom_image.py index dcf2a52b..27e439b5 100644 --- a/rally_openstack/contexts/vm/custom_image.py +++ b/rally_openstack/contexts/vm/custom_image.py @@ -150,10 +150,10 @@ class BaseCustomImageGenerator(context.Context): clients = osclients.Clients(user["credential"]) - image_id = types.GlanceImage.transform( - clients=clients, resource_config=self.config["image"]) - flavor_id = types.Flavor.transform( - clients=clients, resource_config=self.config["flavor"]) + image_id = types.GlanceImage(self.context).pre_process( + resource_spec=self.config["image"], config={}) + flavor_id = types.Flavor(self.context).pre_process( + resource_spec=self.config["flavor"], config={}) vm_scenario = vmtasks.BootRuncommandDelete(self.context, clients=clients) diff --git a/rally_openstack/contexts/watcher/audit_templates.py b/rally_openstack/contexts/watcher/audit_templates.py index 17cb08bd..fecedf4e 100644 --- a/rally_openstack/contexts/watcher/audit_templates.py +++ b/rally_openstack/contexts/watcher/audit_templates.py @@ -21,7 +21,6 @@ from rally.task import context from rally_openstack.cleanup import manager as resource_manager from rally_openstack import consts -from rally_openstack import osclients from rally_openstack.scenarios.watcher import utils as watcher_utils from rally_openstack import types @@ -85,8 +84,6 @@ class AuditTemplateGenerator(context.Context): "api_versions", [])} }) - clients = osclients.Clients(self.context["admin"]["credential"]) - self.context["audit_templates"] = [] for i in six.moves.range(self.config["audit_templates_per_admin"]): cfg_size = len(self.config["params"]) @@ -95,12 +92,10 @@ class AuditTemplateGenerator(context.Context): elif self.config["fill_strategy"] == "random": audit_params = random.choice(self.config["params"]) - goal_id = types.WatcherGoal.transform( - clients=clients, - resource_config=audit_params["goal"]) - strategy_id = types.WatcherStrategy.transform( - clients=clients, - resource_config=audit_params["strategy"]) + goal_id = types.WatcherGoal(self.context).pre_process( + resource_spec=audit_params["goal"], config={}) + strategy_id = types.WatcherStrategy(self.context).pre_process( + resource_spec=audit_params["strategy"], config={}) audit_template = watcher_scenario._create_audit_template( goal_id, strategy_id) diff --git a/rally_openstack/types.py b/rally_openstack/types.py index a744a407..2e134940 100644 --- a/rally_openstack/types.py +++ b/rally_openstack/types.py @@ -13,232 +13,220 @@ # under the License. import copy +import traceback +from rally.common import logging from rally.common.plugin import plugin from rally import exceptions from rally.task import types +import rally_openstack +from rally_openstack import osclients from rally_openstack.services.image import image +from rally_openstack.services.storage import block -@plugin.configure(name="nova_flavor") -class Flavor(types.ResourceType): +LOG = logging.getLogger(__name__) + + +class OpenStackResourceType(types.ResourceType): + """A base class for OpenStack ResourceTypes plugins with help-methods""" + + def __init__(self, context=None, cache=None): + if rally_openstack.__rally_version__ >= (0, 12): + super(OpenStackResourceType, self).__init__(context, cache) + else: + super(OpenStackResourceType, self).__init__() + self._context = context or {} + self._global_cache = cache or {} + self._global_cache.setdefault(self.get_name(), {}) + self._cache = self._global_cache[self.get_name()] + + self._clients = None + if self._context.get("admin"): + self._clients = osclients.Clients( + self._context["admin"]["credential"]) + elif self._context.get("users"): + self._clients = osclients.Clients( + self._context["users"][0]["credential"]) + + if rally_openstack.__rally_version__ < (0, 12): + @classmethod + def _get_doc(cls): + return cls.__doc__ + + +class DeprecatedBehaviourMixin(object): + """A Mixin class which returns deprecated `transform` method.""" @classmethod def transform(cls, clients, resource_config): - """Transform the resource config to id. + caller = traceback.format_stack(limit=2)[0] + if rally_openstack.__rally_version__ >= (0, 12): + # The new interface of ResourceClass is introduced with Rally 0.12 + LOG.warning("Calling method `transform` of %s is deprecated:\n%s" + % (cls.__name__, caller)) + if clients: + # it doesn't matter "permission" of the user. it will pick the + # first one + context = {"admin": {"credential": clients.credential}} + else: + context = {} + self = cls(context, cache={}) + return self.pre_process(resource_spec=resource_config, config={}) - :param clients: openstack admin client handles - :param resource_config: scenario config with `id`, `name` or `regex` - :returns: id matching resource - """ - resource_id = resource_config.get("id") +@plugin.configure(name="nova_flavor") +class Flavor(DeprecatedBehaviourMixin, OpenStackResourceType): + """Find Nova's flavor ID by name or regexp.""" + + def pre_process(self, resource_spec, config): + resource_id = resource_spec.get("id") if not resource_id: - novaclient = clients.nova() + novaclient = self._clients.nova() resource_id = types._id_from_name( - resource_config=resource_config, + resource_config=resource_spec, resources=novaclient.flavors.list(), typename="flavor") return resource_id @plugin.configure(name="ec2_flavor") -class EC2Flavor(types.ResourceType): +class EC2Flavor(DeprecatedBehaviourMixin, OpenStackResourceType): + """Find Nova's flavor Name by it's ID or regexp.""" - @classmethod - def transform(cls, clients, resource_config): - """Transform the resource config to name. - - In the case of using EC2 API, flavor name is used for launching - servers. - - :param clients: openstack admin client handles - :param resource_config: scenario config with `id`, `name` or `regex` - - :returns: name matching resource - """ - resource_name = resource_config.get("name") + def pre_process(self, resource_spec, config): + resource_name = resource_spec.get("name") if not resource_name: # NOTE(wtakase): gets resource name from OpenStack id - novaclient = clients.nova() + novaclient = self._clients.nova() resource_name = types._name_from_id( - resource_config=resource_config, + resource_config=resource_spec, resources=novaclient.flavors.list(), typename="flavor") return resource_name @plugin.configure(name="glance_image") -class GlanceImage(types.ResourceType): +class GlanceImage(DeprecatedBehaviourMixin, OpenStackResourceType): + """Find Glance's image ID by name or regexp.""" - @classmethod - def transform(cls, clients, resource_config): - """Transform the resource config to id. + def pre_process(self, resource_spec, config): + resource_id = resource_spec.get("id") + list_kwargs = resource_spec.get("list_kwargs", {}) - :param clients: openstack admin client handles - :param resource_config: scenario config with `id`, `name` or `regex` - - :returns: id matching resource - """ - resource_id = resource_config.get("id") - list_kwargs = resource_config.get("list_kwargs", {}) if not resource_id: - images = list(image.Image(clients).list_images(**list_kwargs)) + cache_id = hash(frozenset(list_kwargs.items())) + if cache_id not in self._cache: + glance = image.Image(self._clients) + self._cache[cache_id] = glance.list_images(**list_kwargs) + images = self._cache[cache_id] resource_id = types._id_from_name( - resource_config=resource_config, + resource_config=resource_spec, resources=images, typename="image") return resource_id @plugin.configure(name="glance_image_args") -class GlanceImageArguments(types.ResourceType): - - @classmethod - def transform(cls, clients, resource_config): - """Transform the resource config to id. - - :param clients: openstack admin client handles - :param resource_config: scenario config with `id`, `name` or `regex` - - :returns: id matching resource - """ - resource_config = copy.deepcopy(resource_config) - if "is_public" in resource_config: - if "visibility" in resource_config: - resource_config.pop("is_public") +class GlanceImageArguments(DeprecatedBehaviourMixin, OpenStackResourceType): + """Process Glance image create options to look similar in case of V1/V2.""" + def pre_process(self, resource_spec, config): + resource_spec = copy.deepcopy(resource_spec) + if "is_public" in resource_spec: + if "visibility" in resource_spec: + resource_spec.pop("is_public") else: - visibility = ("public" if resource_config.pop("is_public") + visibility = ("public" if resource_spec.pop("is_public") else "private") - resource_config["visibility"] = visibility - return resource_config + resource_spec["visibility"] = visibility + return resource_spec @plugin.configure(name="ec2_image") -class EC2Image(types.ResourceType): +class EC2Image(DeprecatedBehaviourMixin, OpenStackResourceType): + """Find EC2 image ID.""" - @classmethod - def transform(cls, clients, resource_config): - """Transform the resource config to EC2 id. - - If OpenStack resource id is given, this function gets resource name - from the id and then gets EC2 resource id from the name. - - :param clients: openstack admin client handles - :param resource_config: scenario config with `id`, `name` or `regex` - - :returns: EC2 id matching resource - """ - if "name" not in resource_config and "regex" not in resource_config: + def pre_process(self, resource_spec, config): + if "name" not in resource_spec and "regex" not in resource_spec: # NOTE(wtakase): gets resource name from OpenStack id - glanceclient = clients.glance() + glanceclient = self._clients.glance() resource_name = types._name_from_id( - resource_config=resource_config, + resource_config=resource_spec, resources=list(glanceclient.images.list()), typename="image") - resource_config["name"] = resource_name + resource_spec["name"] = resource_name # NOTE(wtakase): gets EC2 resource id from name or regex - ec2client = clients.ec2() + ec2client = self._clients.ec2() resource_ec2_id = types._id_from_name( - resource_config=resource_config, + resource_config=resource_spec, resources=list(ec2client.get_all_images()), typename="ec2_image") return resource_ec2_id @plugin.configure(name="cinder_volume_type") -class VolumeType(types.ResourceType): +class VolumeType(DeprecatedBehaviourMixin, OpenStackResourceType): + """Find Cinder volume type ID by name or regexp.""" - @classmethod - def transform(cls, clients, resource_config): - """Transform the resource config to id. - - :param clients: openstack admin client handles - :param resource_config: scenario config with `id`, `name` or `regex` - - :returns: id matching resource - """ - resource_id = resource_config.get("id") + def pre_process(self, resource_spec, config): + resource_id = resource_spec.get("id") if not resource_id: - cinderclient = clients.cinder() - resource_id = types._id_from_name(resource_config=resource_config, - resources=cinderclient. - volume_types.list(), - typename="volume_type") + cinder = block.BlockStorage(self._clients) + resource_id = types._id_from_name( + resource_config=resource_spec, + resources=cinder.list_types(), + typename="volume_type") return resource_id @plugin.configure(name="neutron_network") -class NeutronNetwork(types.ResourceType): - - @classmethod - def transform(cls, clients, resource_config): - """Transform the resource config to id. - - :param clients: openstack admin client handles - :param resource_config: scenario config with `id`, `name` or `regex` - - :returns: id matching resource - """ - resource_id = resource_config.get("id") +class NeutronNetwork(DeprecatedBehaviourMixin, OpenStackResourceType): + """Find Neutron network ID by it's name.""" + def pre_process(self, resource_spec, config): + resource_id = resource_spec.get("id") if resource_id: return resource_id else: - neutronclient = clients.neutron() + neutronclient = self._clients.neutron() for net in neutronclient.list_networks()["networks"]: - if net["name"] == resource_config.get("name"): + if net["name"] == resource_spec.get("name"): return net["id"] raise exceptions.InvalidScenarioArgument( "Neutron network with name '{name}' not found".format( - name=resource_config.get("name"))) + name=resource_spec.get("name"))) @plugin.configure(name="watcher_strategy") -class WatcherStrategy(types.ResourceType): +class WatcherStrategy(DeprecatedBehaviourMixin, OpenStackResourceType): + """Find Watcher strategy ID by it's name.""" - @classmethod - def transform(cls, clients, resource_config): - """Transform the resource config to id. - - :param clients: openstack admin client handles - :param resource_config: scenario config with `id`, `name` or `regex` - - :returns: id matching resource - """ - resource_id = resource_config.get("id") + def pre_process(self, resource_spec, config): + resource_id = resource_spec.get("id") if not resource_id: - watcherclient = clients.watcher() + watcherclient = self._clients.watcher() resource_id = types._id_from_name( - resource_config=resource_config, + resource_config=resource_spec, resources=[watcherclient.strategy.get( - resource_config.get("name"))], + resource_spec.get("name"))], typename="strategy", id_attr="uuid") return resource_id @plugin.configure(name="watcher_goal") -class WatcherGoal(types.ResourceType): +class WatcherGoal(DeprecatedBehaviourMixin, OpenStackResourceType): + """Find Watcher goal ID by it's name.""" - @classmethod - def transform(cls, clients, resource_config): - """Transform the resource config to id. - - :param clients: openstack admin client handles - :param resource_config: scenario config with `id`, `name` or `regex` - - :returns: id matching resource - """ - resource_id = resource_config.get("id") + def pre_process(self, resource_spec, config): + resource_id = resource_spec.get("id") if not resource_id: - watcherclient = clients.watcher() + watcherclient = self._clients.watcher() resource_id = types._id_from_name( - resource_config=resource_config, - resources=[watcherclient.goal.get( - resource_config.get("name"))], + resource_config=resource_spec, + resources=[watcherclient.goal.get(resource_spec.get("name"))], typename="goal", id_attr="uuid") return resource_id diff --git a/rally_openstack/validators.py b/rally_openstack/validators.py index ba9e2561..8c6153c4 100644 --- a/rally_openstack/validators.py +++ b/rally_openstack/validators.py @@ -98,10 +98,10 @@ class ImageExistsValidator(validation.Validator): return try: for user in context["users"]: - clients = user["credential"].clients() - image_id = openstack_types.GlanceImage.transform( - clients=clients, resource_config=image_args) - clients.glance().images.get(image_id) + image_processor = openstack_types.GlanceImage( + context={"admin": {"credential": user["credential"]}}) + image_id = image_processor.pre_process(image_args, config={}) + user["credential"].clients().glance().images.get(image_id) except (glance_exc.HTTPNotFound, exceptions.InvalidScenarioArgument): self.fail("Image '%s' not found" % image_args) @@ -210,8 +210,9 @@ class FlavorExistsValidator(validation.Validator): if not flavor_value: self.fail("Parameter %s is not specified." % param_name) try: - flavor_id = openstack_types.Flavor.transform( - clients=clients, resource_config=flavor_value) + flavor_processor = openstack_types.Flavor( + context={"admin": {"credential": clients.credential}}) + flavor_id = flavor_processor.pre_process(flavor_value, config={}) flavor = clients.nova().flavors.get(flavor=flavor_id) return flavor except (nova_exc.NotFound, exceptions.InvalidScenarioArgument): @@ -281,8 +282,9 @@ class ImageValidOnFlavorValidator(FlavorExistsValidator): } return image try: - image_id = openstack_types.GlanceImage.transform( - clients=clients, resource_config=image_args) + image_processor = openstack_types.GlanceImage( + context={"admin": {"credential": clients.credential}}) + image_id = image_processor.pre_process(image_args, config={}) image = clients.glance().images.get(image_id) if hasattr(image, "to_dict"): # NOTE(stpierre): Glance v1 images are objects that can be diff --git a/tests/unit/contexts/ec2/test_servers.py b/tests/unit/contexts/ec2/test_servers.py index 95e151b0..60fcd789 100644 --- a/tests/unit/contexts/ec2/test_servers.py +++ b/tests/unit/contexts/ec2/test_servers.py @@ -62,11 +62,8 @@ class EC2ServerGeneratorTestCase(test.TestCase): @mock.patch("%s.ec2.utils.EC2Scenario._boot_servers" % SCN, return_value=[fakes.FakeServer(id=str(i)) for i in range(5)]) - @mock.patch("%s.EC2Image.transform" % TYP, return_value=mock.MagicMock()) - @mock.patch("%s.servers.osclients" % CTX, return_value=fakes.FakeClients()) - def test_setup(self, mock_osclients, - mock_ec2_image_transform, - mock_ec2_scenario__boot_servers): + @mock.patch("%s.EC2Image" % TYP) + def test_setup(self, mock_ec2_image, mock_ec2_scenario__boot_servers): tenants_count = 2 users_per_tenant = 5 @@ -87,9 +84,8 @@ class EC2ServerGeneratorTestCase(test.TestCase): servers_ctx.setup() self.assertEqual(new_context, servers_ctx.context) - @mock.patch("%s.servers.osclients" % CTX) @mock.patch("%s.servers.resource_manager.cleanup" % CTX) - def test_cleanup(self, mock_cleanup, mock_osclients): + def test_cleanup(self, mock_cleanup): tenants_count = 2 users_per_tenant = 5 diff --git a/tests/unit/contexts/nova/test_servers.py b/tests/unit/contexts/nova/test_servers.py index 966d613b..ac07980f 100755 --- a/tests/unit/contexts/nova/test_servers.py +++ b/tests/unit/contexts/nova/test_servers.py @@ -58,12 +58,9 @@ class ServerGeneratorTestCase(test.ScenarioTestCase): fakes.FakeServer(id="uuid"), fakes.FakeServer(id="uuid") ]) - @mock.patch("%s.GlanceImage.transform" % TYP, - return_value=mock.MagicMock()) - @mock.patch("%s.Flavor.transform" % TYP, return_value=mock.MagicMock()) - @mock.patch("%s.servers.osclients" % CTX, return_value=fakes.FakeClients()) - def test_setup(self, mock_osclients, mock_flavor_transform, - mock_glance_image_transform, + @mock.patch("%s.GlanceImage" % TYP) + @mock.patch("%s.Flavor" % TYP) + def test_setup(self, mock_flavor, mock_glance_image, mock_nova_scenario__boot_servers): tenants_count = 2 @@ -112,8 +109,8 @@ class ServerGeneratorTestCase(test.ScenarioTestCase): servers_ctx = servers.ServerGenerator(self.context) servers_ctx.setup() self.assertEqual(new_context, self.context) - image_id = mock_glance_image_transform.return_value - flavor_id = mock_flavor_transform.return_value + image_id = mock_glance_image.return_value.pre_process.return_value + flavor_id = mock_flavor.return_value.pre_process.return_value servers_ctx_config = self.context["config"]["servers"] expected_auto_nic = servers_ctx_config.get("auto_assign_nic", False) expected_requests = servers_ctx_config.get("servers_per_tenant", False) @@ -125,9 +122,8 @@ class ServerGeneratorTestCase(test.ScenarioTestCase): for i in range(called_times)] mock_nova_scenario__boot_servers.assert_has_calls(mock_calls) - @mock.patch("%s.servers.osclients" % CTX) @mock.patch("%s.servers.resource_manager.cleanup" % CTX) - def test_cleanup(self, mock_cleanup, mock_osclients): + def test_cleanup(self, mock_cleanup): tenants_count = 2 users_per_tenant = 5 diff --git a/tests/unit/contexts/vm/test_custom_image.py b/tests/unit/contexts/vm/test_custom_image.py index 018184ce..b33523b0 100644 --- a/tests/unit/contexts/vm/test_custom_image.py +++ b/tests/unit/contexts/vm/test_custom_image.py @@ -61,12 +61,14 @@ 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("%s.types.GlanceImage" % BASE) + @mock.patch("%s.types.Flavor" % BASE) @mock.patch("%s.vmtasks.BootRuncommandDelete" % BASE) def test_create_one_image( - self, mock_boot_runcommand_delete, mock_flavor_transform, - mock_glance_image_transform, mock_clients): + self, mock_boot_runcommand_delete, mock_flavor, + mock_glance_image, mock_clients): + mock_flavor.return_value.pre_process.return_value = "flavor" + mock_glance_image.return_value.pre_process.return_value = "image" ip = {"ip": "foo_ip", "id": "foo_id", "is_floating": True} fake_server = mock.Mock() @@ -90,12 +92,12 @@ class BaseCustomImageContextVMTestCase(test.TestCase): foo_arg="foo_value") self.assertEqual({"id": "image"}, custom_image) - mock_flavor_transform.assert_called_once_with( - clients=mock_clients.return_value, - resource_config={"name": "flavor"}) - mock_glance_image_transform.assert_called_once_with( - clients=mock_clients.return_value, - resource_config={"name": "image"}) + mock_flavor.assert_called_once_with(self.context) + mock_flavor.return_value.pre_process.assert_called_once_with( + resource_spec={"name": "flavor"}, config={}) + mock_glance_image.assert_called_once_with(self.context) + mock_glance_image.return_value.pre_process.assert_called_once_with( + resource_spec={"name": "image"}, config={}) mock_boot_runcommand_delete.assert_called_once_with( self.context, clients=mock_clients.return_value) diff --git a/tests/unit/contexts/watcher/test_audit_templates.py b/tests/unit/contexts/watcher/test_audit_templates.py index d008c8f1..e848dd98 100644 --- a/tests/unit/contexts/watcher/test_audit_templates.py +++ b/tests/unit/contexts/watcher/test_audit_templates.py @@ -16,7 +16,6 @@ import mock from rally_openstack.contexts.watcher import audit_templates from rally_openstack.scenarios.watcher import utils as watcher_utils -from tests.unit import fakes from tests.unit import test @@ -29,14 +28,9 @@ class AuditTemplateTestCase(test.ScenarioTestCase): @mock.patch("%s.utils.WatcherScenario._create_audit_template" % SCN, return_value=mock.MagicMock()) - @mock.patch("%s.WatcherStrategy.transform" % TYP, - return_value=mock.MagicMock()) - @mock.patch("%s.WatcherGoal.transform" % TYP, - return_value=mock.MagicMock()) - @mock.patch("%s.audit_templates.osclients" % CTX, - return_value=fakes.FakeClients()) - def test_setup(self, mock_osclients, mock_watcher_goal_transform, - mock_watcher_strategy_transform, + @mock.patch("%s.WatcherStrategy" % TYP,) + @mock.patch("%s.WatcherGoal" % TYP) + def test_setup(self, mock_watcher_goal, mock_watcher_strategy, mock_watcher_scenario__create_audit_template): users = [{"id": 1, "tenant_id": 1, "credential": mock.MagicMock()}] @@ -72,8 +66,9 @@ class AuditTemplateTestCase(test.ScenarioTestCase): }) audit_template = audit_templates.AuditTemplateGenerator(self.context) audit_template.setup() - goal_id = mock_watcher_goal_transform.return_value - strategy_id = mock_watcher_strategy_transform.return_value + goal_id = mock_watcher_goal.return_value.pre_process.return_value + strategy_id = ( + mock_watcher_strategy.return_value.pre_process.return_value) mock_calls = [mock.call(goal_id, strategy_id)] mock_watcher_scenario__create_audit_template.assert_has_calls( mock_calls) diff --git a/tests/unit/doc/test_docstrings.py b/tests/unit/doc/test_docstrings.py index f4e80f48..64261ce6 100644 --- a/tests/unit/doc/test_docstrings.py +++ b/tests/unit/doc/test_docstrings.py @@ -119,8 +119,6 @@ class DocstringsTestCase(test.TestCase): msg_buffer.extend(msg) if len(msg) else None def test_all_plugins_have_docstrings(self): - self.skipTest("Rally 0.12.0 changed get_doc method of ResourceType." - "The following patch fixes the issue.") msg_buffer = [] self._check_docstrings(msg_buffer) diff --git a/tests/unit/test_types.py b/tests/unit/test_types.py index 03f24700..f69dcffa 100644 --- a/tests/unit/test_types.py +++ b/tests/unit/test_types.py @@ -13,7 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import ddt import mock from rally import exceptions @@ -35,48 +34,51 @@ class FlavorTestCase(test.TestCase): id="44")) self.clients.nova().flavors._cache(fakes.FakeResource(name="m1.large", id="45")) + self.type_cls = types.Flavor( + context={"admin": {"credential": mock.Mock()}}) + self.type_cls._clients = self.clients - def test_transform_by_id(self): - resource_config = {"id": "42"} - flavor_id = types.Flavor.transform( - clients=self.clients, resource_config=resource_config) + def test_preprocess_by_id(self): + resource_spec = {"id": "42"} + flavor_id = self.type_cls.pre_process( + resource_spec=resource_spec, config={}) self.assertEqual("42", flavor_id) - def test_transform_by_name(self): - resource_config = {"name": "m1.nano"} - flavor_id = types.Flavor.transform( - clients=self.clients, resource_config=resource_config) + def test_preprocess_by_name(self): + resource_spec = {"name": "m1.nano"} + flavor_id = self.type_cls.pre_process( + resource_spec=resource_spec, config={}) self.assertEqual("42", flavor_id) - def test_transform_by_name_no_match(self): - resource_config = {"name": "m1.medium"} + def test_preprocess_by_name_no_match(self): + resource_spec = {"name": "m1.medium"} self.assertRaises(exceptions.InvalidScenarioArgument, - types.Flavor.transform, self.clients, - resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) - def test_transform_by_name_multiple_match(self): - resource_config = {"name": "m1.large"} + def test_preprocess_by_name_multiple_match(self): + resource_spec = {"name": "m1.large"} self.assertRaises(exceptions.InvalidScenarioArgument, - types.Flavor.transform, self.clients, - resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) - def test_transform_by_regex(self): - resource_config = {"regex": "m(1|2)\.nano"} - flavor_id = types.Flavor.transform( - clients=self.clients, resource_config=resource_config) + def test_preprocess_by_regex(self): + resource_spec = {"regex": "m(1|2)\.nano"} + flavor_id = self.type_cls.pre_process( + resource_spec=resource_spec, config={}) self.assertEqual("42", flavor_id) - def test_transform_by_regex_multiple_match(self): - resource_config = {"regex": "^m1"} + def test_preprocess_by_regex_multiple_match(self): + resource_spec = {"regex": "^m1"} self.assertRaises(exceptions.InvalidScenarioArgument, - types.Flavor.transform, self.clients, - resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) - def test_transform_by_regex_no_match(self): - resource_config = {} + def test_preprocess_by_regex_no_match(self): + resource_spec = {} self.assertRaises(exceptions.InvalidScenarioArgument, - types.Flavor.transform, self.clients, - resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) class EC2FlavorTestCase(test.TestCase): @@ -92,24 +94,27 @@ class EC2FlavorTestCase(test.TestCase): id="3")) self.clients.nova().flavors._cache(fakes.FakeResource(name="m1.xlarge", id="3")) + self.type_cls = types.EC2Flavor( + context={"admin": {"credential": mock.Mock()}}) + self.type_cls._clients = self.clients - def test_transform_by_name(self): - resource_config = {"name": "m1.nano"} - flavor_name = types.EC2Flavor.transform( - clients=self.clients, resource_config=resource_config) + def test_preprocess_by_name(self): + resource_spec = {"name": "m1.nano"} + flavor_name = self.type_cls.pre_process( + resource_spec=resource_spec, config={}) self.assertEqual("m1.nano", flavor_name) - def test_transform_by_id(self): - resource_config = {"id": "2"} - flavor_name = types.EC2Flavor.transform( - clients=self.clients, resource_config=resource_config) + def test_preprocess_by_id(self): + resource_spec = {"id": "2"} + flavor_name = self.type_cls.pre_process( + resource_spec=resource_spec, config={}) self.assertEqual("m1.nano", flavor_name) - def test_transform_by_id_no_match(self): - resource_config = {"id": "4"} + def test_preprocess_by_id_no_match(self): + resource_spec = {"id": "4"} self.assertRaises(exceptions.InvalidScenarioArgument, - types.EC2Flavor.transform, self.clients, - resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) class GlanceImageTestCase(test.TestCase): @@ -127,65 +132,73 @@ class GlanceImageTestCase(test.TestCase): image4 = fakes.FakeResource(name="cirros-0.3.4-uec-ramdisk-copy", id="103") self.clients.glance().images._cache(image4) + self.type_cls = types.GlanceImage( + context={"admin": {"credential": mock.Mock()}}) + self.type_cls._clients = self.clients - def test_transform_by_id(self): - resource_config = {"id": "100"} - image_id = types.GlanceImage.transform( - clients=self.clients, resource_config=resource_config) + def test_preprocess_by_id(self): + resource_spec = {"id": "100"} + image_id = self.type_cls.pre_process( + resource_spec=resource_spec, config={}) self.assertEqual("100", image_id) - def test_transform_by_name(self): - resource_config = {"name": "^cirros-0.3.4-uec$"} - image_id = types.GlanceImage.transform( - clients=self.clients, resource_config=resource_config) + def test_preprocess_by_name(self): + resource_spec = {"name": "^cirros-0.3.4-uec$"} + image_id = self.type_cls.pre_process( + resource_spec=resource_spec, config={}) self.assertEqual("100", image_id) - def test_transform_by_name_no_match(self): - resource_config = {"name": "cirros-0.3.4-uec-boot"} + def test_preprocess_by_name_no_match(self): + resource_spec = {"name": "cirros-0.3.4-uec-boot"} self.assertRaises(exceptions.InvalidScenarioArgument, - types.GlanceImage.transform, self.clients, - resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) - def test_transform_by_name_match_multiple(self): - resource_config = {"name": "cirros-0.3.4-uec-ramdisk-copy"} + def test_preprocess_by_name_match_multiple(self): + resource_spec = {"name": "cirros-0.3.4-uec-ramdisk-copy"} self.assertRaises(exceptions.InvalidScenarioArgument, - types.GlanceImage.transform, self.clients, - resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) - def test_transform_by_regex(self): - resource_config = {"regex": "-uec$"} - image_id = types.GlanceImage.transform( - clients=self.clients, resource_config=resource_config) + def test_preprocess_by_regex(self): + resource_spec = {"regex": "-uec$"} + image_id = self.type_cls.pre_process( + resource_spec=resource_spec, config={}) self.assertEqual("100", image_id) - def test_transform_by_regex_match_multiple(self): - resource_config = {"regex": "^cirros"} + def test_preprocess_by_regex_match_multiple(self): + resource_spec = {"regex": "^cirros"} self.assertRaises(exceptions.InvalidScenarioArgument, - types.GlanceImage.transform, self.clients, - resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) - def test_transform_by_regex_no_match(self): - resource_config = {"regex": "-boot$"} + def test_preprocess_by_regex_no_match(self): + resource_spec = {"regex": "-boot$"} self.assertRaises(exceptions.InvalidScenarioArgument, - types.GlanceImage.transform, self.clients, - resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) class GlanceImageArgsTestCase(test.TestCase): - def test_transform(self): - self.assertEqual({}, types.GlanceImageArguments.transform( - clients=None, resource_config={})) + def test_preprocess(self): self.assertEqual( - {"visibility": "public"}, types.GlanceImageArguments.transform( - clients=None, resource_config={"visibility": "public"})) + {}, + types.GlanceImageArguments({}).pre_process( + resource_spec={}, config={})) self.assertEqual( - {"visibility": "public"}, types.GlanceImageArguments.transform( - clients=None, resource_config={"visibility": "public", - "is_public": False})) + {"visibility": "public"}, + types.GlanceImageArguments({}).pre_process( + config={}, resource_spec={"visibility": "public"})) self.assertEqual( - {"visibility": "private"}, types.GlanceImageArguments.transform( - clients=None, resource_config={"is_public": False})) + {"visibility": "public"}, + types.GlanceImageArguments({}).pre_process( + config={}, resource_spec={"visibility": "public", + "is_public": False})) + self.assertEqual( + {"visibility": "private"}, + types.GlanceImageArguments({}).pre_process( + config={}, resource_spec={"is_public": False})) class EC2ImageTestCase(test.TestCase): @@ -215,92 +228,102 @@ class EC2ImageTestCase(test.TestCase): self.clients.ec2().get_all_images = mock.Mock( return_value=[ec2_image1, ec2_image2, ec2_image3, ec2_image4]) - def test_transform_by_name(self): - resource_config = {"name": "^cirros-0.3.4-uec$"} - ec2_image_id = types.EC2Image.transform( - clients=self.clients, resource_config=resource_config) + self.type_cls = types.EC2Image( + context={"admin": {"credential": mock.Mock()}}) + self.type_cls._clients = self.clients + + def test_preprocess_by_name(self): + resource_spec = {"name": "^cirros-0.3.4-uec$"} + ec2_image_id = self.type_cls.pre_process(resource_spec=resource_spec, + config={}) self.assertEqual("200", ec2_image_id) - def test_transform_by_id(self): - resource_config = {"id": "100"} - ec2_image_id = types.EC2Image.transform( - clients=self.clients, resource_config=resource_config) + def test_preprocess_by_id(self): + resource_spec = {"id": "100"} + ec2_image_id = self.type_cls.pre_process(resource_spec=resource_spec, + config={}) self.assertEqual("200", ec2_image_id) - def test_transform_by_id_no_match(self): - resource_config = {"id": "101"} + def test_preprocess_by_id_no_match(self): + resource_spec = {"id": "101"} self.assertRaises(exceptions.InvalidScenarioArgument, - types.EC2Image.transform, self.clients, - resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) - def test_transform_by_name_no_match(self): - resource_config = {"name": "cirros-0.3.4-uec-boot"} + def test_preprocess_by_name_no_match(self): + resource_spec = {"name": "cirros-0.3.4-uec-boot"} self.assertRaises(exceptions.InvalidScenarioArgument, - types.EC2Image.transform, self.clients, - resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) - def test_transform_by_name_match_multiple(self): - resource_config = {"name": "cirros-0.3.4-uec-ramdisk-copy"} + def test_preprocess_by_name_match_multiple(self): + resource_spec = {"name": "cirros-0.3.4-uec-ramdisk-copy"} self.assertRaises(exceptions.InvalidScenarioArgument, - types.EC2Image.transform, self.clients, - resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) - def test_transform_by_regex(self): - resource_config = {"regex": "-uec$"} - ec2_image_id = types.EC2Image.transform( - clients=self.clients, resource_config=resource_config) + def test_preprocess_by_regex(self): + resource_spec = {"regex": "-uec$"} + ec2_image_id = self.type_cls.pre_process(resource_spec=resource_spec, + config={}) self.assertEqual("200", ec2_image_id) - def test_transform_by_regex_match_multiple(self): - resource_config = {"regex": "^cirros"} + def test_preprocess_by_regex_match_multiple(self): + resource_spec = {"regex": "^cirros"} self.assertRaises(exceptions.InvalidScenarioArgument, - types.EC2Image.transform, self.clients, - resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) - def test_transform_by_regex_no_match(self): - resource_config = {"regex": "-boot$"} + def test_preprocess_by_regex_no_match(self): + resource_spec = {"regex": "-boot$"} self.assertRaises(exceptions.InvalidScenarioArgument, - types.EC2Image.transform, self.clients, - resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) class VolumeTypeTestCase(test.TestCase): def setUp(self): super(VolumeTypeTestCase, self).setUp() - self.clients = fakes.FakeClients() + cinder = mock.patch("rally_openstack.types.block.BlockStorage") + self.service = cinder.start().return_value + self.addCleanup(cinder.stop) + volume_type1 = fakes.FakeResource(name="lvmdriver-1", id=100) - self.clients.cinder().volume_types._cache(volume_type1) - def test_transform_by_id(self): - resource_config = {"id": 100} - volumetype_id = types.VolumeType.transform( - clients=self.clients, resource_config=resource_config) + self.type_cls = types.VolumeType( + context={"admin": {"credential": mock.Mock()}}) + self.service.list_types.return_value = [volume_type1] + + def test_preprocess_by_id(self): + resource_spec = {"id": 100} + volumetype_id = self.type_cls.pre_process(resource_spec=resource_spec, + config={}) self.assertEqual(100, volumetype_id) - def test_transform_by_name(self): - resource_config = {"name": "lvmdriver-1"} - volumetype_id = types.VolumeType.transform( - clients=self.clients, resource_config=resource_config) + def test_preprocess_by_name(self): + resource_spec = {"name": "lvmdriver-1"} + volumetype_id = self.type_cls.pre_process(resource_spec=resource_spec, + config={}) self.assertEqual(100, volumetype_id) - def test_transform_by_name_no_match(self): - resource_config = {"name": "nomatch-1"} + def test_preprocess_by_name_no_match(self): + resource_spec = {"name": "nomatch-1"} self.assertRaises(exceptions.InvalidScenarioArgument, - types.VolumeType.transform, - self.clients, resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) - def test_transform_by_regex(self): - resource_config = {"regex": "^lvm.*-1"} - volumetype_id = types.VolumeType.transform( - clients=self.clients, resource_config=resource_config) + def test_preprocess_by_regex(self): + resource_spec = {"regex": "^lvm.*-1"} + volumetype_id = self.type_cls.pre_process(resource_spec=resource_spec, + config={}) self.assertEqual(100, volumetype_id) - def test_transform_by_regex_no_match(self): - resource_config = {"regex": "dd"} + def test_preprocess_by_regex_no_match(self): + resource_spec = {"regex": "dd"} self.assertRaises(exceptions.InvalidScenarioArgument, - types.VolumeType.transform, - self.clients, resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) class NeutronNetworkTestCase(test.TestCase): @@ -313,27 +336,29 @@ class NeutronNetworkTestCase(test.TestCase): }} network1 = self.clients.neutron().create_network(net1_data) self.net1_id = network1["network"]["id"] + self.type_cls = types.NeutronNetwork( + context={"admin": {"credential": mock.Mock()}}) + self.type_cls._clients = self.clients - def test_transform_by_id(self): - resource_config = {"id": self.net1_id} - network_id = types.NeutronNetwork.transform( - clients=self.clients, resource_config=resource_config) + def test_preprocess_by_id(self): + resource_spec = {"id": self.net1_id} + network_id = self.type_cls.pre_process(resource_spec=resource_spec, + config={}) self.assertEqual(network_id, self.net1_id) - def test_transform_by_name(self): - resource_config = {"name": "net1"} - network_id = types.NeutronNetwork.transform( - clients=self.clients, resource_config=resource_config) + def test_preprocess_by_name(self): + resource_spec = {"name": "net1"} + network_id = self.type_cls.pre_process(resource_spec=resource_spec, + config={}) self.assertEqual(network_id, self.net1_id) - def test_transform_by_name_no_match(self): - resource_config = {"name": "nomatch-1"} + def test_preprocess_by_name_no_match(self): + resource_spec = {"name": "nomatch-1"} self.assertRaises(exceptions.InvalidScenarioArgument, - types.NeutronNetwork.transform, - self.clients, resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) -@ddt.ddt class WatcherStrategyTestCase(test.TestCase): def setUp(self): @@ -342,22 +367,23 @@ class WatcherStrategyTestCase(test.TestCase): self.strategy = self.clients.watcher().strategy._cache( fakes.FakeResource(name="dummy", id="1")) - @ddt.data({"resource_config": {"name": "dummy"}}) - @ddt.unpack - def test_transform_by_name(self, resource_config=None): - strategy_id = types.WatcherStrategy.transform(self.clients, - resource_config) + self.type_cls = types.WatcherStrategy( + context={"admin": {"credential": mock.Mock()}}) + self.type_cls._clients = self.clients + + def test_preprocess_by_name(self): + resource_spec = {"name": "dummy"} + strategy_id = self.type_cls.pre_process(resource_spec=resource_spec, + config={}) self.assertEqual(self.strategy.uuid, strategy_id) - @ddt.data({"resource_config": {"name": "dummy-1"}}) - @ddt.unpack - def test_transform_by_name_no_match(self, resource_config=None): + def test_preprocess_by_name_no_match(self): + resource_spec = {"name": "dummy-1"} self.assertRaises(exceptions.RallyException, - types.WatcherStrategy.transform, - self.clients, resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) -@ddt.ddt class WatcherGoalTestCase(test.TestCase): def setUp(self): @@ -365,17 +391,18 @@ class WatcherGoalTestCase(test.TestCase): self.clients = fakes.FakeClients() self.goal = self.clients.watcher().goal._cache( fakes.FakeResource(name="dummy", id="1")) + self.type_cls = types.WatcherGoal( + context={"admin": {"credential": mock.Mock()}}) + self.type_cls._clients = self.clients - @ddt.data({"resource_config": {"name": "dummy"}}) - @ddt.unpack - def test_transform_by_name(self, resource_config=None): - goal_id = types.WatcherGoal.transform(self.clients, - resource_config) + def test_preprocess_by_name(self): + resource_spec = {"name": "dummy"} + goal_id = self.type_cls.pre_process(resource_spec=resource_spec, + config={}) self.assertEqual(self.goal.uuid, goal_id) - @ddt.data({"resource_config": {"name": "dummy-1"}}) - @ddt.unpack - def test_transform_by_name_no_match(self, resource_config=None): + def test_preprocess_by_name_no_match(self): + resource_spec = {"name": "dummy-1"} self.assertRaises(exceptions.RallyException, - types.WatcherGoal.transform, - self.clients, resource_config) + self.type_cls.pre_process, + resource_spec=resource_spec, config={}) diff --git a/tests/unit/test_validators.py b/tests/unit/test_validators.py index 50515257..36362539 100644 --- a/tests/unit/test_validators.py +++ b/tests/unit/test_validators.py @@ -109,9 +109,9 @@ class ImageExistsValidatorTestCase(test.TestCase): self.validator.validate(self.context, config, None, None) - @mock.patch("%s.openstack_types.GlanceImage.transform" % PATH, - return_value="image_id") - def test_validator_image_not_in_context(self, mock_glance_image_transform): + @mock.patch("%s.openstack_types.GlanceImage" % PATH) + def test_validator_image_not_in_context(self, mock_glance_image): + mock_glance_image.return_value.pre_process.return_value = "image_id" config = { "args": {"image": "fake_image"}, "contexts": { @@ -124,8 +124,11 @@ class ImageExistsValidatorTestCase(test.TestCase): result = self.validator.validate(self.context, config, None, None) self.assertIsNone(result) - mock_glance_image_transform.assert_called_once_with( - clients=clients, resource_config=config["args"]["image"]) + mock_glance_image.assert_called_once_with( + context={"admin": { + "credential": self.context["users"][0]["credential"]}}) + mock_glance_image.return_value.pre_process.assert_called_once_with( + config["args"]["image"], config={}) clients.glance().images.get.assert_called_with("image_id") exs = [exceptions.InvalidScenarioArgument(), @@ -238,9 +241,9 @@ class FlavorExistsValidatorTestCase(test.TestCase): self.assertEqual("Parameter foo_flavor is not specified.", e.message) - @mock.patch("%s.openstack_types.Flavor.transform" % PATH, - return_value="flavor_id") - def test__get_validated_flavor(self, mock_flavor_transform): + @mock.patch("%s.openstack_types.Flavor" % PATH) + def test__get_validated_flavor(self, mock_flavor): + mock_flavor.return_value.pre_process.return_value = "flavor_id" clients = mock.Mock() clients.nova().flavors.get.return_value = "flavor" @@ -250,20 +253,26 @@ class FlavorExistsValidatorTestCase(test.TestCase): "flavor") self.assertEqual("flavor", result) - mock_flavor_transform.assert_called_once_with( - clients=clients, resource_config=self.config["args"]["flavor"]) + mock_flavor.assert_called_once_with( + context={"admin": {"credential": clients.credential}} + ) + mock_flavor_obj = mock_flavor.return_value + mock_flavor_obj.pre_process.assert_called_once_with( + self.config["args"]["flavor"], config={}) clients.nova().flavors.get.assert_called_once_with(flavor="flavor_id") + mock_flavor_obj.pre_process.reset_mock() clients.side_effect = exceptions.InvalidScenarioArgument("") result = self.validator._get_validated_flavor( self.config, clients, "flavor") self.assertEqual("flavor", result) - mock_flavor_transform.assert_called_with( - clients=clients, resource_config=self.config["args"]["flavor"]) + mock_flavor_obj.pre_process.assert_called_once_with( + self.config["args"]["flavor"], config={}) clients.nova().flavors.get.assert_called_with(flavor="flavor_id") - @mock.patch("%s.openstack_types.Flavor.transform" % PATH) - def test__get_validated_flavor_not_found(self, mock_flavor_transform): + @mock.patch("%s.openstack_types.Flavor" % PATH) + def test__get_validated_flavor_not_found(self, mock_flavor): + mock_flavor.return_value.pre_process.return_value = "flavor_id" clients = mock.MagicMock() clients.nova().flavors.get.side_effect = nova_exc.NotFound("") @@ -275,8 +284,9 @@ class FlavorExistsValidatorTestCase(test.TestCase): self.assertEqual("Flavor '%s' not found" % self.config["args"]["flavor"], e.message) - mock_flavor_transform.assert_called_once_with( - clients=clients, resource_config=self.config["args"]["flavor"]) + mock_flavor_obj = mock_flavor.return_value + mock_flavor_obj.pre_process.assert_called_once_with( + self.config["args"]["flavor"], config={}) @mock.patch("%s.types.obj_from_name" % PATH) @mock.patch("%s.flavors_ctx.FlavorConfig" % PATH) @@ -465,9 +475,9 @@ class ImageValidOnFlavorValidatorTestCase(test.TestCase): self.assertEqual(expected_e, actual_e) - @mock.patch("%s.openstack_types.GlanceImage.transform" % PATH, - return_value="image_id") - def test__get_validated_image(self, mock_glance_image_transform): + @mock.patch("%s.openstack_types.GlanceImage" % PATH) + def test__get_validated_image(self, mock_glance_image): + mock_glance_image.return_value.pre_process.return_value = "image_id" image = { "size": 0, "min_ram": 0, @@ -491,14 +501,15 @@ class ImageValidOnFlavorValidatorTestCase(test.TestCase): clients, "image") self.assertEqual(image, result) - mock_glance_image_transform.assert_called_once_with( - clients=clients, resource_config=self.config["args"]["image"]) + mock_glance_image.assert_called_once_with( + context={"admin": {"credential": clients.credential}}) + mock_glance_image.return_value.pre_process.assert_called_once_with( + config["args"]["image"], config={}) clients.glance().images.get.assert_called_with("image_id") - @mock.patch("%s.openstack_types.GlanceImage.transform" % PATH, - return_value="image_id") - def test__get_validated_image_incorrect_param(self, - mock_glance_image_transform): + @mock.patch("%s.openstack_types.GlanceImage" % PATH) + def test__get_validated_image_incorrect_param(self, mock_glance_image): + mock_glance_image.return_value.pre_process.return_value = "image_id" # Wrong 'param_name' e = self.assertRaises( validators.validation.ValidationError, @@ -521,16 +532,16 @@ class ImageValidOnFlavorValidatorTestCase(test.TestCase): result = self.validator._get_validated_image(config, clients, "image") self.assertEqual(image, result) - mock_glance_image_transform.assert_called_once_with( - clients=clients, resource_config=config["args"]["image"]) + mock_glance_image.assert_called_once_with( + context={"admin": {"credential": clients.credential}}) + mock_glance_image.return_value.pre_process.assert_called_once_with( + config["args"]["image"], config={}) clients.glance().images.get.assert_called_with("image_id") - @mock.patch("%s.openstack_types.GlanceImage.transform" % PATH, - return_value="image_id") - def test__get_validated_image_exceptions(self, - mock_glance_image_transform): + @mock.patch("%s.openstack_types.GlanceImage" % PATH) + def test__get_validated_image_exceptions(self, mock_glance_image): + mock_glance_image.return_value.pre_process.return_value = "image_id" clients = mock.Mock() - clients.glance().images.get.return_value = "image" clients.glance().images.get.side_effect = glance_exc.HTTPNotFound("") e = self.assertRaises( validators.validation.ValidationError, @@ -538,9 +549,13 @@ class ImageValidOnFlavorValidatorTestCase(test.TestCase): config, clients, "image") self.assertEqual("Image '%s' not found" % config["args"]["image"], e.message) - mock_glance_image_transform.assert_called_once_with( - clients=clients, resource_config=config["args"]["image"]) + + mock_glance_image.assert_called_once_with( + context={"admin": {"credential": clients.credential}}) + mock_glance_image.return_value.pre_process.assert_called_once_with( + config["args"]["image"], config={}) clients.glance().images.get.assert_called_with("image_id") + mock_glance_image.return_value.pre_process.reset_mock() clients.side_effect = exceptions.InvalidScenarioArgument("") e = self.assertRaises( @@ -548,8 +563,8 @@ class ImageValidOnFlavorValidatorTestCase(test.TestCase): self.validator._get_validated_image, config, clients, "image") self.assertEqual("Image '%s' not found" % config["args"]["image"], e.message) - mock_glance_image_transform.assert_called_with( - clients=clients, resource_config=config["args"]["image"]) + mock_glance_image.return_value.pre_process.assert_called_once_with( + config["args"]["image"], config={}) clients.glance().images.get.assert_called_with("image_id")