Add additional unit tests and asserts for NS Heal.
[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.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
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, 'do_biz')
53     def test_terminate_vnf_url(self, mock_run):
54         req_data = {
55             "terminationType": "forceful",
56             "gracefulTerminationTimeout": "600"}
57         response = self.client.post("/api/nslcm/v1/ns/%s/terminate" % self.ns_inst_id, data=req_data)
58         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
59
60         response = self.client.delete("/api/nslcm/v1/ns/%s" % self.ns_inst_id)
61         self.failUnlessEqual(status.HTTP_204_NO_CONTENT, response.status_code)
62
63     @mock.patch.object(restcall, "call_req")
64     def test_terminate_vnf(self, mock_call_req):
65         job_id = JobUtil.create_job("VNF", JOB_TYPE.TERMINATE_VNF, self.nf_inst_id)
66
67         mock_vals = {
68             "/api/nslcm/v1/ns/vls/1":
69                 [0, json.JSONEncoder().encode({"jobId": self.job_id}), '200'],
70             "/api/nslcm/v1/ns/sfcs/1":
71                 [0, json.JSONEncoder().encode({"jobId": self.job_id}), '200'],
72             "/api/nslcm/v1/ns/vnfs/1":
73                 [0, json.JSONEncoder().encode({}), '200'],
74             "/api/ztevmanagerdriver/v1/jobs/" + self.job_id + "&responseId=0":
75                 [0, json.JSONEncoder().encode({"jobid": self.job_id,
76                                                "responsedescriptor": {"progress": "100",
77                                                                       "status": JOB_MODEL_STATUS.FINISHED,
78                                                                       "responseid": "3",
79                                                                       "statusdescription": "creating",
80                                                                       "errorcode": "0",
81                                                                       "responsehistorylist": [
82                                                                           {"progress": "0",
83                                                                            "status": JOB_MODEL_STATUS.PROCESSING,
84                                                                            "responseid": "2",
85                                                                            "statusdescription": "creating",
86                                                                            "errorcode": "0"}]}}), '200']}
87
88         def side_effect(*args):
89             return mock_vals[args[4]]
90
91         mock_call_req.side_effect = side_effect
92
93         TerminateNsService(self.nf_inst_id, "forceful", "600", job_id).start()
94         nsinst = NSInstModel.objects.get(id=self.ns_inst_id)
95         if nsinst:
96             self.assertTrue(1, 0)
97         else:
98             self.assertTrue(1, 1)