Allow running validations by name using Ansible by default.

This patch switch over Ansible to run validations by names by
default. The --use-mistral argument will have to be used in order to
execute them through Mistral.

Co-Authored-By: Gaël Chamoulaud <gchamoul@redhat.com>
Change-Id: Ia393f4d776ab2c09439e7772b5596ddbb47e0a5e
This commit is contained in:
Carlos Camacho 2019-04-23 09:44:41 -04:00 committed by Gael Chamoulaud
parent c6d441b24c
commit 27e1e4f833
4 changed files with 110 additions and 29 deletions

View File

@ -131,10 +131,8 @@ class TestRunAnsiblePlaybook(TestCase):
mock_process.returncode = 0
mock_run.return_value = mock_process
retcode = utils.run_ansible_playbook(self.mock_log,
'/tmp',
'existing.yaml',
'localhost,')
retcode, output = utils.run_ansible_playbook(
self.mock_log, '/tmp', 'existing.yaml', 'localhost,')
self.assertEqual(retcode, 0)
mock_exists.assert_called_once_with('/tmp/existing.yaml')
@ -176,9 +174,12 @@ class TestRunAnsiblePlaybook(TestCase):
mock_process.returncode = 0
mock_run.return_value = mock_process
retcode = utils.run_ansible_playbook(self.mock_log, '/tmp',
'existing.yaml', 'localhost,',
ansible_config='/tmp/foo.cfg')
retcode, output = utils.run_ansible_playbook(
self.mock_log,
'/tmp',
'existing.yaml',
'localhost,',
ansible_config='/tmp/foo.cfg')
self.assertEqual(retcode, 0)
mock_isabs.assert_called_once_with('/tmp/foo.cfg')
@ -226,10 +227,12 @@ class TestRunAnsiblePlaybook(TestCase):
mock_process.returncode = 0
mock_run.return_value = mock_process
retcode = utils.run_ansible_playbook(self.mock_log, '/tmp',
'existing.yaml',
'localhost,',
connection='local')
retcode, output = utils.run_ansible_playbook(
self.mock_log,
'/tmp',
'existing.yaml',
'localhost,',
connection='local')
self.assertEqual(retcode, 0)
mock_exists.assert_called_once_with('/tmp/existing.yaml')
env = os.environ.copy()

View File

@ -14,6 +14,7 @@
#
import mock
import sys
from osc_lib.tests import utils
from tripleoclient.v1 import tripleo_validator
@ -54,8 +55,9 @@ class TestValidatorRun(utils.TestCommand):
@mock.patch('tripleoclient.workflows.validations.run_validations',
autospec=True)
def test_validation_run_withargs(self, plan_mock):
def test_validation_run_with_mistral(self, plan_mock):
arglist = [
'--use-mistral',
'--validation-name',
'check-ftype'
]
@ -68,3 +70,39 @@ class TestValidatorRun(utils.TestCommand):
mock.ANY,
{'plan': 'overcloud',
'validation_names': ['check-ftype']})
@mock.patch('logging.getLogger')
@mock.patch('pwd.getpwuid')
@mock.patch('os.getuid')
@mock.patch('tripleoclient.utils.run_ansible_playbook',
autospec=True)
def test_validation_run_with_ansible(self, plan_mock, mock_getuid,
mock_getpwuid, mock_logger):
mock_pwuid = mock.Mock()
mock_pwuid.pw_dir = '/home/stack'
mock_getpwuid.return_value = mock_pwuid
mock_log = mock.Mock()
mock_logger.return_value = mock_log
playbooks_dir = '/usr/share/openstack-tripleo-validations/playbooks'
arglist = [
'--validation-name',
'check-ftype'
]
verifylist = []
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
plan_mock.assert_called_once_with(
logger=mock_log,
connection='local',
inventory='/usr/bin/tripleo-ansible-inventory',
workdir=playbooks_dir,
log_path_dir='/home/stack',
playbook='check-ftype.yaml',
retries=False,
output_callback='validation_output',
python_interpreter='/usr/bin/python{}'.format(sys.version_info[0])
)

View File

@ -216,7 +216,7 @@ def run_ansible_playbook(logger,
cleanup and os.unlink(tmp_config)
if proc.returncode != 0:
raise RuntimeError(proc.stdout.read())
return proc.returncode
return proc.returncode, proc.stdout.read()
else:
cleanup and os.unlink(tmp_config)
raise RuntimeError('No such playbook: %s' % play)

View File

@ -15,6 +15,9 @@
import argparse
import logging
import os
import pwd
import sys
from osc_lib.command import command
from osc_lib.i18n import _
@ -123,6 +126,15 @@ class TripleOValidatorRun(command.Command):
"Defaults to: overcloud")
)
parser.add_argument(
'--use-mistral',
action='store_true',
default=False,
help=_("Execute the validations using "
"Mistral. "
"Defaults to: false")
)
ex_group = parser.add_mutually_exclusive_group(required=True)
ex_group.add_argument(
@ -155,25 +167,53 @@ class TripleOValidatorRun(command.Command):
def _run_validator_run(self, parsed_args):
clients = self.app.client_manager
LOG = logging.getLogger(__name__ + ".ValidationsRun")
if not parsed_args.validation_name:
workflow_input = {
"plan": parsed_args.plan,
"group_names": parsed_args.group
}
if parsed_args.use_mistral or parsed_args.group:
if not parsed_args.validation_name:
workflow_input = {
"plan": parsed_args.plan,
"group_names": parsed_args.group
}
else:
workflow_input = {
"plan": parsed_args.plan,
"validation_names": parsed_args.validation_name
}
LOG.debug(_('Running the validations with Mistral'))
output = validations.run_validations(clients, workflow_input)
for out in output:
print('[{}] - {}\n{}'.format(
out.get('status'),
out.get('validation_name'),
oooutils.indent(out.get('stdout'))))
else:
workflow_input = {
"plan": parsed_args.plan,
"validation_names": parsed_args.validation_name
}
playbooks = []
for pb in parsed_args.validation_name:
playbooks.append(pb + '.yaml')
LOG.debug(_('Runnning the validations'))
output = validations.run_validations(clients, workflow_input)
for out in output:
print('[{}] - {}\n{}'.format(
out.get('status'),
out.get('validation_name'),
oooutils.indent(out.get('stdout'))))
python_interpreter = \
"/usr/bin/python{}".format(sys.version_info[0])
for playbook in playbooks:
try:
LOG.debug(_('Running the validations with Ansible'))
rc, output = oooutils.run_ansible_playbook(
logger=LOG,
workdir=constants.ANSIBLE_VALIDATION_DIR,
log_path_dir=pwd.getpwuid(os.getuid()).pw_dir,
playbook=playbook,
inventory='/usr/bin/tripleo-ansible-inventory',
retries=False,
connection='local',
output_callback='validation_output',
python_interpreter=python_interpreter)
print('[SUCCESS] - {}\n{}'.format(
playbook, oooutils.indent(output)))
except Exception as e:
print('[FAILED] - {}\n{}'.format(
playbook, oooutils.indent(e.args[0])))
def take_action(self, parsed_args):
self._run_validator_run(parsed_args)