Additional Unit Tests for core modules 45/28545/14
authorashishk1994 <ashishk.iiit@gmail.com>
Mon, 13 Feb 2017 17:55:58 +0000 (23:25 +0530)
committerCédric Ollivier <cedric.ollivier@orange.com>
Mon, 15 May 2017 13:49:08 +0000 (15:49 +0200)
This patch adds unit tests for vnf and pytest_suite_runner.
It also fixes several pylint warnings/errors.

Additional tests are required to check args values.

Co-Authored-By: Cédric Ollivier <cedric.ollivier@orange.com>
Change-Id: I75a829233481655c794402830e1da037ab7cdb8d
Signed-off-by: ashishk1994 <ashishk.iiit@gmail.com>
functest/tests/unit/core/test_pytest_suite_runner.py [new file with mode: 0644]
functest/tests/unit/core/test_vnf.py

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 (file)
index 0000000..15e5bd7
--- /dev/null
@@ -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)
index f348c0d..793e957 100644 (file)
@@ -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__":