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')
95 def assert_job_result(self, job_id, job_progress, job_detail):
96 jobs = JobStatusModel.objects.filter(
98 progress=job_progress,
100 self.assertEqual(1, len(jobs))
102 def test_swagger_ok(self):
103 response = self.client.get("/gvnfmapi/lcm/v1/swagger.json", format='json')
104 self.assertEqual(response.status_code, status.HTTP_200_OK)
106 @mock.patch.object(restcall, 'call_req')
107 def test_create_vnf_identifier(self, mock_call_req):
108 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
109 mock_call_req.side_effect = [r1]
112 "vnfInstanceName": "vFW_01",
113 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
114 response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances", data=data, format='json')
115 self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
116 context = json.loads(response.content)
117 self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
119 @mock.patch.object(InstVnf, 'run')
120 def test_instantiate_vnf(self, mock_run):
121 response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances/12/instantiate", data={}, format='json')
122 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
124 def test_instantiate_vnf_when_inst_id_not_exist(self):
125 self.nf_inst_id = str(uuid.uuid4())
126 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
127 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
129 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
130 self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
132 @mock.patch.object(restcall, 'call_req')
133 def test_instantiate_vnf_when_input_para_not_define_in_vnfd(self, mock_call_req):
134 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
135 r2 = [0, json.JSONEncoder().encode(''), '200']
136 mock_call_req.side_effect = [r1, r2]
139 "vnfInstanceName": "vFW_01",
140 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
141 self.nf_inst_id = CreateVnf(create_data).do_biz()
142 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
143 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
145 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
146 self.assert_job_result(self.job_id, 255, "Input parameter is not defined in vnfd_info.")
148 @mock.patch.object(restcall, 'call_req')
149 def test_instantiate_vnf_when_get_nfvo_config_failed(self, mock_call_req):
150 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
151 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
152 mock_call_req.side_effect = [r1, r2]
155 "vnfInstanceName": "vFW_01",
156 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
157 self.nf_inst_id = CreateVnf(create_data).do_biz()
158 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
159 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
161 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
162 self.assert_job_result(self.job_id, 255, "Nfvo was not registered")
164 @mock.patch.object(restcall, 'call_req')
165 def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
166 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
167 nfvouser='root', nfvopassword='root123')
168 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
169 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
170 r3 = [1, json.JSONEncoder().encode(''), '200']
171 mock_call_req.side_effect = [r1, r2, r3]
174 "vnfInstanceName": "vFW_01",
175 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
176 self.nf_inst_id = CreateVnf(create_data).do_biz()
177 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
178 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
180 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
181 self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
183 @mock.patch.object(restcall, 'call_req')
184 # @mock.patch.object(adaptor, 'create_vim_res')
185 def test_instantiate_vnf_when_create_res_failed(self, mock_call_req):
186 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
187 nfvouser='root', nfvopassword='root123')
188 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
189 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
190 r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
191 # r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
192 mock_call_req.side_effect = [r1, r2, r3]
193 # mock_create_vim_res.re.return_value = None
196 "vnfInstanceName": "vFW_01",
197 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
198 self.nf_inst_id = CreateVnf(create_data).do_biz()
199 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
200 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
202 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
203 self.assert_job_result(self.job_id, 255, "Create resource failed")
205 # @mock.patch.object(restcall, 'call_req')
206 # # @mock.patch.object(adaptor, 'create_vim_res')
207 # def test_instantiate_vnf_success(self, mock_call_req):
208 # NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
209 # nfvouser='root', nfvopassword='root123')
210 # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
211 # r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
212 # r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
213 # # r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
214 # mock_call_req.side_effect = [r1, r2, r3]
215 # # mock_create_vim_res.re.return_value = None
218 # "vnfInstanceName": "vFW_01",
219 # "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
220 # self.nf_inst_id = CreateVnf(create_data).do_biz()
221 # self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
222 # JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
223 # data = inst_req_data
224 # InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
225 # self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
229 'local_storages': [],
232 'volumn_storages': [],
236 'local_storages': [],
237 'vdu_id': u'vdu_omm.001',
238 'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
244 'support_scaling': False,
247 'storage_policy': '',
250 'availability_zone': '',
255 'inject_data_list': [],
259 'local_affinity_antiaffinity_rule': {},
260 'template_id': u'omm.001',
261 'manual_scale_select_vim': False},
262 'description': u'singleommvm'},
264 'volumn_storages': [],
268 'local_storages': [],
270 'image_file': u'sss',
276 'support_scaling': False,
279 'storage_policy': '',
282 'availability_zone': '',
287 'inject_data_list': [],
291 'local_affinity_antiaffinity_rule': {},
293 'manual_scale_select_vim': False},
294 'description': u'ompvm'},
296 'volumn_storages': [],
300 'local_storages': [],
302 'image_file': u'sss',
308 'support_scaling': False,
311 'storage_policy': '',
314 'availability_zone': '',
319 'inject_data_list': [],
323 'local_affinity_antiaffinity_rule': {},
325 'manual_scale_select_vim': False},
326 'description': u'ompvm'},
328 'volumn_storages': [],
332 'local_storages': [],
334 'image_file': u'sss',
340 'support_scaling': False,
343 'storage_policy': '',
346 'availability_zone': '',
351 'inject_data_list': [],
355 'local_affinity_antiaffinity_rule': {},
357 'manual_scale_select_vim': False},
358 'description': u'ompvm'},
360 'volumn_storages': [],
364 'local_storages': [],
366 'image_file': u'sss',
372 'support_scaling': False,
375 'storage_policy': '',
378 'availability_zone': '',
383 'inject_data_list': [],
387 'local_affinity_antiaffinity_rule': {},
388 'template_id': u'10',
389 'manual_scale_select_vim': False},
390 'description': u'ppvm'},
392 'volumn_storages': [],
396 'local_storages': [],
398 'image_file': u'sss',
404 'support_scaling': False,
407 'storage_policy': '',
410 'availability_zone': '',
415 'inject_data_list': [],
419 'local_affinity_antiaffinity_rule': {},
420 'template_id': u'11',
421 'manual_scale_select_vim': False},
422 'description': u'ppvm'},
424 'volumn_storages': [],
428 'local_storages': [],
430 'image_file': u'sss',
436 'support_scaling': False,
439 'storage_policy': '',
442 'availability_zone': '',
447 'inject_data_list': [],
451 'local_affinity_antiaffinity_rule': {},
452 'template_id': u'12',
453 'manual_scale_select_vim': False},
454 'description': u'ppvm'}],
455 'volumn_storages': [],
459 'policy_id': u'policy_scale_sss-vnf-template',
461 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
467 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
469 'disk_format': u'VMDK',
470 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
471 'container_type': 'vm',
473 'hypervisor_type': 'kvm'},
474 'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
480 'disk_format': u'VMDK',
481 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
482 'container_type': 'vm',
484 'hypervisor_type': 'kvm'},
485 'image_file_id': u'sss'}],
492 'domain_type': u'CN',
493 'version': u'v4.14.10',
494 'vmnumber_overquota_alarm': False,
497 'vnfd_version': u'V00000001',
498 'id': u'sss-vnf-template',
499 'name': u'sss-vnf-template'},
500 "flavourId": "flavour_1",
501 "instantiationLevelId": "instantiationLevel_1",
511 "protocolType": "http"
514 "tenant": "tenant_vCPE",
516 "password": "vCPE_321"
518 "interfaceEndpoint": "http://10.43.21.105:80/"
520 "resourceId": "1246",
526 "addressType": "MAC",
527 "l2AddressData": "00:f3:43:20:a2:a3"
532 "iPAddressType": "IPv4",
533 "iPAddress": "192.168.104.2"
537 "numDynamicAddresses": 0
542 "localizationLanguage": "en_US",
543 "additionalParams": {}