4f27cee032e0963a3ccc0e0134cc1b42eb69cac5
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / vnfs / tests / test_vnf_cancel.py
1 # Copyright 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
15 import json
16 import uuid
17
18 import mock
19 from django.test import TestCase, Client
20 from rest_framework import status
21
22 from lcm.nf.vnfs.vnf_cancel.term_vnf import TermVnf
23 from lcm.pub.database.models import NfInstModel, JobStatusModel, VmInstModel, NetworkInstModel, SubNetworkInstModel, \
24     PortInstModel, FlavourInstModel, StorageInstModel, NfvoRegInfoModel
25 from lcm.pub.utils import restcall
26 from lcm.pub.utils.jobutil import JobUtil
27 from lcm.pub.utils.timeutil import now_time
28 from lcm.pub.vimapi import api
29
30
31 class TestNFTerminate(TestCase):
32     def setUp(self):
33         self.client = Client()
34         StorageInstModel.objects.create(storageid="1", vimid="1", resouceid="11", insttype=0, instid="1111",
35                                         is_predefined=1)
36         NetworkInstModel.objects.create(networkid='1', vimid='1', resouceid='1', name='pnet_network',
37                                         is_predefined=1, tenant='admin', insttype=0, instid='1111')
38         SubNetworkInstModel.objects.create(subnetworkid='1', vimid='1', resouceid='1', networkid='1',
39                                            is_predefined=1, name='sub_pnet', tenant='admin', insttype=0, instid='1111')
40         PortInstModel.objects.create(portid='1', networkid='1', subnetworkid='1', vimid='1', resouceid='1',
41                                      is_predefined=1, name='aaa_pnet_cp', tenant='admin', insttype=0, instid='1111')
42         FlavourInstModel.objects.create(flavourid="1", vimid="1", resouceid="11", instid="1111", is_predefined=1)
43         VmInstModel.objects.create(vmid="1", vimid="1", resouceid="11", insttype=0, instid="1111", vmname="test_01",
44                                    is_predefined=1, operationalstate=1)
45         NfvoRegInfoModel.objects.create(nfvoid='1111', vnfminstid='11111', apiurl='1')
46
47     def tearDown(self):
48         VmInstModel.objects.all().delete()
49         NetworkInstModel.objects.all().delete()
50         SubNetworkInstModel.objects.all().delete()
51         PortInstModel.objects.all().delete()
52
53     def assert_job_result(self, job_id, job_progress, job_detail):
54         jobs = JobStatusModel.objects.filter(
55             jobid=job_id,
56             progress=job_progress,
57             descp=job_detail)
58         self.assertEqual(1, len(jobs))
59
60     @mock.patch.object(restcall, 'call_req_aai')
61     def test_delete_vnf_identifier(self, mock_call_req_aai):
62         NfInstModel.objects.create(nfinstid='1111', nf_name='2222', package_id='todo', version='', vendor='',
63                                    netype='', vnfd_model='', status='NOT_INSTANTIATED', nf_desc='', vnfdid='',
64                                    vnfSoftwareVersion='', vnfConfigurableProperties='todo',
65                                    localizationLanguage='EN_US', create_time=now_time())
66         r1_create_vnf_to_aai = [0, json.JSONEncoder().encode({}), '200']
67         mock_call_req_aai.side_effect = [r1_create_vnf_to_aai]
68         response = self.client.delete("/api/vnflcm/v1/vnf_instances/1111")
69         self.failUnlessEqual(status.HTTP_204_NO_CONTENT, response.status_code)
70         self.assertEqual(None, response.data)
71
72     """
73     def test_delete_vnf_identifier_when_vnf_not_exist(self):
74         response = self.client.delete("/api/vnflcm/v1/vnf_instances/1111")
75         self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
76         self.assertEqual("VnfInst(1111) does not exist", response.data["error"])
77
78     def test_delete_vnf_identifier_when_status_check_failed(self):
79         NfInstModel.objects.create(nfinstid='1111', nf_name='2222', package_id='todo', version='', vendor='',
80                                    netype='', vnfd_model='', status='VNF_INSTANTIATED', nf_desc='', vnfdid='',
81                                    vnfSoftwareVersion='', vnfConfigurableProperties='todo',
82                                    localizationLanguage='EN_US', create_time=now_time())
83         response = self.client.delete("/api/vnflcm/v1/vnf_instances/1111")
84         self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
85         self.assertEqual("Don't allow to delete vnf(status:[VNF_INSTANTIATED])", response.data["error"])
86     """
87
88     @mock.patch.object(TermVnf, 'run')
89     def test_terminate_vnf(self, mock_run):
90         mock_run.re.return_value = None
91         response = self.client.post("/api/vnflcm/v1/vnf_instances/12/terminate", data={}, format='json')
92         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
93
94     """
95     def test_terminate_vnf_when_inst_id_not_exist(self):
96         data = {"terminationType": "GRACEFUL",
97                 "gracefulTerminationTimeout": 120}
98         self.nf_inst_id = str(uuid.uuid4())
99         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
100         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
101         TermVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
102         self.assert_job_result(self.job_id, 255, "VnfInst(%s) does not exist" % self.nf_inst_id)
103     """
104
105     @mock.patch.object(restcall, 'call_req')
106     @mock.patch.object(api, 'call')
107     def test_terminate_vnf_success(self, mock_call, mock_call_req):
108         NfInstModel.objects.create(nfinstid='1111', nf_name='2222', package_id='todo', version='', vendor='',
109                                    netype='', vnfd_model='', status='VNF_INSTANTIATED', nf_desc='', vnfdid='',
110                                    vnfSoftwareVersion='', vnfConfigurableProperties='todo',
111                                    localizationLanguage='EN_US', create_time=now_time())
112         t1_apply_grant_result = [0, json.JSONEncoder().encode(
113             {"vim": {"vimid": 'vimid_1', "accessinfo": {"tenant": 'tenantname_1'}}}), '200']
114         t2_lcm_notify_result = [0, json.JSONEncoder().encode(''), '200']
115         mock_call_req.side_effect = [t1_apply_grant_result, t2_lcm_notify_result]
116         mock_call.return_value = None
117         data = {"terminationType": "FORCEFUL",
118                 "gracefulTerminationTimeout": 120}
119         self.nf_inst_id = '1111'
120         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
121         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
122         TermVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
123         self.assert_job_result(self.job_id, 100, "Terminate Vnf success.")