From da7fa99b4472a891d9e0ab16e0224acdc4f63e8e Mon Sep 17 00:00:00 2001 From: hongyuzhao Date: Wed, 10 Jul 2019 11:48:52 +0800 Subject: improve code coverage rate after vfclcm upgraded from python2 to python3 Change-Id: I9c62ad2638ff8eeaafcae4d51dcf1e92a688eb5e Issue-ID: VFC-1429 Signed-off-by: hongyuzhao --- lcm/lcm/nf/biz/instantiate_vnf.py | 4 ++- lcm/lcm/nf/tests/test_create_vnf.py | 2 +- lcm/lcm/nf/tests/test_delete_vnf.py | 10 ++++++- lcm/lcm/nf/tests/test_instantiate_vnf.py | 49 +++++++++++++++++++++++++++++++- lcm/lcm/nf/views/curd_vnf_views.py | 2 +- 5 files changed, 62 insertions(+), 5 deletions(-) (limited to 'lcm') diff --git a/lcm/lcm/nf/biz/instantiate_vnf.py b/lcm/lcm/nf/biz/instantiate_vnf.py index 57711f75..58ca591b 100644 --- a/lcm/lcm/nf/biz/instantiate_vnf.py +++ b/lcm/lcm/nf/biz/instantiate_vnf.py @@ -59,10 +59,10 @@ class InstantiateVnf(Thread): operation=OPERATION_TYPE.INSTANTIATE, task=OPERATION_TASK.INSTANTIATE ) - self.pre_deal() def run(self): try: + self.pre_deal() self.inst_pre() self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.STARTING) self.apply_grant() @@ -84,6 +84,8 @@ class InstantiateVnf(Thread): ) except NFLCMException as e: self.vnf_inst_failed_handle(e.args[0]) + except NFLCMExceptionConflict as e: + self.vnf_inst_failed_handle(e.args[0]) except Exception as e: logger.error(str(e)) logger.error(traceback.format_exc()) diff --git a/lcm/lcm/nf/tests/test_create_vnf.py b/lcm/lcm/nf/tests/test_create_vnf.py index 3ac414f9..240ed17f 100644 --- a/lcm/lcm/nf/tests/test_create_vnf.py +++ b/lcm/lcm/nf/tests/test_create_vnf.py @@ -116,7 +116,7 @@ class TestNFInstantiate(TestCase): @mock.patch.object(restcall, 'call_req') @mock.patch.object(uuid, 'uuid4') def test_create_vnf_inner_error(self, mock_uuid4, mock_call_req): - mock_call_req.return_value = NFLCMException('Boom!') + mock_call_req.side_effect = NFLCMException('Boom!') mock_uuid4.return_value = "1" data = { "vnfdId": "111", diff --git a/lcm/lcm/nf/tests/test_delete_vnf.py b/lcm/lcm/nf/tests/test_delete_vnf.py index 61d173eb..4ef1ab64 100644 --- a/lcm/lcm/nf/tests/test_delete_vnf.py +++ b/lcm/lcm/nf/tests/test_delete_vnf.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. - +import mock from django.test import TestCase, Client from rest_framework import status @@ -26,6 +26,8 @@ from lcm.pub.database.models import FlavourInstModel from lcm.pub.database.models import StorageInstModel from lcm.pub.database.models import NfvoRegInfoModel from lcm.pub.utils.timeutil import now_time +from lcm.nf.biz.delete_vnf import DeleteVnf +from lcm.pub.exceptions import NFLCMException class TestNFTerminate(TestCase): @@ -133,3 +135,9 @@ class TestNFTerminate(TestCase): def test_delete_vnf_identifier_when_vnf_not_exist(self): response = self.client.delete("/api/vnflcm/v1/vnf_instances/1111") self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) + + @mock.patch.object(DeleteVnf, 'do_biz') + def test_delete_vnf_inner_error(self, mock_DeleteVnf_do_biz): + mock_DeleteVnf_do_biz.side_effect = NFLCMException('Boom!') + response = self.client.delete("/api/vnflcm/v1/vnf_instances/1234") + self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code) diff --git a/lcm/lcm/nf/tests/test_instantiate_vnf.py b/lcm/lcm/nf/tests/test_instantiate_vnf.py index 16351f98..c243c44b 100644 --- a/lcm/lcm/nf/tests/test_instantiate_vnf.py +++ b/lcm/lcm/nf/tests/test_instantiate_vnf.py @@ -34,7 +34,7 @@ from .const import inst_req_data from .const import vnfpackage_info from .const import instantiate_grant_result -from lcm.pub.database.models import NfInstModel +from lcm.pub.database.models import NfInstModel, VNFLcmOpOccModel from lcm.pub.database.models import JobStatusModel from lcm.pub.database.models import SubscriptionModel from lcm.pub.utils import restcall @@ -45,6 +45,7 @@ from lcm.pub.vimapi import api from lcm.pub.exceptions import NFLCMException from lcm.nf.biz.instantiate_vnf import InstantiateVnf +from lcm.nf import const class TestNFInstantiate(TestCase): @@ -390,6 +391,11 @@ class TestNFInstantiate(TestCase): nf_inst_id=self.nf_inst_id, job_id=self.job_id ).run() + self.assert_job_result( + self.job_id, + 100, + 'Instantiate Vnf success.' + ) @mock.patch.object(JobUtil, 'create_job') def test_instantiate_inner_error(self, mock_run): @@ -420,3 +426,44 @@ class TestNFInstantiate(TestCase): ) NfInstModel.objects.filter(nfinstid='144').delete() self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code) + + @mock.patch.object(restcall, 'call_req') + def test_instantiate_operating_fail(self, mock_call_req): + NfInstModel.objects.create( + nfinstid='1111', + nf_name='vFW_01', + package_id='222', + version='', + vendor='', + netype='', + vnfd_model='', + status='NOT_INSTANTIATED', + nf_desc='vFW in Nanjing TIC Edge', + vnfdid='111', + create_time=now_time() + ) + r1_get_vnfpackage_by_vnfdid = [ + 0, + json.JSONEncoder().encode(vnfpackage_info), + '200' + ] + mock_call_req.side_effect = [ + r1_get_vnfpackage_by_vnfdid + ] + self.nf_inst_id = '1111' + self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id) + JobUtil.add_job_status(self.job_id, 0, 'INST_VNF_READY') + VNFLcmOpOccModel.objects.create(vnf_instance_id=self.nf_inst_id, + id=self.job_id, + operation=const.OPERATION_TYPE.INSTANTIATE, + operation_state=const.OPERATION_STATE_TYPE.PROCESSING) + InstantiateVnf( + inst_req_data, + nf_inst_id=self.nf_inst_id, + job_id=self.job_id + ).run() + self.assert_job_result( + self.job_id, + 255, + 'VNF(%s) %s in processing.' % (self.nf_inst_id, const.OPERATION_TYPE.INSTANTIATE) + ) diff --git a/lcm/lcm/nf/views/curd_vnf_views.py b/lcm/lcm/nf/views/curd_vnf_views.py index f5c7a77a..2992814b 100644 --- a/lcm/lcm/nf/views/curd_vnf_views.py +++ b/lcm/lcm/nf/views/curd_vnf_views.py @@ -108,7 +108,7 @@ class DeleteVnfAndQueryVnf(APIView): ) @view_safe_call_with_log(logger=logger) def delete(self, request, instanceid): - logger.debug("DeleteVnfIdentifier--delete::> %s" % request.data) + logger.debug("DeleteVnfIdentifier--delete::> %s" % instanceid) DeleteVnf(request.data, instanceid).do_biz() -- cgit 1.2.3-korg