validations-libs/validations_libs/tests/cli/test_file.py

146 lines
7.0 KiB
Python

# 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('validations_libs.validation_actions.ValidationActions.'
'run_validations',
return_value=copy.deepcopy(fakes.FAKE_SUCCESS_RUN),
autospec=True)
@mock.patch('validations_libs.utils.create_log_dir', return_value='/foo/bar/logdir/')
@mock.patch('validations_libs.utils.parse_all_validations_on_disk', return_value=[])
def test_run_validation_success_full(self, mock_parse_all_validations_on_disk, mock_create_log_dir, 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('validations_libs.validation_actions.ValidationActions.'
'run_validations',
return_value=copy.deepcopy(fakes.FAKE_SUCCESS_RUN),
autospec=True)
@mock.patch('validations_libs.utils.parse_all_validations_on_disk')
def test_run_validation_success_validations_on_disk_exists(self, mock_validation_dir, mock_run):
args = self._set_args(['--path-to-file', 'preliminary-file-structure.yaml'])
verifylist = [('path_to_file', 'preliminary-file-structure.yaml')]
mock_validation_dir.return_value = [{'id': 'foo',
'description': 'foo',
'groups': ['prep', 'pre-deployment'],
'categories': ['os', 'storage'],
'products': ['product1'],
'name': 'Advanced Format 512e Support',
'path': '/tmp'}]
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)