1 # Copyright 2016-2017 ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 from django.test import TestCase, Client
19 from rest_framework import status
21 from lcm.pub.database.models import NfInstModel, NSInstModel
22 from lcm.pub.utils import restcall
23 from lcm.pub.utils.jobutil import JOB_MODEL_STATUS
24 from lcm.ns.ns_terminate import TerminateNsService
25 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
28 class TestTerminateNsViews(TestCase):
30 self.client = Client()
33 self.vnffg_id = str(uuid.uuid4())
34 self.vim_id = str(uuid.uuid4())
35 self.job_id = str(uuid.uuid4())
36 self.nf_uuid = '1-1-1'
37 self.tenant = "tenantname"
38 model = '{"metadata": {"vnfdId": "1","vnfdName": "PGW001","vnfProvider": "zte","vnfdVersion": "V00001",' \
39 '"vnfVersion": "V5.10.20","productType": "CN","vnfType": "PGW",' \
40 '"description": "PGW VNFD description","isShared":true,"vnfExtendType":"driver"}}'
41 NSInstModel(id=self.ns_inst_id, name="ns_name", status='null').save()
42 NfInstModel.objects.create(nfinstid=self.nf_inst_id, nf_name='name_1', vnf_id='1',
43 vnfm_inst_id='1', ns_inst_id='1-1-1,2-2-2',
44 max_cpu='14', max_ram='12296', max_hd='101', max_shd="20", max_net=10,
45 status='null', mnfinstid=self.nf_uuid, package_id='pkg1',
49 NSInstModel.objects.all().delete()
50 NfInstModel.objects.all().delete()
52 @mock.patch.object(TerminateNsService, 'run')
53 def test_terminate_vnf_url(self, mock_run):
54 mock_run.re.return_value = "1"
56 "terminationType": "forceful",
57 "gracefulTerminationTimeout": "600"}
58 response = self.client.post("/api/nslcm/v1/ns/%s/terminate" % self.ns_inst_id, data=req_data)
59 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
61 response = self.client.delete("/api/nslcm/v1/ns/%s" % self.ns_inst_id)
62 self.failUnlessEqual(status.HTTP_204_NO_CONTENT, response.status_code)
64 @mock.patch.object(restcall, 'call_req')
65 def test_terminate_vnf(self, mock_call_req):
66 job_id = JobUtil.create_job("VNF", JOB_TYPE.TERMINATE_VNF, self.nf_inst_id)
69 "/api/nslcm/v1/ns/vls/1":
70 [0, json.JSONEncoder().encode({"jobId": self.job_id}), '200'],
71 "/api/nslcm/v1/ns/sfcs/1":
72 [0, json.JSONEncoder().encode({"jobId": self.job_id}), '200'],
73 "/api/nslcm/v1/ns/vnfs/1":
74 [0, json.JSONEncoder().encode({}), '200'],
75 "/api/ztevnfmdriver/v1/jobs/" + self.job_id + "&responseId=0":
76 [0, json.JSONEncoder().encode({"jobid": self.job_id,
77 "responsedescriptor": {"progress": "100",
78 "status": JOB_MODEL_STATUS.FINISHED,
80 "statusdescription": "creating",
82 "responsehistorylist": [
84 "status": JOB_MODEL_STATUS.PROCESSING,
86 "statusdescription": "creating",
87 "errorcode": "0"}]}}), '200']}
89 def side_effect(*args):
90 return mock_vals[args[4]]
92 mock_call_req.side_effect = side_effect
94 TerminateNsService(self.nf_inst_id, "forceful", "600", job_id).run()
95 nsinst = NSInstModel.objects.get(id=self.ns_inst_id)
101 @mock.patch.object(TerminateNsService, 'run')
102 def test_terminate_non_existing_ns_inst_id(self, mock_run):
103 mock_run.re.return_value = "1"
108 "terminationType": "forceful",
109 "gracefulTerminationTimeout": "600"}
110 response = self.client.post("/api/nslcm/v1/ns/%s/terminate" % ns_inst_id, data=req_data)
111 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
113 self.assertRaises(NSInstModel.DoesNotExist, NSInstModel.objects.get, id=ns_inst_id)