2021-03-23 18:33:04 +01:00
|
|
|
# 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.
|
|
|
|
#
|
2021-05-12 07:03:36 +02:00
|
|
|
import sys
|
Test cross contamination prevention
The existing tests were using shared fakes as return value
of certain mocks. Unfortunately, while perfectly fine in most cases,
certain methods change values passed in place.
This created a race condition, which caused fluctuation in the reported
coverage of the relevant modules.
The undesirable test behavior only becomes apparent when the tests
are run in a succession and their behavior is analyzed, for example
during the run of the recently implemented 'coverchange' job.
Final statistics of the coverage measurement, displaying the totals
for number of lines, lines without coverage, branches etc., display
following pattern. Note the third column.
TOTAL 995 105 340 56 86%
TOTAL 995 105 340 56 86%
TOTAL 995 105 340 56 86%
TOTAL 995 108 340 56 86%
TOTAL 995 108 340 56 86%
TOTAL 995 105 340 56 86%
TOTAL 995 105 340 56 86%
TOTAL 995 108 340 56 86%
TOTAL 995 105 340 56 86%
Number of lines without coverage oscillates between '108' and '105' depending
on the order in which the tests were run. The culprit being one of the functions
of the 'validations_libs.cli.common' module, which edits data passed in place.
As the data processed are passed from patched object, and the data itself are
defined withing the relevant 'fakes' module, it emerges with inevitability,
that the tested code operates on data previously altered by the tests.
Furthermore the 'test_run_command_failed_validation' test was augmented
to place further assertion on the arguments passed.
Closes-Bug: #1935003
Signed-off-by: Jiri Podivin <jpodivin@redhat.com>
Change-Id: I2210fa1775691dd503eb882a24824eaf18d7bd3e
2021-07-07 10:24:53 +02:00
|
|
|
import copy
|
2021-03-23 18:33:04 +01:00
|
|
|
try:
|
|
|
|
from unittest import mock
|
|
|
|
except ImportError:
|
|
|
|
import mock
|
|
|
|
|
|
|
|
from validations_libs.cli import run
|
2022-03-18 12:31:18 +01:00
|
|
|
from validations_libs.exceptions import ValidationRunException
|
2021-03-23 18:33:04 +01:00
|
|
|
from validations_libs.tests import fakes
|
|
|
|
from validations_libs.tests.cli.fakes import BaseCommand
|
|
|
|
|
|
|
|
|
|
|
|
class TestRun(BaseCommand):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(TestRun, self).setUp()
|
|
|
|
self.cmd = run.Run(self.app, None)
|
|
|
|
|
|
|
|
@mock.patch('validations_libs.validation_actions.ValidationActions.'
|
|
|
|
'run_validations',
|
2021-07-28 16:48:40 +02:00
|
|
|
return_value=None,
|
|
|
|
autospec=True)
|
2021-03-23 18:33:04 +01:00
|
|
|
def test_run_command_return_none(self, mock_run):
|
2021-05-20 19:09:37 +02:00
|
|
|
args = self._set_args(['--validation', 'foo'])
|
2021-03-23 18:33:04 +01:00
|
|
|
verifylist = [('validation_name', ['foo'])]
|
|
|
|
|
2021-05-20 19:09:37 +02:00
|
|
|
parsed_args = self.check_parser(self.cmd, args, verifylist)
|
2022-03-18 12:31:18 +01:00
|
|
|
self.assertRaises(ValidationRunException, self.cmd.take_action, parsed_args)
|
2021-03-23 18:33:04 +01:00
|
|
|
|
2021-11-25 13:28:15 +01:00
|
|
|
@mock.patch('validations_libs.cli.common.open')
|
2021-03-23 18:33:04 +01:00
|
|
|
@mock.patch('validations_libs.validation_actions.ValidationActions.'
|
|
|
|
'run_validations',
|
2021-07-28 16:48:40 +02:00
|
|
|
return_value=copy.deepcopy(fakes.FAKE_SUCCESS_RUN),
|
|
|
|
autospec=True)
|
2021-11-25 13:28:15 +01:00
|
|
|
def test_run_command_success(self, mock_run, mock_open):
|
2021-05-20 19:09:37 +02:00
|
|
|
args = self._set_args(['--validation', 'foo'])
|
2021-03-23 18:33:04 +01:00
|
|
|
verifylist = [('validation_name', ['foo'])]
|
|
|
|
|
2021-05-20 19:09:37 +02:00
|
|
|
parsed_args = self.check_parser(self.cmd, args, verifylist)
|
2021-03-23 18:33:04 +01:00
|
|
|
self.cmd.take_action(parsed_args)
|
|
|
|
|
|
|
|
def test_run_command_exclusive_group(self):
|
|
|
|
arglist = ['--validation', 'foo', '--group', 'bar']
|
2021-05-20 19:09:37 +02:00
|
|
|
self._set_args(arglist)
|
2021-03-23 18:33:04 +01:00
|
|
|
verifylist = [('validation_name', ['foo'], 'group', 'bar')]
|
|
|
|
|
|
|
|
self.assertRaises(Exception, self.check_parser, self.cmd,
|
|
|
|
arglist, verifylist)
|
|
|
|
|
2021-05-05 11:18:27 +02:00
|
|
|
@mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR')
|
2021-03-23 18:33:04 +01:00
|
|
|
@mock.patch('validations_libs.cli.common.print_dict')
|
|
|
|
@mock.patch('getpass.getuser',
|
|
|
|
return_value='doe')
|
|
|
|
@mock.patch('validations_libs.validation_actions.ValidationActions.'
|
|
|
|
'run_validations',
|
2021-07-28 16:48:40 +02:00
|
|
|
return_value=copy.deepcopy(fakes.FAKE_SUCCESS_RUN),
|
|
|
|
autospec=True)
|
2021-05-20 19:09:37 +02:00
|
|
|
@mock.patch('validations_libs.utils.load_config', return_value={})
|
2021-07-28 16:48:40 +02:00
|
|
|
def test_run_command_extra_vars(self, mock_config,
|
|
|
|
mock_run, mock_user,
|
|
|
|
mock_print, mock_log_dir):
|
2021-03-23 18:33:04 +01:00
|
|
|
run_called_args = {
|
|
|
|
'inventory': 'localhost',
|
|
|
|
'limit_hosts': None,
|
|
|
|
'group': [],
|
2021-07-08 12:31:38 +02:00
|
|
|
'category': [],
|
2021-07-12 13:58:04 +02:00
|
|
|
'product': [],
|
2021-03-23 18:33:04 +01:00
|
|
|
'extra_vars': {'key': 'value'},
|
|
|
|
'validations_dir': '/usr/share/ansible/validation-playbooks',
|
2021-01-20 16:04:53 +01:00
|
|
|
'base_dir': '/usr/share/ansible',
|
2021-03-23 18:33:04 +01:00
|
|
|
'validation_name': ['foo'],
|
|
|
|
'extra_env_vars': None,
|
2021-05-12 07:03:36 +02:00
|
|
|
'python_interpreter': sys.executable,
|
2021-03-23 18:33:04 +01:00
|
|
|
'quiet': True,
|
2021-05-05 11:18:27 +02:00
|
|
|
'ssh_user': 'doe',
|
2021-09-15 23:01:13 +02:00
|
|
|
'validation_config': {},
|
|
|
|
'skip_list': None
|
2021-05-20 19:09:37 +02:00
|
|
|
}
|
2021-03-23 18:33:04 +01:00
|
|
|
|
|
|
|
arglist = ['--validation', 'foo',
|
|
|
|
'--extra-vars', 'key=value']
|
|
|
|
verifylist = [('validation_name', ['foo']),
|
|
|
|
('extra_vars', {'key': 'value'})]
|
2021-05-20 19:09:37 +02:00
|
|
|
self._set_args(arglist)
|
2021-03-23 18:33:04 +01:00
|
|
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
|
|
|
self.cmd.take_action(parsed_args)
|
2021-07-28 16:48:40 +02:00
|
|
|
call_args = mock_run.mock_calls[0][2]
|
|
|
|
|
|
|
|
self.assertDictEqual(call_args, run_called_args)
|
2021-03-23 18:33:04 +01:00
|
|
|
|
2021-05-05 11:18:27 +02:00
|
|
|
@mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR')
|
2021-03-23 18:33:04 +01:00
|
|
|
@mock.patch('validations_libs.cli.common.print_dict')
|
|
|
|
@mock.patch('getpass.getuser',
|
|
|
|
return_value='doe')
|
|
|
|
@mock.patch('validations_libs.validation_actions.ValidationActions.'
|
|
|
|
'run_validations',
|
2021-07-28 16:48:40 +02:00
|
|
|
return_value=copy.deepcopy(fakes.FAKE_SUCCESS_RUN),
|
|
|
|
autospec=True)
|
2021-05-20 19:09:37 +02:00
|
|
|
@mock.patch('validations_libs.utils.load_config', return_value={})
|
2021-07-28 16:48:40 +02:00
|
|
|
def test_run_command_extra_vars_twice(self, mock_config, mock_run,
|
|
|
|
mock_user, mock_print,
|
2021-05-20 19:09:37 +02:00
|
|
|
mock_log_dir):
|
2021-03-23 18:33:04 +01:00
|
|
|
run_called_args = {
|
|
|
|
'inventory': 'localhost',
|
|
|
|
'limit_hosts': None,
|
|
|
|
'group': [],
|
2021-07-08 12:31:38 +02:00
|
|
|
'category': [],
|
2021-07-12 13:58:04 +02:00
|
|
|
'product': [],
|
2021-03-23 18:33:04 +01:00
|
|
|
'extra_vars': {'key': 'value2'},
|
|
|
|
'validations_dir': '/usr/share/ansible/validation-playbooks',
|
2021-01-20 16:04:53 +01:00
|
|
|
'base_dir': '/usr/share/ansible',
|
2021-03-23 18:33:04 +01:00
|
|
|
'validation_name': ['foo'],
|
|
|
|
'extra_env_vars': None,
|
2021-05-12 07:03:36 +02:00
|
|
|
'python_interpreter': sys.executable,
|
2021-03-23 18:33:04 +01:00
|
|
|
'quiet': True,
|
2021-05-05 11:18:27 +02:00
|
|
|
'ssh_user': 'doe',
|
2021-09-15 23:01:13 +02:00
|
|
|
'validation_config': {},
|
|
|
|
'skip_list': None
|
2021-05-20 19:09:37 +02:00
|
|
|
}
|
2021-03-23 18:33:04 +01:00
|
|
|
|
|
|
|
arglist = ['--validation', 'foo',
|
|
|
|
'--extra-vars', 'key=value1',
|
|
|
|
'--extra-vars', 'key=value2']
|
|
|
|
verifylist = [('validation_name', ['foo']),
|
|
|
|
('extra_vars', {'key': 'value2'})]
|
2021-05-20 19:09:37 +02:00
|
|
|
self._set_args(arglist)
|
2021-03-23 18:33:04 +01:00
|
|
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
|
|
|
self.cmd.take_action(parsed_args)
|
2021-07-28 16:48:40 +02:00
|
|
|
call_args = mock_run.mock_calls[0][2]
|
|
|
|
|
|
|
|
self.assertDictEqual(call_args, run_called_args)
|
2021-03-23 18:33:04 +01:00
|
|
|
|
|
|
|
def test_run_command_exclusive_vars(self):
|
|
|
|
arglist = ['--validation', 'foo',
|
|
|
|
'--extra-vars', 'key=value1',
|
|
|
|
'--extra-vars-file', '/foo/vars.yaml']
|
|
|
|
verifylist = [('validation_name', ['foo']),
|
|
|
|
('extra_vars', {'key': 'value2'})]
|
|
|
|
|
|
|
|
self.assertRaises(Exception, self.check_parser, self.cmd,
|
|
|
|
arglist, verifylist)
|
|
|
|
|
2021-05-05 11:18:27 +02:00
|
|
|
@mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR')
|
2021-03-23 18:33:04 +01:00
|
|
|
@mock.patch('yaml.safe_load', return_value={'key': 'value'})
|
2022-05-10 01:11:03 +09:00
|
|
|
@mock.patch('builtins.open')
|
2021-03-23 18:33:04 +01:00
|
|
|
@mock.patch('getpass.getuser',
|
|
|
|
return_value='doe')
|
|
|
|
@mock.patch('validations_libs.validation_actions.ValidationActions.'
|
|
|
|
'run_validations',
|
2021-07-28 16:48:40 +02:00
|
|
|
return_value=copy.deepcopy(fakes.FAKE_SUCCESS_RUN),
|
|
|
|
autospec=True)
|
2021-05-20 19:09:37 +02:00
|
|
|
@mock.patch('validations_libs.utils.load_config', return_value={})
|
|
|
|
def test_run_command_extra_vars_file(self, mock_config, mock_run,
|
|
|
|
mock_user, mock_open,
|
2021-05-05 11:18:27 +02:00
|
|
|
mock_yaml, mock_log_dir):
|
|
|
|
|
2021-03-23 18:33:04 +01:00
|
|
|
run_called_args = {
|
|
|
|
'inventory': 'localhost',
|
|
|
|
'limit_hosts': None,
|
|
|
|
'group': [],
|
2021-07-08 12:31:38 +02:00
|
|
|
'category': [],
|
2021-07-12 13:58:04 +02:00
|
|
|
'product': [],
|
2021-03-23 18:33:04 +01:00
|
|
|
'extra_vars': {'key': 'value'},
|
|
|
|
'validations_dir': '/usr/share/ansible/validation-playbooks',
|
2021-01-20 16:04:53 +01:00
|
|
|
'base_dir': '/usr/share/ansible',
|
2021-03-23 18:33:04 +01:00
|
|
|
'validation_name': ['foo'],
|
|
|
|
'extra_env_vars': None,
|
2021-05-12 07:03:36 +02:00
|
|
|
'python_interpreter': sys.executable,
|
2021-03-23 18:33:04 +01:00
|
|
|
'quiet': True,
|
2021-05-05 11:18:27 +02:00
|
|
|
'ssh_user': 'doe',
|
2021-09-15 23:01:13 +02:00
|
|
|
'validation_config': {},
|
|
|
|
'skip_list': None
|
2021-05-20 19:09:37 +02:00
|
|
|
}
|
2021-03-23 18:33:04 +01:00
|
|
|
|
|
|
|
arglist = ['--validation', 'foo',
|
|
|
|
'--extra-vars-file', '/foo/vars.yaml']
|
|
|
|
verifylist = [('validation_name', ['foo']),
|
|
|
|
('extra_vars_file', '/foo/vars.yaml')]
|
2021-05-20 19:09:37 +02:00
|
|
|
self._set_args(arglist)
|
2021-03-23 18:33:04 +01:00
|
|
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
|
|
|
self.cmd.take_action(parsed_args)
|
2021-07-28 16:48:40 +02:00
|
|
|
call_args = mock_run.mock_calls[0][2]
|
|
|
|
|
|
|
|
self.assertDictEqual(call_args, run_called_args)
|
2021-03-23 18:33:04 +01:00
|
|
|
|
2021-05-05 11:18:27 +02:00
|
|
|
@mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR')
|
2021-03-23 18:33:04 +01:00
|
|
|
@mock.patch('getpass.getuser',
|
|
|
|
return_value='doe')
|
|
|
|
@mock.patch('validations_libs.validation_actions.ValidationActions.'
|
|
|
|
'run_validations',
|
2021-07-28 16:48:40 +02:00
|
|
|
return_value=copy.deepcopy(fakes.FAKE_SUCCESS_RUN),
|
|
|
|
autospec=True)
|
2021-05-20 19:09:37 +02:00
|
|
|
@mock.patch('validations_libs.utils.load_config', return_value={})
|
|
|
|
def test_run_command_extra_env_vars(self, mock_config, mock_run,
|
|
|
|
mock_user, mock_log_dir):
|
2021-03-23 18:33:04 +01:00
|
|
|
run_called_args = {
|
|
|
|
'inventory': 'localhost',
|
|
|
|
'limit_hosts': None,
|
|
|
|
'group': [],
|
2021-07-08 12:31:38 +02:00
|
|
|
'category': [],
|
2021-07-12 13:58:04 +02:00
|
|
|
'product': [],
|
2021-03-23 18:33:04 +01:00
|
|
|
'extra_vars': None,
|
|
|
|
'validations_dir': '/usr/share/ansible/validation-playbooks',
|
2021-01-20 16:04:53 +01:00
|
|
|
'base_dir': '/usr/share/ansible',
|
2021-03-23 18:33:04 +01:00
|
|
|
'validation_name': ['foo'],
|
|
|
|
'extra_env_vars': {'key': 'value'},
|
2021-05-12 07:03:36 +02:00
|
|
|
'python_interpreter': sys.executable,
|
2021-03-23 18:33:04 +01:00
|
|
|
'quiet': True,
|
2021-05-05 11:18:27 +02:00
|
|
|
'ssh_user': 'doe',
|
2021-09-15 23:01:13 +02:00
|
|
|
'validation_config': {},
|
|
|
|
'skip_list': None
|
2021-05-20 19:09:37 +02:00
|
|
|
}
|
2021-03-23 18:33:04 +01:00
|
|
|
|
|
|
|
arglist = ['--validation', 'foo',
|
|
|
|
'--extra-env-vars', 'key=value']
|
|
|
|
verifylist = [('validation_name', ['foo']),
|
|
|
|
('extra_env_vars', {'key': 'value'})]
|
2021-05-20 19:09:37 +02:00
|
|
|
self._set_args(arglist)
|
2021-03-23 18:33:04 +01:00
|
|
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
|
|
|
self.cmd.take_action(parsed_args)
|
2021-07-28 16:48:40 +02:00
|
|
|
call_args = mock_run.mock_calls[0][2]
|
|
|
|
|
|
|
|
self.assertDictEqual(call_args, run_called_args)
|
2021-03-23 18:33:04 +01:00
|
|
|
|
2021-05-05 11:18:27 +02:00
|
|
|
@mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR')
|
2021-05-11 10:37:32 +02:00
|
|
|
@mock.patch('getpass.getuser',
|
|
|
|
return_value='doe')
|
|
|
|
@mock.patch('validations_libs.validation_actions.ValidationActions.'
|
|
|
|
'run_validations',
|
2021-07-28 16:48:40 +02:00
|
|
|
return_value=copy.deepcopy(fakes.FAKE_SUCCESS_RUN),
|
|
|
|
autospec=True)
|
2021-05-20 19:09:37 +02:00
|
|
|
@mock.patch('validations_libs.utils.load_config', return_value={})
|
2021-05-11 10:37:32 +02:00
|
|
|
def test_run_command_extra_env_vars_with_custom_callback(self,
|
2021-05-20 19:09:37 +02:00
|
|
|
mock_config,
|
2021-05-11 10:37:32 +02:00
|
|
|
mock_run,
|
2021-05-05 11:18:27 +02:00
|
|
|
mock_user,
|
|
|
|
mock_log_dir):
|
2021-05-11 10:37:32 +02:00
|
|
|
run_called_args = {
|
|
|
|
'inventory': 'localhost',
|
|
|
|
'limit_hosts': None,
|
2021-05-05 11:18:27 +02:00
|
|
|
'quiet': False,
|
2021-05-11 10:37:32 +02:00
|
|
|
'group': [],
|
2021-07-08 12:31:38 +02:00
|
|
|
'category': [],
|
2021-07-12 13:58:04 +02:00
|
|
|
'product': [],
|
2021-05-11 10:37:32 +02:00
|
|
|
'extra_vars': None,
|
|
|
|
'validations_dir': '/usr/share/ansible/validation-playbooks',
|
2021-01-20 16:04:53 +01:00
|
|
|
'base_dir': '/usr/share/ansible',
|
2021-05-11 10:37:32 +02:00
|
|
|
'validation_name': ['foo'],
|
|
|
|
'extra_env_vars': {'ANSIBLE_STDOUT_CALLBACK': 'default'},
|
2021-05-12 07:03:36 +02:00
|
|
|
'python_interpreter': sys.executable,
|
2021-05-11 10:37:32 +02:00
|
|
|
'quiet': False,
|
2021-05-20 19:09:37 +02:00
|
|
|
'ssh_user': 'doe',
|
2021-09-15 23:01:13 +02:00
|
|
|
'validation_config': {},
|
|
|
|
'skip_list': None
|
2021-05-20 19:09:37 +02:00
|
|
|
}
|
2021-05-11 10:37:32 +02:00
|
|
|
|
|
|
|
arglist = ['--validation', 'foo',
|
|
|
|
'--extra-env-vars', 'ANSIBLE_STDOUT_CALLBACK=default']
|
|
|
|
verifylist = [('validation_name', ['foo']),
|
|
|
|
('extra_env_vars', {'ANSIBLE_STDOUT_CALLBACK': 'default'})]
|
2021-05-20 19:09:37 +02:00
|
|
|
self._set_args(arglist)
|
2021-05-11 10:37:32 +02:00
|
|
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
|
|
|
self.cmd.take_action(parsed_args)
|
2021-07-28 16:48:40 +02:00
|
|
|
call_args = mock_run.mock_calls[0][2]
|
|
|
|
|
|
|
|
self.assertDictEqual(call_args, run_called_args)
|
2021-05-11 10:37:32 +02:00
|
|
|
|
2021-05-05 11:18:27 +02:00
|
|
|
@mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR')
|
2021-03-23 18:33:04 +01:00
|
|
|
@mock.patch('getpass.getuser',
|
|
|
|
return_value='doe')
|
|
|
|
@mock.patch('validations_libs.validation_actions.ValidationActions.'
|
|
|
|
'run_validations',
|
2021-07-28 16:48:40 +02:00
|
|
|
return_value=copy.deepcopy(fakes.FAKE_SUCCESS_RUN),
|
|
|
|
autospec=True)
|
2021-05-20 19:09:37 +02:00
|
|
|
@mock.patch('validations_libs.utils.load_config', return_value={})
|
|
|
|
def test_run_command_extra_env_vars_twice(self, mock_config,
|
|
|
|
mock_run, mock_user,
|
|
|
|
mock_log_dir):
|
2021-03-23 18:33:04 +01:00
|
|
|
run_called_args = {
|
|
|
|
'inventory': 'localhost',
|
|
|
|
'limit_hosts': None,
|
|
|
|
'group': [],
|
2021-07-08 12:31:38 +02:00
|
|
|
'category': [],
|
2021-07-12 13:58:04 +02:00
|
|
|
'product': [],
|
2021-03-23 18:33:04 +01:00
|
|
|
'extra_vars': None,
|
|
|
|
'validations_dir': '/usr/share/ansible/validation-playbooks',
|
2021-01-20 16:04:53 +01:00
|
|
|
'base_dir': '/usr/share/ansible',
|
2021-03-23 18:33:04 +01:00
|
|
|
'validation_name': ['foo'],
|
|
|
|
'extra_env_vars': {'key': 'value2'},
|
2021-05-12 07:03:36 +02:00
|
|
|
'python_interpreter': sys.executable,
|
2021-03-23 18:33:04 +01:00
|
|
|
'quiet': True,
|
2021-05-05 11:18:27 +02:00
|
|
|
'ssh_user': 'doe',
|
2021-09-15 23:01:13 +02:00
|
|
|
'validation_config': {},
|
|
|
|
'skip_list': None
|
2021-05-20 19:09:37 +02:00
|
|
|
}
|
2021-03-23 18:33:04 +01:00
|
|
|
|
|
|
|
arglist = ['--validation', 'foo',
|
|
|
|
'--extra-env-vars', 'key=value1',
|
|
|
|
'--extra-env-vars', 'key=value2']
|
|
|
|
verifylist = [('validation_name', ['foo']),
|
|
|
|
('extra_env_vars', {'key': 'value2'})]
|
2021-05-20 19:09:37 +02:00
|
|
|
self._set_args(arglist)
|
2021-03-23 18:33:04 +01:00
|
|
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
|
|
|
self.cmd.take_action(parsed_args)
|
2021-07-28 16:48:40 +02:00
|
|
|
call_args = mock_run.mock_calls[0][2]
|
|
|
|
|
|
|
|
self.assertDictEqual(call_args, run_called_args)
|
2021-03-23 18:33:04 +01:00
|
|
|
|
2021-05-05 11:18:27 +02:00
|
|
|
@mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR')
|
2021-03-23 18:33:04 +01:00
|
|
|
@mock.patch('getpass.getuser',
|
|
|
|
return_value='doe')
|
|
|
|
@mock.patch('validations_libs.validation_actions.ValidationActions.'
|
|
|
|
'run_validations',
|
2021-07-28 16:48:40 +02:00
|
|
|
return_value=copy.deepcopy(fakes.FAKE_SUCCESS_RUN),
|
|
|
|
autospec=True)
|
2021-05-20 19:09:37 +02:00
|
|
|
@mock.patch('validations_libs.utils.load_config', return_value={})
|
2021-05-05 11:18:27 +02:00
|
|
|
def test_run_command_extra_env_vars_and_extra_vars(self,
|
2021-05-20 19:09:37 +02:00
|
|
|
mock_config,
|
2021-05-05 11:18:27 +02:00
|
|
|
mock_run,
|
|
|
|
mock_user,
|
|
|
|
mock_log_dir):
|
2021-03-23 18:33:04 +01:00
|
|
|
run_called_args = {
|
|
|
|
'inventory': 'localhost',
|
|
|
|
'limit_hosts': None,
|
|
|
|
'group': [],
|
2021-07-08 12:31:38 +02:00
|
|
|
'category': [],
|
2021-07-12 13:58:04 +02:00
|
|
|
'product': [],
|
2021-03-23 18:33:04 +01:00
|
|
|
'extra_vars': {'key': 'value'},
|
|
|
|
'validations_dir': '/usr/share/ansible/validation-playbooks',
|
2021-01-20 16:04:53 +01:00
|
|
|
'base_dir': '/usr/share/ansible',
|
2021-03-23 18:33:04 +01:00
|
|
|
'validation_name': ['foo'],
|
|
|
|
'extra_env_vars': {'key2': 'value2'},
|
2021-05-12 07:03:36 +02:00
|
|
|
'python_interpreter': sys.executable,
|
2021-03-23 18:33:04 +01:00
|
|
|
'quiet': True,
|
2021-05-05 11:18:27 +02:00
|
|
|
'ssh_user': 'doe',
|
2021-09-15 23:01:13 +02:00
|
|
|
'validation_config': {},
|
|
|
|
'skip_list': None
|
2021-05-20 19:09:37 +02:00
|
|
|
}
|
2021-03-23 18:33:04 +01:00
|
|
|
|
|
|
|
arglist = ['--validation', 'foo',
|
|
|
|
'--extra-vars', 'key=value',
|
|
|
|
'--extra-env-vars', 'key2=value2']
|
|
|
|
verifylist = [('validation_name', ['foo']),
|
|
|
|
('extra_vars', {'key': 'value'}),
|
|
|
|
('extra_env_vars', {'key2': 'value2'})]
|
2021-05-20 19:09:37 +02:00
|
|
|
self._set_args(arglist)
|
2021-03-23 18:33:04 +01:00
|
|
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
|
|
|
self.cmd.take_action(parsed_args)
|
2021-07-28 16:48:40 +02:00
|
|
|
call_args = mock_run.mock_calls[0][2]
|
|
|
|
|
|
|
|
self.assertDictEqual(call_args, run_called_args)
|
2021-03-23 18:33:04 +01:00
|
|
|
|
2021-07-28 16:48:40 +02:00
|
|
|
@mock.patch('validations_libs.utils.find_config_file',
|
|
|
|
return_value="/etc/validations_foo.cfg")
|
Test cross contamination prevention
The existing tests were using shared fakes as return value
of certain mocks. Unfortunately, while perfectly fine in most cases,
certain methods change values passed in place.
This created a race condition, which caused fluctuation in the reported
coverage of the relevant modules.
The undesirable test behavior only becomes apparent when the tests
are run in a succession and their behavior is analyzed, for example
during the run of the recently implemented 'coverchange' job.
Final statistics of the coverage measurement, displaying the totals
for number of lines, lines without coverage, branches etc., display
following pattern. Note the third column.
TOTAL 995 105 340 56 86%
TOTAL 995 105 340 56 86%
TOTAL 995 105 340 56 86%
TOTAL 995 108 340 56 86%
TOTAL 995 108 340 56 86%
TOTAL 995 105 340 56 86%
TOTAL 995 105 340 56 86%
TOTAL 995 108 340 56 86%
TOTAL 995 105 340 56 86%
Number of lines without coverage oscillates between '108' and '105' depending
on the order in which the tests were run. The culprit being one of the functions
of the 'validations_libs.cli.common' module, which edits data passed in place.
As the data processed are passed from patched object, and the data itself are
defined withing the relevant 'fakes' module, it emerges with inevitability,
that the tested code operates on data previously altered by the tests.
Furthermore the 'test_run_command_failed_validation' test was augmented
to place further assertion on the arguments passed.
Closes-Bug: #1935003
Signed-off-by: Jiri Podivin <jpodivin@redhat.com>
Change-Id: I2210fa1775691dd503eb882a24824eaf18d7bd3e
2021-07-07 10:24:53 +02:00
|
|
|
@mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR')
|
2021-03-23 18:33:04 +01:00
|
|
|
@mock.patch('getpass.getuser',
|
|
|
|
return_value='doe')
|
|
|
|
@mock.patch('validations_libs.validation_actions.ValidationActions.'
|
|
|
|
'run_validations',
|
2021-07-28 16:48:40 +02:00
|
|
|
return_value=copy.deepcopy(fakes.FAKE_FAILED_RUN),
|
|
|
|
autospec=True)
|
2021-05-20 19:09:37 +02:00
|
|
|
@mock.patch('validations_libs.utils.load_config', return_value={})
|
2021-07-28 16:48:40 +02:00
|
|
|
def test_run_command_failed_validation(self, mock_config, mock_run, mock_user,
|
|
|
|
mock_log_dir, mock_config_file):
|
2021-03-23 18:33:04 +01:00
|
|
|
run_called_args = {
|
|
|
|
'inventory': 'localhost',
|
|
|
|
'limit_hosts': None,
|
|
|
|
'group': [],
|
2021-07-08 12:31:38 +02:00
|
|
|
'category': [],
|
2021-07-12 13:58:04 +02:00
|
|
|
'product': [],
|
2021-07-28 16:48:40 +02:00
|
|
|
'extra_vars': {'key': 'value'},
|
2021-03-23 18:33:04 +01:00
|
|
|
'validations_dir': '/usr/share/ansible/validation-playbooks',
|
2021-01-20 16:04:53 +01:00
|
|
|
'base_dir': '/usr/share/ansible',
|
2021-03-23 18:33:04 +01:00
|
|
|
'validation_name': ['foo'],
|
2021-07-28 16:48:40 +02:00
|
|
|
'extra_env_vars': {'key2': 'value2'},
|
2021-05-12 07:03:36 +02:00
|
|
|
'python_interpreter': sys.executable,
|
2021-03-23 18:33:04 +01:00
|
|
|
'quiet': True,
|
Test cross contamination prevention
The existing tests were using shared fakes as return value
of certain mocks. Unfortunately, while perfectly fine in most cases,
certain methods change values passed in place.
This created a race condition, which caused fluctuation in the reported
coverage of the relevant modules.
The undesirable test behavior only becomes apparent when the tests
are run in a succession and their behavior is analyzed, for example
during the run of the recently implemented 'coverchange' job.
Final statistics of the coverage measurement, displaying the totals
for number of lines, lines without coverage, branches etc., display
following pattern. Note the third column.
TOTAL 995 105 340 56 86%
TOTAL 995 105 340 56 86%
TOTAL 995 105 340 56 86%
TOTAL 995 108 340 56 86%
TOTAL 995 108 340 56 86%
TOTAL 995 105 340 56 86%
TOTAL 995 105 340 56 86%
TOTAL 995 108 340 56 86%
TOTAL 995 105 340 56 86%
Number of lines without coverage oscillates between '108' and '105' depending
on the order in which the tests were run. The culprit being one of the functions
of the 'validations_libs.cli.common' module, which edits data passed in place.
As the data processed are passed from patched object, and the data itself are
defined withing the relevant 'fakes' module, it emerges with inevitability,
that the tested code operates on data previously altered by the tests.
Furthermore the 'test_run_command_failed_validation' test was augmented
to place further assertion on the arguments passed.
Closes-Bug: #1935003
Signed-off-by: Jiri Podivin <jpodivin@redhat.com>
Change-Id: I2210fa1775691dd503eb882a24824eaf18d7bd3e
2021-07-07 10:24:53 +02:00
|
|
|
'ssh_user': 'doe',
|
2021-09-15 23:01:13 +02:00
|
|
|
'validation_config': {},
|
|
|
|
'skip_list': None
|
2021-05-20 19:09:37 +02:00
|
|
|
}
|
2021-03-23 18:33:04 +01:00
|
|
|
|
2021-07-28 16:48:40 +02:00
|
|
|
arglist = [
|
|
|
|
'--validation', 'foo',
|
|
|
|
'--extra-vars', 'key=value',
|
|
|
|
'--extra-env-vars', 'key2=value2']
|
|
|
|
verifylist = [
|
|
|
|
('validation_name', ['foo']),
|
|
|
|
('extra_vars', {'key': 'value'}),
|
|
|
|
('extra_env_vars', {'key2': 'value2'})]
|
2021-05-20 19:09:37 +02:00
|
|
|
|
|
|
|
self._set_args(arglist)
|
2021-04-20 17:23:00 +02:00
|
|
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
2022-03-18 12:31:18 +01:00
|
|
|
self.assertRaises(ValidationRunException, self.cmd.take_action, parsed_args)
|
2021-07-28 16:48:40 +02:00
|
|
|
call_args = mock_run.mock_calls[0][2]
|
|
|
|
|
|
|
|
self.assertDictEqual(call_args, run_called_args)
|
2021-04-20 17:23:00 +02:00
|
|
|
|
2021-07-28 16:48:40 +02:00
|
|
|
@mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR')
|
2021-04-20 17:23:00 +02:00
|
|
|
@mock.patch('getpass.getuser',
|
|
|
|
return_value='doe')
|
|
|
|
@mock.patch('validations_libs.validation_actions.ValidationActions.'
|
|
|
|
'run_validations',
|
2021-07-28 16:48:40 +02:00
|
|
|
return_value=[],
|
|
|
|
autospec=True)
|
|
|
|
def test_run_command_no_validation(self, mock_run, mock_user, mock_log_dir):
|
2021-04-20 17:23:00 +02:00
|
|
|
run_called_args = {
|
|
|
|
'inventory': 'localhost',
|
|
|
|
'limit_hosts': None,
|
|
|
|
'group': [],
|
2021-07-08 12:31:38 +02:00
|
|
|
'category': [],
|
2021-07-12 13:58:04 +02:00
|
|
|
'product': [],
|
2021-04-20 17:23:00 +02:00
|
|
|
'extra_vars': {'key': 'value'},
|
|
|
|
'validations_dir': '/usr/share/ansible/validation-playbooks',
|
2021-01-20 16:04:53 +01:00
|
|
|
'base_dir': '/usr/share/ansible',
|
2021-04-20 17:23:00 +02:00
|
|
|
'validation_name': ['foo'],
|
|
|
|
'extra_env_vars': {'key2': 'value2'},
|
2021-05-12 07:03:36 +02:00
|
|
|
'python_interpreter': sys.executable,
|
2021-04-20 17:23:00 +02:00
|
|
|
'quiet': True,
|
2021-05-20 19:09:37 +02:00
|
|
|
'ssh_user': 'doe',
|
2021-09-15 23:01:13 +02:00
|
|
|
'validation_config': {},
|
2021-07-28 16:48:40 +02:00
|
|
|
'skip_list': None,
|
|
|
|
'log_path': mock_log_dir}
|
|
|
|
|
|
|
|
arglist = [
|
|
|
|
'--validation', 'foo',
|
|
|
|
'--extra-vars', 'key=value',
|
|
|
|
'--extra-env-vars', 'key2=value2']
|
|
|
|
verifylist = [
|
|
|
|
('validation_name', ['foo']),
|
|
|
|
('extra_vars', {'key': 'value'}),
|
|
|
|
('extra_env_vars', {'key2': 'value2'})]
|
2021-03-23 18:33:04 +01:00
|
|
|
|
2021-05-20 19:09:37 +02:00
|
|
|
self._set_args(arglist)
|
2021-03-23 18:33:04 +01:00
|
|
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
2022-03-18 12:31:18 +01:00
|
|
|
self.assertRaises(ValidationRunException, self.cmd.take_action, parsed_args)
|
2021-05-20 19:09:37 +02:00
|
|
|
|
|
|
|
@mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR')
|
|
|
|
@mock.patch('getpass.getuser',
|
|
|
|
return_value='doe')
|
|
|
|
@mock.patch('validations_libs.validation_actions.ValidationActions.'
|
|
|
|
'run_validations',
|
|
|
|
return_value=fakes.FAKE_SUCCESS_RUN)
|
|
|
|
def test_run_with_wrong_config(self, mock_run,
|
|
|
|
mock_user, mock_log_dir):
|
|
|
|
arglist = ['--validation', 'foo', '--config', 'wrong.cfg']
|
|
|
|
verifylist = [('validation_name', ['foo']),
|
|
|
|
('config', 'wrong.cfg')]
|
|
|
|
|
|
|
|
run_called_args = {
|
|
|
|
'inventory': 'localhost',
|
|
|
|
'limit_hosts': None,
|
|
|
|
'group': [],
|
|
|
|
'category': [],
|
|
|
|
'product': [],
|
|
|
|
'extra_vars': None,
|
|
|
|
'validations_dir': '/usr/share/ansible/validation-playbooks',
|
|
|
|
'base_dir': '/usr/share/ansible',
|
|
|
|
'validation_name': ['foo'],
|
|
|
|
'extra_env_vars': None,
|
|
|
|
'python_interpreter': sys.executable,
|
|
|
|
'quiet': True,
|
|
|
|
'ssh_user': 'doe',
|
2021-09-15 23:01:13 +02:00
|
|
|
'validation_config': {},
|
|
|
|
'skip_list': None
|
2021-05-20 19:09:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self._set_args(arglist)
|
|
|
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
|
|
|
self.cmd.take_action(parsed_args)
|
|
|
|
mock_run.assert_called_with(**run_called_args)
|
|
|
|
|
|
|
|
@mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR')
|
|
|
|
@mock.patch('getpass.getuser',
|
|
|
|
return_value='doe')
|
|
|
|
@mock.patch('validations_libs.validation_actions.ValidationActions.'
|
|
|
|
'run_validations',
|
|
|
|
return_value=fakes.FAKE_SUCCESS_RUN)
|
|
|
|
@mock.patch('os.path.exists', return_value=True)
|
|
|
|
def test_run_with_config(self, mock_exists,
|
|
|
|
mock_run, mock_user,
|
|
|
|
mock_log_dir):
|
|
|
|
arglist = ['--validation', 'foo', '--config', 'config.cfg']
|
|
|
|
verifylist = [('validation_name', ['foo']),
|
|
|
|
('config', 'config.cfg')]
|
|
|
|
|
|
|
|
run_called_args = {
|
|
|
|
'inventory': 'localhost',
|
|
|
|
'limit_hosts': None,
|
|
|
|
'group': [],
|
|
|
|
'category': [],
|
|
|
|
'product': [],
|
|
|
|
'extra_vars': None,
|
|
|
|
'validations_dir': '/usr/share/ansible/validation-playbooks',
|
|
|
|
'base_dir': '/usr/share/ansible',
|
|
|
|
'validation_name': ['foo'],
|
|
|
|
'extra_env_vars': None,
|
|
|
|
'python_interpreter': sys.executable,
|
|
|
|
'quiet': True,
|
|
|
|
'ssh_user': 'doe',
|
2021-09-15 23:01:13 +02:00
|
|
|
'validation_config': {},
|
|
|
|
'skip_list': None
|
2021-05-20 19:09:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self._set_args(arglist)
|
|
|
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
|
|
|
self.cmd.take_action(parsed_args)
|
|
|
|
mock_run.assert_called_with(**run_called_args)
|
2021-09-15 23:01:13 +02:00
|
|
|
|
|
|
|
@mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR')
|
|
|
|
@mock.patch('yaml.safe_load', return_value={'key': 'value'})
|
2022-05-10 01:11:03 +09:00
|
|
|
@mock.patch('builtins.open')
|
2021-09-15 23:01:13 +02:00
|
|
|
@mock.patch('getpass.getuser',
|
|
|
|
return_value='doe')
|
|
|
|
@mock.patch('validations_libs.validation_actions.ValidationActions.'
|
|
|
|
'run_validations',
|
|
|
|
return_value=copy.deepcopy(fakes.FAKE_SUCCESS_RUN))
|
|
|
|
@mock.patch('validations_libs.utils.load_config', return_value={})
|
|
|
|
def test_run_command_with_skip_list(self, mock_config, mock_run,
|
|
|
|
mock_user, mock_open,
|
|
|
|
mock_yaml, mock_log_dir):
|
|
|
|
|
|
|
|
run_called_args = {
|
|
|
|
'inventory': 'localhost',
|
|
|
|
'limit_hosts': None,
|
|
|
|
'group': [],
|
|
|
|
'category': [],
|
|
|
|
'product': [],
|
|
|
|
'extra_vars': None,
|
|
|
|
'validations_dir': '/usr/share/ansible/validation-playbooks',
|
|
|
|
'base_dir': '/usr/share/ansible',
|
|
|
|
'validation_name': ['foo'],
|
|
|
|
'extra_env_vars': None,
|
|
|
|
'python_interpreter': sys.executable,
|
|
|
|
'quiet': True,
|
|
|
|
'ssh_user': 'doe',
|
|
|
|
'validation_config': {},
|
|
|
|
'skip_list': {'key': 'value'}
|
|
|
|
}
|
|
|
|
|
|
|
|
arglist = ['--validation', 'foo',
|
|
|
|
'--skiplist', '/foo/skip.yaml']
|
|
|
|
verifylist = [('validation_name', ['foo']),
|
|
|
|
('skip_list', '/foo/skip.yaml')]
|
|
|
|
self._set_args(arglist)
|
|
|
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
|
|
|
self.cmd.take_action(parsed_args)
|
|
|
|
mock_run.assert_called_with(**run_called_args)
|
|
|
|
|
|
|
|
@mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR')
|
|
|
|
@mock.patch('yaml.safe_load', return_value=[{'key': 'value'}])
|
2022-05-10 01:11:03 +09:00
|
|
|
@mock.patch('builtins.open')
|
2021-09-15 23:01:13 +02:00
|
|
|
@mock.patch('getpass.getuser',
|
|
|
|
return_value='doe')
|
|
|
|
@mock.patch('validations_libs.validation_actions.ValidationActions.'
|
|
|
|
'run_validations',
|
|
|
|
return_value=copy.deepcopy(fakes.FAKE_SUCCESS_RUN))
|
|
|
|
@mock.patch('validations_libs.utils.load_config', return_value={})
|
|
|
|
def test_run_command_with_skip_list_bad_format(self, mock_config, mock_run,
|
|
|
|
mock_user, mock_open,
|
|
|
|
mock_yaml, mock_log_dir):
|
|
|
|
|
|
|
|
arglist = ['--validation', 'foo',
|
|
|
|
'--skiplist', '/foo/skip.yaml']
|
|
|
|
verifylist = [('validation_name', ['foo']),
|
|
|
|
('skip_list', '/foo/skip.yaml')]
|
|
|
|
self._set_args(arglist)
|
|
|
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
2022-03-18 12:31:18 +01:00
|
|
|
self.assertRaises(ValidationRunException, self.cmd.take_action, parsed_args)
|