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.ns.biz.ns_terminate import TerminateNsService
22 from lcm.pub.database.models import NfInstModel, NSInstModel
23 from lcm.pub.utils import restcall
24 from lcm.jobs.enum import JOB_MODEL_STATUS, JOB_TYPE, JOB_ACTION
25 from lcm.pub.utils.jobutil import JobUtil
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_ns_api(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.assertEqual(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.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
64 @mock.patch.object(restcall, 'call_req')
65 def test_terminate_ns_service(self, mock_call_req):
66 job_id = JobUtil.create_job(JOB_TYPE.NS, JOB_ACTION.TERMINATE, self.ns_inst_id)
69 "/api/nslcm/v1/ns/ns_vls/1":
70 [0, json.JSONEncoder().encode({"jobId": self.job_id}), '200'],
71 "/api/nslcm/v1/ns/ns_sfcs/1":
72 [0, json.JSONEncoder().encode({"jobId": self.job_id}), '200'],
73 "/api/nslcm/v1/ns/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'],
88 "/api/catalog/v1/ns_descriptors/None":
89 [0, json.JSONEncoder().encode({}), '202'],
92 def side_effect(*args):
93 return mock_vals[args[4]]
95 mock_call_req.side_effect = side_effect
97 "terminationType": "FORCEFUL",
98 "gracefulTerminationTimeout": "600"}
99 TerminateNsService(self.ns_inst_id, job_id, req_data).run()
100 nsinst = NSInstModel.objects.get(id=self.ns_inst_id)
102 self.assertTrue(1, 0)
104 self.assertTrue(1, 1)
106 @mock.patch.object(TerminateNsService, 'run')
107 def test_terminate_non_existing_ns_inst_id(self, mock_run):
108 mock_run.re.return_value = "1"
111 "terminationType": "forceful",
112 "gracefulTerminationTimeout": "600"}
113 response = self.client.post("/api/nslcm/v1/ns/%s/terminate" % ns_inst_id, data=req_data)
114 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
115 self.assertRaises(NSInstModel.DoesNotExist, NSInstModel.objects.get, id=ns_inst_id)