From 491a27b53ca7cceaf959c2e55cc206fdefddaaab Mon Sep 17 00:00:00 2001 From: Mathieu Bultel Date: Wed, 18 Mar 2020 23:10:27 +0100 Subject: [PATCH] Add validation_actions class and group info implementation Move all validation actions into a ValidationActions class in order to have one object with several validations actions Add group info implementation And add group object like validation object to have a simpler representation of the validation group. Change-Id: Idc7a55e26de20968f0a6a90f2a005d21a30c9e70 --- validations_libs/{show.py => group.py} | 34 ++++++++----- validations_libs/group_info.py | 0 validations_libs/list.py | 42 ---------------- .../tests/test_validations_list.py | 4 +- .../tests/test_validations_run.py | 8 +-- .../tests/test_validations_show.py | 4 +- validations_libs/utils.py | 49 ++++-------------- .../{run.py => validation_actions.py} | 50 +++++++++++++++++-- 8 files changed, 84 insertions(+), 107 deletions(-) rename validations_libs/{show.py => group.py} (51%) create mode 100644 validations_libs/group_info.py delete mode 100644 validations_libs/list.py rename validations_libs/{run.py => validation_actions.py} (65%) diff --git a/validations_libs/show.py b/validations_libs/group.py similarity index 51% rename from validations_libs/show.py rename to validations_libs/group.py index b24391aa..3df7d0b7 100644 --- a/validations_libs/show.py +++ b/validations_libs/group.py @@ -14,21 +14,29 @@ # import logging -from validations_libs import utils as v_utils +import yaml -LOG = logging.getLogger(__name__ + ".show") +LOG = logging.getLogger(__name__ + ".Group") -class Show(object): +class Group(object): - def __init__(self): - self.log = logging.getLogger(__name__ + ".Show") + def __init__(self, groups): + self.data = self._get_content(groups) - def show_validations(self, validation): - """Display detailed information about a Validation""" - # Get validation data: - data = v_utils.get_validations_data(validation) - format = v_utils.get_validations_stats( - v_utils.parse_all_validations_logs_on_disk()) - data.update(format) - return data + def _get_content(self, groups): + with open(groups, 'r') as gp: + return yaml.safe_load(gp) + + @property + def get_data(self): + return self.data + + @property + def get_formated_group(self): + return [(gp_n, gp_d[0].get('description')) + for (gp_n, gp_d) in sorted(self.data.items())] + + @property + def get_groups_keys_list(self): + return [gp for gp in self.data.keys()] diff --git a/validations_libs/group_info.py b/validations_libs/group_info.py new file mode 100644 index 00000000..e69de29b diff --git a/validations_libs/list.py b/validations_libs/list.py deleted file mode 100644 index 1603d845..00000000 --- a/validations_libs/list.py +++ /dev/null @@ -1,42 +0,0 @@ -# Copyright 2020 Red Hat, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. -# - -import logging -from validations_libs import constants -from validations_libs import utils as validations_utils - -LOG = logging.getLogger(__name__ + ".list") - - -class List(object): - - def __init__(self, group, validations_dir=None): - self.log = logging.getLogger(__name__ + ".List") - self.validations_dir = (validations_dir if validations_dir - else constants.ANSIBLE_VALIDATION_DIR) - self.group = group - - def list_validations(self): - """List the available validations""" - validations = validations_utils.parse_all_validations_on_disk( - self.validations_dir, self.group) - - return_values = [] - column_name = ('ID', 'Name', 'Groups') - - for val in validations: - return_values.append((val.get('id'), val.get('name'), - val.get('groups'))) - return (column_name, return_values) diff --git a/validations_libs/tests/test_validations_list.py b/validations_libs/tests/test_validations_list.py index aebd7e56..3e30cf04 100644 --- a/validations_libs/tests/test_validations_list.py +++ b/validations_libs/tests/test_validations_list.py @@ -17,7 +17,7 @@ from unittest import mock from unittest import TestCase from validations_libs.tests import fakes -from validations_libs.list import List +from validations_libs.validation_actions import ValidationActions class TestValidatorList(TestCase): @@ -29,7 +29,7 @@ class TestValidatorList(TestCase): @mock.patch('validations_libs.utils.parse_all_validations_on_disk', return_value=fakes.VALIDATIONS_LIST) def test_validation_list(self, mock_validation_dir): - validations_list = List(fakes.GROUPS_LIST, '/tmp/foo') + validations_list = ValidationActions(fakes.GROUPS_LIST, '/tmp/foo') self.assertEqual(validations_list.list_validations(), (self.column_name, [('my_val1', diff --git a/validations_libs/tests/test_validations_run.py b/validations_libs/tests/test_validations_run.py index 02ef9bc2..a1391a29 100644 --- a/validations_libs/tests/test_validations_run.py +++ b/validations_libs/tests/test_validations_run.py @@ -17,7 +17,7 @@ from unittest import mock from unittest import TestCase from validations_libs.tests import fakes -from validations_libs.run import Run +from validations_libs.validation_actions import ValidationActions class TestValidatorRun(TestCase): @@ -52,7 +52,7 @@ class TestValidatorRun(TestCase): playbook = ['fake.yaml'] inventory = 'tmp/inventory.yaml' - run = Run() + run = ValidationActions() run_return = run.run_validations(playbook, inventory, group=fakes.GROUPS_LIST, validations_dir='/tmp/foo') @@ -85,7 +85,7 @@ class TestValidatorRun(TestCase): playbook = ['fake.yaml'] inventory = 'tmp/inventory.yaml' - run = Run() + run = ValidationActions() run_return = run.run_validations(playbook, inventory, group=fakes.GROUPS_LIST, validations_dir='/tmp/foo') @@ -95,6 +95,6 @@ class TestValidatorRun(TestCase): playbook = ['fake.yaml'] inventory = 'tmp/inventory.yaml' - run = Run() + run = ValidationActions() self.assertRaises(RuntimeError, run.run_validations, playbook, inventory) diff --git a/validations_libs/tests/test_validations_show.py b/validations_libs/tests/test_validations_show.py index 888239b8..e38cc916 100644 --- a/validations_libs/tests/test_validations_show.py +++ b/validations_libs/tests/test_validations_show.py @@ -17,7 +17,7 @@ from unittest import mock from unittest import TestCase from validations_libs.tests import fakes -from validations_libs.show import Show +from validations_libs.validation_actions import ValidationActions class TestValidatorShow(TestCase): @@ -39,6 +39,6 @@ class TestValidatorShow(TestCase): 'ID': '512e'} data.update({'Last execution date': '2019-11-25 13:40:14', 'Number of execution': 'Total: 1, Passed: 1, Failed: 0'}) - validations_show = Show() + validations_show = ValidationActions() out = validations_show.show_validations('512e') self.assertEqual(out, data) diff --git a/validations_libs/utils.py b/validations_libs/utils.py index 32796300..e32feae4 100644 --- a/validations_libs/utils.py +++ b/validations_libs/utils.py @@ -19,9 +19,9 @@ import logging import os import six import time -import yaml from validations_libs import constants +from validations_libs.group import Group from validations_libs.validation import Validation from uuid import uuid4 @@ -67,54 +67,23 @@ def parse_all_validations_on_disk(path, groups=None): return results -def parse_all_validation_groups_on_disk(groups_file_path=None): - results = [] - - if not groups_file_path: - groups_file_path = constants.VALIDATION_GROUPS_INFO - - if not os.path.exists(groups_file_path): - return results - - with open(groups_file_path, 'r') as grps: - contents = yaml.safe_load(grps) - - for grp_name, grp_desc in sorted(contents.items()): - results.append((grp_name, grp_desc[0].get('description'))) - - return results - - def get_validation_parameters(validation): """Return dictionary of parameters""" return Validation(validation).get_vars -def read_validation_groups_file(groups_file_path=None): +def read_validation_groups_file(groups_path=None): """Load groups.yaml file and return a dictionary with its contents""" - if not groups_file_path: - groups_file_path = constants.VALIDATION_GROUPS_INFO - - if not os.path.exists(groups_file_path): - return [] - - with open(groups_file_path, 'r') as grps: - contents = yaml.safe_load(grps) - - return contents + gp = Group((groups_path if groups_path else + constants.VALIDATION_GROUPS_INFO)) + return gp.get_data -def get_validation_group_name_list(): +def get_validation_group_name_list(groups_path=None): """Get the validation group name list only""" - results = [] - - groups = read_validation_groups_file() - - if groups and isinstance(dict, groups): - for grp_name in six.viewkeys(groups): - results.append(grp_name) - - return results + gp = Group((groups_path if groups_path else + constants.VALIDATION_GROUPS_INFO)) + return gp.get_groups_keys_list def get_new_validations_logs_on_disk(validations_logs_dir): diff --git a/validations_libs/run.py b/validations_libs/validation_actions.py similarity index 65% rename from validations_libs/run.py rename to validations_libs/validation_actions.py index c175e1d0..b1758786 100644 --- a/validations_libs/run.py +++ b/validations_libs/validation_actions.py @@ -17,16 +17,44 @@ import logging import os from validations_libs.ansible import Ansible as v_ansible +from validations_libs.group import Group from validations_libs import constants from validations_libs import utils as v_utils -LOG = logging.getLogger(__name__ + ".run") +LOG = logging.getLogger(__name__ + ".validation_actions") -class Run(object): +class ValidationActions(object): - def __init__(self): - self.log = logging.getLogger(__name__ + ".Run") + def __init__(self, validation_path=None, group=None): + self.log = logging.getLogger(__name__ + ".ValidationActions") + self.validation_path = (validation_path if validation_path + else constants.ANSIBLE_VALIDATION_DIR) + self.group = group + + def list_validations(self): + """List the available validations""" + self.log = logging.getLogger(__name__ + ".list_validations") + validations = v_utils.parse_all_validations_on_disk( + self.validation_path, self.group) + + return_values = [] + column_name = ('ID', 'Name', 'Groups') + + for val in validations: + return_values.append((val.get('id'), val.get('name'), + val.get('groups'))) + return (column_name, return_values) + + def show_validations(self, validation): + """Display detailed information about a Validation""" + self.log = logging.getLogger(__name__ + ".show_validations") + # Get validation data: + data = v_utils.get_validations_data(validation) + format = v_utils.get_validations_stats( + v_utils.parse_all_validations_logs_on_disk()) + data.update(format) + return data def run_validations(self, playbook=[], inventory='localhost', group=None, extra_vars=None, validations_dir=None, @@ -94,3 +122,17 @@ class Run(object): 'validation_id': validation_uuid }}) return results + + def group_information(self, groups): + """Get Information about Validation Groups""" + val_gp = Group(groups) + group = val_gp.get_formated_group + + group_info = [] + # Get validations number by groups + for gp in group: + validations = v_utils.parse_all_validations_on_disk( + constants.ANSIBLE_VALIDATION_DIR, gp[0]) + group_info.append((gp[0], gp[1], len(validations))) + column_name = ("Groups", "Description", "Number of Validations") + return (column_name, group_info)