From 3cca438799a38970816b0c5f33e5cbe111ec8d92 Mon Sep 17 00:00:00 2001 From: erlei Date: Fri, 26 Jul 2019 20:41:55 +0800 Subject: [PATCH] ADD UT for ns_sfcs Issue-ID: VFC-1429 Signed-off-by: zhuerlei Change-Id: I4946769215dd9668379e9e463e8ff242bf35cc08 Signed-off-by: erlei --- lcm/ns_sfcs/biz/sfc_instance.py | 4 +- lcm/ns_sfcs/tests/test_create_port_pair_group.py | 34 +- lcm/ns_sfcs/tests/test_create_sfc_worker.py | 101 ++++ lcm/ns_sfcs/tests/test_data.py | 5 + lcm/ns_sfcs/tests/test_sfc.py | 558 ++--------------------- lcm/ns_sfcs/tests/test_sfc_instance.py | 58 ++- lcm/ns_sfcs/tests/test_sfcdetailview.py | 2 +- lcm/ns_sfcs/views/views.py | 10 +- 8 files changed, 212 insertions(+), 560 deletions(-) create mode 100644 lcm/ns_sfcs/tests/test_create_sfc_worker.py diff --git a/lcm/ns_sfcs/biz/sfc_instance.py b/lcm/ns_sfcs/biz/sfc_instance.py index b63b13cb..c6cb1b3a 100644 --- a/lcm/ns_sfcs/biz/sfc_instance.py +++ b/lcm/ns_sfcs/biz/sfc_instance.py @@ -40,9 +40,9 @@ class SfcInstance(object): if not self.fp_model: return logger.info("sfc_inst_symmetric %s" % self.fp_model["properties"].get("symmetric")) - self.symmetric = self.fp_model["properties"].get("symmetric") + self.symmetric = self.fp_model["properties"].get("symmetric", "") logger.info("sfc_inst_symmetric %s" % self.symmetric) - self.policyinfo = self.fp_model["properties"].get("policy") + self.policyinfo = self.fp_model["properties"].get("policy", "") self.status = "processing" vnffg_database_info = VNFFGInstModel.objects.filter(vnffgdid=self.get_vnffgdid_by_fp_id(), nsinstid=self.ns_inst_id).get() diff --git a/lcm/ns_sfcs/tests/test_create_port_pair_group.py b/lcm/ns_sfcs/tests/test_create_port_pair_group.py index 9ed0707e..7bf5c4ea 100644 --- a/lcm/ns_sfcs/tests/test_create_port_pair_group.py +++ b/lcm/ns_sfcs/tests/test_create_port_pair_group.py @@ -16,7 +16,7 @@ import json from .test_data import nsd_model, vnfd_model_dict1, vnfd_model_dict2 from rest_framework import status from lcm.pub.utils import restcall -from lcm.pub.database.models import FPInstModel, NfInstModel +from lcm.pub.database.models import FPInstModel, NfInstModel, VNFCInstModel, CPInstModel, PortInstModel from django.test import Client from django.test import TestCase @@ -26,6 +26,7 @@ class TestSfc(TestCase): self.client = Client() FPInstModel.objects.all().delete() NfInstModel.objects.all().delete() + VNFCInstModel.objects.all().delete() NfInstModel( nfinstid="vnf_inst_1", ns_inst_id="ns_inst_1", @@ -36,6 +37,34 @@ class TestSfc(TestCase): vnf_id="vnf_2", ns_inst_id="ns_inst_1", vnfd_model=json.dumps(vnfd_model_dict2)).save() + VNFCInstModel(nfinstid="vnf_inst_1", vnfcinstanceid="vnfc_instance_id_1").save() + VNFCInstModel(nfinstid="vnf_inst_2", vnfcinstanceid="vnfc_instance_id_2").save() + CPInstModel( + cpinstanceid="cp_instance_id_1", + cpdid="cpd_1", + cpinstancename="cpinstancename_1", + ownertype=3, + ownerid="vnfc_instance_id_1", + relatedtype=1, + relatedvl="relatedvl_1", + relatedcp="relatedcp_1", + relatedport="relatedport_1", + status="ACTIVE" + ).save() + CPInstModel( + cpinstanceid="cp_instance_id_2", + cpdid="cpd_2", + cpinstancename="cpinstancename_2", + ownertype=3, + ownerid="vnfc_instance_id_2", + relatedtype=1, + relatedvl="relatedvl_2", + relatedcp="relatedcp_2", + relatedport="relatedport_2", + status="ACTIVE" + ).save() + PortInstModel(portid="relatedport_1").save() + PortInstModel(portid="relatedport_2").save() FPInstModel( fpid="fpd_1", fpinstid="fp_inst_1", @@ -59,6 +88,9 @@ class TestSfc(TestCase): def tearDown(self): FPInstModel.objects.all().delete() NfInstModel.objects.all().delete() + VNFCInstModel.objects.all().delete() + CPInstModel.objects.all().delete() + PortInstModel.objects.all().delete() @mock.patch.object(restcall, 'call_req') def test_create_port_pair_group_success(self, mock_call_req): diff --git a/lcm/ns_sfcs/tests/test_create_sfc_worker.py b/lcm/ns_sfcs/tests/test_create_sfc_worker.py new file mode 100644 index 00000000..0dcc6a81 --- /dev/null +++ b/lcm/ns_sfcs/tests/test_create_sfc_worker.py @@ -0,0 +1,101 @@ +# Copyright 2018 ZTE Corporation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json +import mock +import uuid + +from django.test import Client +from django.test import TestCase + +from lcm.pub.database.models import FPInstModel +from lcm.pub.database.models import JobModel +from lcm.ns_sfcs.biz.create_sfc_worker import CreateSfcWorker +from lcm.ns_sfcs.tests.test_data import nsd_model +from lcm.ns_sfcs.biz.utils import get_fp_id +from lcm.ns_sfcs.biz.create_flowcla import CreateFlowClassifier +from lcm.ns_sfcs.biz.create_portpairgp import CreatePortPairGroup +from lcm.ns_sfcs.biz.create_port_chain import CreatePortChain + + +class TestCreateSfcWorker(TestCase): + def setUp(self): + self.client = Client() + FPInstModel.objects.filter().delete() + self.fpinstid = str(uuid.uuid4()) + FPInstModel( + fpid="fpd_1", + fpinstid=self.fpinstid, + nsinstid="ns_inst_1", + vnffginstid="vnffg_inst_1", + policyinfo=[{ + "type": "ACL", + "criteria": { + "dest_port_range": [80, 1024], + "source_port_range": [80, 1024], + "ip_protocol": "tcp", + "dest_ip_range": ["192.168.1.2", "192.168.1.100"], + "source_ip_range": ["192.168.1.2", "192.168.1.100"], + "dscp": 100, + } + }], + status="enabled", + sdncontrollerid="sdn_controller_1", + portpairgroups=json.JSONEncoder().encode([{"groupid": "1"}]), + symmetric=1, + flowclassifiers="test_flowclassifiers", + + ).save() + + def tearDown(self): + FPInstModel.objects.filter().delete() + + @mock.patch.object(CreateFlowClassifier, 'do_biz') + @mock.patch.object(CreatePortPairGroup, 'do_biz') + @mock.patch.object(CreatePortChain, 'do_biz') + def test_create_sfc_worker(self, mock_port_chain_do_biz, mock_port_pair_group_do_biz, mock_flow_classifier_do_biz): + mock_port_chain_do_biz.return_value = None + mock_port_pair_group_do_biz.return_value = None + mock_flow_classifier_do_biz.return_value = None + data = { + 'nsinstid': "ns_inst_1", + "ns_model_data": nsd_model, + 'fpindex': get_fp_id("1", nsd_model), + 'fpinstid': self.fpinstid, + 'sdncontrollerid': "sdnControllerId_1" + } + + create_sfc_worker = CreateSfcWorker(data) + job_id = create_sfc_worker.init_data() + create_sfc_worker.run() + self.assertEqual(JobModel.objects.filter(jobid=job_id).first().progress, 100) + + @mock.patch.object(CreatePortPairGroup, 'do_biz') + @mock.patch.object(CreatePortChain, 'do_biz') + def test_create_sfc_worker_when_error(self, mock_port_chain_do_biz, mock_port_pair_group_do_biz): + mock_port_chain_do_biz.return_value = None + mock_port_pair_group_do_biz.return_value = None + data = { + 'nsinstid': "ns_inst_1", + "ns_model_data": nsd_model, + 'fpindex': get_fp_id("1", nsd_model), + 'fpinstid': self.fpinstid, + 'sdncontrollerid': "sdnControllerId_1" + } + + create_sfc_worker = CreateSfcWorker(data) + job_id = create_sfc_worker.init_data() + create_sfc_worker.run() + self.assertEqual(JobModel.objects.filter(jobid=job_id).first().progress, 255) + self.assertEqual(FPInstModel.objects.filter(fpinstid=self.fpinstid).first().status, 'failed') diff --git a/lcm/ns_sfcs/tests/test_data.py b/lcm/ns_sfcs/tests/test_data.py index 82902ad5..0efe5be4 100644 --- a/lcm/ns_sfcs/tests/test_data.py +++ b/lcm/ns_sfcs/tests/test_data.py @@ -300,6 +300,11 @@ nsd_model = { "node_name": "vnf_2", "capability": "forwarder2", }, + { + "type": "vnf", + "node_name": "vnf_2", + "capability": "forwarder3", + }, { "type": "cp", "node_name": "forwarder_brasDP_dcPort", diff --git a/lcm/ns_sfcs/tests/test_sfc.py b/lcm/ns_sfcs/tests/test_sfc.py index f490cef9..da930423 100644 --- a/lcm/ns_sfcs/tests/test_sfc.py +++ b/lcm/ns_sfcs/tests/test_sfc.py @@ -14,6 +14,9 @@ import json import mock +import uuid +import time + from django.test import Client from django.test import TestCase from rest_framework import status @@ -23,6 +26,9 @@ from lcm.pub.database.models import VNFFGInstModel from lcm.pub.msapi import extsys from lcm.pub.msapi import sdncdriver from lcm.pub.utils import restcall +from lcm.ns_sfcs.biz.create_sfc_worker import CreateSfcWorker +from lcm.pub.utils.jobutil import JobUtil +from lcm.ns_sfcs.tests.test_data import nsd_model class TestSfc(TestCase): @@ -41,19 +47,11 @@ class TestSfc(TestCase): self.save_fp_inst_data() def tearDown(self): - pass - - @mock.patch.object(restcall, 'call_req') - def test_sfc_instanciate(self, mock_call_req): - pass - # data = { - # "nsInstanceId": "ns_inst_1", - # "context": nsd_model, - # "fpindex": "fpd_1", - # "sdnControllerId": "sdnControllerId_1" - # } - # resp = self.client.post("/api/nslcm/v1/ns/sfc_instance", data, format='json') - # self.assertEqual(resp.status_code, status.HTTP_200_OK, resp.data) + FPInstModel.objects.filter().delete() + VNFFGInstModel.objects.filter().delete() + CPInstModel.objects.filter().delete() + PortInstModel.objects.filter().delete() + NfInstModel.objects.filter().delete() @mock.patch.object(extsys, "get_sdn_controller_by_id") @mock.patch.object(sdncdriver, "create_flow_classfier") @@ -100,18 +98,23 @@ class TestSfc(TestCase): resp = self.client.post("/api/nslcm/v1/ns/create_port_chain", data) self.assertEqual(resp.status_code, status.HTTP_200_OK) - @mock.patch.object(restcall, 'call_req') - def test_create_sfc(self, mock_call_req): - pass - # data = { - # "nsInstanceId": "ns_inst_1", - # "context": json.dumps(nsd_model), - # "fpindex": "1", - # 'fpinstid': str(uuid.uuid4()), - # "sdnControllerId": "sdnControllerId_1" - # } - # resp = self.client.post("/api/nslcm/v1/ns/ns_sfcs", data, format='json') - # self.assertEqual(resp.status_code, status.HTTP_200_OK, resp.data) + @mock.patch.object(CreateSfcWorker, 'run') + @mock.patch.object(JobUtil, 'create_job') + @mock.patch.object(time, 'sleep') + def test_create_sfc(self, mock_sleep, mock_create_job, mock_run): + mock_create_job.return_value = 'job_id_1' + mock_sleep.return_value = None + mock_run.return_value = None + data = { + 'nsInstanceid': "ns_inst_1", + "context": json.dumps(nsd_model), + "fpindex": "1", + 'fpinstid': str(uuid.uuid4()), + "sdnControllerId": "sdnControllerId_1" + } + resp = self.client.post("/api/nslcm/v1/ns/sfcs", data, format='json') + self.assertEqual(resp.status_code, status.HTTP_200_OK) + self.assertEqual(resp.data['jobId'], 'job_id_1') def update_fp_inst_data(self): FPInstModel.objects.filter(fpinstid="fp_inst_1").update(flowclassifiers="1", @@ -192,21 +195,6 @@ class TestSfc(TestCase): sdncontrollerid="sdn_controller_1" ).save() - def save_sdnc_inst_data(self): - pass - # SDNCModel( - # uuid="uuid_111", - # sdncontrollerid="sdn_controller_1", - # name="111", - # type="vnf", - # url="192.168.0.1:8080", - # username="admin", - # pwd="admin", - # ver="ver", - # longitude="longitude", - # latitude="latitude" - # ).save() - def save_port_inst_data(self): PortInstModel( portid="port_inst_1", @@ -1151,493 +1139,3 @@ vnfd_model_dict2 = { ], } } - -nsd_model = { - "metadata": { - "id": "nsd_demo", - "vendor": "zte", - "version": "1.1.0", - "name": "testNSD", - "description": "demo nsd", - }, - - "inputs": { - "param1": "11", - "param2": "22", - }, - - "vnfs": [ - { - "type": "tosca.nodes.nfv.ext.VNF.FireWall", - "vnf_id": "vnf_1", - "description": "", - "properties": { - "id": "vnfd_1", - "vendor": "zte", - "version": "1.2.0", - "vnfd_version": "1.1.0", - "vnf_type": "vnf1", - "domain_type": "CN", - "name": "vnf1", - "is_shared": False, - "cross_dc": False, - "request_reclassification": False, - "nsh_aware": False, - "custom_properties": { - "key1": "value1", - "keyN": "valueN", - }, - }, - "dependencies": [ - "vnf_id1", "vnf_id2" - ], - "networks": [ - { - "key_name": "virtualLink1", - "vl_id": "vl_id1", - }, - ], - }, - { - "type": "tosca.nodes.nfv.ext.VNF.FireWall", - "vnf_id": "vnf_2", - "description": "", - "properties": { - "id": "vnfd_2", - "vendor": "zte", - "version": "1.2.0", - "vnfd_version": "1.1.0", - "vnf_type": "vnf2", - "domain_type": "CN", - "name": "vnf1", - "is_shared": False, - "cross_dc": False, - "request_reclassification": False, - "nsh_aware": False, - "custom_properties": { - "key1": "value1", - "keyN": "valueN", - }, - }, - "dependencies": [ - "vnf_id1", "vnf_id2" - ], - "networks": [ - { - "key_name": "virtualLink1", - "vl_id": "vl_id1", - }, - ], - } - ], - - "pnfs": [ - { - "pnf_id": "pnf1", - "description": "", - "properties": { - "id": "pnf1", - "vendor": "zte", - "version": "1.1.0", - "pnf_type": "TTGW", - "request_reclassification": False, - "nsh_aware": False, - "management_address": "10.44.56.78" - }, - "cps": [ - "cpd_1", "cpd_22", - ] - } - ], - - "nested_ns": [ - { - "ns_id": "ns2", - "description": "", - "properties": { - "id": "ns2_demo", - "vendor": "zte", - "version": "1.1.0", - "name": "NSD2", - }, - "networks": [ - { - "key_name": "virtualLink1", - "vl_id": "vl_id1", - }, - ], - } - ], - - "vls": [ - { - "vl_id": "vldId1", - "description": "", - "properties": { - "name": "umac_241_control", - "network_id": "fgdhsj434hfjdfd", - "network_name": "umac_control", - "vendor": "zte", - "mtu": 1500, - "network_type": "vlan", - "physical_network": "phynet01", - "segmentation_id": "30", - "vlan_transparent": False, - "vds_name": "vds1", - "cidr": "192.168.199.0/24", - "ip_version": 4, - "gateway_ip": "192.168.199.1", - "dhcp_enabled": False, - "dns_nameservers": ["192.168.0.4", "192.168.0.10"], - "start_ip": "192.168.199.2", - "end_ip": "192.168.199.254", - "host_routes": [ - { - "destination": "10.43.26.0/24", - "nexthop": "10.41.23.1", - }, - ], - "location_info": { - "vimId": "vimid", - "tenant": "tenantname", - }, - "vlan_transparent": False, - }, - }, - ], - - "cps": [ - { - "cp_id": "cpd_1", - "description": "", - "properties": { - "mac_address": "00:d9:00:82:11:e1", - "ip_address": "192.168.1.21", - "ip_range_start": "192.168.1.20", - "ip_range_end": "192.168.1.29", - "floating_ip_address": { - "external_network": "extnet01", - "ip_address": "10.43.53.23", - }, - "service_ip_address": "192.168.1.23", - "order": 1, - "bandwidth": 1000, - "vnic_type": "normal", - "allowed_address_pairs": [ - { - "ip": "192.168.1.13", - "mac": "00:f3:43:20:a2:a3" - }, - ], - "bond": "none", - "macbond": "00:d9:00:82:11:d1", - "sfc_encapsulation": "", - "direction": "", - }, - "vl_id": "vlid1", - "pnf_id": "pnf1", - }, - - { - "cp_id": "forwarder_brasDP_dcPort", - "description": "", - "properties": { - "mac_address": "00:d9:00:82:14:e1", - "ip_address": "192.168.1.24", - "ip_range_start": "192.168.1.20", - "ip_range_end": "192.168.1.29", - "floating_ip_address": { - "external_network": "extnet01", - "ip_address": "10.43.53.23", - }, - "service_ip_address": "192.168.1.23", - "order": 1, - "bandwidth": 1000, - "vnic_type": "normal", - "allowed_address_pairs": [ - { - "ip": "192.168.1.13", - "mac": "00:f3:43:20:a2:a3" - }, - ], - "bond": "none", - "macbond": "00:d9:00:82:11:d1", - "sfc_encapsulation": "", - "direction": "", - }, - "vl_id": "vlid1", - "pnf_id": "pnf1", - }, - { - "cp_id": "forwarder_brasDP_internetPort", - "description": "", - "properties": { - "mac_address": "00:d9:00:82:15:e1", - "ip_address": "192.168.1.25", - "ip_range_start": "192.168.1.20", - "ip_range_end": "192.168.1.29", - "floating_ip_address": { - "external_network": "extnet01", - "ip_address": "10.43.53.23", - }, - "service_ip_address": "192.168.1.23", - "order": 1, - "bandwidth": 1000, - "vnic_type": "normal", - "allowed_address_pairs": [ - { - "ip": "192.168.1.13", - "mac": "00:f3:43:20:a2:a3" - }, - ], - "bond": "none", - "macbond": "00:d9:00:82:11:d1", - "sfc_encapsulation": "", - "direction": "", - }, - "vl_id": "vlid1", - "pnf_id": "pnf1", - }, - - ], - - "fps": [ - { - "fp_id": "fpd_1", - "description": "", - "properties": { - "policy": { - "type": "ACL", - "criteria": { - "dest_port_range": [80, 1024], - "source_port_range": [80, 1024], - "ip_protocol": "tcp", - "dest_ip_range": ["192.168.1.2", "192.168.1.100"], - "source_ip_range": ["192.168.1.2", "192.168.1.100"], - "dscp": 100, - }, - }, - "symmetric": True, - }, - "forwarder_list": [ - { - "type": "cp", - "node_name": "cpd_1", - "capability": "", - }, - { - "type": "cp", - "node_name": "forwarder_brasDP_dcPort", - "capability": "", - }, - { - "type": "vnf", - "node_name": "vnf_1", - "capability": "forwarder1", - }, - { - "type": "vnf", - "node_name": "vnf_2", - "capability": "forwarder2", - }, - { - "type": "cp", - "node_name": "forwarder_brasDP_dcPort", - "capability": "", - }, - { - "type": "cp", - "node_name": "forwarder_brasDP_internetPort", - "capability": "", - }, - ], - }, - - { - "fp_id": "fpd_2", - "description": "", - "properties": { - "policy": { - "type": "ACL", - "criteria": { - "dest_port_range": [80, 1024], - "source_port_range": [80, 1024], - "ip_protocol": "tcp", - "dest_ip_range": ["192.168.1.2", "192.168.1.100"], - "source_ip_range": ["192.168.1.2", "192.168.1.100"], - "dscp": 100, - }, - }, - "symmetric": True, - }, - "forwarder_list": [ - - { - "type": "cp", - "node_name": "forwarder_brasDP_internetPort", - "capability": "", - }, - { - "type": "cp", - "node_name": "forwarder_brasDP_dcPort", - "capability": "", - }, - { - "type": "vnf", - "node_name": "vnf_2", - "capability": "forwarder2", - }, - - ], - }, - ], - - "vnffgs": [ - { - "vnffg_id": "vnffg_id1", - "description": "", - "properties": { - "vendor": "zte", - "version": "1.1.2", - "number_of_endpoints": 7, - "dependent_virtual_link": ["vldId1"], - "connection_point": ["CP01", "CP02"], - "constituent_vnfs": ["vnf_id1", "vnf_id2"], - "constituent_pnfs": ["pnf1", "pnf2"], - }, - "members": ["fpd_1", "fpd_2"], - } - ], - - "server_groups": [ - { - "group_id": "", - "description": "", - "properties": { - "name": "server_group1", - "affinity_antiaffinity": "anti-affinity", - "scope": "host", - }, - "members": ["vnf1", "vnf2"], - }, - ], - - "ns_exposed": { - "external_cps": [ - { - "key_name": "virtualLink1", - "cp_id": "cp1", - }, - ], - "forward_cps": [ - { - "key_name": "forwarder_brasDP_userPort", - "cp_id": "cpd_1", - }, - { - "key_name": "forwarder_brasDP_internetPort", - "cp_id": "cpd_4", - }, - { - "key_name": "forwarder_brasDP_dcPort", - "cp_id": "cpd_5", - }, - - ], - }, - - "policies": [ - { - "scaling": [ - { - "policy_id": "id1", - "description": "", - "properties": { - "policy_file": "Policies/ns1-policy.xml", - }, - "targets": ['pfu_vm'], - }, - ], - }, - ], - - "ns_flavours": [ - { - "flavour_id": "flavour1", - "description": "", - "vnf_profiles": [ - { - "vnf_id": "vnf1", - "flavour_id": "flavour1", - "instances_minimum_number": 1, - "instances_maximum_number": 4, - "local_affinity_antiaffinity_rule": [ - { - "affinity_antiaffinity": "affinity", - "scope": "node", - } - ] - }, - ], - "pnf_profiles": [ - { - "pnf_id": "pnf1", - }, - ], - "vl_profiles": [ - { - "vl_id": "vlid1", - "bitrate_requirements": { - "root": 1000, - "leaf": 100 - }, - "qos": { - "maximum_latency": "1 ms", - "maximum_jitter": "10 ms", - "maximum_packet_loss_ratio": 0.5 - }, - } - ], - "instantiation_levels": [ - { - "id": "instLevel1", - "description": "", - "vnf_levels": [ - { - "vnf_id": "", - "vnf_instantiation_level": "small", - "instances_number": 1 - }, - ], - "scale_level_id": "scaleLevel1", - } - ], - "default_instantiation_level": "instLevel1", - "scale_levels": [ - { - "id": "scaleLevel1", - "order": 1, - "vnf_levels": [ - { - "vnf_id": "", - "vnf_instantiation_level": "small", - "instances_number": 1 - }, - ], - }, - ], - "supported_operations": ["Scale", "Heal"], - "affinity_antiaffinity_groups": [ - { - "group_id": "group1Id", - "name": "groupName", - "affinity_antiaffinity": "affinity", - "scope": "node", - "members": [ - "vnfId1", "vnfIdN", - ], - }, - ], - }, - ], -} diff --git a/lcm/ns_sfcs/tests/test_sfc_instance.py b/lcm/ns_sfcs/tests/test_sfc_instance.py index f2e7d391..f4cf7689 100644 --- a/lcm/ns_sfcs/tests/test_sfc_instance.py +++ b/lcm/ns_sfcs/tests/test_sfc_instance.py @@ -11,37 +11,57 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# import json -# from rest_framework import status -# from test_data import nsd_model +import json +import copy +from rest_framework import status from django.test import Client from django.test import TestCase from lcm.pub.database.models import FPInstModel, VNFFGInstModel +from lcm.ns_sfcs.tests.test_data import nsd_model -class TestSfc(TestCase): +class TestSfcInstance(TestCase): def setUp(self): self.client = Client() + self.data = { + "nsInstanceId": "ns_inst_1", + "context": json.dumps(nsd_model), + "fpindex": "fpd_1", + "sdnControllerId": "sdnControllerId_1" + } VNFFGInstModel.objects.all().delete() FPInstModel.objects.all().delete() VNFFGInstModel(vnffgdid="vnffg_id1", vnffginstid="vnffg_inst_1", nsinstid="ns_inst_1", endpointnumber=2, - vllist="vlinst1", cplist="cp1", vnflist="vnf1,vnf2").save() + vllist="vlinst1", cplist="cp1", vnflist="vnf1,vnf2", fplist="fp1").save() def tearDown(self): VNFFGInstModel.objects.all().delete() FPInstModel.objects.all().delete() - # def test_sfc_instance_success(self): - # data = { - # "nsInstanceId": "ns_inst_1", - # "context": json.dumps(nsd_model), - # "fpindex": "fpd_1", - # "sdnControllerId": "sdnControllerId_1" - # } - # resp = self.client.post("/api/nslcm/v1/ns/sfc_instance", data, format='json') - - # self.assertEqual(resp.status_code, status.HTTP_200_OK, resp.data) - # vnffg = VNFFGInstModel.objects.get(vnffginstid="vnffg_inst_1") - # ret = FPInstModel.objects.get(fpinstid=resp.data["fpinstid"]) - # self.assertEqual(vnffg.fplist, resp.data["fpinstid"]) - # self.assertIsNotNone(ret) + def test_sfc_instance_success(self): + resp = self.client.post("/api/nslcm/v1/ns/sfc_instance", self.data, format="json") + + self.assertEqual(resp.status_code, status.HTTP_200_OK) + vnffg = VNFFGInstModel.objects.get(vnffginstid="vnffg_inst_1") + self.assertEqual(vnffg.fplist, "fp1," + resp.data["fpinstid"]) + ret = FPInstModel.objects.get(fpinstid=resp.data["fpinstid"]) + self.assertIsNotNone(ret) + + def test_sfc_instance_when_request_data_is_not_valid(self): + data = {} + resp = self.client.post("/api/nslcm/v1/ns/sfc_instance", data, format="json") + self.assertEqual(resp.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + + def test_sfc_instance_when_error_request_data(self): + data = copy.deepcopy(self.data) + data.pop("fpindex") + resp = self.client.post("/api/nslcm/v1/ns/sfc_instance", data, format="json") + + self.assertEqual(resp.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + + def test_sfc_instance_when_no_fp_model(self): + data = copy.deepcopy(self.data) + data["context"] = json.dumps({"fps": []}) + resp = self.client.post("/api/nslcm/v1/ns/sfc_instance", data, format="json") + + self.assertEqual(resp.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/lcm/ns_sfcs/tests/test_sfcdetailview.py b/lcm/ns_sfcs/tests/test_sfcdetailview.py index 72ae3ef3..8059007c 100644 --- a/lcm/ns_sfcs/tests/test_sfcdetailview.py +++ b/lcm/ns_sfcs/tests/test_sfcdetailview.py @@ -65,7 +65,7 @@ class TestSfcDetailViews(TestCase): def test_sfc_get_failed(self): sfc_inst_id = "10" - response = self.client.get("/api/nslcm/v1/ns/ns_sfcs/%s" % sfc_inst_id) + response = self.client.get("/api/nslcm/v1/ns/sfcs/%s" % sfc_inst_id) self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code) def test_sfc_get_success(self): diff --git a/lcm/ns_sfcs/views/views.py b/lcm/ns_sfcs/views/views.py index 6f1d81c0..57e7346a 100644 --- a/lcm/ns_sfcs/views/views.py +++ b/lcm/ns_sfcs/views/views.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. - import json import logging import time @@ -145,7 +144,7 @@ class SfcView(APIView): logger.info("service_function_chain_instanceid : %s" % ignorcase_get(request.data, 'nsInstanceId')) logger.info("service_function_chain_sdncontrollerid : %s" % ignorcase_get(request.data, 'sdnControllerId')) logger.info("service_function_chain_fpindex : %s" % ignorcase_get(request.data, 'fpindex')) - ns_model_data = request.data['context'] + ns_model_data = json.loads(request.data['context']) req_serializer = CreateSfcReqSerializer(data=request.data) if not req_serializer.is_valid(): @@ -171,11 +170,8 @@ class SfcView(APIView): logger.info("Service Function Chain Thread Sleep end: %s" % time.ctime()) logger.info("Create Service Function Chain end") - resp_serializer = CreateSfcRespSerializer(data={"jobId": job_id, - "sfcInstId": data["fpinstid"]}) + resp_serializer = CreateSfcRespSerializer(data={"jobId": job_id, "sfcInstId": data["fpinstid"]}) if not resp_serializer.is_valid(): logger.error(resp_serializer.errors) - return Response(data={"jobId": job_id, - "sfcInstId": data["fpinstid"]}, - status=status.HTTP_200_OK) + return Response(data={"jobId": job_id, "sfcInstId": data["fpinstid"]}, status=status.HTTP_200_OK) -- 2.16.6