diff options
author | fujinhua <fu.jinhua@zte.com.cn> | 2018-08-11 09:17:46 +0800 |
---|---|---|
committer | fujinhua <fu.jinhua@zte.com.cn> | 2018-08-11 09:31:26 +0800 |
commit | ac6140524faefa083871649473f1637c14b80be5 (patch) | |
tree | 060753f593e59b973432597a37b3c626ad07da28 | |
parent | f574b8d4c2a830520a6f15232a88a4862ddc3b5d (diff) |
Add unit test for ns instantiate
Change-Id: If92efd7df5ca70d84a4e82d65a3534cbf791c307
Issue-ID: VFC-1009
Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
-rw-r--r-- | lcm/ns/serializers.py | 13 | ||||
-rw-r--r-- | lcm/ns/tests/test_ns_instant.py | 94 | ||||
-rw-r--r-- | lcm/ns/views.py | 1 |
3 files changed, 97 insertions, 11 deletions
diff --git a/lcm/ns/serializers.py b/lcm/ns/serializers.py index a3b14127..91c19c40 100644 --- a/lcm/ns/serializers.py +++ b/lcm/ns/serializers.py @@ -71,17 +71,22 @@ class QueryNsRespSerializer(serializers.Serializer): class VimSerializer(serializers.Serializer): - vimid = serializers.CharField(help_text="ID of VIM", required=False, allow_null=True) + vimId = serializers.CharField(help_text="ID of VIM", required=False, allow_null=True) class LocationConstraintSerializer(serializers.Serializer): vnfProfileId = serializers.CharField(help_text="ID of VNF profile", required=False, allow_null=True) - locationConstraints = VimSerializer(help_text="Location constraints", required=False, allow_null=True) + locationConstraints = VimSerializer(help_text="Location constraint", required=False, allow_null=True) class InstantNsReqSerializer(serializers.Serializer): - locationConstraints = LocationConstraintSerializer(required=False, allow_null=True) - additionalParamForNs = serializers.CharField(help_text="Additional param for NS", required=False, allow_null=True) + locationConstraints = LocationConstraintSerializer(help_text="Location constraints", required=False, many=True) + additionalParamForNs = serializers.DictField( + help_text="Additional param for NS", + child=serializers.CharField(help_text="KeyValue Pairs", allow_blank=True), + required=False, + allow_null=True + ) class NsOperateJobSerializer(serializers.Serializer): diff --git a/lcm/ns/tests/test_ns_instant.py b/lcm/ns/tests/test_ns_instant.py index f4df1075..7034b703 100644 --- a/lcm/ns/tests/test_ns_instant.py +++ b/lcm/ns/tests/test_ns_instant.py @@ -14,40 +14,120 @@ from rest_framework import status from django.test import TestCase -from django.test import Client +from rest_framework.test import APIClient import mock +import json from lcm.pub.database.models import NSInstModel from lcm.ns.ns_instant import InstantNSService +from lcm.ns.ns_instant import BuildInWorkflowThread from lcm.pub.utils import restcall class TestNsInstant(TestCase): def setUp(self): - self.client = Client() + self.client = APIClient() NSInstModel.objects.filter().delete() self.url = "/api/nslcm/v1/ns/2/instantiate" self.req_data = { "additionalParamForNs": { - "location": "1", "sdnControllerId": "2" - } + }, + "locationConstraints": [{ + "vnfProfileId": "vnfd1", + "locationConstraints": { + "vimId": "3" + } + }] + } + self.nsd_model = json.dumps({ + "model": json.dumps({ + "vnfs": [{ + "vnf_id": "vnf1", + "properties": { + "id": "vnfd1", + "nf_type": "xgw" + }, + "dependencies": [{ + "vl_id": "5" + }] + }], + "vls": [{ + "vl_id": "5", + "properties": {} + }] + }) + }) + self.updated_nsd_model = { + "vnfs": [{ + "dependencies": [{ + "vl_id": "5" + }], + "vnf_id": "vnf1", + "properties": { + "nf_type": "xgw", + "id": "vnfd1" + } + }], + "vls": [{ + "vl_id": "5", + "properties": { + "location_info": { + "vimid": "3" + } + } + }] } - NSInstModel(id="2", nspackage_id="7", nsd_id="2").save() + self.vnfms = json.dumps({ + "esr-vnfm": [{ + "vnfm-id": "4" + }] + }) + self.vnfm = json.dumps({ + "type": "xgw", + "vim-id": "3", + "vnfm-id": "4", + "certificate-url": "http://127.0.0.0/ztevnfm/v1/auth", + "esr-system-info-list": { + "esr-system-info": [{ + "type": "xgw", + "vendor": "zte", + "version": "1.0", + "service-url": "http://127.0.0.0/ztevnfm/v1", + "user-name": "admin", + "password": "admin123" + }] + } + }) + NSInstModel(id="2", nspackage_id="7", nsd_id="2", status="active").save() def tearDown(self): pass + @mock.patch.object(restcall, 'call_req') + @mock.patch.object(BuildInWorkflowThread, 'run') + def test_ns_instantiate_when_succeed_to_enter_workflow(self, mock_run, mock_call_req): + mock_call_req.side_effect = [ + [0, self.nsd_model, '200'], + [0, self.vnfms, '200'], + [0, self.vnfm, '200'] + ] + resp = self.client.post(self.url, data=self.req_data, format='json') + self.failUnlessEqual(status.HTTP_200_OK, resp.status_code) + self.assertIn("jobId", resp.data) + upd_nsd_model = NSInstModel.objects.filter(id="2").first().nsd_model + self.assertEqual(self.updated_nsd_model, json.loads(upd_nsd_model)) + @mock.patch.object(InstantNSService, 'do_biz') def test_ns_instantiate_normal(self, mock_do_biz): mock_do_biz.return_value = dict(data={'jobId': "1"}, status=status.HTTP_200_OK) - resp = self.client.post(self.url, data=self.req_data) + resp = self.client.post(self.url, data=self.req_data, format='json') self.failUnlessEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual({'jobId': "1"}, resp.data) @mock.patch.object(restcall, 'call_req') def test_ns_instantiate_when_fail_to_parse_nsd(self, mock_call_req): mock_call_req.return_value = [1, "Failed to parse nsd", '500'] - resp = self.client.post(self.url, data=self.req_data) + resp = self.client.post(self.url, data=self.req_data, format='json') self.assertEqual(resp.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) self.assertIn("error", resp.data) diff --git a/lcm/ns/views.py b/lcm/ns/views.py index 413a89a6..c78b60be 100644 --- a/lcm/ns/views.py +++ b/lcm/ns/views.py @@ -114,6 +114,7 @@ class NSInstView(APIView): ) def post(self, request, ns_instance_id): logger.debug("Enter NSInstView::post::ns_instance_id=%s", ns_instance_id) + logger.debug("request.data=%s", request.data) req_serializer = InstantNsReqSerializer(data=request.data) if not req_serializer.is_valid(): return Response({'error': req_serializer.errors}, |