From 0fb4205479c4b3743244554cec801bc10c69aa26 Mon Sep 17 00:00:00 2001 From: Mathieu Bultel Date: Fri, 6 Mar 2020 11:05:25 +0100 Subject: [PATCH] Add unit tests for validations_run class --- validations_libs/run.py | 5 +- validations_libs/tests/test_ansible.py | 0 .../tests/test_validations_run.py | 90 +++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 validations_libs/tests/test_ansible.py create mode 100644 validations_libs/tests/test_validations_run.py diff --git a/validations_libs/run.py b/validations_libs/run.py index 5569cfec..a7901940 100644 --- a/validations_libs/run.py +++ b/validations_libs/run.py @@ -48,7 +48,7 @@ class Run(object): except Exception as e: raise(e) - else: + elif validation_name: for pb in validation_name: if pb not in v_utils.get_validation_group_name_list(): playbooks.append(pb + '.yaml') @@ -57,6 +57,9 @@ class Run(object): "'--validation' to run validation(s) by their " "name(s)." ) + else: + raise RuntimeError("No validations found") + run_ansible = v_ansible() self.log.debug('Running the validations with Ansible') results = [] diff --git a/validations_libs/tests/test_ansible.py b/validations_libs/tests/test_ansible.py new file mode 100644 index 00000000..e69de29b diff --git a/validations_libs/tests/test_validations_run.py b/validations_libs/tests/test_validations_run.py new file mode 100644 index 00000000..fee2b8a7 --- /dev/null +++ b/validations_libs/tests/test_validations_run.py @@ -0,0 +1,90 @@ +# Copyright 2018 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 mock +import unittest + +from validations_libs.tests import fakes +from validations_libs.run import Run + + +class TestValidatorRun(unittest.TestCase): + + def setUp(self): + super(TestValidatorRun, self).setUp() + + @mock.patch('validations_libs.utils.parse_all_validations_on_disk') + @mock.patch('validations_libs.ansible.Ansible.run') + def test_validation_run_success(self, mock_ansible_run, + mock_validation_dir): + mock_validation_dir.return_value = [{ + 'description': 'My Validation One Description', + 'groups': ['prep', 'pre-deployment'], + 'id': 'foo', + 'name': 'My Validition One Name', + 'parameters': {}}] + mock_ansible_run.return_value = ('/tmp/validation/stdout.log', + 'foo.yaml', 0, 'successful') + + expected_run_return = [ + {'validation': {'playbook': 'foo.yaml', + 'rc_code': 0, + 'status': 'successful', + 'stdout_file': '/tmp/validation/stdout.log'}}] + + playbook = ['fake.yaml'] + inventory = 'tmp/inventory.yaml' + + run = Run() + run_return = run.run_validations(playbook, inventory, + group=fakes.GROUPS_LIST, + validations_dir='/tmp/foo') + self.assertEqual(run_return, expected_run_return) + + @mock.patch('validations_libs.utils.parse_all_validations_on_disk') + @mock.patch('validations_libs.ansible.Ansible.run') + def test_validation_run_failed(self, mock_ansible_run, + mock_validation_dir): + mock_validation_dir.return_value = [{ + 'description': 'My Validation One Description', + 'groups': ['prep', 'pre-deployment'], + 'id': 'foo', + 'name': 'My Validition One Name', + 'parameters': {}}] + mock_ansible_run.return_value = ('/tmp/validation/stdout.log', + 'foo.yaml', 0, 'failed') + + expected_run_return = [ + {'validation': {'playbook': 'foo.yaml', + 'rc_code': 0, + 'status': 'failed', + 'stdout_file': '/tmp/validation/stdout.log'}}] + + playbook = ['fake.yaml'] + inventory = 'tmp/inventory.yaml' + + run = Run() + run_return = run.run_validations(playbook, inventory, + group=fakes.GROUPS_LIST, + validations_dir='/tmp/foo') + self.assertEqual(run_return, expected_run_return) + + def test_validation_run_no_validation(self): + playbook = ['fake.yaml'] + inventory = 'tmp/inventory.yaml' + + run = Run() + self.assertRaises(RuntimeError, run.run_validations, playbook, + inventory)