Browse Source

Add better conversion of string parameters into a list

The current conversion works pretty well when passing a simple string
of validation or group names. But don't work when passing multiple
validation or group name.

```
>>> groups = "check-cpu"
>>> groups = [groups]
>>> print(groups)
['check-cpu']

>>> groups = "check-cpu,check-ram"
>>> groups = [groups]
>>> print(groups)
['check-cpu,check-ram']
```

This patch brings a better conversion when multiple validations or
groups are sent through a string.

Change-Id: Iba023a704dd873ea2df75c8cc6d78f929ef45bc2
Signed-off-by: Gael Chamoulaud (Strider) <gchamoul@redhat.com>
changes/73/761173/4
Gael Chamoulaud (Strider) 2 months ago
parent
commit
63e30c744c
No known key found for this signature in database GPG Key ID: 4119D0305C651D66
3 changed files with 112 additions and 12 deletions
  1. +54
    -1
      validations_libs/tests/test_utils.py
  2. +57
    -8
      validations_libs/utils.py
  3. +1
    -3
      validations_libs/validation_actions.py

+ 54
- 1
validations_libs/tests/test_utils.py View File

@@ -70,9 +70,24 @@ class TestUtils(TestCase):
mock_listdir, mock_isfile):
mock_listdir.return_value = ['foo.yaml']
mock_isfile.return_value = True
result = utils.get_validations_playbook('/foo/playbook', 'foo')
result = utils.get_validations_playbook('/foo/playbook',
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)
@@ -176,3 +191,41 @@ class TestUtils(TestCase):
result = utils.get_validations_parameters(['/foo/playbook/foo.yaml'],
[], [])
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)

+ 57
- 8
validations_libs/utils.py View File

@@ -53,9 +53,12 @@ def parse_all_validations_on_disk(path, groups=None):
Can be sorted by Groups
"""
results = []
if not groups:
groups = []
else:
groups = convert_data(groups)

validations_abspath = glob.glob("{path}/*.yaml".format(path=path))
if isinstance(groups, six.string_types):
groups = [groups]

for pl in validations_abspath:
val = Validation(pl)
@@ -73,10 +76,10 @@ def get_validations_playbook(path, validation_id=None, groups=None):
:type path: `string`

:param validation_id: List of validation name
:type validation_id: `list`
:type validation_id: `list` or a `string` of comma-separated validations

:param groups: List of validation group
:type groups: `list`
:type groups: `list` or a `string` of comma-separated groups

:return: A list of absolute validations playbooks path

@@ -89,10 +92,16 @@ 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 isinstance(groups, six.string_types):
groups = [groups]
if isinstance(validation_id, six.string_types):
validation_id = [validation_id]
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)
@@ -168,3 +177,43 @@ def get_validations_parameters(validations_data, validation_name=[],
raise RuntimeError(msg)

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`.

It will raise an exception if `data` is not a list or a string.

:param data: A string or a list
:type data: `string` or `list`

:return: A list of data

:exemple:

>>> 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

+ 1
- 3
validations_libs/validation_actions.py View File

@@ -15,7 +15,6 @@
import logging
import os
import json
import six
import yaml

from validations_libs.ansible import Ansible as v_ansible
@@ -86,8 +85,7 @@ class ValidationActions(object):
except Exception as e:
raise(e)
elif validation_name:
if isinstance(validation_name, six.string_types):
validation_name = [validation_name]
validation_name = v_utils.convert_data(validation_name)

playbooks = v_utils.get_validations_playbook(validations_dir,
validation_name,


Loading…
Cancel
Save