Run validations with parameters from a file

Resolves: rhbz#2122209

Signed-off-by: Veronika Fisarova <vfisarov@redhat.com>
Change-Id: Ifc6c28003c4c2c5f3dd6198e650f9713a02dc82d
This commit is contained in:
Veronika Fisarova 2022-11-11 15:22:09 +01:00
parent b520719f77
commit 17f641ebdf
5 changed files with 284 additions and 0 deletions

View File

@ -0,0 +1,55 @@
---
# This file is generated by the `validation ... ` CLI.
#
# As shown in this template, you can specify validation(s) of you choice by the
# following options:
#
# validation(s), group(s), product(s) and category(ies) included in the run
# + parameters to each specific validation,
# validation, group(s), product(s), category(ies) excluded in the run,
#
# optional arguments for the run,
# e.g.:
# --config
# --limit
# --validation-dir
# and other.
#
# Delete the comment sign for the use of the required action. Add the '-' sign for
# including, respectively excluding, more items on the list.
#
#
#Example:
#
#Note: Skip list isn't included in the run_arguments list because it has the same
#functionality as the exclude_validation parameter.
#
#
include_validation:
- check-rhsm-version
include_group:
- prep
- pre-deployment
# include_category:
# -
# include_product:
# -
# exclude_validation:
# - fips-enabled
# exclude_group:
# -
# exclude_category:
# -
# exclude_product:
# -
config: CONFIG_PATH
limit: undercloud-0, undercloud-1
ssh-user: SSH_USER
validation-dir: VALIDATION_DIR
ansible-base-dir: ANSIBLE_BASE_DIR
validation-log-dir: VALIDATION_LOG_DIR
inventory: INVENTORY_DIR
output-log: foo
python-interpreter: PYTHON_INTERPRETER_PATH
extra-env-vars: key1=val1, key2=val1
extra-vars-file: JSON/YAML_PATH

View File

@ -40,6 +40,7 @@ validation.cli:
show_group = validations_libs.cli.show:ShowGroup
show_parameter = validations_libs.cli.show:ShowParameter
run = validations_libs.cli.run:Run
file = validations_libs.cli.file:File
history_list = validations_libs.cli.history:ListHistory
history_get = validations_libs.cli.history:GetHistory
init = validations_libs.cli.community:CommunityValidationInit

View File

@ -0,0 +1,79 @@
from validations_libs.cli import common
from validations_libs.cli.base import BaseCommand
from validations_libs.validation_actions import ValidationActions
from validations_libs.exceptions import ValidationRunException
class File(BaseCommand):
"""Include validations by name(s), group(s), category(ies) or by product(s),
or exclude validations by name(s), group(s), category(ies) or by product(s),
and run them from File"""
def get_parser(self, parser):
"""Argument parser ..."""
parser = super(File, self).get_parser(parser)
parser.add_argument(
'--path-to-file',
dest='path_to_file',
required=True,
default=None,
help=("The path where the YAML file is stored.\n"))
parser.add_argument(
'--junitxml',
dest='junitxml',
default=None,
help=("Path where the run result in JUnitXML "
"format will be stored.\n")
)
return parser
def take_action(self, parsed_args):
"""Take action"""
self.base.set_argument_parser(self, parsed_args)
if parsed_args.path_to_file:
try:
yaml_file = common.read_cli_data_file(parsed_args.path_to_file)
if not isinstance(yaml_file, dict):
raise ValidationRunException("Wrong format of the File.")
except (FileNotFoundError) as e:
raise FileNotFoundError(e)
v_actions = ValidationActions(yaml_file.get('validation-dir'),
log_path=yaml_file.get('validation-log-dir'))
##Rozsirit metodu run_validations o exclude...
# Solve the skip_list
try:
results = v_actions.run_validations(
validation_name=yaml_file.get('include_validation'),
group=yaml_file.get('incldue_group'),
category=yaml_file.get('include_category'),
product=yaml_file.get('include_product'),
validation_config=yaml_file.get('config'),
limit_hosts=yaml_file.get('limit'),
ssh_user=yaml_file.get('ssh-user'),
validations_dir=yaml_file.get('validation-dir'),
inventory=yaml_file.get('inventory'),
base_dir=yaml_file.get('ansible-base-dir'),
python_interpreter=yaml_file.get('python-interpreter'),
extra_vars=yaml_file.get('extra-vars-file'),
extra_env_vars=yaml_file.get('extra-env-vars')
)
except (RuntimeError, ValidationRunException) as e:
raise ValidationRunException(e)
if results:
failed_rc = any([r for r in results if r['Status'] == 'FAILED'])
if yaml_file.get('output-log'):
common.write_output(yaml_file.get('output-log'), results)
if parsed_args.junitxml:
common.write_junitxml(parsed_args.junitxml, results)
common.print_dict(results)
if failed_rc:
raise ValidationRunException("One or more validations have failed.")
else:
msg = ("No validation has been run, please check "
"log in the Ansible working directory.")
raise ValidationRunException(msg)

View File

