Merge "Move 'volume_type_exists' scenario to '../validators.py'"

This commit is contained in:
Jenkins 2017-06-13 03:03:45 +00:00 committed by Gerrit Code Review
commit 350b5e5bcd
6 changed files with 74 additions and 1 deletions

View File

@ -508,6 +508,7 @@
CinderVolumes.create_snapshot_and_attach_volume:
-
args:
volume_type: "lvmdriver-1"
size:
min: 1
max: 1

View File

@ -440,9 +440,9 @@ class CreateAndAttachVolume(cinder_utils.CinderBasic,
@validation.add("restricted_parameters", param_names=["name", "display_name"])
@validation.volume_type_exists("volume_type")
@validation.add("required_services", services=[consts.Service.NOVA,
consts.Service.CINDER])
@validation.add("volume_type_exists", param_name="volume_type")
@validation.add("required_platform", platform="openstack", users=True)
@scenario.configure(context={"cleanup": ["cinder", "nova"]},
name="CinderVolumes.create_snapshot_and_attach_volume")

View File

@ -518,3 +518,35 @@ class RequiredAPIVersionsValidator(validation.Validator):
return self.fail(msg % {"component": self.component,
"version": versions_str,
"found_version": used_version})
@validation.add("required_platform", platform="openstack", users=True)
@validation.configure(name="volume_type_exists", namespace="openstack")
class VolumeTypeExistsValidator(validation.Validator):
def __init__(self, param_name):
"""Returns validator for volume types.
:param param_name: defines variable to be used as the flag to
determine if volume types should be checked for
existence.
"""
super(VolumeTypeExistsValidator, self).__init__()
self.param = param_name
def validate(self, config, credentials, plugin_cls, plugin_cfg):
volume_type = config.get("args", {}).get(self.param, False)
if volume_type:
for user in credentials["openstack"]["users"]:
clients = user["credential"].clients()
vt_names = [vt.name for vt in
clients.cinder().volume_types.list()]
volume_types_ctx = config.get(
"context", {}).get("volume_types", [])
if volume_type not in vt_names + volume_types_ctx:
msg = ("Specified volume type {} not found for user {}. "
"List of available types: {}")
return self.fail(msg.format(volume_type, user, vt_names))
else:
msg = ("The parameter '{}' is required and should not be empty.")
return self.fail(msg.format(self.param))

View File

@ -3,6 +3,7 @@
"CinderVolumes.create_snapshot_and_attach_volume": [
{
"args": {
"volume_type": "lvmdriver-1",
"size": {
"min": 1,
"max": 5

View File

@ -3,6 +3,7 @@
CinderVolumes.create_snapshot_and_attach_volume:
-
args:
volume_type: "lvmdriver-1"
size:
min: 1
max: 5

View File

@ -781,3 +781,41 @@ class RequiredAPIVersionsValidatorTestCase(test.TestCase):
self.assertEqual(err_msg, result.msg)
else:
self.assertIsNone(result)
@ddt.ddt
class VolumeTypeExistsValidatorTestCase(test.TestCase):
def setUp(self):
super(VolumeTypeExistsValidatorTestCase, self).setUp()
self.validator = validators.VolumeTypeExistsValidator("volume_type")
self.credentials = copy.deepcopy(credentials)
@ddt.unpack
@ddt.data(
{"context": {"args": {"volume_type": "fake_type"}},
"volume_type": "fake_type"},
{"context": {"args": {"volume_type": False}}, "volume_type": None,
"err_msg": "The parameter 'volume_type' is required and should "
"not be empty."},
{"context": {"args": {"volume_type": False}},
"volume_type": ["fake_type"],
"err_msg": "The parameter 'volume_type' is required and should "
"not be empty."},
{"context": {"args": {"volume_type": "fake_type"}}, "volume_type": [],
"err_msg": "Specified volume type fake_type not found for user {}. "
"List of available types: [[]]"}
)
def test_volume_type_exists(self, context, volume_type, err_msg=None):
clients = self.credentials["openstack"]["users"][0][
"credential"].clients()
clients.cinder().volume_types.list.return_value = [mock.MagicMock()]
clients.cinder().volume_types.list.return_value[0].name = volume_type
result = self.validator.validate(context, self.credentials, None, None)
if err_msg:
self.assertIsNotNone(result)
fake_user = self.credentials["openstack"]["users"][0]
self.assertEqual(err_msg.format(fake_user), result.msg)
else:
self.assertIsNone(result)