From: ashishk1994 Date: Tue, 28 Mar 2017 08:46:14 +0000 (+0530) Subject: More Unit Tests for utils module X-Git-Tag: opnfv-5.0.RC1~513^2 X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F09%2F32009%2F1;p=functest.git More Unit Tests for utils module This patch adds some more unit tests for utils/openstack_tacker file and also adds unit tests for utils/functest_logger file Change-Id: I1a4c91eff87aebc3614ef01c3b5de0d2e6716afa Signed-off-by: ashishk1994 --- diff --git a/functest/tests/unit/utils/test_functest_logger.py b/functest/tests/unit/utils/test_functest_logger.py new file mode 100644 index 000000000..42e41a141 --- /dev/null +++ b/functest/tests/unit/utils/test_functest_logger.py @@ -0,0 +1,48 @@ +#!/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 + +import logging +import unittest + +import mock + +from functest.utils import functest_logger +from functest.utils.constants import CONST + + +class OSUtilsLogger(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + def setUp(self): + with mock.patch('__builtin__.open', mock.mock_open()): + with mock.patch('functest.utils.functest_logger.os.path.exists', + return_value=True), \ + mock.patch('functest.utils.functest_logger.' + 'json.load'), \ + mock.patch('functest.utils.functest_logger.' + 'logging.config.dictConfig') as m: + self.logger = functest_logger.Logger('os_utils') + self.assertTrue(m.called) + with mock.patch('functest.utils.functest_logger.os.path.exists', + return_value=False), \ + mock.patch('functest.utils.functest_logger.' + 'logging.basicConfig') as m: + self.logger = functest_logger.Logger('os_utils') + self.assertTrue(m.called) + + def test_is_debug_false(self): + CONST.CI_DEBUG = False + self.assertFalse(self.logger.is_debug()) + + def test_is_debug_true(self): + CONST.CI_DEBUG = "True" + self.assertTrue(self.logger.is_debug()) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/utils/test_openstack_tacker.py b/functest/tests/unit/utils/test_openstack_tacker.py index dc7172580..37d77a18f 100644 --- a/functest/tests/unit/utils/test_openstack_tacker.py +++ b/functest/tests/unit/utils/test_openstack_tacker.py @@ -6,9 +6,11 @@ # http://www.apache.org/licenses/LICENSE-2.0 import logging -import mock import unittest +import mock +from tackerclient.v1_0 import client as tackerclient + from functest.utils import openstack_tacker from functest.tests.unit import test_utils @@ -56,6 +58,13 @@ class OSTackerTesting(unittest.TestCase): } return cred_dict + def test_get_tacker_client(self): + with mock.patch('functest.utils.openstack_tacker.' + 'os_utils.get_session'): + tackerclient.Client = mock.Mock + ret = openstack_tacker.get_tacker_client() + self.assertTrue(isinstance(ret, mock.Mock)) + def test_get_id_from_name(self): with mock.patch.object(self.tacker_client, 'get', return_value=self.getresponse): @@ -183,8 +192,16 @@ class OSTackerTesting(unittest.TestCase): vnfd_name=self.vnfd) self.assertEqual(resp, self.vnfd) - # TODO: Exception('You need to provide an VNFD' - # 'id or name') AssertionError + def test_delete_vnfd_missing_vnfd_name(self): + with mock.patch('functest.utils.openstack_tacker.get_vnfd_id', + return_value=self.vnfd), \ + self.assertRaises(Exception) as context: + resp = openstack_tacker.delete_vnfd(self.tacker_client, + vnfd_id=None, + vnfd_name=None) + self.assertIsNone(resp) + msg = 'You need to provide VNFD id or VNFD name' + self.assertTrue(msg in context) @mock.patch('functest.utils.openstack_tacker.logger.error') def test_delete_vnfd_exception(self, mock_logger_error): @@ -253,7 +270,47 @@ class OSTackerTesting(unittest.TestCase): "client")) self.assertIsNone(resp) - # TODO: wait_for_vnf + def test_wait_for_vnf_vnf_retrieval_failed(self): + with mock.patch('functest.utils.openstack_tacker.get_vnf', + return_value=None), \ + self.assertRaises(Exception) as context: + openstack_tacker.wait_for_vnf(self.tacker_client, + vnf_id='vnf_id', + vnf_name='vnf_name') + msg = ("Could not retrieve VNF - id='vnf_id', " + "name='vnf_name'") + self.assertTrue(msg in context) + with mock.patch('functest.utils.openstack_tacker.get_vnf', + side_effect=Exception): + ret = openstack_tacker.wait_for_vnf(self.tacker_client, + vnf_id='vnf_id', + vnf_name='vnf_name') + self.assertEqual(ret, None) + + def test_wait_for_vnf_vnf_status_error(self): + vnf = {'id': 'vnf_id', + 'status': 'ERROR'} + with mock.patch('functest.utils.openstack_tacker.get_vnf', + return_value=vnf), \ + self.assertRaises(Exception) as context: + openstack_tacker.wait_for_vnf(self.tacker_client, + vnf_id='vnf_id', + vnf_name='vnf_name') + msg = ('Error when booting vnf vnf_id') + self.assertTrue(msg in context) + + def test_wait_for_vnf_vnf_timeout(self): + vnf = {'id': 'vnf_id', + 'status': 'PENDING_CREATE'} + with mock.patch('functest.utils.openstack_tacker.get_vnf', + return_value=vnf), \ + self.assertRaises(Exception) as context: + openstack_tacker.wait_for_vnf(self.tacker_client, + vnf_id='vnf_id', + vnf_name='vnf_name', + timeout=2) + msg = ('Timeout when booting vnf vnf_id') + self.assertTrue(msg in context) def test_delete_vnf(self): with mock.patch('functest.utils.openstack_tacker.get_vnf_id', @@ -265,8 +322,13 @@ class OSTackerTesting(unittest.TestCase): vnf_name=self.vnf) self.assertEqual(resp, self.vnf) - # TODO: Exception('You need to provide an VNF' - # 'classifier id or name') AssertionError + def test_delete_vnf_missing_vnf_name(self): + with self.assertRaises(Exception) as context: + openstack_tacker.delete_vnf(self.tacker_client, + vnf_id=None, + vnf_name=None) + msg = 'You need to provide a VNF id or name' + self.assertTrue(msg in context) @mock.patch('functest.utils.openstack_tacker.logger.error') def test_delete_vnf_exception(self, mock_logger_error): @@ -345,8 +407,13 @@ class OSTackerTesting(unittest.TestCase): sfc_name=self.sfc) self.assertEqual(resp, self.sfc) - # TODO: Exception('You need to provide an SFC' - # 'id or name') AssertionError + def test_delete_sfc_missing_sfc_name(self): + with self.assertRaises(Exception) as context: + openstack_tacker.delete_sfc(self.tacker_client, + sfc_id=None, + sfc_name=None) + msg = 'You need to provide an SFC id or name' + self.assertTrue(msg in context) @mock.patch('functest.utils.openstack_tacker.logger.error') def test_delete_sfc_exception(self, mock_logger_error): @@ -431,8 +498,13 @@ class OSTackerTesting(unittest.TestCase): sfc_clf_name=cl) self.assertEqual(resp, cl) - # TODO: Exception('You need to provide an SFC' - # 'classifier id or name') AssertionError + def test_delete_sfc_classifier_missing_sfc_name(self): + with self.assertRaises(Exception) as context: + openstack_tacker.delete_vnf(self.tacker_client, + sfc_clf_id=None, + sfc_clf_name=None) + msg = 'You need to provide an SFCclassifier id or name' + self.assertTrue(msg in context) @mock.patch('functest.utils.openstack_tacker.logger.error') def test_delete_sfc_classifier_exception(self, mock_logger_error):