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