From 87ba2f52d9e649b48ca76be0d53f53010613709c Mon Sep 17 00:00:00 2001 From: hongyuzhao Date: Mon, 8 Jul 2019 15:21:35 +0800 Subject: improve code coverage rate after vfclcm upgraded from python2 to python3 Change-Id: Icd7d3cec2831f68568697fc0c3eb329c3245df13 Issue-ID: VFC-1429 Signed-off-by: hongyuzhao --- lcm/lcm/nf/tests/test_create_vnf.py | 52 +++++++++++++++++++++++++++++++++++++ lcm/lcm/nf/views/curd_vnf_views.py | 3 ++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/lcm/lcm/nf/tests/test_create_vnf.py b/lcm/lcm/nf/tests/test_create_vnf.py index 39640b57..3ac414f9 100644 --- a/lcm/lcm/nf/tests/test_create_vnf.py +++ b/lcm/lcm/nf/tests/test_create_vnf.py @@ -25,6 +25,8 @@ from lcm.pub.database.models import NfInstModel from lcm.pub.database.models import JobStatusModel from lcm.pub.utils import restcall from lcm.pub.utils.timeutil import now_time +from lcm.pub.exceptions import NFLCMException +from lcm.nf.biz.create_vnf import CreateVnf class TestNFInstantiate(TestCase): @@ -110,3 +112,53 @@ class TestNFInstantiate(TestCase): } self.assertEqual(status.HTTP_201_CREATED, response.status_code) self.assertEqual(expect_data, response.data) + + @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_uuid4.return_value = "1" + data = { + "vnfdId": "111", + "vnfInstanceName": "vFW_01", + "vnfInstanceDescription": "vFW in Nanjing TIC Edge" + } + response = self.client.post( + "/api/vnflcm/v1/vnf_instances", + data=data, + format='json' + ) + self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code) + + def test_create_vnf_bad_req(self): + data = { + "vnfInstanceName": "vFW_01", + "vnfInstanceDescription": "vFW in Nanjing TIC Edge" + } + response = self.client.post( + "/api/vnflcm/v1/vnf_instances", + data=data, + format='json' + ) + self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code) + + @mock.patch.object(CreateVnf, 'do_biz') + def test_create_vnf_bad_response(self, moc_CreateVnf_do_biz): + moc_CreateVnf_do_biz.return_value = { + # "id": "1", + "vnfProvider": "huawei", + "vnfdVersion": "1.0", + "vnfPkgId": "111", + "instantiationState": "NOT_INSTANTIATED" + } + data = { + "vnfdId": "111", + "vnfInstanceName": "vFW_01", + "vnfInstanceDescription": "vFW in Nanjing TIC Edge" + } + response = self.client.post( + "/api/vnflcm/v1/vnf_instances", + data=data, + format='json' + ) + self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code) diff --git a/lcm/lcm/nf/views/curd_vnf_views.py b/lcm/lcm/nf/views/curd_vnf_views.py index e8c19d67..f5c7a77a 100644 --- a/lcm/lcm/nf/views/curd_vnf_views.py +++ b/lcm/lcm/nf/views/curd_vnf_views.py @@ -33,6 +33,7 @@ from lcm.nf.const import OPERATION_TYPE from .common import view_safe_call_with_log from .common import deal_vnf_action from .common import deal_indivdual_query +from lcm.pub.exceptions import NFLCMExceptionBadRequest logger = logging.getLogger(__name__) @@ -68,7 +69,7 @@ class CreateVnfAndQueryVnfs(APIView): logger.debug("CreateVnfIdentifier--post::> %s" % request.data) req_serializer = CreateVnfReqSerializer(data=request.data) if not req_serializer.is_valid(): - raise NFLCMException(req_serializer.errors) + raise NFLCMExceptionBadRequest(req_serializer.errors) nf_inst = CreateVnf(request.data).do_biz() rsp_data = {"id": nf_inst.nfinstid, -- cgit 1.2.3-korg