Improved test coverage for tripleo validator

The validator run subcommand which was previously without
any unit test coverage now has it's basic operation mode tested.

Assertions are placed on calls and return values of the parser.

The existing test structure was improved with enabling of autospec
on mocked API.

Closes-Bug: #1970387

Signed-off-by: Jiri Podivin <jpodivin@redhat.com>
Change-Id: I008ac81cd803adbd08a9509902e46967f8b7d2e4
This commit is contained in:
Jiri Podivin 2022-04-27 12:18:02 +02:00
parent ea8795d005
commit 6a9db445d6
2 changed files with 50 additions and 1 deletions

View File

@ -148,6 +148,14 @@ VALIDATIONS_LOGS_CONTENTS_LIST = [{
'validation_output': []
}]
FAKE_SUCCESS_RUN = [{'Duration': '0:00:01.761',
'Host_Group': 'overcloud',
'Status': 'PASSED',
'Status_by_Host': 'subnode-1,PASSED, subnode-2,PASSED',
'UUID': '123',
'Unreachable_Hosts': '',
'Validations': 'foo'}]
class FakeOptions(object):
def __init__(self):

View File

@ -18,6 +18,7 @@ from unittest import mock
from tripleoclient.tests import base
from tripleoclient.v1 import tripleo_validator
from tripleoclient import constants
from tripleoclient.tests import fakes
@ -54,7 +55,8 @@ class TestValidatorGroupInfo(base.TestCommand):
self.cmd = tripleo_validator.TripleOValidatorGroupInfo(self.app, None)
@mock.patch('validations_libs.validation_actions.ValidationActions.'
'group_information', return_value=fakes.GROUPS_LIST)
'group_information', autospec=True,
return_value=fakes.GROUPS_LIST)
def test_show_group_info(self, mock_validations):
arglist = []
verifylist = []
@ -74,6 +76,7 @@ class TestValidatorList(base.TestCommand):
@mock.patch('validations_libs.validation_actions.ValidationActions.'
'list_validations',
autospec=True,
return_value=fakes.VALIDATIONS_LIST)
def test_validation_list_noargs(self, mock_validations):
arglist = []
@ -94,6 +97,7 @@ class TestValidatorShow(base.TestCommand):
@mock.patch('validations_libs.validation_actions.ValidationActions.'
'show_validations',
autospec=True,
return_value=fakes.VALIDATIONS_LIST[0])
def test_validation_show(self, mock_validations):
arglist = ['my_val1']
@ -115,6 +119,7 @@ class TestValidatorShowParameter(base.TestCommand):
@mock.patch('validations_libs.validation_actions.ValidationActions.'
'show_validations_parameters',
autospec=True,
return_value=fakes.VALIDATIONS_LIST[1])
def test_validation_show_parameter(self, mock_validations):
arglist = ['--validation', 'my_val2']
@ -136,6 +141,7 @@ class TestValidatorShowRun(base.TestCommand):
@mock.patch('validations_libs.validation_actions.ValidationLogs.'
'get_logfile_content_by_uuid',
autospec=True,
return_value=fakes.VALIDATIONS_LOGS_CONTENTS_LIST)
def test_validation_show_run(self, mock_validations):
arglist = ['008886df-d297-1eaa-2a74-000000000008']
@ -157,6 +163,7 @@ class TestValidatorShowHistory(base.TestCommand):
@mock.patch('validations_libs.validation_actions.ValidationActions.'
'show_history',
autospec=True,
return_value=fakes.VALIDATIONS_LOGS_CONTENTS_LIST)
def test_validation_show_history(self, mock_validations):
arglist = []
@ -168,6 +175,7 @@ class TestValidatorShowHistory(base.TestCommand):
@mock.patch('validations_libs.validation_actions.ValidationActions.'
'show_history',
autospec=True,
return_value=fakes.VALIDATIONS_LOGS_CONTENTS_LIST)
def test_validation_show_history_for_a_validation(self, mock_validations):
arglist = [
@ -179,3 +187,36 @@ class TestValidatorShowHistory(base.TestCommand):
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
class TestValidatorRun(base.TestCommand):
def setUp(self):
super().setUp()
self.cmd = tripleo_validator.TripleOValidatorRun(self.app, None)
@mock.patch('validations_libs.cli.run.common.print_dict', autospec=True)
@mock.patch('validations_libs.cli.run.common.write_output', autospec=True)
@mock.patch('validations_libs.validation_actions.ValidationActions.'
'run_validations',
return_value=fakes.FAKE_SUCCESS_RUN,
autospec=True)
def test_validation_run(self, mock_validations, mock_write_output,
mock_print_dict):
arglist = ['--validation', 'mock_validation']
verify_list = [
('validation_name', ['mock_validation']),
('validation_log_dir', constants.VALIDATIONS_LOG_BASEDIR)]
parsed_args = self.check_parser(self.cmd, arglist, verify_list)
self.cmd.take_action(parsed_args)
# The 'output.log' argument value isn't derived from CLI arguments
# but from the VF configuration file. Changes to it, or to the way
# it is handled, should be reflected here.
mock_write_output.assert_called_once_with(
'output.log', fakes.FAKE_SUCCESS_RUN)
mock_print_dict.assert_called_once_with(fakes.FAKE_SUCCESS_RUN)