From 4cc7edcb3064dc59bfdef2d2cab117a1482b97aa Mon Sep 17 00:00:00 2001 From: astaroverov Date: Fri, 5 May 2017 12:20:07 +0300 Subject: [PATCH] Move 'volume_type_exists' scenario to '../validators.py' Change-Id: I8028b8a8b3b9d7ff43df00ca80eb95e73c0674c6 --- rally-jobs/cinder.yaml | 1 + .../openstack/scenarios/cinder/volumes.py | 2 +- rally/plugins/openstack/validators.py | 32 ++++++++++++++++ .../create-snapshot-and-attach-volume.json | 1 + .../create-snapshot-and-attach-volume.yaml | 1 + .../unit/plugins/openstack/test_validators.py | 38 +++++++++++++++++++ 6 files changed, 74 insertions(+), 1 deletion(-) diff --git a/rally-jobs/cinder.yaml b/rally-jobs/cinder.yaml index 05900513..a5cbd16f 100755 --- a/rally-jobs/cinder.yaml +++ b/rally-jobs/cinder.yaml @@ -508,6 +508,7 @@ CinderVolumes.create_snapshot_and_attach_volume: - args: + volume_type: "lvmdriver-1" size: min: 1 max: 1 diff --git a/rally/plugins/openstack/scenarios/cinder/volumes.py b/rally/plugins/openstack/scenarios/cinder/volumes.py index 52fd31b5..c6adc0cc 100644 --- a/rally/plugins/openstack/scenarios/cinder/volumes.py +++ b/rally/plugins/openstack/scenarios/cinder/volumes.py @@ -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") diff --git a/rally/plugins/openstack/validators.py b/rally/plugins/openstack/validators.py index 6746e910..0eb337d1 100644 --- a/rally/plugins/openstack/validators.py +++ b/rally/plugins/openstack/validators.py @@ -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)) diff --git a/samples/tasks/scenarios/cinder/create-snapshot-and-attach-volume.json b/samples/tasks/scenarios/cinder/create-snapshot-and-attach-volume.json index 7e24ba47..4ada671e 100644 --- a/samples/tasks/scenarios/cinder/create-snapshot-and-attach-volume.json +++ b/samples/tasks/scenarios/cinder/create-snapshot-and-attach-volume.json @@ -3,6 +3,7 @@ "CinderVolumes.create_snapshot_and_attach_volume": [ { "args": { + "volume_type": "lvmdriver-1", "size": { "min": 1, "max": 5 diff --git a/samples/tasks/scenarios/cinder/create-snapshot-and-attach-volume.yaml b/samples/tasks/scenarios/cinder/create-snapshot-and-attach-volume.yaml index f77a22fc..ed778887 100644 --- a/samples/tasks/scenarios/cinder/create-snapshot-and-attach-volume.yaml +++ b/samples/tasks/scenarios/cinder/create-snapshot-and-attach-volume.yaml @@ -3,6 +3,7 @@ CinderVolumes.create_snapshot_and_attach_volume: - args: + volume_type: "lvmdriver-1" size: min: 1 max: 5 diff --git a/tests/unit/plugins/openstack/test_validators.py b/tests/unit/plugins/openstack/test_validators.py index 78f3f95f..f440e22c 100644 --- a/tests/unit/plugins/openstack/test_validators.py +++ b/tests/unit/plugins/openstack/test_validators.py @@ -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)