diff options
author | Jose Lausuch <jose.lausuch@ericsson.com> | 2017-05-16 15:23:41 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2017-05-16 15:23:41 +0000 |
commit | 66071d8a3e1eed290aa081cc7cb43eb059eee6bc (patch) | |
tree | fe58e19874ecc2c664c627e122dd1e8bcf19ab16 | |
parent | 49d9b2ee585c1fb9481327fa9af883f8e66fc159 (diff) | |
parent | 6aab00bdf6a606716af7ab73d41031f644abca9b (diff) |
Merge "Additional Unit Tests for core modules"
-rw-r--r-- | functest/tests/unit/core/test_pytest_suite_runner.py | 51 | ||||
-rw-r--r-- | functest/tests/unit/core/test_vnf.py | 142 |
2 files changed, 180 insertions, 13 deletions
diff --git a/functest/tests/unit/core/test_pytest_suite_runner.py b/functest/tests/unit/core/test_pytest_suite_runner.py new file mode 100644 index 00000000..15e5bd73 --- /dev/null +++ b/functest/tests/unit/core/test_pytest_suite_runner.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python + +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +# pylint: disable=missing-docstring + +import logging +import unittest + +import mock + +from functest.core import pytest_suite_runner +from functest.core import testcase + + +class PyTestSuiteRunnerTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + def setUp(self): + self.psrunner = pytest_suite_runner.PyTestSuiteRunner() + self.result = mock.Mock() + attrs = {'errors': [('test1', 'error_msg1')], + 'failures': [('test2', 'failure_msg1')]} + self.result.configure_mock(**attrs) + + self.pass_results = mock.Mock() + attrs = {'errors': None, + 'failures': None} + self.pass_results.configure_mock(**attrs) + + def test_run(self): + self.psrunner.case_name = 'test_case_name' + with mock.patch('functest.core.pytest_suite_runner.' + 'unittest.TextTestRunner.run', + return_value=self.result): + self.assertEqual(self.psrunner.run(), + testcase.TestCase.EX_OK) + + with mock.patch('functest.core.pytest_suite_runner.' + 'unittest.TextTestRunner.run', + return_value=self.pass_results): + self.assertEqual(self.psrunner.run(), + testcase.TestCase.EX_OK) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/core/test_vnf.py b/functest/tests/unit/core/test_vnf.py index f348c0db..793e9576 100644 --- a/functest/tests/unit/core/test_vnf.py +++ b/functest/tests/unit/core/test_vnf.py @@ -7,10 +7,16 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 +# pylint: disable=missing-docstring + import logging +import os import unittest +import mock + from functest.core import vnf +from functest.core import testcase class VnfBaseTesting(unittest.TestCase): @@ -18,21 +24,126 @@ class VnfBaseTesting(unittest.TestCase): logging.disable(logging.CRITICAL) def setUp(self): - self.test = vnf.VnfOnBoarding(project='functest', - case_name='aaa') + self.test = vnf.VnfOnBoarding( + project='functest', case_name='aaa') self.test.project = "functest" self.test.start_time = "1" self.test.stop_time = "5" self.test.result = "" - self.test.details = {"orchestrator": {"status": "PASS", - "result": "", - "duration": 20}, - "vnf": {"status": "PASS", - "result": "", - "duration": 15}, - "test_vnf": {"status": "FAIL", - "result": "", - "duration": 5}} + self.test.details = { + "orchestrator": {"status": "PASS", "result": "", "duration": 20}, + "vnf": {"status": "PASS", "result": "", "duration": 15}, + "test_vnf": {"status": "FAIL", "result": "", "duration": 5}} + self.test.keystone_client = 'test_client' + self.test.tenant_name = 'test_tenant_name' + + def test_execute_deploy_vnf_fail(self): + with mock.patch.object(self.test, 'prepare'),\ + mock.patch.object(self.test, 'deploy_orchestrator', + return_value=None), \ + mock.patch.object(self.test, 'deploy_vnf', + side_effect=Exception): + self.assertEqual(self.test.execute(), + testcase.TestCase.EX_TESTCASE_FAILED) + + def test_execute_test_vnf_fail(self): + with mock.patch.object(self.test, 'prepare'),\ + mock.patch.object(self.test, 'deploy_orchestrator', + return_value=None), \ + mock.patch.object(self.test, 'deploy_vnf'), \ + mock.patch.object(self.test, 'test_vnf', + side_effect=Exception): + self.assertEqual(self.test.execute(), + testcase.TestCase.EX_TESTCASE_FAILED) + + @mock.patch('functest.core.vnf.os_utils.get_tenant_id', + return_value='test_tenant_id') + @mock.patch('functest.core.vnf.os_utils.delete_tenant', + return_value=True) + @mock.patch('functest.core.vnf.os_utils.get_user_id', + return_value='test_user_id') + @mock.patch('functest.core.vnf.os_utils.delete_user', + return_value=True) + def test_execute_default(self, *args): + with mock.patch.object(self.test, 'prepare'),\ + mock.patch.object(self.test, 'deploy_orchestrator', + return_value=None), \ + mock.patch.object(self.test, 'deploy_vnf'), \ + mock.patch.object(self.test, 'test_vnf'), \ + mock.patch.object(self.test, 'parse_results', + return_value='ret_exit_code'), \ + mock.patch.object(self.test, 'log_results'): + self.assertEqual(self.test.execute(), + 'ret_exit_code') + + @mock.patch('functest.core.vnf.os_utils.get_credentials') + @mock.patch('functest.core.vnf.os_utils.get_keystone_client') + @mock.patch('functest.core.vnf.os_utils.get_user_id', return_value='') + def test_prepare_missing_userid(self, *args): + with self.assertRaises(Exception): + self.test.prepare() + + @mock.patch('functest.core.vnf.os_utils.get_credentials') + @mock.patch('functest.core.vnf.os_utils.get_keystone_client') + @mock.patch('functest.core.vnf.os_utils.get_user_id', + return_value='test_roleid') + @mock.patch('functest.core.vnf.os_utils.create_tenant', + return_value='') + def test_prepare_missing_tenantid(self, *args): + with self.assertRaises(Exception): + self.test.prepare() + + @mock.patch('functest.core.vnf.os_utils.get_credentials') + @mock.patch('functest.core.vnf.os_utils.get_keystone_client') + @mock.patch('functest.core.vnf.os_utils.get_user_id', + return_value='test_roleid') + @mock.patch('functest.core.vnf.os_utils.create_tenant', + return_value='test_tenantid') + @mock.patch('functest.core.vnf.os_utils.get_role_id', + return_value='') + def test_prepare_missing_roleid(self, *args): + with self.assertRaises(Exception): + self.test.prepare() + + @mock.patch('functest.core.vnf.os_utils.get_credentials') + @mock.patch('functest.core.vnf.os_utils.get_keystone_client') + @mock.patch('functest.core.vnf.os_utils.get_user_id', + return_value='test_roleid') + @mock.patch('functest.core.vnf.os_utils.create_tenant', + return_value='test_tenantid') + @mock.patch('functest.core.vnf.os_utils.get_role_id', + return_value='test_roleid') + @mock.patch('functest.core.vnf.os_utils.add_role_user', + return_value='') + def test_prepare_role_add_failure(self, *args): + with self.assertRaises(Exception): + self.test.prepare() + + @mock.patch('functest.core.vnf.os_utils.get_credentials') + @mock.patch('functest.core.vnf.os_utils.get_keystone_client') + @mock.patch('functest.core.vnf.os_utils.get_user_id', + return_value='test_roleid') + @mock.patch('functest.core.vnf.os_utils.create_tenant', + return_value='test_tenantid') + @mock.patch('functest.core.vnf.os_utils.get_role_id', + return_value='test_roleid') + @mock.patch('functest.core.vnf.os_utils.add_role_user') + @mock.patch('functest.core.vnf.os_utils.create_user', + return_value='') + def test_create_user_failure(self, *args): + with self.assertRaises(Exception): + self.test.prepare() + + def test_log_results_default(self): + with mock.patch('functest.core.vnf.' + 'ft_utils.logger_test_results') \ + as mock_method: + self.test.log_results() + self.assertTrue(mock_method.called) + + def test_step_failures_default(self): + with self.assertRaises(Exception): + self.test.step_failure("error_msg") def test_deploy_vnf_unimplemented(self): with self.assertRaises(Exception) as context: @@ -44,8 +155,13 @@ class VnfBaseTesting(unittest.TestCase): self.test.test_vnf()() self.assertTrue('VNF not tested' in context.exception) - def test_parse_results(self): - self.assertNotEqual(self.test.parse_results(), 0) + def test_parse_results_ex_ok(self): + self.test.details['test_vnf']['status'] = 'PASS' + self.assertEqual(self.test.parse_results(), os.EX_OK) + + def test_parse_results_ex_run_error(self): + self.test.details['vnf']['status'] = 'FAIL' + self.assertEqual(self.test.parse_results(), os.EX_SOFTWARE) if __name__ == "__main__": |