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.
17 from django.test import TestCase, Client
18 from rest_framework import status
20 from lcm.nf.vnfs.vnf_cancel.term_vnf import TermVnf
21 from lcm.pub.database.models import NfInstModel, JobStatusModel, VmInstModel, NetworkInstModel, SubNetworkInstModel, \
22 PortInstModel, NfvoRegInfoModel
23 from lcm.pub.utils.jobutil import JobUtil
24 from lcm.pub.utils.timeutil import now_time
27 class TestNFTerminate(TestCase):
29 self.client = Client()
30 VmInstModel.objects.create(vmid="1", vimid="1", resouceid="11", insttype=0, instid="1111", vmname="test_01",
31 is_predefined=1, operationalstate=1)
32 VmInstModel.objects.create(vmid="2", vimid="2", resouceid="22", insttype=0, instid="1111",
33 is_predefined=1, vmname="test_02", operationalstate=1)
34 NetworkInstModel.objects.create(networkid='1', vimid='1', resouceid='1', name='pnet_network',
35 is_predefined=1, tenant='admin', insttype=0, instid='1111')
36 SubNetworkInstModel.objects.create(subnetworkid='1', vimid='1', resouceid='1', networkid='1',
37 is_predefined=1, name='sub_pnet', tenant='admin', insttype=0, instid='1111')
38 PortInstModel.objects.create(portid='1', networkid='1', subnetworkid='1', vimid='1', resouceid='1',
39 is_predefined=1, name='aaa_pnet_cp', tenant='admin', insttype=0, instid='1111')
42 VmInstModel.objects.all().delete()
43 NetworkInstModel.objects.all().delete()
44 SubNetworkInstModel.objects.all().delete()
45 PortInstModel.objects.all().delete()
47 def assert_job_result(self, job_id, job_progress, job_detail):
48 jobs = JobStatusModel.objects.filter(
50 progress=job_progress,
52 self.assertEqual(1, len(jobs))
54 def test_delete_vnf_identifier(self):
55 NfInstModel.objects.create(nfinstid='1111', mnfinstid='1111', nf_name='2222',
56 package_id='todo', vnfm_inst_id='todo', version='', vendor='',
57 producttype='', netype='', vnfd_model='',
58 instantiationState='VNF_INSTANTIATED', nf_desc='', vnfdid='',
59 vnfSoftwareVersion='', vnfConfigurableProperties='todo',
60 localizationLanguage='EN_US', create_time=now_time())
61 response = self.client.delete("/openoapi/vnflcm/v1/vnf_instances/1111")
62 self.failUnlessEqual(status.HTTP_204_NO_CONTENT, response.status_code)
63 self.assertEqual(None, response.data)
65 def test_delete_vnf_identifier_when_vnf_not_exist(self):
66 response = self.client.delete("/openoapi/vnflcm/v1/vnf_instances/1111")
67 self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
68 self.assertEqual("VnfInst(1111) does not exist", response.data["error"])
70 def test_delete_vnf_identifier_when_instantiationState_check_failed(self):
71 NfInstModel.objects.create(nfinstid='1111', mnfinstid='1111', nf_name='2222',
72 package_id='todo', vnfm_inst_id='todo', version='', vendor='',
73 producttype='', netype='', vnfd_model='',
74 instantiationState='NOT_INSTANTIATED', nf_desc='', vnfdid='',
75 vnfSoftwareVersion='', vnfConfigurableProperties='todo',
76 localizationLanguage='EN_US', create_time=now_time())
77 response = self.client.delete("/openoapi/vnflcm/v1/vnf_instances/1111")
78 self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
79 self.assertEqual("No instantiated vnf", response.data["error"])
81 @mock.patch.object(TermVnf, 'run')
82 def test_terminate_vnf(self, mock_run):
83 mock_run.re.return_value = None
84 response = self.client.post("/openoapi/vnflcm/v1/vnf_instances/12/terminate", data={}, format='json')
85 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
87 def test_terminate_vnf_when_inst_id_not_exist(self):
88 data = {"terminationType": "GRACEFUL",
89 "gracefulTerminationTimeout": 120}
90 self.nf_inst_id = str(uuid.uuid4())
91 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
92 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
93 TermVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
94 self.assert_job_result(self.job_id, 255, "VnfInst(%s) does not exist" % self.nf_inst_id)
96 def test_instantiate_vnf_when_get_nfvo_config_failed(self):
97 NfInstModel.objects.create(nfinstid='1111', mnfinstid='1111', nf_name='2222',
98 package_id='todo', vnfm_inst_id='todo', version='', vendor='',
99 producttype='', netype='', vnfd_model='',
100 instantiationState='VNF_INSTANTIATED', nf_desc='', vnfdid='',
101 vnfSoftwareVersion='', vnfConfigurableProperties='todo',
102 localizationLanguage='EN_US', create_time=now_time())
103 data = {"terminationType": "FORCEFUL",
104 "gracefulTerminationTimeout": 120}
105 self.nf_inst_id = '1111'
106 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
107 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
108 TermVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
109 self.assert_job_result(self.job_id, 255, "Nfvo was not registered")
111 def test_terminate_vnf_success(self):
112 NfInstModel.objects.create(nfinstid='1111', mnfinstid='1111', nf_name='2222',
113 package_id='todo', vnfm_inst_id='todo', version='', vendor='',
114 producttype='', netype='', vnfd_model='',
115 instantiationState='VNF_INSTANTIATED', nf_desc='', vnfdid='',
116 vnfSoftwareVersion='', vnfConfigurableProperties='todo',
117 localizationLanguage='EN_US', create_time=now_time())
118 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
119 nfvouser='root', nfvopassword='root123')
120 data = {"terminationType": "FORCEFUL",
121 "gracefulTerminationTimeout": 120}
122 self.nf_inst_id = '1111'
123 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
124 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
125 TermVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
126 self.assert_job_result(self.job_id, 100, "Terminate Vnf success.")