@ -0,0 +1,131 @@
# Copyright 2021 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 sys
import copy
try:
from unittest import mock
except ImportError:
import mock
from validations_libs.cli import file
from validations_libs.exceptions import ValidationRunException
from validations_libs.tests import fakes
from validations_libs.tests.cli.fakes import BaseCommand
class TestRun(BaseCommand):
maxDiff = None
def setUp(self):
super(TestRun, self).setUp()
self.cmd = file.File(self.app, None)
@mock.patch('validations_libs.validation_actions.ValidationActions.'
'run_validations',
return_value=copy.deepcopy(fakes.FAKE_SUCCESS_RUN),
autospec=True)
def test_run_validation_success(self, mock_run):
args = self._set_args(['--path-to-file', 'preliminary-file-structure.yaml'])
verifylist = [('path_to_file', 'preliminary-file-structure.yaml')]
parsed_args = self.check_parser(self.cmd, args, verifylist)
self.cmd.take_action(parsed_args)
@mock.patch('os.path.exists', return_value=True)
@mock.patch('validations_libs.validation_actions.ValidationActions.'
'run_validations',
return_value=copy.deepcopy(fakes.FAKE_SUCCESS_RUN),
autospec=True)
def test_run_validation_success_with_junitxml(self, mock_run, mock_exists):
args = self._set_args(['--path-to-file', 'preliminary-file-structure.yaml',
'--junitxml', 'foo'])
verifylist = [('path_to_file', 'preliminary-file-structure.yaml'),
('junitxml', 'foo')]
parsed_args = self.check_parser(self.cmd, args, verifylist)
self.cmd.take_action(parsed_args)
def test_run_validation_cmd_parser_error(self):
args = self._set_args(
['foo', 'preliminary-file-structure.yaml'])
verifylist = [('path_to_file', 'preliminary-file-structure.yaml')]
self.assertRaises(Exception, self.check_parser, self.cmd,
args, verifylist)
@mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR')
@mock.patch('validations_libs.validation_actions.ValidationActions.'
'run_validations',
return_value=copy.deepcopy(fakes.FAKE_FAILED_RUN),
autospec=True)
def test_run_validation_failed_validation(self, mock_run, mock_exists):
args = self._set_args(['--path-to-file', 'preliminary-file-structure.yaml'])
verifylist = [
('path_to_file', 'preliminary-file-structure.yaml')]
parsed_args = self.check_parser(self.cmd, args, verifylist)
self.assertRaises(ValidationRunException,
self.cmd.take_action, parsed_args)
@mock.patch('validations_libs.validation_actions.ValidationActions.'
'run_validations',
return_value=copy.deepcopy(fakes.FAKE_FAILED_RUN),
autospec=True)
@mock.patch('os.path.exists', return_values=True)
def test_run_validation_failed_validation_junitxml_module_disabled(self, mock_exists,
mock_run):
args = self._set_args(['--path-to-file', 'preliminary-file-structure.yaml',
'--junitxml', 'foo'])
verifylist = [('path_to_file', 'preliminary-file-structure.yaml'),
('junitxml', 'foo')]
parsed_args = self.check_parser(self.cmd, args, verifylist)
self.assertRaises(ValidationRunException,
self.cmd.take_action, parsed_args)
@mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR')
@mock.patch('validations_libs.validation_actions.ValidationActions.'
'run_validations',
return_value=copy.deepcopy(fakes.FAKE_FAILED_RUN),
autospec=True)
@mock.patch('validations_libs.cli.common.write_junitxml', return_value={})
@mock.patch('os.path.exists', return_values=True)
def test_run_validation_failed_validation_junitxml_success(self, mock_junitxml,
mock_junitxml_module,
mock_run, mock_log_dir
):
args = self._set_args(['--path-to-file', 'preliminary-file-structure.yaml',
'--junitxml', 'foo'])
verifylist = [('path_to_file', 'preliminary-file-structure.yaml'),
('junitxml', 'foo')]
parsed_args = self.check_parser(self.cmd, args, verifylist)
self.assertRaises(ValidationRunException,
self.cmd.take_action, parsed_args)
# Deff test with all arguments in the YAML file
@mock.patch('os.path.exists', return_value=True)
@mock.patch('validations_libs.validation_actions.ValidationActions.'
'run_validations',
return_value=copy.deepcopy(fakes.FAKE_SUCCESS_RUN),
autospec=True)
def test_run_validation_success_all_arguments(self, mock_run, mock_exists):
args = self._set_args(['--path-to-file', 'preliminary-file-structure.yaml',
'--junitxml', 'foo'])
verifylist = [('path_to_file', 'preliminary-file-structure.yaml'),
('junitxml', 'foo')]
parsed_args = self.check_parser(self.cmd, args, verifylist)
self.cmd.take_action(parsed_args)

View File

@ -21,6 +21,7 @@ import yaml
from validations_libs.ansible import Ansible as v_ansible
from validations_libs.group import Group
from validations_libs.cli.common import Spinner
from validations_libs.validation import Validation
from validations_libs.validation_logs import ValidationLogs, ValidationLog
from validations_libs import constants
from validations_libs import utils as v_utils
@ -876,3 +877,20 @@ class ValidationActions:
values.append((task['name'], host, task['status'],
task['hosts'][host]))
return (column_name, values)
# def run_file_validations(self, validation_name=None,
# group=None, category=None, product=None,
# excluded_validaton=None, excluded_group=None,
# excluded_category=None, excluded_product=None,
# run_arguments=None,
# ):
# """
# Docstring...
# """
# self.log = logging.getLogger(__name__ + ".run_validations")
# validations_dir = (validations_dir if validations_dir
# else self.validation_path)
# return None