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.vnf_create.create_vnf_identifier import CreateVnf
23 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
24 from lcm.pub.database.models import NfInstModel, JobStatusModel, NfvoRegInfoModel, VmInstModel, NetworkInstModel, \
25 SubNetworkInstModel, PortInstModel
26 from lcm.pub.utils import restcall
27 from lcm.pub.vimapi import adaptor
28 from lcm.pub.utils.jobutil import JobUtil
31 "flavourId": "flavour_1",
32 "instantiationLevelId": "instantiationLevel_1",
42 "protocolType": "http"
45 "tenant": "tenant_vCPE",
47 "password": "vCPE_321"
49 "interfaceEndpoint": "http://10.43.21.105:80/"
58 "l2AddressData": "00:f3:43:20:a2:a3"
63 "iPAddressType": "IPv4",
64 "iPAddress": "192.168.104.2"
68 "numDynamicAddresses": 0
73 "localizationLanguage": "en_US",
74 "additionalParams": {}
78 class TestNsInstantiate(TestCase):
80 self.client = Client()
81 VmInstModel.objects.create(vmid="1", vimid="1", resouceid="11", insttype=0, instid="1", vmname="test_01",
83 VmInstModel.objects.create(vmid="2", vimid="2", resouceid="22", insttype=0, instid="2", vmname="test_02",
85 NetworkInstModel.objects.create(networkid='1', vimid='1', resouceid='1', name='pnet_network',
86 tenant='admin', insttype=0, instid='1')
87 SubNetworkInstModel.objects.create(subnetworkid='1', vimid='1', resouceid='1', networkid='1',
88 name='sub_pnet',tenant='admin', insttype=0, instid='1')
89 PortInstModel.objects.create(portid='1', networkid='1', subnetworkid='1', vimid='1', resouceid='1',
90 name='aaa_pnet_cp', tenant='admin', insttype=0, instid='1')
93 VmInstModel.objects.all().delete()
94 NetworkInstModel.objects.all().delete()
95 SubNetworkInstModel.objects.all().delete()
96 PortInstModel.objects.all().delete()
98 def assert_job_result(self, job_id, job_progress, job_detail):
99 jobs = JobStatusModel.objects.filter(
101 progress=job_progress,
103 self.assertEqual(1, len(jobs))
105 def test_swagger_ok(self):
106 response = self.client.get("/gvnfmapi/lcm/v1/swagger.json", format='json')
107 self.assertEqual(response.status_code, status.HTTP_200_OK)
109 @mock.patch.object(restcall, 'call_req')
110 def test_create_vnf_identifier(self, mock_call_req):
111 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
112 mock_call_req.side_effect = [r1]
115 "vnfInstanceName": "vFW_01",
116 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
117 response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances", data=data, format='json')
118 self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
119 context = json.loads(response.content)
120 self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
122 @mock.patch.object(InstVnf, 'run')
123 def test_instantiate_vnf(self, mock_run):
124 mock_run.re.return_value = None
125 response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances/12/instantiate", data={}, format='json')
126 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
128 def test_instantiate_vnf_when_inst_id_not_exist(self):
129 self.nf_inst_id = str(uuid.uuid4())
130 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
131 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
133 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
134 self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
136 @mock.patch.object(restcall, 'call_req')
137 def test_instantiate_vnf_when_input_para_not_define_in_vnfd(self, mock_call_req):
138 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
139 r2 = [0, json.JSONEncoder().encode(''), '200']
140 mock_call_req.side_effect = [r1, r2]
143 "vnfInstanceName": "vFW_01",
144 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
145 self.nf_inst_id = CreateVnf(create_data).do_biz()
146 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
147 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
149 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
150 self.assert_job_result(self.job_id, 255, "Input parameter is not defined in vnfd_info.")
152 @mock.patch.object(restcall, 'call_req')
153 def test_instantiate_vnf_when_get_nfvo_config_failed(self, mock_call_req):
154 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
155 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
156 mock_call_req.side_effect = [r1, r2]
159 "vnfInstanceName": "vFW_01",
160 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
161 self.nf_inst_id = CreateVnf(create_data).do_biz()
162 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
163 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
165 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
166 self.assert_job_result(self.job_id, 255, "Nfvo was not registered")
168 @mock.patch.object(restcall, 'call_req')
169 def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
170 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
171 nfvouser='root', nfvopassword='root123')
172 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
173 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
174 r3 = [1, json.JSONEncoder().encode(''), '200']
175 mock_call_req.side_effect = [r1, r2, r3]
178 "vnfInstanceName": "vFW_01",
179 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
180 self.nf_inst_id = CreateVnf(create_data).do_biz()
181 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
182 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
184 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
185 self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
187 @mock.patch.object(restcall, 'call_req')
188 # @mock.patch.object(adaptor, 'create_vim_res')
189 def test_instantiate_vnf_when_create_res_failed(self, mock_call_req):
190 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
191 nfvouser='root', nfvopassword='root123')
192 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
193 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
194 r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
195 # r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
196 mock_call_req.side_effect = [r1, r2, r3]
197 # mock_create_vim_res.re.return_value = None
200 "vnfInstanceName": "vFW_01",
201 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
202 self.nf_inst_id = CreateVnf(create_data).do_biz()
203 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
204 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
206 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
207 self.assert_job_result(self.job_id, 255, "Create resource failed")
209 # @mock.patch.object(restcall, 'call_req')
210 # # @mock.patch.object(adaptor, 'create_vim_res')
211 # def test_instantiate_vnf_success(self, mock_call_req):
212 # NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
213 # nfvouser='root', nfvopassword='root123')
214 # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
215 # r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
216 # r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
217 # # r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
218 # mock_call_req.side_effect = [r1, r2, r3]
219 # # mock_create_vim_res.re.return_value = None
222 # "vnfInstanceName": "vFW_01",
223 # "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
224 # self.nf_inst_id = CreateVnf(create_data).do_biz()
225 # self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
226 # JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
227 # data = inst_req_data
228 # InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
229 # self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
233 'local_storages': [],
236 'volumn_storages': [],
240 'local_storages': [],
241 'vdu_id': u'vdu_omm.001',
242 'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
248 'support_scaling': False,
251 'storage_policy': '',
254 'availability_zone': '',
259 'inject_data_list': [],
263 'local_affinity_antiaffinity_rule': {},
264 'template_id': u'omm.001',
265 'manual_scale_select_vim': False},
266 'description': u'singleommvm'},
268 'volumn_storages': [],
272 'local_storages': [],
274 'image_file': u'sss',
280 'support_scaling': False,
283 'storage_policy': '',
286 'availability_zone': '',
291 'inject_data_list': [],
295 'local_affinity_antiaffinity_rule': {},
297 'manual_scale_select_vim': False},
298 'description': u'ompvm'},
300 'volumn_storages': [],
304 'local_storages': [],
306 'image_file': u'sss',
312 'support_scaling': False,
315 'storage_policy': '',
318 'availability_zone': '',
323 'inject_data_list': [],
327 'local_affinity_antiaffinity_rule': {},
329 'manual_scale_select_vim': False},
330 'description': u'ompvm'},
332 'volumn_storages': [],
336 'local_storages': [],
338 'image_file': u'sss',
344 'support_scaling': False,
347 'storage_policy': '',
350 'availability_zone': '',
355 'inject_data_list': [],
359 'local_affinity_antiaffinity_rule': {},
361 'manual_scale_select_vim': False},
362 'description': u'ompvm'},
364 'volumn_storages': [],
368 'local_storages': [],
370 'image_file': u'sss',
376 'support_scaling': False,
379 'storage_policy': '',
382 'availability_zone': '',
387 'inject_data_list': [],
391 'local_affinity_antiaffinity_rule': {},
392 'template_id': u'10',
393 'manual_scale_select_vim': False},
394 'description': u'ppvm'},
396 'volumn_storages': [],
400 'local_storages': [],
402 'image_file': u'sss',
408 'support_scaling': False,
411 'storage_policy': '',
414 'availability_zone': '',
419 'inject_data_list': [],
423 'local_affinity_antiaffinity_rule': {},
424 'template_id': u'11',
425 'manual_scale_select_vim': False},
426 'description': u'ppvm'},
428 'volumn_storages': [],
432 'local_storages': [],
434 'image_file': u'sss',
440 'support_scaling': False,
443 'storage_policy': '',
446 'availability_zone': '',
451 'inject_data_list': [],
455 'local_affinity_antiaffinity_rule': {},
456 'template_id': u'12',
457 'manual_scale_select_vim': False},
458 'description': u'ppvm'}],
459 'volumn_storages': [],
463 'policy_id': u'policy_scale_sss-vnf-template',
465 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
471 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
473 'disk_format': u'VMDK',
474 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
475 'container_type': 'vm',
477 'hypervisor_type': 'kvm'},
478 'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
484 'disk_format': u'VMDK',
485 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
486 'container_type': 'vm',
488 'hypervisor_type': 'kvm'},
489 'image_file_id': u'sss'}],
496 'domain_type': u'CN',
497 'version': u'v4.14.10',
498 'vmnumber_overquota_alarm': False,
501 'vnfd_version': u'V00000001',
502 'id': u'sss-vnf-template',
503 'name': u'sss-vnf-template'},
504 "flavourId": "flavour_1",
505 "instantiationLevelId": "instantiationLevel_1",
515 "protocolType": "http"
518 "tenant": "tenant_vCPE",
520 "password": "vCPE_321"
522 "interfaceEndpoint": "http://10.43.21.105:80/"
524 "resourceId": "1246",
530 "addressType": "MAC",
531 "l2AddressData": "00:f3:43:20:a2:a3"
536 "iPAddressType": "IPv4",
537 "iPAddress": "192.168.104.2"
541 "numDynamicAddresses": 0
546 "localizationLanguage": "en_US",
547 "additionalParams": {}