Remove convert_data function

This patch removes the convert_data function which has been introduced
with the first one and deprecated Command Line Interface. The
--validation and --group arguments were sending comma-separated strings
instead of list. The new Command Line Interface is now enforcing this by
using its own CommaList Parser Action and only accepting List of groups
or validation names.

This function is now useless and can be removed safely. Tests and usages
have been modified accordingly.

Change-Id: Ief9459e914ea73fbd59a85c07a55c26078e0126a
Signed-off-by: Gael Chamoulaud (Strider) <gchamoul@redhat.com>
This commit is contained in:
Gael Chamoulaud (Strider) 2021-07-12 11:57:29 +02:00
parent 85947ee8e0
commit e9eae212b7
No known key found for this signature in database
GPG Key ID: 4119D0305C651D66
3 changed files with 8 additions and 117 deletions

View File

@ -70,20 +70,6 @@ class TestUtils(TestCase):
validation_id='foo')
self.assertEqual(result, ['/foo/playbook/foo.yaml'])
@mock.patch('os.path.isfile')
@mock.patch('os.listdir')
@mock.patch('yaml.safe_load', return_value=fakes.FAKE_PLAYBOOK)
@mock.patch('six.moves.builtins.open')
def test_get_validations_playbook_by_string_id(self, mock_open, mock_load,
mock_listdir, mock_isfile):
validation_id = "foo,foo2,foo3"
mock_listdir.return_value = ['foo.yaml', 'foo2.yaml', 'foo3.yaml']
mock_isfile.return_value = True
result = utils.get_validations_playbook('/foo/playbook', validation_id)
self.assertEqual(result, ['/foo/playbook/foo.yaml',
'/foo/playbook/foo2.yaml',
'/foo/playbook/foo3.yaml'])
@mock.patch('os.path.isfile')
@mock.patch('os.listdir')
@mock.patch('yaml.safe_load', return_value=fakes.FAKE_PLAYBOOK)
@ -93,8 +79,7 @@ class TestUtils(TestCase):
mock_listdir.return_value = ['foo.yaml']
mock_isfile.return_value = True
result = utils.get_validations_playbook('/foo/playbook', 'foo', 'prep')
self.assertEqual(result, ['/foo/playbook/foo.yaml',
'/foo/playbook/foo.yaml'])
self.assertEqual(result, ['/foo/playbook/foo.yaml'])
@mock.patch('os.path.isfile')
@mock.patch('os.listdir')
@ -175,44 +160,6 @@ class TestUtils(TestCase):
[], [])
self.assertEqual(result, {})
@mock.patch('six.moves.builtins.open')
def test_convert_data(self, mock_open):
data_string = "check-cpu,check-ram,check-disk-space"
data_list = ["check-cpu", "check-ram", "check-disk-space"]
result = utils.convert_data(data_string)
self.assertEqual(result, data_list)
@mock.patch('six.moves.builtins.open')
def test_convert_data_with_spaces(self, mock_open):
data_string = "check-cpu, check-ram , check-disk-space"
data_list = ["check-cpu", "check-ram", "check-disk-space"]
result = utils.convert_data(data_string)
self.assertEqual(result, data_list)
@mock.patch('six.moves.builtins.open')
def test_convert_data_with_comma_at_end(self, mock_open):
data_string = "check-cpu,"
data_list = ["check-cpu"]
result = utils.convert_data(data_string)
self.assertEqual(result, data_list)
@mock.patch('six.moves.builtins.open')
def test_convert_data_with_list(self, mock_open):
data_list = ["check-cpu", "check-ram", "check-disk-space"]
result = utils.convert_data(data_list)
self.assertEqual(result, data_list)
@mock.patch('six.moves.builtins.open')
def test_convert_data_with_non_list(self, mock_open):
data_dict = {
"val1": "check-cpu",
"val2": "check-ram",
"val3": "check-disk-space"
}
self.assertRaises(TypeError,
utils.convert_data,
data=data_dict)
@mock.patch('validations_libs.utils.LOG', autospec=True)
@mock.patch('validations_libs.utils.os.makedirs')
@mock.patch(

View File

@ -128,15 +128,14 @@ def create_artifacts_dir(log_path=constants.VALIDATIONS_LOG_BASEDIR,
raise RuntimeError()
def parse_all_validations_on_disk(path, groups=None):
def parse_all_validations_on_disk(path, groups=[]):
"""Return a list of validations metadata which can be sorted by Groups
:param path: The absolute path of the validations directory
:type path: `string`
:param groups: Groups of validations. Could be a `list` or a
comma-separated `string` of groups
:type groups: `list` or `string`
:return: A list of validations metadata.
:param groups: Groups of validations
:type groups: `list`
:return: A list of validations metadata
:rtype: `list`
:Example:
@ -152,12 +151,7 @@ def parse_all_validations_on_disk(path, groups=None):
'id': 'check-cpu',
'name': 'Verify if the server fits the CPU core requirements'}]
"""
results = []
if not groups:
groups = []
else:
groups = convert_data(groups)
validations_abspath = glob.glob("{path}/*.yaml".format(path=path))
@ -176,16 +170,16 @@ def parse_all_validations_on_disk(path, groups=None):
return results
def get_validations_playbook(path, validation_id=None, groups=None):
def get_validations_playbook(path, validation_id=[], groups=[]):
"""Get a list of validations playbooks paths either by their names
or their groups
:param path: Path of the validations playbooks
:type path: `string`
:param validation_id: List of validation name
:type validation_id: `list` or a `string` of comma-separated validations
:type validation_id: `list`
:param groups: List of validation group
:type groups: `list` or a `string` of comma-separated groups
:type groups: `list`
:return: A list of absolute validations playbooks path
:rtype: `list`
@ -198,16 +192,6 @@ def get_validations_playbook(path, validation_id=None, groups=None):
['/usr/share/ansible/validation-playbooks/512e.yaml',
'/usr/share/ansible/validation-playbooks/check-cpu.yaml',]
"""
if not validation_id:
validation_id = []
else:
validation_id = convert_data(validation_id)
if not groups:
groups = []
else:
groups = convert_data(groups)
pl = []
for f in os.listdir(path):
pl_path = join(path, f)
@ -372,41 +356,3 @@ def get_validations_parameters(validations_data, validation_name=[],
}
return params
def convert_data(data=''):
"""Transform a string containing comma-separated validation or group name
into a list. If `data` is already a list, it will simply return `data`.
:param data: A string or a list
:type data: `string` or `list`
:return: A list of data
:rtype: `list`
:raises: a `TypeError` exception if `data` is not a list or a string
:Example:
>>> data = "check-cpu,check-ram,check-disk-space"
>>> convert_data(data)
['check-cpu', 'check-ram', 'check-disk-space']
...
>>> data = "check-cpu , check-ram , check-disk-space"
>>> convert_data(data)
['check-cpu', 'check-ram', 'check-disk-space']
...
>>> data = "check-cpu,"
>>> convert_data(data)
['check-cpu']
...
>>> data = ['check-cpu', 'check-ram', 'check-disk-space']
>>> convert_data(data)
['check-cpu', 'check-ram', 'check-disk-space']
"""
if isinstance(data, six.string_types):
return [
conv_data.strip() for conv_data in data.split(',') if conv_data
]
elif not isinstance(data, list):
raise TypeError("The input data should be either a List or a String")
else:
return data

View File

@ -341,8 +341,6 @@ class ValidationActions(object):
except Exception as e:
raise(e)
elif validation_name:
validation_name = v_utils.convert_data(validation_name)
playbooks = v_utils.get_validations_playbook(validations_dir,
validation_name,
group)