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.
19 from django.test import TestCase, Client
20 from rest_framework import status
22 from lcm.nf.vnfs.const import c1_data_get_tenant_id, c4_data_create_network, c2_data_create_volume, \
23 c5_data_create_subnet, c3_data_get_volume, c6_data_create_port, c7_data_create_flavor, c8_data_list_image, \
24 c9_data_create_vm, c10_data_get_vm, inst_req_data, vnfpackage_info
25 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
26 from lcm.pub.database.models import NfInstModel, JobStatusModel
27 from lcm.pub.utils import restcall
28 from lcm.pub.utils.jobutil import JobUtil
29 from lcm.pub.utils.timeutil import now_time
30 from lcm.pub.vimapi import api
33 class TestNFInstantiate(TestCase):
35 self.client = Client()
40 "tenant": 'tenantname_1'
48 def assert_job_result(self, job_id, job_progress, job_detail):
49 jobs = JobStatusModel.objects.filter(jobid=job_id,
50 progress=job_progress,
52 self.assertEqual(1, len(jobs))
54 def test_create_vnf_identifier_when_vnf_is_exist(self):
55 NfInstModel.objects.create(nfinstid='1111',
62 status='NOT_INSTANTIATED',
63 nf_desc='vFW in Nanjing TIC Edge',
65 create_time=now_time())
68 "vnfInstanceName": "vFW_01",
69 "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
71 response = self.client.post("/api/vnflcm/v1/vnf_instances", data=data, format='json')
72 self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
73 context = json.loads(response.content)
74 self.assertEqual({'error': 'VNF is already exist.'}, context)
76 @mock.patch.object(restcall, 'call_req')
77 def test_create_vnf_identifier(self, mock_call_req):
78 r2_get_vnfpackage_from_catalog = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
79 mock_call_req.side_effect = [r2_get_vnfpackage_from_catalog]
82 "vnfInstanceName": "vFW_01",
83 "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
85 response = self.client.post("/api/vnflcm/v1/vnf_instances", data=data, format='json')
86 self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
87 context = json.loads(response.content)
88 self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
90 @mock.patch.object(InstVnf, 'run')
91 def test_instantiate_vnf(self, mock_run):
92 mock_run.re.return_value = None
93 response = self.client.post("/api/vnflcm/v1/vnf_instances/12/instantiate", data={}, format='json')
94 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
96 def test_instantiate_vnf_when_inst_id_not_exist(self):
97 self.nf_inst_id = str(uuid.uuid4())
98 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
99 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
100 jobs = JobStatusModel.objects.filter(jobid=self.job_id,
102 descp="INST_VNF_READY")
103 self.assertEqual(1, len(jobs))
105 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
106 self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
108 @mock.patch.object(restcall, 'call_req')
109 def test_instantiate_vnf_when_get_rawdata_by_csarid_failed(self, mock_call_req):
110 NfInstModel.objects.create(nfinstid='1111',
117 status='NOT_INSTANTIATED',
118 nf_desc='vFW in Nanjing TIC Edge',
120 create_time=now_time())
121 r1_get_vnfpackage_by_vnfdid = [1, json.JSONEncoder().encode(vnfpackage_info), '200']
122 mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid]
123 self.nf_inst_id = '1111'
124 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
125 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
127 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
128 self.assert_job_result(self.job_id, 255, "Failed to query vnf CSAR(111) from catalog.")
130 @mock.patch.object(restcall, 'call_req')
131 def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
132 NfInstModel.objects.create(nfinstid='1111',
139 status='NOT_INSTANTIATED',
140 nf_desc='vFW in Nanjing TIC Edge',
142 create_time=now_time())
143 r1_get_vnfpackage_by_vnfdid = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
144 r2_apply_grant_result = [1, json.JSONEncoder().encode(self.grant_result), '200']
145 mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result]
146 self.nf_inst_id = '1111'
147 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
148 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
150 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
151 self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
153 @mock.patch.object(restcall, 'call_req')
154 @mock.patch.object(api, 'call')
155 def test_instantiate_vnf_when_unexpected_exception(self, mock_call, mock_call_req):
156 NfInstModel.objects.create(nfinstid='1111',
163 status='NOT_INSTANTIATED',
164 nf_desc='vFW in Nanjing TIC Edge',
166 create_time=now_time())
167 r1_get_vnfpackage_by_vnfdid = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
168 r2_apply_grant_result = [0, json.JSONEncoder().encode(self.grant_result), '200']
169 mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result]
170 mock_call.side_effect = [c1_data_get_tenant_id, c2_data_create_volume, c3_data_get_volume]
171 self.nf_inst_id = '1111'
172 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
173 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
175 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
176 self.assert_job_result(self.job_id, 255, "unexpected exception")
178 @mock.patch.object(restcall, 'call_req')
179 @mock.patch.object(api, 'call')
180 def test_instantiate_vnf_success(self, mock_call, mock_call_req):
181 NfInstModel.objects.create(nfinstid='1111',
188 status='NOT_INSTANTIATED',
189 nf_desc='vFW in Nanjing TIC Edge',
191 create_time=now_time())
192 r1_get_vnfpackage_by_vnfdid = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
193 r2_apply_grant_result = [0, json.JSONEncoder().encode(self.grant_result), '200']
194 r3_lcm_notify_result = [0, json.JSONEncoder().encode(''), '200']
195 mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result, r3_lcm_notify_result]
196 mock_call.side_effect = [c1_data_get_tenant_id, c2_data_create_volume, c3_data_get_volume,
197 c4_data_create_network, c5_data_create_subnet, c6_data_create_port,
198 c7_data_create_flavor, c8_data_list_image, c9_data_create_vm, c10_data_get_vm]
199 self.nf_inst_id = '1111'
200 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
201 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
203 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
204 self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")