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.
18 from django.test import TestCase, Client
19 from rest_framework import status
21 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
22 from lcm.pub.database.models import NfInstModel, JobStatusModel, VmInstModel, NetworkInstModel, \
23 SubNetworkInstModel, PortInstModel
24 from lcm.pub.utils import restcall
25 from lcm.pub.utils.jobutil import JobUtil
26 from lcm.pub.utils.timeutil import now_time
29 class TestNFInstantiate(TestCase):
31 self.client = Client()
32 VmInstModel.objects.create(vmid="1", vimid="1", resouceid="11", insttype=0, instid="1", vmname="test_01",
34 VmInstModel.objects.create(vmid="2", vimid="2", resouceid="22", insttype=0, instid="2",
35 vmname="test_02", operationalstate=1)
36 NetworkInstModel.objects.create(networkid='1', vimid='1', resouceid='1', name='pnet_network',
37 tenant='admin', insttype=0, instid='1')
38 SubNetworkInstModel.objects.create(subnetworkid='1', vimid='1', resouceid='1', networkid='1',
39 name='sub_pnet', tenant='admin', insttype=0, instid='1')
40 PortInstModel.objects.create(portid='1', networkid='1', subnetworkid='1', vimid='1', resouceid='1',
41 name='aaa_pnet_cp', tenant='admin', insttype=0, instid='1')
45 VmInstModel.objects.all().delete()
46 NetworkInstModel.objects.all().delete()
47 SubNetworkInstModel.objects.all().delete()
48 PortInstModel.objects.all().delete()
50 def assert_job_result(self, job_id, job_progress, job_detail):
51 jobs = JobStatusModel.objects.filter(
53 progress=job_progress,
55 self.assertEqual(1, len(jobs))
57 def test_swagger_ok(self):
58 response = self.client.get("/openoapi/vnflcm/v1/swagger.json", format='json')
59 self.assertEqual(response.status_code, status.HTTP_200_OK)
61 @mock.patch.object(restcall, 'call_req')
62 def test_create_vnf_identifier(self, mock_call_req):
63 r1 = [0, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200'] # get csar_id from nslcm by vnfd_id
64 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
65 mock_call_req.side_effect = [r1, r2]
68 "vnfInstanceName": "vFW_01",
69 "vnfInstanceDescription": "vFW in Nanjing TIC Edge"}
70 response = self.client.post("/openoapi/vnflcm/v1/vnf_instances", data=data, format='json')
71 self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
72 context = json.loads(response.content)
73 self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
75 @mock.patch.object(InstVnf, 'run')
76 def test_instantiate_vnf(self, mock_run):
77 mock_run.re.return_value = None
78 response = self.client.post("/openoapi/vnflcm/v1/vnf_instances/12/instantiate", data={}, format='json')
79 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
81 def test_instantiate_vnf_when_inst_id_not_exist(self):
82 self.nf_inst_id = str(uuid.uuid4())
83 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
84 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
86 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
87 self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
89 @mock.patch.object(restcall, 'call_req')
90 def test_instantiate_vnf_when_get_package_info_by_vnfdid_failed(self, mock_call_req):
91 NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='todo',
92 version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
93 nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
94 r1 = [1, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200'] # get csar_id from nslcm by vnfd_id
95 mock_call_req.side_effect = [r1]
96 self.nf_inst_id = '1111'
97 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
98 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
100 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
101 self.assert_job_result(self.job_id, 255, "Failed to query package_info of vnfdid(111) from nslcm.")
103 # @mock.patch.object(restcall, 'call_req')
104 # def test_instantiate_vnf_when_input_para_not_define_in_vnfd(self, mock_call_req):
105 # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
106 # r2 = [0, json.JSONEncoder().encode(''), '200']
107 # mock_call_req.side_effect = [r1, r2]
110 # "vnfInstanceName": "vFW_01",
111 # "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
112 # self.nf_inst_id = CreateVnf(create_data).do_biz()
113 # self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
114 # JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
115 # data = inst_req_data
116 # InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
117 # self.assert_job_result(self.job_id, 255, "Input parameter is not defined in vnfd_info.")
119 # @mock.patch.object(restcall, 'call_req')
120 # def test_instantiate_vnf_when_get_nfvo_config_failed(self, mock_call_req):
121 # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
122 # r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
123 # mock_call_req.side_effect = [r1, r2]
126 # "vnfInstanceName": "vFW_01",
127 # "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
128 # self.nf_inst_id = CreateVnf(create_data).do_biz()
129 # self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
130 # JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
131 # data = inst_req_data
132 # InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
133 # self.assert_job_result(self.job_id, 255, "Nfvo was not registered")
135 # @mock.patch.object(restcall, 'call_req')
136 # def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
137 # NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
138 # nfvouser='root', nfvopassword='root123')
139 # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
140 # r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
141 # r3 = [1, json.JSONEncoder().encode(''), '200']
142 # mock_call_req.side_effect = [r1, r2, r3]
145 # "vnfInstanceName": "vFW_01",
146 # "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
147 # self.nf_inst_id = CreateVnf(create_data).do_biz()
148 # self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
149 # JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
150 # data = inst_req_data
151 # InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
152 # self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
154 # @mock.patch.object(restcall, 'call_req')
155 # # @mock.patch.object(adaptor, 'create_vim_res')
156 # def test_instantiate_vnf_when_create_res_failed(self, mock_call_req):
157 # NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
158 # nfvouser='root', nfvopassword='root123')
159 # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
160 # r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
161 # r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
162 # # r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
163 # mock_call_req.side_effect = [r1, r2, r3]
164 # # mock_create_vim_res.re.return_value = None
167 # "vnfInstanceName": "vFW_01",
168 # "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
169 # self.nf_inst_id = CreateVnf(create_data).do_biz()
170 # self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
171 # JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
172 # data = inst_req_data
173 # InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
174 # self.assert_job_result(self.job_id, 255, "Create resource failed")
176 # @mock.patch.object(restcall, 'call_req')
177 # # @mock.patch.object(adaptor, 'create_vim_res')
178 # def test_instantiate_vnf_success(self, mock_call_req):
179 # NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
180 # nfvouser='root', nfvopassword='root123')
181 # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
182 # r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
183 # r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
184 # r4 = [0, json.JSONEncoder().encode('None'), '200']
185 # mock_call_req.side_effect = [r1, r2, r3, r4]
186 # # mock_create_vim_res.re.return_value = None
189 # "vnfInstanceName": "vFW_01",
190 # "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
191 # self.nf_inst_id = CreateVnf(create_data).do_biz()
192 # self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
193 # JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
194 # data = inst_req_data
195 # InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
196 # self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
199 "flavourId": "flavour_1",
200 "instantiationLevelId": "instantiationLevel_1",
210 "protocolType": "http"
213 "tenant": "tenant_vCPE",
215 "password": "vCPE_321"
217 "interfaceEndpoint": "http://10.43.21.105:80/"
219 "resourceId": "1246",
225 "addressType": "MAC",
226 "l2AddressData": "00:f3:43:20:a2:a3"
231 "iPAddressType": "IPv4",
232 "iPAddress": "192.168.104.2"
236 "numDynamicAddresses": 0
241 "localizationLanguage": "en_US",
242 "additionalParams": {}
246 'local_storages': [],
249 'volumn_storages': [],
253 'local_storages': [],
254 'vdu_id': u'vdu_omm.001',
255 'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
261 'support_scaling': False,
264 'storage_policy': '',
267 'availability_zone': '',
272 'inject_data_list': [],
276 'local_affinity_antiaffinity_rule': {},
277 'template_id': u'omm.001',
278 'manual_scale_select_vim': False},
279 'description': u'singleommvm'},
281 'volumn_storages': [],
285 'local_storages': [],
287 'image_file': u'sss',
293 'support_scaling': False,
296 'storage_policy': '',
299 'availability_zone': '',
304 'inject_data_list': [],
308 'local_affinity_antiaffinity_rule': {},
310 'manual_scale_select_vim': False},
311 'description': u'ompvm'},
313 'volumn_storages': [],
317 'local_storages': [],
319 'image_file': u'sss',
325 'support_scaling': False,
328 'storage_policy': '',
331 'availability_zone': '',
336 'inject_data_list': [],
340 'local_affinity_antiaffinity_rule': {},
342 'manual_scale_select_vim': False},
343 'description': u'ompvm'},
345 'volumn_storages': [],
349 'local_storages': [],
351 'image_file': u'sss',
357 'support_scaling': False,
360 'storage_policy': '',
363 'availability_zone': '',
368 'inject_data_list': [],
372 'local_affinity_antiaffinity_rule': {},
374 'manual_scale_select_vim': False},
375 'description': u'ompvm'},
377 'volumn_storages': [],
381 'local_storages': [],
383 'image_file': u'sss',
389 'support_scaling': False,
392 'storage_policy': '',
395 'availability_zone': '',
400 'inject_data_list': [],
404 'local_affinity_antiaffinity_rule': {},
405 'template_id': u'10',
406 'manual_scale_select_vim': False},
407 'description': u'ppvm'},
409 'volumn_storages': [],
413 'local_storages': [],
415 'image_file': u'sss',
421 'support_scaling': False,
424 'storage_policy': '',
427 'availability_zone': '',
432 'inject_data_list': [],
436 'local_affinity_antiaffinity_rule': {},
437 'template_id': u'11',
438 'manual_scale_select_vim': False},
439 'description': u'ppvm'},
441 'volumn_storages': [],
445 'local_storages': [],
447 'image_file': u'sss',
453 'support_scaling': False,
456 'storage_policy': '',
459 'availability_zone': '',
464 'inject_data_list': [],
468 'local_affinity_antiaffinity_rule': {},
469 'template_id': u'12',
470 'manual_scale_select_vim': False},
471 'description': u'ppvm'}],
472 'volumn_storages': [],
476 'policy_id': u'policy_scale_sss-vnf-template',
478 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
484 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
486 'disk_format': u'VMDK',
487 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
488 'container_type': 'vm',
490 'hypervisor_type': 'kvm'},
491 'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
497 'disk_format': u'VMDK',
498 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
499 'container_type': 'vm',
501 'hypervisor_type': 'kvm'},
502 'image_file_id': u'sss'}],
509 'domain_type': u'CN',
510 'version': u'v4.14.10',
511 'vmnumber_overquota_alarm': False,
514 'vnfd_version': u'V00000001',
515 'id': u'sss-vnf-template',
516 'name': u'sss-vnf-template'},
517 "flavourId": "flavour_1",
518 "instantiationLevelId": "instantiationLevel_1",
528 "protocolType": "http"
531 "tenant": "tenant_vCPE",
533 "password": "vCPE_321"
535 "interfaceEndpoint": "http://10.43.21.105:80/"
537 "resourceId": "1246",
543 "addressType": "MAC",
544 "l2AddressData": "00:f3:43:20:a2:a3"
549 "iPAddressType": "IPv4",
550 "iPAddress": "192.168.104.2"
554 "numDynamicAddresses": 0
559 "localizationLanguage": "en_US",
560 "additionalParams": {}