Convert ResourceType classes to plugins
This also deprecates the old ResourceType classes in rally.task.types, and also deprecates types.set() in favor of types.convert(). Existing scenarios will be switched over to the new usage in a subsequent commit. Change-Id: Iacbf812f87d3c65617b2e7a992c42b8673778d6a Implements: blueprint pluggable-types
This commit is contained in:
parent
9b30925590
commit
7392ec8e91
87
rally/plugins/common/types.py
Normal file
87
rally/plugins/common/types.py
Normal file
@ -0,0 +1,87 @@
|
||||
# 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 os
|
||||
|
||||
import requests
|
||||
|
||||
from rally.common.plugin import plugin
|
||||
from rally import exceptions
|
||||
from rally.task import types
|
||||
|
||||
|
||||
@plugin.configure(name="path_or_url")
|
||||
class PathOrUrl(types.ResourceType):
|
||||
|
||||
@classmethod
|
||||
def transform(cls, clients, resource_config):
|
||||
"""Check whether file exists or url available.
|
||||
|
||||
:param clients: openstack admin client handles
|
||||
:param resource_config: path or url
|
||||
|
||||
:returns: url or expanded file path
|
||||
"""
|
||||
|
||||
path = os.path.expanduser(resource_config)
|
||||
if os.path.isfile(path):
|
||||
return path
|
||||
try:
|
||||
head = requests.head(path)
|
||||
if head.status_code == 200:
|
||||
return path
|
||||
raise exceptions.InvalidScenarioArgument(
|
||||
"Url %s unavailable (code %s)" % (path, head.status_code))
|
||||
except Exception as ex:
|
||||
raise exceptions.InvalidScenarioArgument(
|
||||
"Url error %s (%s)" % (path, ex))
|
||||
|
||||
|
||||
@plugin.configure(name="file")
|
||||
class FileType(types.ResourceType):
|
||||
|
||||
@classmethod
|
||||
def transform(cls, clients, resource_config):
|
||||
"""Return content of the file by its path.
|
||||
|
||||
:param clients: openstack admin client handles
|
||||
:param resource_config: path to file
|
||||
|
||||
:returns: content of the file
|
||||
"""
|
||||
|
||||
with open(os.path.expanduser(resource_config), "r") as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
@plugin.configure(name="file_dict")
|
||||
class FileTypeDict(types.ResourceType):
|
||||
|
||||
@classmethod
|
||||
def transform(cls, clients, resource_config):
|
||||
"""Return the dictionary of items with file path and file content.
|
||||
|
||||
:param clients: openstack admin client handles
|
||||
:param resource_config: list of file paths
|
||||
|
||||
:returns: dictionary {file_path: file_content, ...}
|
||||
"""
|
||||
|
||||
file_type_dict = {}
|
||||
for file_path in resource_config:
|
||||
file_path = os.path.expanduser(file_path)
|
||||
with open(file_path, "r") as f:
|
||||
file_type_dict[file_path] = f.read()
|
||||
|
||||
return file_type_dict
|
168
rally/plugins/openstack/types.py
Normal file
168
rally/plugins/openstack/types.py
Normal file
@ -0,0 +1,168 @@
|
||||
# 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.common.plugin import plugin
|
||||
from rally import exceptions
|
||||
from rally.task import types
|
||||
|
||||
|
||||
@plugin.configure(name="nova_flavor")
|
||||
class Flavor(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")
|
||||
if not resource_id:
|
||||
novaclient = clients.nova()
|
||||
resource_id = types._id_from_name(
|
||||
resource_config=resource_config,
|
||||
resources=novaclient.flavors.list(),
|
||||
typename="flavor")
|
||||
return resource_id
|
||||
|
||||
|
||||
@plugin.configure(name="ec2_flavor")
|
||||
class EC2Flavor(types.ResourceType):
|
||||
|
||||
@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")
|
||||
if not resource_name:
|
||||
# NOTE(wtakase): gets resource name from OpenStack id
|
||||
novaclient = clients.nova()
|
||||
resource_name = types._name_from_id(
|
||||
resource_config=resource_config,
|
||||
resources=novaclient.flavors.list(),
|
||||
typename="flavor")
|
||||
return resource_name
|
||||
|
||||
|
||||
@plugin.configure(name="glance_image")
|
||||
class GlanceImage(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")
|
||||
if not resource_id:
|
||||
glanceclient = clients.glance()
|
||||
resource_id = types._id_from_name(
|
||||
resource_config=resource_config,
|
||||
resources=list(glanceclient.images.list()),
|
||||
typename="image")
|
||||
return resource_id
|
||||
|
||||
|
||||
@plugin.configure(name="ec2_image")
|
||||
class EC2Image(types.ResourceType):
|
||||
|
||||
@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:
|
||||
# NOTE(wtakase): gets resource name from OpenStack id
|
||||
glanceclient = clients.glance()
|
||||
resource_name = types._name_from_id(
|
||||
resource_config=resource_config,
|
||||
resources=list(glanceclient.images.list()),
|
||||
typename="image")
|
||||
resource_config["name"] = resource_name
|
||||
|
||||
# NOTE(wtakase): gets EC2 resource id from name or regex
|
||||
ec2client = clients.ec2()
|
||||
resource_ec2_id = types._id_from_name(
|
||||
resource_config=resource_config,
|
||||
resources=list(ec2client.get_all_images()),
|
||||
typename="ec2_image")
|
||||
return resource_ec2_id
|
||||
|
||||
|
||||
@plugin.configure(name="cinder_volume_type")
|
||||
class VolumeType(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")
|
||||
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")
|
||||
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")
|
||||
if resource_id:
|
||||
return resource_id
|
||||
else:
|
||||
neutronclient = clients.neutron()
|
||||
for net in neutronclient.list_networks()["networks"]:
|
||||
if net["name"] == resource_config.get("name"):
|
||||
return net["id"]
|
||||
|
||||
raise exceptions.InvalidScenarioArgument(
|
||||
"Neutron network with name '{name}' not found".format(
|
||||
name=resource_config.get("name")))
|
@ -21,11 +21,17 @@ import re
|
||||
|
||||
import requests
|
||||
|
||||
from rally.common.i18n import _
|
||||
from rally.common import logging
|
||||
from rally.common.plugin import plugin
|
||||
from rally import exceptions
|
||||
from rally import osclients
|
||||
from rally.task import scenario
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@logging.log_deprecated("Use types.convert() instead", "0.3.2")
|
||||
def set(**kwargs):
|
||||
"""Decorator to define resource transformation(s) on scenario parameters.
|
||||
|
||||
@ -119,7 +125,21 @@ def preprocess(name, context, args):
|
||||
return processed_args
|
||||
|
||||
|
||||
class ResourceType(object):
|
||||
class ResourceType(plugin.Plugin):
|
||||
|
||||
@classmethod
|
||||
@abc.abstractmethod
|
||||
def transform(cls, clients, resource_config):
|
||||
"""Transform the resource.
|
||||
|
||||
:param clients: openstack admin client handles
|
||||
:param resource_config: scenario config of resource
|
||||
|
||||
:returns: transformed value of resource
|
||||
"""
|
||||
|
||||
|
||||
class DeprecatedResourceType(object):
|
||||
|
||||
@classmethod
|
||||
@abc.abstractmethod
|
||||
@ -253,9 +273,24 @@ def _name_from_id(resource_config, resources, typename):
|
||||
return obj_from_id(resource_config, resources, typename).name
|
||||
|
||||
|
||||
class FlavorResourceType(ResourceType):
|
||||
def log_deprecated_resource_type(func):
|
||||
"""Decorator that logs use of deprecated resource type classes.
|
||||
|
||||
This should only be used on the transform() function.
|
||||
"""
|
||||
def inner(cls, clients, resource_config):
|
||||
LOG.warning(_("%s is deprecated in Rally v0.3.2; use the "
|
||||
"equivalent resource plugin name instead") %
|
||||
cls.__name__)
|
||||
return func(cls, clients, resource_config)
|
||||
|
||||
return inner
|
||||
|
||||
|
||||
class FlavorResourceType(DeprecatedResourceType):
|
||||
|
||||
@classmethod
|
||||
@log_deprecated_resource_type
|
||||
def transform(cls, clients, resource_config):
|
||||
"""Transform the resource config to id.
|
||||
|
||||
@ -273,9 +308,10 @@ class FlavorResourceType(ResourceType):
|
||||
return resource_id
|
||||
|
||||
|
||||
class EC2FlavorResourceType(ResourceType):
|
||||
class EC2FlavorResourceType(DeprecatedResourceType):
|
||||
|
||||
@classmethod
|
||||
@log_deprecated_resource_type
|
||||
def transform(cls, clients, resource_config):
|
||||
"""Transform the resource config to name.
|
||||
|
||||
@ -297,9 +333,10 @@ class EC2FlavorResourceType(ResourceType):
|
||||
return resource_name
|
||||
|
||||
|
||||
class ImageResourceType(ResourceType):
|
||||
class ImageResourceType(DeprecatedResourceType):
|
||||
|
||||
@classmethod
|
||||
@log_deprecated_resource_type
|
||||
def transform(cls, clients, resource_config):
|
||||
"""Transform the resource config to id.
|
||||
|
||||
@ -318,9 +355,10 @@ class ImageResourceType(ResourceType):
|
||||
return resource_id
|
||||
|
||||
|
||||
class EC2ImageResourceType(ResourceType):
|
||||
class EC2ImageResourceType(DeprecatedResourceType):
|
||||
|
||||
@classmethod
|
||||
@log_deprecated_resource_type
|
||||
def transform(cls, clients, resource_config):
|
||||
"""Transform the resource config to EC2 id.
|
||||
|
||||
@ -350,9 +388,10 @@ class EC2ImageResourceType(ResourceType):
|
||||
return resource_ec2_id
|
||||
|
||||
|
||||
class VolumeTypeResourceType(ResourceType):
|
||||
class VolumeTypeResourceType(DeprecatedResourceType):
|
||||
|
||||
@classmethod
|
||||
@log_deprecated_resource_type
|
||||
def transform(cls, clients, resource_config):
|
||||
"""Transform the resource config to id.
|
||||
|
||||
@ -371,9 +410,10 @@ class VolumeTypeResourceType(ResourceType):
|
||||
return resource_id
|
||||
|
||||
|
||||
class NeutronNetworkResourceType(ResourceType):
|
||||
class NeutronNetworkResourceType(DeprecatedResourceType):
|
||||
|
||||
@classmethod
|
||||
@log_deprecated_resource_type
|
||||
def transform(cls, clients, resource_config):
|
||||
"""Transform the resource config to id.
|
||||
|
||||
@ -396,9 +436,10 @@ class NeutronNetworkResourceType(ResourceType):
|
||||
name=resource_config.get("name")))
|
||||
|
||||
|
||||
class FilePathOrUrlType(ResourceType):
|
||||
class FilePathOrUrlType(DeprecatedResourceType):
|
||||
|
||||
@classmethod
|
||||
@log_deprecated_resource_type
|
||||
def transform(cls, clients, resource_config):
|
||||
"""Check whether file exists or url available.
|
||||
|
||||
@ -422,9 +463,10 @@ class FilePathOrUrlType(ResourceType):
|
||||
"Url error %s (%s)" % (path, ex))
|
||||
|
||||
|
||||
class FileType(ResourceType):
|
||||
class FileType(DeprecatedResourceType):
|
||||
|
||||
@classmethod
|
||||
@log_deprecated_resource_type
|
||||
def transform(cls, clients, resource_config):
|
||||
"""Return content of the file by its path.
|
||||
|
||||
@ -438,9 +480,10 @@ class FileType(ResourceType):
|
||||
return f.read()
|
||||
|
||||
|
||||
class FileTypeDict(ResourceType):
|
||||
class FileTypeDict(DeprecatedResourceType):
|
||||
|
||||
@classmethod
|
||||
@log_deprecated_resource_type
|
||||
def transform(cls, clients, resource_config):
|
||||
"""Return the dictionary of items with file path and file content.
|
||||
|
||||
|
89
tests/unit/plugins/common/test_types.py
Normal file
89
tests/unit/plugins/common/test_types.py
Normal file
@ -0,0 +1,89 @@
|
||||
# Copyright (C) 2014 Yahoo! Inc. All Rights Reserved.
|
||||
# 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 mock
|
||||
|
||||
from rally import exceptions
|
||||
from rally.plugins.common import types
|
||||
from tests.unit import test
|
||||
|
||||
|
||||
class PathOrUrlTestCase(test.TestCase):
|
||||
|
||||
@mock.patch("os.path.isfile")
|
||||
@mock.patch("requests.head")
|
||||
def test_transform_file(self, mock_requests_head, mock_isfile):
|
||||
mock_isfile.return_value = True
|
||||
path = types.PathOrUrl.transform(None, "fake_path")
|
||||
self.assertEqual("fake_path", path)
|
||||
|
||||
@mock.patch("os.path.isfile")
|
||||
@mock.patch("requests.head")
|
||||
def test_transform_bogus(self, mock_requests_head, mock_isfile):
|
||||
mock_isfile.return_value = False
|
||||
mock_requests_head.return_value = mock.Mock(status_code=500)
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.PathOrUrl.transform,
|
||||
None, "fake_path")
|
||||
mock_requests_head.assert_called_once_with("fake_path")
|
||||
|
||||
@mock.patch("os.path.isfile")
|
||||
@mock.patch("requests.head")
|
||||
def test_transform_url(self, mock_requests_head, mock_isfile):
|
||||
mock_isfile.return_value = False
|
||||
mock_requests_head.return_value = mock.Mock(status_code=200)
|
||||
path = types.PathOrUrl.transform(None, "fake_url")
|
||||
self.assertEqual("fake_url", path)
|
||||
|
||||
|
||||
class FileTypeTestCase(test.TestCase):
|
||||
|
||||
@mock.patch("six.moves.builtins.open",
|
||||
side_effect=mock.mock_open(read_data="file_context"),
|
||||
create=True)
|
||||
def test_transform_by_path(self, mock_open):
|
||||
resource_config = "file.yaml"
|
||||
file_context = types.FileType.transform(
|
||||
clients=None, resource_config=resource_config)
|
||||
self.assertEqual(file_context, "file_context")
|
||||
|
||||
@mock.patch("six.moves.builtins.open", side_effect=IOError, create=True)
|
||||
def test_transform_by_path_no_match(self, mock_open):
|
||||
resource_config = "nonexistant.yaml"
|
||||
self.assertRaises(IOError,
|
||||
types.FileType.transform,
|
||||
clients=None,
|
||||
resource_config=resource_config)
|
||||
|
||||
|
||||
class FileTypeDictTestCase(test.TestCase):
|
||||
|
||||
@mock.patch("six.moves.builtins.open",
|
||||
side_effect=mock.mock_open(read_data="file_context"),
|
||||
create=True)
|
||||
def test_transform_by_path(self, mock_open):
|
||||
resource_config = ["file.yaml"]
|
||||
file_context = types.FileTypeDict.transform(
|
||||
clients=None,
|
||||
resource_config=resource_config)
|
||||
self.assertEqual(file_context, {"file.yaml": "file_context"})
|
||||
|
||||
@mock.patch("six.moves.builtins.open", side_effect=IOError, create=True)
|
||||
def test_transform_by_path_no_match(self, mock_open):
|
||||
resource_config = ["nonexistant.yaml"]
|
||||
self.assertRaises(IOError,
|
||||
types.FileTypeDict.transform,
|
||||
clients=None,
|
||||
resource_config=resource_config)
|
327
tests/unit/plugins/openstack/test_types.py
Normal file
327
tests/unit/plugins/openstack/test_types.py
Normal file
@ -0,0 +1,327 @@
|
||||
# Copyright (C) 2014 Yahoo! Inc. All Rights Reserved.
|
||||
# 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 mock
|
||||
|
||||
from rally import exceptions
|
||||
from rally.plugins.openstack import types
|
||||
from tests.unit import fakes
|
||||
from tests.unit import test
|
||||
|
||||
|
||||
class FlavorTestCase(test.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(FlavorTestCase, self).setUp()
|
||||
self.clients = fakes.FakeClients()
|
||||
self.clients.nova().flavors._cache(fakes.FakeResource(name="m1.tiny",
|
||||
id="1"))
|
||||
self.clients.nova().flavors._cache(fakes.FakeResource(name="m1.nano",
|
||||
id="42"))
|
||||
self.clients.nova().flavors._cache(fakes.FakeResource(name="m1.large",
|
||||
id="44"))
|
||||
self.clients.nova().flavors._cache(fakes.FakeResource(name="m1.large",
|
||||
id="45"))
|
||||
|
||||
def test_transform_by_id(self):
|
||||
resource_config = {"id": "42"}
|
||||
flavor_id = types.Flavor.transform(
|
||||
clients=self.clients, resource_config=resource_config)
|
||||
self.assertEqual(flavor_id, "42")
|
||||
|
||||
def test_transform_by_name(self):
|
||||
resource_config = {"name": "m1.nano"}
|
||||
flavor_id = types.Flavor.transform(
|
||||
clients=self.clients, resource_config=resource_config)
|
||||
self.assertEqual(flavor_id, "42")
|
||||
|
||||
def test_transform_by_name_no_match(self):
|
||||
resource_config = {"name": "m1.medium"}
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.Flavor.transform, self.clients,
|
||||
resource_config)
|
||||
|
||||
def test_transform_by_name_multiple_match(self):
|
||||
resource_config = {"name": "m1.large"}
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.Flavor.transform, self.clients,
|
||||
resource_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)
|
||||
self.assertEqual(flavor_id, "42")
|
||||
|
||||
def test_transform_by_regex_multiple_match(self):
|
||||
resource_config = {"regex": "^m1"}
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.Flavor.transform, self.clients,
|
||||
resource_config)
|
||||
|
||||
def test_transform_by_regex_no_match(self):
|
||||
resource_config = {}
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.Flavor.transform, self.clients,
|
||||
resource_config)
|
||||
|
||||
|
||||
class EC2FlavorTestCase(test.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(EC2FlavorTestCase, self).setUp()
|
||||
self.clients = fakes.FakeClients()
|
||||
self.clients.nova().flavors._cache(fakes.FakeResource(name="m1.tiny",
|
||||
id="1"))
|
||||
self.clients.nova().flavors._cache(fakes.FakeResource(name="m1.nano",
|
||||
id="2"))
|
||||
self.clients.nova().flavors._cache(fakes.FakeResource(name="m1.large",
|
||||
id="3"))
|
||||
self.clients.nova().flavors._cache(fakes.FakeResource(name="m1.xlarge",
|
||||
id="3"))
|
||||
|
||||
def test_transform_by_name(self):
|
||||
resource_config = {"name": "m1.nano"}
|
||||
flavor_name = types.EC2Flavor.transform(
|
||||
clients=self.clients, resource_config=resource_config)
|
||||
self.assertEqual(flavor_name, "m1.nano")
|
||||
|
||||
def test_transform_by_id(self):
|
||||
resource_config = {"id": "2"}
|
||||
flavor_name = types.EC2Flavor.transform(
|
||||
clients=self.clients, resource_config=resource_config)
|
||||
self.assertEqual(flavor_name, "m1.nano")
|
||||
|
||||
def test_transform_by_id_no_match(self):
|
||||
resource_config = {"id": "4"}
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.EC2Flavor.transform, self.clients,
|
||||
resource_config)
|
||||
|
||||
def test_transform_by_id_multiple_match(self):
|
||||
resource_config = {"id": "3"}
|
||||
self.assertRaises(exceptions.MultipleMatchesFound,
|
||||
types.EC2Flavor.transform, self.clients,
|
||||
resource_config)
|
||||
|
||||
|
||||
class GlanceImageTestCase(test.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(GlanceImageTestCase, self).setUp()
|
||||
self.clients = fakes.FakeClients()
|
||||
image1 = fakes.FakeResource(name="cirros-0.3.4-uec", id="100")
|
||||
self.clients.glance().images._cache(image1)
|
||||
image2 = fakes.FakeResource(name="cirros-0.3.4-uec-ramdisk", id="101")
|
||||
self.clients.glance().images._cache(image2)
|
||||
image3 = fakes.FakeResource(name="cirros-0.3.4-uec-ramdisk-copy",
|
||||
id="102")
|
||||
self.clients.glance().images._cache(image3)
|
||||
image4 = fakes.FakeResource(name="cirros-0.3.4-uec-ramdisk-copy",
|
||||
id="103")
|
||||
self.clients.glance().images._cache(image4)
|
||||
|
||||
def test_transform_by_id(self):
|
||||
resource_config = {"id": "100"}
|
||||
image_id = types.GlanceImage.transform(
|
||||
clients=self.clients, resource_config=resource_config)
|
||||
self.assertEqual(image_id, "100")
|
||||
|
||||
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)
|
||||
self.assertEqual(image_id, "100")
|
||||
|
||||
def test_transform_by_name_no_match(self):
|
||||
resource_config = {"name": "cirros-0.3.4-uec-boot"}
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.GlanceImage.transform, self.clients,
|
||||
resource_config)
|
||||
|
||||
def test_transform_by_name_match_multiple(self):
|
||||
resource_config = {"name": "cirros-0.3.4-uec-ramdisk-copy"}
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.GlanceImage.transform, self.clients,
|
||||
resource_config)
|
||||
|
||||
def test_transform_by_regex(self):
|
||||
resource_config = {"regex": "-uec$"}
|
||||
image_id = types.GlanceImage.transform(
|
||||
clients=self.clients, resource_config=resource_config)
|
||||
self.assertEqual(image_id, "100")
|
||||
|
||||
def test_transform_by_regex_match_multiple(self):
|
||||
resource_config = {"regex": "^cirros"}
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.GlanceImage.transform, self.clients,
|
||||
resource_config)
|
||||
|
||||
def test_transform_by_regex_no_match(self):
|
||||
resource_config = {"regex": "-boot$"}
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.GlanceImage.transform, self.clients,
|
||||
resource_config)
|
||||
|
||||
|
||||
class EC2ImageTestCase(test.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(EC2ImageTestCase, self).setUp()
|
||||
self.clients = fakes.FakeClients()
|
||||
image1 = fakes.FakeResource(name="cirros-0.3.4-uec", id="100")
|
||||
self.clients.glance().images._cache(image1)
|
||||
image2 = fakes.FakeResource(name="cirros-0.3.4-uec-ramdisk", id="102")
|
||||
self.clients.glance().images._cache(image2)
|
||||
image3 = fakes.FakeResource(name="cirros-0.3.4-uec-ramdisk-copy",
|
||||
id="102")
|
||||
self.clients.glance().images._cache(image3)
|
||||
image4 = fakes.FakeResource(name="cirros-0.3.4-uec-ramdisk-copy",
|
||||
id="103")
|
||||
self.clients.glance().images._cache(image4)
|
||||
|
||||
ec2_image1 = fakes.FakeResource(name="cirros-0.3.4-uec", id="200")
|
||||
ec2_image2 = fakes.FakeResource(name="cirros-0.3.4-uec-ramdisk",
|
||||
id="201")
|
||||
ec2_image3 = fakes.FakeResource(name="cirros-0.3.4-uec-ramdisk-copy",
|
||||
id="202")
|
||||
ec2_image4 = fakes.FakeResource(name="cirros-0.3.4-uec-ramdisk-copy",
|
||||
id="203")
|
||||
|
||||
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.assertEqual(ec2_image_id, "200")
|
||||
|
||||
def test_transform_by_id(self):
|
||||
resource_config = {"id": "100"}
|
||||
ec2_image_id = types.EC2Image.transform(
|
||||
clients=self.clients, resource_config=resource_config)
|
||||
self.assertEqual(ec2_image_id, "200")
|
||||
|
||||
def test_transform_by_id_no_match(self):
|
||||
resource_config = {"id": "101"}
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.EC2Image.transform, self.clients,
|
||||
resource_config)
|
||||
|
||||
def test_transform_by_id_match_multiple(self):
|
||||
resource_config = {"id": "102"}
|
||||
self.assertRaises(exceptions.MultipleMatchesFound,
|
||||
types.EC2Image.transform, self.clients,
|
||||
resource_config)
|
||||
|
||||
def test_transform_by_name_no_match(self):
|
||||
resource_config = {"name": "cirros-0.3.4-uec-boot"}
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.EC2Image.transform, self.clients,
|
||||
resource_config)
|
||||
|
||||
def test_transform_by_name_match_multiple(self):
|
||||
resource_config = {"name": "cirros-0.3.4-uec-ramdisk-copy"}
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.EC2Image.transform, self.clients,
|
||||
resource_config)
|
||||
|
||||
def test_transform_by_regex(self):
|
||||
resource_config = {"regex": "-uec$"}
|
||||
ec2_image_id = types.EC2Image.transform(
|
||||
clients=self.clients, resource_config=resource_config)
|
||||
self.assertEqual(ec2_image_id, "200")
|
||||
|
||||
def test_transform_by_regex_match_multiple(self):
|
||||
resource_config = {"regex": "^cirros"}
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.EC2Image.transform, self.clients,
|
||||
resource_config)
|
||||
|
||||
def test_transform_by_regex_no_match(self):
|
||||
resource_config = {"regex": "-boot$"}
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.EC2Image.transform, self.clients,
|
||||
resource_config)
|
||||
|
||||
|
||||
class VolumeTypeTestCase(test.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(VolumeTypeTestCase, self).setUp()
|
||||
self.clients = fakes.FakeClients()
|
||||
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.assertEqual(volumetype_id, 100)
|
||||
|
||||
def test_transform_by_name(self):
|
||||
resource_config = {"name": "lvmdriver-1"}
|
||||
volumetype_id = types.VolumeType.transform(
|
||||
clients=self.clients, resource_config=resource_config)
|
||||
self.assertEqual(volumetype_id, 100)
|
||||
|
||||
def test_transform_by_name_no_match(self):
|
||||
resource_config = {"name": "nomatch-1"}
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.VolumeType.transform,
|
||||
self.clients, resource_config)
|
||||
|
||||
def test_transform_by_regex(self):
|
||||
resource_config = {"regex": "^lvm.*-1"}
|
||||
volumetype_id = types.VolumeType.transform(
|
||||
clients=self.clients, resource_config=resource_config)
|
||||
self.assertEqual(volumetype_id, 100)
|
||||
|
||||
def test_transform_by_regex_no_match(self):
|
||||
resource_config = {"regex": "dd"}
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.VolumeType.transform,
|
||||
self.clients, resource_config)
|
||||
|
||||
|
||||
class NeutronNetworkTestCase(test.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(NeutronNetworkTestCase, self).setUp()
|
||||
self.clients = fakes.FakeClients()
|
||||
net1_data = {"network": {
|
||||
"name": "net1"
|
||||
}}
|
||||
network1 = self.clients.neutron().create_network(net1_data)
|
||||
self.net1_id = network1["network"]["id"]
|
||||
|
||||
def test_transform_by_id(self):
|
||||
resource_config = {"id": self.net1_id}
|
||||
network_id = types.NeutronNetwork.transform(
|
||||
clients=self.clients, resource_config=resource_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)
|
||||
self.assertEqual(network_id, self.net1_id)
|
||||
|
||||
def test_transform_by_name_no_match(self):
|
||||
resource_config = {"name": "nomatch-1"}
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.NeutronNetwork.transform,
|
||||
self.clients, resource_config)
|
Loading…
Reference in New Issue
Block a user