Update python2 to python3
[vfc/nfvo/lcm.git] / lcm / ns / tests / tests_ns_terminate.py
1 # Copyright 2016-2017 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14 import json
15 import uuid
16
17 import mock
18 from django.test import TestCase, Client
19 from rest_framework import status
20
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.pub.enum import JOB_MODEL_STATUS, JOB_TYPE
25 from lcm.pub.utils.jobutil import JobUtil
26
27
28 class TestTerminateNsViews(TestCase):
29     def setUp(self):
30         self.client = Client()
31         self.ns_inst_id = '1'
32         self.nf_inst_id = '1'
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',
46                                    vnfd_model=model)
47
48     def tearDown(self):
49         NSInstModel.objects.all().delete()
50         NfInstModel.objects.all().delete()
51
52     @mock.patch.object(TerminateNsService, 'run')
53     def test_terminate_vnf_url(self, mock_run):
54         mock_run.re.return_value = "1"
55         req_data = {
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)
60
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)
63
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)
67
68         mock_vals = {
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,
79                                                                       "responseid": "3",
80                                                                       "statusdescription": "creating",
81                                                                       "errorcode": "0",
82                                                                       "responsehistorylist": [
83                                                                           {"progress": "0",
84                                                                            "status": JOB_MODEL_STATUS.PROCESSING,
85                                                                            "responseid": "2",
86                                                                            "statusdescription": "creating",
87                                                                            "errorcode": "0"}]}}), '200']}
88
89         def side_effect(*args):
90             return mock_vals[args[4]]
91
92         mock_call_req.side_effect = side_effect
93         req_data = {
94             "terminationType": "FORCEFUL",
95             "gracefulTerminationTimeout": "600"}
96         TerminateNsService(self.nf_inst_id, job_id, req_data).run()
97         nsinst = NSInstModel.objects.get(id=self.ns_inst_id)
98         if nsinst:
99             self.assertTrue(1, 0)
100         else:
101             self.assertTrue(1, 1)
102
103     @mock.patch.object(TerminateNsService, 'run')
104     def test_terminate_non_existing_ns_inst_id(self, mock_run):
105         mock_run.re.return_value = "1"
106         ns_inst_id = '100'
107         req_data = {
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.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
112         self.assertRaises(NSInstModel.DoesNotExist, NSInstModel.objects.get, id=ns_inst_id)