From 0ce861f72cd78ecd52084175c3c3ed417c88fed7 Mon Sep 17 00:00:00 2001 From: Vitaly Gridnev Date: Tue, 10 Feb 2015 17:02:23 +0300 Subject: [PATCH] Improve messages for validation Change-Id: Icce40f843e97ffff11c6f7e26438e94d7fed247b Closes-bug: #1283289 --- sahara/service/validations/base.py | 35 ++++++++++++++----- .../test_cluster_scaling_validation.py | 8 ++--- .../test_ng_template_validation_create.py | 5 ++- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/sahara/service/validations/base.py b/sahara/service/validations/base.py index 93f74ee1..80e7ee89 100644 --- a/sahara/service/validations/base.py +++ b/sahara/service/validations/base.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import collections import operator import novaclient.exceptions as nova_ex @@ -49,6 +50,17 @@ def _get_plugin_configs(plugin_name, hadoop_version, scope=None): return pl_confs +def _check_duplicates(lst, message): + invalid = [] + lst = collections.Counter(lst) + for (key, value) in lst.iteritems(): + if value > 1: + invalid.append(key) + + if len(invalid) > 0: + raise ex.InvalidDataException(message % invalid) + + # Common validation checks def check_plugin_name_exists(name): @@ -181,25 +193,30 @@ def check_floatingip_pool_exists(ng_name, pool_id): def check_node_processes(plugin_name, version, node_processes): - if len(set(node_processes)) != len(node_processes): - raise ex.InvalidDataException( - _("Duplicates in node processes have been detected")) + _check_duplicates(node_processes, _("Duplicates in node processes have " + "been detected: %s")) + plugin_processes = [] for process in plugin_base.PLUGINS.get_plugin( plugin_name).get_node_processes(version).values(): plugin_processes += process + plugin_processes = set(plugin_processes) - if not set(node_processes).issubset(set(plugin_processes)): + invalid_processes = [] + for node_process in node_processes: + if node_process not in plugin_processes: + invalid_processes.append(node_process) + + if len(invalid_processes) > 0: raise ex.InvalidReferenceException( - _("Plugin supports the following node procesess: %s") - % sorted(plugin_processes)) + _("Plugin doesn't support the following node processes: %s") + % sorted(invalid_processes)) def check_duplicates_node_groups_names(node_groups): ng_names = [ng['name'] for ng in node_groups] - if len(set(ng_names)) < len(node_groups): - raise ex.InvalidDataException( - _("Duplicates in node group names are detected")) + _check_duplicates( + ng_names, _("Duplicates in node group names are detected: %s")) def check_availability_zone_exist(az): diff --git a/sahara/tests/unit/service/validation/test_cluster_scaling_validation.py b/sahara/tests/unit/service/validation/test_cluster_scaling_validation.py index c22dec49..7f80e7a9 100644 --- a/sahara/tests/unit/service/validation/test_cluster_scaling_validation.py +++ b/sahara/tests/unit/service/validation/test_cluster_scaling_validation.py @@ -37,6 +37,8 @@ class TestScalingValidation(u.ValidationTestCase): super(TestScalingValidation, self).setUp() api.plugin_base.setup_plugins() self._create_object_fun = mock.Mock() + self.duplicates_detected = ("Duplicates in node group names are" + " detected: ['a']") @mock.patch('sahara.service.api.get_cluster') @mock.patch('sahara.plugins.base.PluginManager.get_plugin') @@ -101,8 +103,7 @@ class TestScalingValidation(u.ValidationTestCase): ]}) self._assert_check_scaling( data=data, cluster=cluster, - expected_message='Duplicates in node ' - 'group names are detected', + expected_message=self.duplicates_detected, expected_exception=ex.InvalidDataException) @mock.patch("sahara.service.api.OPS") @@ -128,8 +129,7 @@ class TestScalingValidation(u.ValidationTestCase): } self._assert_check_scaling( data=data, cluster=cluster, - expected_message='Duplicates in node ' - 'group names are detected', + expected_message=self.duplicates_detected, expected_exception=ex.InvalidDataException) data = { 'add_node_groups': [ diff --git a/sahara/tests/unit/service/validation/test_ng_template_validation_create.py b/sahara/tests/unit/service/validation/test_ng_template_validation_create.py index 94293bd4..e6e7c4dd 100644 --- a/sahara/tests/unit/service/validation/test_ng_template_validation_create.py +++ b/sahara/tests/unit/service/validation/test_ng_template_validation_create.py @@ -101,9 +101,8 @@ class TestNGTemplateCreateValidation(u.ValidationTestCase): 'node_processes': ['wrong_process'] }, bad_req_i=(1, 'INVALID_REFERENCE', - "Plugin supports the following node procesess: [" - "'datanode', 'hiveserver', 'jobtracker', 'namenode', " - "'oozie', 'secondarynamenode', 'tasktracker']") + "Plugin doesn't support the following node processes: " + "['wrong_process']") ) def test_ng_template_create_v_right(self):