Browse Source

Format output in json or yaml for validations show param

Change-Id: I93b35cd1619e2c2b4a3b1294171dddf56033e94f
changes/85/718685/5 1.0.0
Mathieu Bultel 2 years ago committed by mathieu bultel
parent
commit
c0de9faa41
  1. 9
      validations_libs/tests/fakes.py
  2. 37
      validations_libs/tests/test_utils.py
  3. 20
      validations_libs/utils.py
  4. 11
      validations_libs/validation_actions.py

9
validations_libs/tests/fakes.py

@ -153,6 +153,15 @@ FAKE_PLAYBOOK = [{'hosts': 'undercloud',
'name':
'Advanced Format 512e Support'}}}]
FAKE_PLAYBOOK2 = [{'hosts': 'undercloud',
'roles': ['advanced_format_512e_support'],
'vars': {'metadata': {'description': 'foo',
'groups': ['prep', 'pre-deployment'],
'name':
'Advanced Format 512e Support'},
'foo': 'bar'}}]
FAKE_METADATA = {'id': 'foo',
'description': 'foo',
'groups': ['prep', 'pre-deployment'],

37
validations_libs/tests/test_utils.py

@ -13,6 +13,8 @@
# under the License.
#
import json
import yaml
try:
from unittest import mock
except ImportError:
@ -129,21 +131,46 @@ class TestUtils(TestCase):
result = utils.get_validations_details('foo')
self.assertEqual(result, fakes.FAKE_METADATA)
@mock.patch('yaml.safe_load', return_value=fakes.FAKE_PLAYBOOK)
@mock.patch('yaml.safe_load', return_value=fakes.FAKE_PLAYBOOK2)
@mock.patch('six.moves.builtins.open')
def test_get_validations_parameters_no_group(self, mock_open, mock_load):
result = utils.get_validations_parameters(['/foo/playbook/foo.yaml'],
'foo')
self.assertEqual(result, {'foo': {'parameters': fakes.FAKE_METADATA}})
output = {'foo': {'parameters': {'foo': 'bar'}}}
self.assertEqual(result, json.dumps(output, indent=4, sort_keys=True))
@mock.patch('yaml.safe_load', return_value=fakes.FAKE_PLAYBOOK)
@mock.patch('yaml.safe_load', return_value=fakes.FAKE_PLAYBOOK2)
@mock.patch('six.moves.builtins.open')
def test_get_validations_parameters_yaml(self, mock_open, mock_load):
result = utils.get_validations_parameters(
validations_data=['/foo/playbook/foo.yaml'],
validation_name='foo',
format='yaml')
output = {'foo': {'parameters': {'foo': 'bar'}}}
self.assertEqual(result, yaml.safe_dump(output,
allow_unicode=True,
default_flow_style=False,
indent=2))
@mock.patch('yaml.safe_load', return_value=fakes.FAKE_PLAYBOOK2)
@mock.patch('six.moves.builtins.open')
def test_get_validations_parameters_wrong_format(self, mock_open,
mock_load):
self.assertRaises(RuntimeError, utils.get_validations_parameters,
validations_data=['/foo/playbook/foo.yaml'],
validation_name='foo',
format='wrong')
@mock.patch('yaml.safe_load', return_value=fakes.FAKE_PLAYBOOK2)
@mock.patch('six.moves.builtins.open')
def test_get_validations_parameters_no_val(self, mock_open, mock_load):
result = utils.get_validations_parameters(['/foo/playbook/foo.yaml'],
[], ['prep'])
self.assertEqual(result, {'foo': {'parameters': fakes.FAKE_METADATA}})
output = {'foo': {'parameters': {'foo': 'bar'}}}
self.assertEqual(result, json.dumps(output, indent=4, sort_keys=True))
@mock.patch('yaml.safe_load', return_value=fakes.FAKE_PLAYBOOK)
@mock.patch('six.moves.builtins.open')
@ -151,4 +178,4 @@ class TestUtils(TestCase):
result = utils.get_validations_parameters(['/foo/playbook/foo.yaml'],
[], [])
self.assertEqual(result, {})
self.assertEqual(result, json.dumps({}))

20
validations_libs/utils.py

@ -14,10 +14,12 @@
#
import datetime
import glob
import json
import logging
import os
import six
import uuid
import yaml
from os.path import join
from validations_libs import constants
@ -121,13 +123,23 @@ def get_validations_data(validation, path=constants.ANSIBLE_VALIDATION_DIR):
def get_validations_parameters(validations_data, validation_name=[],
groups=[]):
groups=[], format='json'):
"""
Return parameters for a list of validations
The return format can be in json or yaml
"""
params = {}
for val in validations_data:
v = Validation(val)
if v.id in validation_name or set(groups).intersection(v.groups):
params[v.id] = {
'parameters': (v.get_metadata if v.get_metadata else
v.get_vars)
'parameters': v.get_vars
}
return params
if format not in ['json', 'yaml']:
msg = 'Invalid output format, {} is not supported'.format(format)
raise RuntimeError(msg)
elif format == 'json':
return json.dumps(params, indent=4, sort_keys=True)
else:
return yaml.safe_dump(params, allow_unicode=True,
default_flow_style=False, indent=2)

11
validations_libs/validation_actions.py

@ -135,12 +135,17 @@ class ValidationActions(object):
column_name = ("Groups", "Description", "Number of Validations")
return (column_name, group_info)
def show_validations_parameters(self, validation, group=None):
def show_validations_parameters(self, validation, group=None,
format='json', download_file=None):
"""Return Validations Parameters"""
validations = v_utils.get_validations_playbook(
self.validation_path, validation, group)
return v_utils.get_validations_parameters(validations, validation,
group)
params = v_utils.get_validations_parameters(validations, validation,
group, format)
if download_file:
with open(download_file, 'w') as f:
f.write(params)
return params
def show_history(self, validation_id=None):
"""Return validations history"""

Loading…
Cancel
Save